Ice Cream Parlor

Sort by

recency

|

963 Discussions

|

  • + 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();
        }
    
  • + 0 comments

    **Python Solution : **

    from itertools import combinations
        
    def icecreamParlor(m, arr):
        # Write your code here
        store=[]
        comb=combinations(enumerate(arr),2)
        for (i,_),(j,_) in comb:
            if(arr[i]+arr[j]==m):
                store=[i+1,j+1]
        return store
    
  • + 0 comments

    Mega easy Perl solution:

    sub icecreamParlor {
        my $m = shift;
        my $arr = shift;
    
        my @res_arr;
    
        for (my $i = 0; $i < scalar(@$arr); $i++){
            for (my $j = $i + 1; $j < scalar(@$arr); $j++) {
                if ($m == ($arr->[$i] + $arr->[$j])) {
                    push(@res_arr, $i+1, $j+1);
                    return sort {$a <=> $b} @res_arr;
                }
                
            }
        }
    }
    
  • + 0 comments

    C#

        public static List<int> icecreamParlor(int m, List<int> arr)
        {   
            List<int> returnA = new List<int>();
    
            for (int i = 0; i < arr.Count; i++){
                
                for (int j = 0; j < arr.Count; j++){
                    
                    if (arr[i] + arr[j] == m && i != j){
                        returnA = [(i + 1), (j + 1)];
                        returnA.Sort();
                        break;
                    }
                }
            
            }
            
            return returnA;
            
        }
    
  • + 0 comments

    result = [] for index,i in enumerate(arr): try: pos = arr.index(m-i, index+1, len(arr)) result=[index+1,pos+1] return result except ValueError: pass