-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblems.java
More file actions
70 lines (56 loc) · 1.58 KB
/
Copy pathProblems.java
File metadata and controls
70 lines (56 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package hashingAndHashmap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Problems {
static int countDistinctElements(int a []){
Set<Integer> set = new HashSet<>();
for(int e :a){
set.add(e);
}
System.out.println(set);
return set.size();
}
static void frequencyOfElement(int a[]){
Map <Integer,Integer> map = new HashMap<>();
for(int e:a){
if(map.containsKey(e)){
int temp = map.get(e);
map.put(e,temp+1);
}else{
map.put(e,1);
}
}
System.out.println(map);
}
static boolean zeroSumSubarray(int a[]){
//cumSum means cumulutative sum
Set<Integer>set = new HashSet<>();
int cumSum = 0;
set.add(0);
for(int e:a){
if(set.contains(cumSum)) return true;
set.add(cumSum);
}
return false;
}
static boolean pairWithGivenSum(int a[],int sum){
Set<Integer> set = new HashSet<>();
for(int e:a){
int comp = sum-e;
if(set.contains(comp)) return true;
set.add(e);
}
return false;
}
public static void main(String[] args){
// int a[]={1,2,5,6,8,2,2,4,4,5};
// int a[]={10,-10};
int a[]={0,0};
// System.out.println(countDistinctElements(a));
// frequencyOfElement(a);
// System.out.println(pairWithGivenSum(a,8));
System.out.println(zeroSumSubarray(a));
}
}