-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93c46bc
commit a2fc5a7
Showing
12 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
Day4-Arrays/CodeStudio/Count_Subarrays_with_Given_XOR.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.*; | ||
|
||
public class Solution { | ||
public static int subarraysXor(ArrayList<Integer> arr, int x) { | ||
// Write your code here. | ||
HashMap<Integer,Integer> visited = new HashMap<Integer,Integer>(); | ||
int c = 0; | ||
int cpx = 0; | ||
int n = arr.size(); | ||
for(int i = 0;i<n;i++) { | ||
cpx = cpx ^ arr.get(i); | ||
if(visited.get(cpx^x) != null) | ||
c += visited.get(cpx ^ x); | ||
if(cpx == x) { | ||
c++; | ||
} | ||
if(visited.get(cpx) != null) | ||
visited.put(cpx, visited.get(cpx) + 1); | ||
else | ||
visited.put(cpx, 1); | ||
} | ||
return c; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Day4-Arrays/CodeStudio/Find_Four_Elements_That_Sums_To_A_Given_Value.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import java.util.* ; | ||
import java.io.*; | ||
class Pair | ||
{ | ||
public int x, y; | ||
|
||
Pair(int x, int y) | ||
{ | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
public class Solution { | ||
public static String fourSum(int[] nums, int target, int n) { | ||
// Write your code here. | ||
Map<Integer, List<Pair>> map = new HashMap<>(); | ||
for (int i = 0; i < n - 1; i++) | ||
{ | ||
for (int j = i + 1; j < n; j++) | ||
{ | ||
int val = target - (nums[i] + nums[j]); | ||
if (map.containsKey(val)) | ||
{ | ||
for (Pair pair: map.get(val)) | ||
{ | ||
int x = pair.x; | ||
int y = pair.y; | ||
if ((x != i && x != j) && (y != i && y != j)) | ||
{ | ||
// System.out.println("Quadruplet Found (" | ||
// + nums[i] + ", " + nums[j] + ", " | ||
// + nums[x] + ", " + nums[y] + ")"); | ||
return "Yes"; | ||
} | ||
} | ||
} | ||
map.putIfAbsent(nums[i] + nums[j], new ArrayList<>()); | ||
map.get(nums[i] + nums[j]).add(new Pair(i, j)); | ||
} | ||
} | ||
|
||
// return false if quadruplet doesn't exist | ||
return "No"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.util.*; | ||
public class Solution { | ||
public static int lengthOfLongestConsecutiveSequence(int[] arr, int N) { | ||
// Write your code here. | ||
int c = 0; | ||
HashSet<Integer> hs = new HashSet<>(); | ||
for(int i=0;i<N;i++){ | ||
hs.add(arr[i]); | ||
} | ||
for(int num: arr){ | ||
if(!hs.contains(num-1)){ | ||
int cur = num; | ||
int count = 1; | ||
while(hs.contains(cur+1)){ | ||
cur++; | ||
count++; | ||
} | ||
c = Math.max(c,count); | ||
} | ||
} | ||
return c; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import java.util.*; | ||
public class Solution { | ||
|
||
public static int LongestSubsetWithZeroSum(ArrayList<Integer> arr) { | ||
|
||
// Write your code here. | ||
int max=0,sum=0; | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
map.put(sum,-1); | ||
for(int i=0;i<arr.size();i++){ | ||
sum+=arr.get(i); | ||
if(map.containsKey(sum)==false){ | ||
map.put(sum,i); | ||
}else{ | ||
max=Math.max(max,i-map.get(sum)); | ||
} | ||
} | ||
return max; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Day4-Arrays/CodeStudio/Longest_Substring_Without_Repeating_Characters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.util.*; | ||
public class Solution | ||
{ | ||
public static int uniqueSubstrings(String input) | ||
{ | ||
//write your code here | ||
int i = 0, j = 0, max = 0; | ||
Set<Character> set = new HashSet<>(); | ||
|
||
while (j < input.length()) { | ||
if (!set.contains(input.charAt(j))) { | ||
set.add(input.charAt(j++)); | ||
max = Math.max(max, set.size()); | ||
} else { | ||
set.remove(input.charAt(i++)); | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.util.*; | ||
public class Solution{ | ||
public static int[][] pairSum(int[] arr, int s) { | ||
// Write your code here. | ||
ArrayList<Integer> list = new ArrayList<>(); | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
for(int i=0;i<arr.length;i++){ | ||
int sum = s - arr[i]; | ||
if(map.containsKey(sum)){ | ||
int n = map.get(sum); | ||
while(n-->0){ | ||
list.add(sum); | ||
list.add(arr[i]); | ||
} | ||
} | ||
if(map.containsKey(arr[i])){ | ||
int n = map.get(arr[i])+1; | ||
map.put(arr[i],n); | ||
}else | ||
map.put(arr[i],1); | ||
} | ||
int[][] res = new int[list.size()/2][2]; | ||
int j=0; | ||
for(int i=0;i<list.size();i=i+2){ | ||
int a =list.get(i),b=list.get(i+1); | ||
if(a<b){ | ||
res[j][0]=list.get(i); | ||
res[j][1]=list.get(i+1); | ||
}else{ | ||
res[j][0]=list.get(i+1); | ||
res[j][1]=list.get(i); | ||
} | ||
j++; | ||
} | ||
Arrays.sort(res,(a,b)->a[0]-b[0]); | ||
return res; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Day4-Arrays/GeeksForGeeks/Largest_subarray_with_0_sum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// { Driver Code Starts | ||
import java.util.*; | ||
|
||
class MaxLenZeroSumSub | ||
{ | ||
|
||
// Returns length of the maximum length subarray with 0 sum | ||
|
||
// Drive method | ||
public static void main(String arg[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int T = sc.nextInt(); | ||
while (T > 0) | ||
{ | ||
int n = sc.nextInt(); | ||
int arr[] = new int[n]; | ||
for (int i = 0; i < n; i++) | ||
arr[i] = sc.nextInt(); | ||
|
||
GfG g = new GfG(); | ||
System.out.println(g.maxLen(arr, n)); | ||
T--; | ||
} | ||
} | ||
}// } Driver Code Ends | ||
|
||
|
||
class GfG | ||
{ | ||
int maxLen(int arr[], int n) | ||
{ | ||
// Your code here | ||
int max=0,sum=0; | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
map.put(sum,-1); | ||
for(int i=0;i<n;i++){ | ||
sum+=arr[i]; | ||
if(map.containsKey(sum)==false){ | ||
map.put(sum,i); | ||
}else{ | ||
max=Math.max(max,i-map.get(sum)); | ||
} | ||
} | ||
return max; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
static long subarrayXor(int arr[], int n, int m) | ||
{ | ||
long ans = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int xorSum = 0; | ||
for (int j = i; j < n; j++) | ||
{ | ||
xorSum = xorSum ^ arr[j]; | ||
if (xorSum == m) | ||
ans++; | ||
} | ||
} | ||
return ans; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
int[] res = new int[2]; | ||
HashMap<Integer, Integer> map = new HashMap<>(); | ||
for(int i=0;i<nums.length;i++){ | ||
int sum = target-nums[i]; | ||
if(map.containsKey(sum)){ | ||
res[0]=map.get(sum); | ||
res[1]=i; | ||
} | ||
map.put(nums[i],i); | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
class Solution { | ||
public List<List<Integer>> fourSum(int[] nums, int target) { | ||
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>(); | ||
int len = nums.length; | ||
if (nums == null || len < 4) | ||
return res; | ||
|
||
Arrays.sort(nums); | ||
|
||
int max = nums[len - 1]; | ||
if (4 * nums[0] > target || 4 * max < target) | ||
return res; | ||
|
||
int i, z; | ||
for (i = 0; i < len; i++) { | ||
z = nums[i]; | ||
if (i > 0 && z == nums[i - 1])// avoid duplicate | ||
continue; | ||
if (z + 3 * max < target) // z is too small | ||
continue; | ||
if (4 * z > target) // z is too large | ||
break; | ||
if (4 * z == target) { // z is the boundary | ||
if (i + 3 < len && nums[i + 3] == z) | ||
res.add(Arrays.asList(z, z, z, z)); | ||
break; | ||
} | ||
|
||
threeSumForFourSum(nums, target - z, i + 1, len - 1, res, z); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
public void threeSumForFourSum(int[] nums, int target, int low, int high, ArrayList<List<Integer>> fourSumList, | ||
int z1) { | ||
if (low + 1 >= high) | ||
return; | ||
|
||
int max = nums[high]; | ||
if (3 * nums[low] > target || 3 * max < target) | ||
return; | ||
|
||
int i, z; | ||
for (i = low; i < high - 1; i++) { | ||
z = nums[i]; | ||
if (i > low && z == nums[i - 1]) // avoid duplicate | ||
continue; | ||
if (z + 2 * max < target) // z is too small | ||
continue; | ||
|
||
if (3 * z > target) // z is too large | ||
break; | ||
|
||
if (3 * z == target) { // z is the boundary | ||
if (i + 1 < high && nums[i + 2] == z) | ||
fourSumList.add(Arrays.asList(z1, z, z, z)); | ||
break; | ||
} | ||
|
||
twoSumForFourSum(nums, target - z, i + 1, high, fourSumList, z1, z); | ||
} | ||
|
||
} | ||
|
||
public void twoSumForFourSum(int[] nums, int target, int low, int high, ArrayList<List<Integer>> fourSumList, | ||
int z1, int z2) { | ||
|
||
if (low >= high) | ||
return; | ||
|
||
if (2 * nums[low] > target || 2 * nums[high] < target) | ||
return; | ||
|
||
int i = low, j = high, sum, x; | ||
while (i < j) { | ||
sum = nums[i] + nums[j]; | ||
if (sum == target) { | ||
fourSumList.add(Arrays.asList(z1, z2, nums[i], nums[j])); | ||
|
||
x = nums[i]; | ||
while (++i < j && x == nums[i]) // avoid duplicate | ||
; | ||
x = nums[j]; | ||
while (i < --j && x == nums[j]) // avoid duplicate | ||
; | ||
} | ||
if (sum < target) | ||
i++; | ||
if (sum > target) | ||
j--; | ||
} | ||
return; | ||
} | ||
} |
Oops, something went wrong.