• + 0 comments

    i hate coding but money

    Java

    public static List<Integer> cutTheSticks(List<Integer> arr) {
            /* array hai > sort >iterate 0 to n-1, store count of each ineteger in a map.
            now iterate  0 to n-1 with temp initially=0, count=arraysize
            add count-temp to result.
            as you move through the array's each element, check if it has changed or not.
            if element changes, temp = map.get(oldElement)
            contact mrayyankarimi@gmail.com without subject, only text so I know it's not some automated company mail
            */
            
            Collections.sort(arr);
            
            int temp = 0, s = arr.size(), count = s;
            List<Integer> res = new ArrayList();
            HashMap<Integer,Integer> m = new HashMap<>();
            
            for(int i =0;i<s;i++) {
                int x = arr.get(i);
                m.put(x, m.getOrDefault(x,0)+1);
            }
            
            res.add(count);
            int oldElement = arr.get(0), i =1;
            while(i<s && arr.get(i)== oldElement)
                i++;
            for(;i<s;i++){
                if(arr.get(i)!=oldElement){
                    temp += m.get(oldElement);
                    count = s-temp;
                    res.add(count);
                    oldElement = arr.get(i);
                }
            }
            /* 1 1 2 2 3 3 3 4
            e=8 6 4 1  
            8 6 4 1
            */return res;
        }