Ice Cream Parlor

  • + 0 comments

    Solution with time complexity O(nlogn) it's consider effcient with large Lists and there is possible to sorting one time but Iam a little bit lazy :)

    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
        int n = arr.size();
        Integer[] indices = new Integer[n];
        
        for (int i = 0; i < n; i++) {
            indices[i] = i;
        }
        
        Arrays.sort(indices, Comparator.comparingInt(i -> arr.get(i)));
        Collections.sort(arr);
        
        int i = 0, j = n - 1;
        while(i <= j){
          if( arr.get(i) + arr.get(j)  > m)
            j--;
          else if(arr.get(i) + arr.get(j)  < m)
            i++;
          else{
            int firstIndex = indices[i] + 1;
            int secondIndex = indices[j] + 1;
            List<Integer>result = Arrays.asList(firstIndex, secondIndex);
            Collections.sort(result);
            return result;
            }  
          }
        return new ArrayList();
        }