|
| 1 | +package hashmaps; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +public class practiceProblems { |
| 5 | + |
| 6 | + public static int lengthOfLongestSubsetWithZeroSum(ArrayList<Integer> arr) |
| 7 | + { |
| 8 | + if(arr.size()==1){ |
| 9 | + |
| 10 | + if(arr.get(0)==0){ |
| 11 | + return 1; |
| 12 | + } |
| 13 | + else{ |
| 14 | + return 0; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + int mx=0; |
| 20 | + int len=0; |
| 21 | + HashMap<Integer,Integer>mp=new HashMap<>(); |
| 22 | + int cum=0; |
| 23 | + mp.put(arr.get(0),0); |
| 24 | + |
| 25 | + for(int i=1;i<arr.size();i++){ |
| 26 | + cum+=arr.get(i); |
| 27 | + |
| 28 | + if(arr.get(i)==0 && mx==0){ |
| 29 | + mx=1; |
| 30 | + } |
| 31 | + |
| 32 | + if(mp.containsKey(cum) || cum==0){ |
| 33 | + if(mp.containsKey(cum)){ |
| 34 | + len=i-mp.get(cum); |
| 35 | + } |
| 36 | + if(cum==0){ |
| 37 | + len=i+1; |
| 38 | + } |
| 39 | + |
| 40 | + if(len>mx){ |
| 41 | + mx=len; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + else{ |
| 46 | + |
| 47 | + mp.put(cum,i); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + return mx; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public static void findPairsDifferenceK(int[] input, int k){ |
| 59 | + |
| 60 | + HashMap<Integer,Integer> mp=new HashMap<>(); |
| 61 | + |
| 62 | + |
| 63 | + for(int i=0;i<input.length;i++){ |
| 64 | + |
| 65 | + if(mp.containsKey(input[i])){ |
| 66 | + |
| 67 | + mp.put(input[i],mp.get(input[i])+1); |
| 68 | + } |
| 69 | + else{ |
| 70 | + mp.put(input[i],1); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + int count1=0; |
| 75 | + int count2=0; |
| 76 | + for(int i=0;i<input.length;i++){ |
| 77 | + |
| 78 | + if(k==0){ |
| 79 | + count1=mp.get(input[i]); |
| 80 | + int total=((count1-1)*count1)/2; |
| 81 | + while(total-->0){ |
| 82 | + System.out.println(input[i]+" "+input[i]); |
| 83 | + } |
| 84 | + } |
| 85 | + else{ |
| 86 | + if(mp.containsKey(input[i]-k) ){ |
| 87 | + count1=mp.get(input[i]); |
| 88 | + count2=mp.get(input[i]-k); |
| 89 | + |
| 90 | + int total=count1*count2; |
| 91 | + while(total-->0){ |
| 92 | + System.out.println((input[i]-k)+" "+input[i]); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + if(mp.containsKey(input[i]+k)){ |
| 97 | + count1=mp.get(input[i]); |
| 98 | + count2=mp.get(input[i]+k); |
| 99 | + |
| 100 | + int total=count1*count2; |
| 101 | + while(total-->0){ |
| 102 | + System.out.println(input[i]+" "+(input[i]+k)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + mp.put(input[i],0); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) { |
| 114 | + |
| 115 | + int n=arr.length; |
| 116 | + HashMap<Integer,Boolean> mp=new HashMap<>(); |
| 117 | + HashMap<Integer,Integer>pos=new HashMap<>(); |
| 118 | + |
| 119 | + for(int i=0;i<n;i++){ |
| 120 | + |
| 121 | + |
| 122 | + mp.put(arr[i],true); |
| 123 | + pos.put(arr[i],i); |
| 124 | + |
| 125 | + } |
| 126 | + int mx=1,start=0; |
| 127 | + |
| 128 | + for(int i=0;i<n;i++){ |
| 129 | + |
| 130 | + |
| 131 | + int len=0; |
| 132 | + int val=arr[i]; |
| 133 | + int curr=arr[i]; |
| 134 | + int index=i; |
| 135 | + while(mp.containsKey(val) && mp.get(val)==true){ |
| 136 | + |
| 137 | + len++; |
| 138 | + mp.put(val,false); |
| 139 | + val+=1; |
| 140 | + } |
| 141 | + |
| 142 | + val=curr-1; |
| 143 | + |
| 144 | + while(mp.containsKey(val) && mp.get(val)==true){ |
| 145 | + len++; |
| 146 | + // curr=val; |
| 147 | + |
| 148 | + mp.put(val,false); |
| 149 | + index= pos.get(val); |
| 150 | + val-=1; |
| 151 | + // index-=1; |
| 152 | + } |
| 153 | + |
| 154 | + if(mx<len){ |
| 155 | + mx=len; |
| 156 | + start=index; |
| 157 | + } |
| 158 | + else if(mx==len){ |
| 159 | + |
| 160 | + if(index<start){ |
| 161 | + start=index; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // mp.put(arr[i],false); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + ArrayList<Integer> out=new ArrayList<>(); |
| 170 | + out.add(arr[start]); |
| 171 | + int value=arr[start]; |
| 172 | + mx--; |
| 173 | + |
| 174 | + while(mx!=0) |
| 175 | + { |
| 176 | + value+=1; |
| 177 | + out.add(value); |
| 178 | + |
| 179 | + mx--; |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + return out; |
| 184 | + |
| 185 | + |
| 186 | + } |
| 187 | + // remove all duplicates |
| 188 | + public static String uniqueChar(String str){ |
| 189 | + |
| 190 | + HashMap<Character,Boolean> mp=new HashMap<>(); |
| 191 | + |
| 192 | + for(int i=0;i<str.length();i++){ |
| 193 | + mp.put(str.charAt(i),true); |
| 194 | + |
| 195 | + } |
| 196 | + String ans=""; |
| 197 | + for(int i=0;i<str.length();i++){ |
| 198 | + |
| 199 | + if(mp.get(str.charAt(i))==true){ |
| 200 | + ans+=str.charAt(i); |
| 201 | + mp.put(str.charAt(i),false); |
| 202 | + } |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + return ans; |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + public static void PairSumToZERO(int[] arr, int size) { |
| 211 | + |
| 212 | + HashMap<Integer,Integer>mp=new HashMap<>(); |
| 213 | + int sum=0; |
| 214 | + for(int i=0;i<size;i++){ |
| 215 | + |
| 216 | + if(mp.containsKey(sum-arr[i])){ |
| 217 | + |
| 218 | + int count=mp.get(sum-arr[i]); |
| 219 | + |
| 220 | + for(int j=0;j<count;j++){ |
| 221 | + |
| 222 | + if((sum-arr[i])<arr[i]){ |
| 223 | + System.out.println((sum-arr[i])+" "+arr[i]); |
| 224 | + } |
| 225 | + else{ |
| 226 | + System.out.println(arr[i]+" "+(sum-arr[i])); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + if(mp.containsKey(arr[i])){ |
| 231 | + mp.put(arr[i],mp.get(arr[i])+1); |
| 232 | + } |
| 233 | + else{ |
| 234 | + mp.put(arr[i],1); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + public static void intersection(int[] arr1, int[] arr2){ |
| 241 | + |
| 242 | + HashMap<Integer,Integer> mp=new HashMap<>(); |
| 243 | + |
| 244 | + for(int i:arr1){ |
| 245 | + |
| 246 | + if(mp.containsKey(i)){ |
| 247 | + |
| 248 | + mp.put(i,mp.get(i)+1); |
| 249 | + } |
| 250 | + |
| 251 | + else |
| 252 | + mp.put(i,1); |
| 253 | + } |
| 254 | + for(int i:arr2){ |
| 255 | + if(mp.containsKey(i)){ |
| 256 | + if(mp.get(i)>0){ |
| 257 | + System.out.println(i); |
| 258 | + } |
| 259 | + mp.put(i,mp.get(i)-1); |
| 260 | + // mp.put(i,false); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + } |
| 265 | + |
| 266 | + public static int maxFrequencyNumber(int[] arr){ |
| 267 | + |
| 268 | + HashMap<Integer,Integer> mp=new HashMap<>(); |
| 269 | + int countmax=Integer.MIN_VALUE; |
| 270 | + int ans=-1; |
| 271 | + for(int i=0;i<arr.length;i++){ |
| 272 | + |
| 273 | + if(mp.containsKey(arr[i])){ |
| 274 | + |
| 275 | + int val=mp.get(arr[i]); |
| 276 | + mp.put(arr[i],val+1); |
| 277 | + |
| 278 | + } |
| 279 | + else{ |
| 280 | + |
| 281 | + mp.put(arr[i],1); |
| 282 | + } |
| 283 | + |
| 284 | + } |
| 285 | + |
| 286 | + for( int str:arr){ |
| 287 | + |
| 288 | + if(mp.get(str)>countmax){ |
| 289 | + |
| 290 | + ans=str; |
| 291 | + countmax=mp.get(str); |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + return ans; |
| 296 | + |
| 297 | + } |
| 298 | +} |
0 commit comments