Ice Cream Parlor

Problem Statement :

Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money.

Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.


The two flavors that cost  and  meet the criteria. Using -based indexing, they are at indices  and .

Function Description

Complete the icecreamParlor function in the editor below.

icecreamParlor has the following parameter(s):

int m: the amount of money they have to spend
int cost[n]: the cost of each flavor of ice cream

int[2]: the indices of the prices of the two flavors they buy, sorted ascending

Input Format

The first line contains an integer, , the number of trips to the ice cream parlor. The next  sets of lines each describe a visit.

Each trip is described as follows:

The integer , the amount of money they have pooled.
The integer , the number of flavors offered at the time.
 space-separated integers denoting the cost of each flavor: .
Note: The index within the cost array represents the flavor of the ice cream purchased.

Solution :


                            Solution in C :

In   C++  :

#include <vector> // headers {{{
#include <list>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <ctime>

using namespace std; // }}}

int main()
    //time_t start, end;

    //ifstream cin("");
    //ofstream cout("test.out");

    int T;
    cin >> T;
    for (int i = 0; i < T; ++i) {
        int C, L;
        cin >> C >> L;
        vector<int> v(L);
        map<int, int> M;
        for (int j = 0; j < L; ++j)
            cin >> v[j];
        for (int j = 0; j < L; ++j) {
            int diff = C - v[j];
            if (M.count(diff)) {
                cout << 1 + M[diff] << " " << 1 + j << endl;
            M[v[j]] = j;

    //cout << difftime(end, start) << endl;

    return 0;

In  Java  :

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        int n = sc.nextInt();
        for(int k = 0; k < n; k++){
            int C = sc.nextInt();
            int P = sc.nextInt();
            int [] p = new int[P+1];
            for(int i = 1; i <= P;i++)p[i] = sc.nextInt();
            for(int i = 1; i <= P; i++){
                for(int j = i+1; j <= P; j++){
                    if(p[i]+p[j] == C){
                        System.out.println(i + " " + j);

In   C  :

int main()
	int t,c,l,i,j,arr[20000];
			printf("%d %d\n",i+1,j+1);
	return 0;

In   Python3 :

def main():
    # Loop through for each test.
    for _ in range(int(input())):
        dollars = int(input())
        numFlavors = int(input())
        flavors = [int(i) for i in input().split()]

        # Determine the correct indexes.
        index1, index2 = -1, -1
        for i in range(numFlavors):
            for j in range(numFlavors):
                if (i != j) and (flavors[i] + flavors[j] == dollars):
                    index1 = i + 1
                    index2 = j + 1
            if index1 != -1 and index2 != -1:
        # Print the answer.
        print(str(index1) + " " + str(index2))
if __name__ == "__main__":

View More Similar Problems

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →


We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →