Ice Cream Parlor

Sort by

recency

|

953 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/WvLuA-LCsb0

    vector<int> icecreamParlor(int m, vector<int> arr) {
        map<int, int> mp;
        for (int i = 0; i < arr.size(); i++) {
            int complement = m - arr[i];
            if (mp.count(complement)) {
                return {mp[complement] + 1, i + 1};
            }
            mp[arr[i]] = i;
        }
        return {};
    }
    
  • + 0 comments

    JAVA

    import java.util.Scanner; import java.util.HashMap; public class Solution { public static void main(String[]args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while (t-->0) { int m=scan.nextInt(); int n=scan.nextInt(); int[]costs=new int[n]; for(int i=0;imap=new HashMap(); for (int i=0;i int otherCost=money-cost; if (map.containsKey(otherCost)) { System.out.println(map.get(otherCost)+" "+icecreamID); } map.putIfAbsent(cost,icecreamID); } } }

  • + 0 comments

    Python

    def icecreamParlor(m, arr):
        n = len(arr)
        for i in range(0, n):
            for j in range(i+1, n):
                if arr[i] + arr[j] == m:
                    return i+1, j+1
    
  • + 0 comments

    using a hash_map in a simple O(n) sol

    def icecreamParlor(m, arr):
        # Write your code here
        hash_table = {}
        for index, element in enumerate(arr):
            if element in hash_table:
                return sorted((index+1, hash_table[element]))
            else:
                hash_table[m - element] = index+1
    
  • + 0 comments
    def icecreamParlor(m, arr):
        selected_flavors = [[i+1, j+1] for i in range(0, n-1) for j in range(i+1, n) if arr[i]+arr[j] == m]
        return selected_flavors[0]