Posts

Showing posts from January, 2023

Make array elements unique. Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/6e63df6d2ebdf6408a9b364128bb1123b5b13450/1 It is also the problem of the day for 11-01-2023 Solution: class Solution {     public long minIncrements(int[] arr, int n) {        long count=0;        Arrays.sort(arr);     //   for(int a:arr){     //       System.out.print(a+" ");     //   }        HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();       int index=1;       for(int a:arr){           if(index<a){               index=a;               index++;               map.put(a,1);               //condition1           }           else if(index==a){               index++;               map.put(index,1);               //condition2           }           else{               map.put(index,1);               count+=index-a;               index++;               //condition3           }         //   System.out.print(count+" ");       }     //   System.out.println(map);       return count;     }