-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_maps_Question.java
More file actions
108 lines (88 loc) · 3 KB
/
hash_maps_Question.java
File metadata and controls
108 lines (88 loc) · 3 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.*;
public class hash_maps_Question {
public static void majorityElement (int nums[]){
HashMap <Integer, Integer > map = new HashMap<>();
int n = nums.length ;
for (int i = 0 ; i <n ; i++){
if (map.containsKey(nums[i])){
map.put (nums[i] , map.get(nums[i]) + 1);
}
else {
map.put (nums[i] ,1);
}
}
for (int key : map.keySet()){
if (map.get(key) > n/3){
System.out.print(key + " ");
}
}
}
public static int Intersection (int [] arr1 , int [] arr2){
int count =0 ;
HashSet <Integer> set = new HashSet<>();
for (int i = 0 ; i < arr1.length ; i++){
set.add(arr1[i]);}
for (int j = 0 ; j <arr2.length ; j++ ){
if(set.contains(arr2[j])){
System.out.println(arr2[j]);
set.remove(arr2[j]) ;
count++;
}
}
return count ;
}
public static String Iternernary (HashMap <String , String > ticket){
HashMap <String , String > revMap = new HashMap<>();
for (String key : ticket.keySet()){
revMap.put(ticket.get(key) , key);
}
for (String key : ticket.keySet()){
if (!revMap.containsKey(key)){
return key ;
}
}
return null ;
}
public static int SubarraySum( int [] arr , int k){
HashMap <Integer , Integer > map = new HashMap<>() ;
map.put(0, 1);
int ans = 0 ;
int sum = 0 ;
for (int i = 0 ; i <arr.length ; i++){
sum += arr[i] ;
if (map.containsKey (sum-k)) {
ans += map.get(sum-k);
}
if (map.containsKey(sum)){
map.put(sum, map.get(sum)+1);
}
else{
map.put(sum, 1) ;
}
}
return ans ;
}
public static void main(String[] args) {
/* int [] nums = {1,3,2,5,1,3,1,5,5,5,1};
majorityElement(nums); */
/*int arr1 [] = {7,3,9};
int arr2[] = {6,3,9,2,9,4};
System.out.println( "count is :- "+Intersection(arr1, arr2));
*/
/*HashMap <String ,String > ticket = new HashMap<>( );
ticket.put("chennai", "bengaluru");
ticket.put("mumbai", "delhi");
ticket.put("goa", "chennai");
ticket.put("delhi", "goa" ) ;
String start = Iternernary (ticket);
while (ticket.containsKey(start)){
System.out.print(start + "--->");
start = ticket.get(start);
}
System.out.println(start);
*/
int []arr = {10,2,-2,-20,10};
int k = -10 ;
System.out.println(SubarraySum(arr, k));
}
}