Skip to content

Commit

Permalink
largest sub array of sum equal to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush1oo8 committed Mar 16, 2024
1 parent 602a705 commit 786449d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Hashing/largestSubarraySum0.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Hashing;
import java.util.*;

public class largestSubarraySum0{
public static void main(String[] args) {
int arr[] = {15, -2, -2, -8, 1, 7, 10, 23};

HashMap<Integer, Integer> map = new HashMap<>();
int sum = 0;
int len = 0;

for (int j = 0; j < arr.length; j++) {
sum += arr[j];
if (sum == 0) {
len = j + 1;
} else {
if (map.containsKey(sum)) {
len = Math.max(len, j - map.get(sum));
} else {
map.put(sum, j);
}
}
}
System.out.println("Largest subarray with sum equal to 0: " + len);
}
}

0 comments on commit 786449d

Please sign in to comment.