Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 7d8517d

Browse files
Issue 945 (#948)
1 parent 9507e5b commit 7d8517d

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
class KadanesAlgo
4+
{
5+
public static void main (String[] args) throws java.lang.Exception
6+
{
7+
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter the number of elements you want to store: ");
10+
int n=sc.nextInt();
11+
int[] a = new int[100];
12+
System.out.println("Enter the elements of the array: ");
13+
for(int i=0; i<n; i++) {
14+
a[i]=sc.nextInt();
15+
}
16+
int local_max = 0;
17+
int global_max = Integer.MIN_VALUE;
18+
19+
for(int i=0;i<n;i++)
20+
{
21+
local_max = Math.max(a[i], a[i] + local_max);
22+
23+
if(local_max > global_max)
24+
{
25+
global_max = local_max;
26+
}
27+
28+
}
29+
System.out.println("Maximum Subarray sum: " +global_max);
30+
}
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.Scanner;
2+
3+
class LCSExample1 {
4+
public static String findLengthOfLCS(String str1, String str2, int p, int q) {
5+
int[][] tableForLCS = new int[p + 1][q + 1];
6+
for (int i = 0; i <= p; i++) {
7+
for (int j = 0; j <= q; j++) {
8+
if (i == 0 || j == 0)
9+
tableForLCS[i][j] = 0;
10+
else if (str1.charAt(i - 1) == str2.charAt(j - 1))
11+
tableForLCS[i][j] = tableForLCS[i - 1][j - 1] + 1;
12+
else
13+
tableForLCS[i][j] = Math.max(tableForLCS[i - 1][j], tableForLCS[i][j - 1]);
14+
}
15+
}
16+
int index = tableForLCS[p][q];
17+
int temp = index;
18+
char[] longestCommonSubsequence = new char[index + 1];
19+
longestCommonSubsequence[index] = '\0';
20+
int i = p, j = q;
21+
String lcs ="";
22+
while (i > 0 && j > 0) {
23+
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
24+
25+
longestCommonSubsequence[index - 1] = str1.charAt(i - 1);
26+
i--;
27+
j--;
28+
index--;
29+
}
30+
else if (tableForLCS[i - 1][j] > tableForLCS[i][j - 1])
31+
i--;
32+
else
33+
j--;
34+
}
35+
for (int k = 0; k <= temp; k++)
36+
lcs = lcs + longestCommonSubsequence[k];
37+
38+
return lcs;
39+
}
40+
public static void main(String[] args) {
41+
42+
String str1, str2, LCS;
43+
44+
Scanner sc= new Scanner(System.in);
45+
System.out.print("Enter first sequence: ");
46+
str1 = sc.nextLine();
47+
48+
System.out.print("Enter second sequence: ");
49+
str2 = sc.nextLine();
50+
51+
int p = str1.length();
52+
int q = str2.length();
53+
54+
LCS = findLengthOfLCS(str1, str2, p, q);
55+
56+
System.out.println("LCS: "+LCS);
57+
58+
}
59+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.*;
2+
3+
public class LIS {
4+
public static void main(String[] args){
5+
6+
Scanner sc=new Scanner(System.in);
7+
System.out.print("Enter the number of elements you want to store: ");
8+
int n=sc.nextInt();
9+
int[] nums= new int[100];
10+
System.out.println("Enter the elements of the array: ");
11+
for(int i=0; i<n; i++) {
12+
nums[i]=sc.nextInt();
13+
}
14+
printLIS(nums);
15+
}
16+
public static void printLIS(int[] nums){
17+
String[] paths = new String[nums.length];
18+
int[] sizes = new int[nums.length];
19+
20+
for(int i=0; i<nums.length; i++){
21+
sizes[i] = 1;
22+
paths[i] = nums[i] + " " ;
23+
}
24+
int maxLength = 1;
25+
26+
for(int i=1; i<nums.length; i++){
27+
for(int j=0; j<nums.length; j++){
28+
if(nums[i]>nums[j] && sizes[i] < sizes[j] + 1){;
29+
sizes[i] = sizes[j] + 1;
30+
paths[i] = paths[j] + nums[i] + " ";
31+
if(maxLength < sizes[i])
32+
maxLength = sizes[i];
33+
}
34+
}
35+
}
36+
for(int i=1; i<nums.length; i++){
37+
if(sizes[i] == maxLength)
38+
System.out.println("LIS: " + paths[i]);
39+
}
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
3+
public class Subsequences {
4+
public static void main(String[] args) {
5+
Scanner sc=new Scanner(System.in);
6+
System.out.print("Enter the number of elements you want to store: ");
7+
int n=sc.nextInt();
8+
int[] arr= new int[100];
9+
System.out.println("Enter the elements of the array: ");
10+
for(int i=0; i<n; i++) {
11+
arr[i]=sc.nextInt();
12+
}
13+
System.out.print("Enter Sum: ");
14+
int X = sc.nextInt();
15+
System.out.println(countSubsequence(arr, n, X));
16+
}
17+
18+
public static int countSubsequence(int[] A, int N, int X) {
19+
int[][] dp = new int[N+1][X+1];
20+
for (int[] row : dp) {
21+
Arrays.fill(row, 0);
22+
}
23+
for (int i = 0; i <= N; i++) {
24+
for (int j = 0; j <= X; j++) {
25+
if (i == N) {
26+
dp[i][j] = 1;
27+
}
28+
}
29+
}
30+
31+
for (int i = N-1; i >= 0; i--) {
32+
for (int j = 1; j <= X; j++) {
33+
if (A[i] <= j) {
34+
dp[i][j] = dp[i+1][j] + dp[i+1][j-A[i]];
35+
} else {
36+
dp[i][j] = dp[i+1][j];
37+
}
38+
}
39+
}
40+
System.out.print("Number of Subsequences with sum less than or equal to given sum: ");
41+
return dp[0][X] - 1;
42+
}
43+
}

0 commit comments

Comments
 (0)