Skip to content

Commit aaef137

Browse files
committed
#Modification 27
1 parent f057dc7 commit aaef137

13 files changed

Lines changed: 144 additions & 12 deletions

arrays/Array_Problem_21.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/*
3+
* Problem Title :->
4+
* Find if there is any sub array with sum equal to 0
5+
*/
6+
7+
import java.util.*;
8+
9+
/*
10+
* Generalisation
11+
* a[2, 3, -1, -2, 4, 5]; -> The array
12+
* here their are n^2 + 1 sub arrays, in an array of any size n.
13+
* now the sub array of have a sum equal to zero is from index a[1] to index a[3] in this case.
14+
*/
15+
316
public class Array_Problem_21 {
17+
static Boolean subArrayExists(int[] a) {
18+
// Creates an empty hash set h_s
19+
Set<Integer> hs = new HashSet<>();
20+
// Initialise sum of elements
21+
int sum = 0;
22+
// Traverse through the given array
23+
for(int i = 0; i < a.length; i++) {
24+
// Add current element to sum
25+
sum += a[i];
26+
/*
27+
* Return true in following cases
28+
* a). Current element is 0
29+
* b). sum of elements from 0 to i is 0
30+
* c). sum is already present in hash map
31+
*/
32+
if(a[i] == 0 || sum == 0 || hs.contains(sum)) return true;
33+
// Add sum to hash set
34+
hs.add(sum);
35+
}
36+
// We reach here only when there is no sub_array with 0 sum
37+
return false;
38+
}
39+
// Driver Code
40+
public static void main(String[] args) {
41+
int[] a = {2, 3, -1, -2, 4, 5};
42+
if(subArrayExists(a))
43+
System.out.println("Found a subarray with 0 sum");
44+
else
45+
System.out.println("No Such Sub Array Exists!");
46+
}
447
}

arrays/Array_Problem_22.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Find factorial of a large number
3+
*/
34
public class Array_Problem_22 {
5+
6+
// This function finds factorial of large numbers and prints them
7+
static void factorial(int n) {
8+
9+
int[] res = new int[500];
10+
11+
res[0] = 1;
12+
13+
int res_size = 1;
14+
15+
for(int x = 2; x <= n; x++)
16+
res_size = multiply(x, res, res_size);
17+
18+
System.out.println("Factorial of given number is ");
19+
20+
for(int i = res_size - 1; i >= 0; i--)
21+
System.out.print(res[i]);
22+
}
23+
24+
// This function multiplies x with the number represented by res[].
25+
static int multiply(int x, int[] res, int res_size) {
26+
27+
int carry = 0;
28+
29+
// one by one multiply n with individual digits of res[].
30+
for(int i = 0; i < res_size; i++) {
31+
int prod = res[i] * x + carry;
32+
res[i] = prod % 10;
33+
carry = prod/10;
34+
}
35+
36+
// put carry in res and increase result size
37+
while(carry != 0) {
38+
res[res_size] = carry % 10;
39+
carry = carry/10;
40+
res_size++;
41+
}
42+
return res_size;
43+
}
44+
45+
// Driver program
46+
public static void main(String[] args) {
47+
factorial(100);
48+
}
449
}

arrays/Array_Problem_24.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/*
3+
* Problem Title :-> Find longest consecutive subsequence
4+
*/
5+
6+
import java.util.*;
7+
38
public class Array_Problem_24 {
9+
10+
static int findLogestSubseq(int[] a, int n) {
11+
// Sort the array
12+
Arrays.sort(a);
13+
int ans = 0, count = 0;
14+
15+
ArrayList<Integer> v = new ArrayList<>();
16+
v.add(10);
17+
18+
// Insert repeated elements only once in the vector
19+
for(int i = 1; i < n; i++) {
20+
if(a[i] != a[i -1]) v.add(a[i]);
21+
}
22+
23+
// Find the max length by traversing the array
24+
for(int i = 0; i < v.size(); i++) {
25+
// check if the current element is equal to previous element + 1
26+
if(i > 0 && v.get(i) == v.get(i - 1) + 1) count++;
27+
else count = 1;
28+
// Update the maximum
29+
ans = Math.max(ans, count);
30+
}
31+
return ans;
32+
}
33+
34+
// Driver Code
35+
public static void main(String[] args) {
36+
int[] a = {1,9,3,10,4,20,2};
37+
int n = a.length;
38+
System.out.println("Length of the Longest "+ "contiguous subsequence is " + findLogestSubseq(a, n));
39+
}
440
}

arrays/Array_Problem_25.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Given an array of size n and a number k, fin all elements that appear more than " n/k " times.
3+
*/
34
public class Array_Problem_25 {
45
}

arrays/Array_Problem_26.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Maximum profit by buying and selling a share atmost twice
3+
*/
34
public class Array_Problem_26 {
45
}

arrays/Array_Problem_27.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package arrays;
22

3-
/* Problem Title :-> */
3+
/* Problem Title :-> Find whether an array is a subset of another array
4+
*/
45
public class Array_Problem_27 {
56

67

arrays/Array_Problem_28.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Find whether an array is a subset of another array
3+
*/
34
public class Array_Problem_28 {
45
}

arrays/Array_Problem_29.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package arrays;
2-
/* Problem Title :-> */
2+
/* Problem Title :-> Find the triplet that sum to a given value
3+
*/
34
public class Array_Problem_29 {
45
}

arrays/Array_Problem_4_Approach1.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package arrays;
23
import java.io.*;
34

binary_search_tree/Deletion_of_a_node_in_bst.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package binary_search_tree;
2+
13
/**
24
* Deletion_of_a_node_in_bst
35
*/

0 commit comments

Comments
 (0)