Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Ice Cream Parlor problem solution

YASH PAL, 31 July 202423 January 2026

In this HackerRank Ice Cream Parlor problem solution, 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.

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

Returns

  • int[2]: the indices of the prices of the two flavors they buy, sorted ascending
HackerRank Ice Cream Parlor problem solution

HackerRank Ice Cream Parlor problem solution in Python.

for i in range(int(input())):
target = int(input())
loop = int(input())
costs = input().split(" ")
cur = 0
done = False
for j in range(loop - 1):
for k in range(j+1, loop):
if(int(costs[j]) + int(costs[k]) == target):
print(str(j+1) + " " + str(k+1))
done = True
break;
if(done): break

Ice Cream Parlor problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- != 0){
int m = sc.nextInt();
int n = sc.nextInt();
int[] np = new int[n];
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n ; i++){
np[i] = sc.nextInt();
if(map.containsKey(np[i]) == false)
map.put(np[i], i+1);
}

Arrays.sort(np);

int s = 0;
int e = n - 1;
while(s < e){
if(np[s] + np[e] > m){
e--;
} else if (np[s] + np[e] < m){
s++;
} else {
int i1 = map.get(np[s]);
int i2 = map.get(np[e]);
if(np[s] == np[e])
i2++;

System.out.printf("%d %d%n", Math.min(i1, i2), Math.max(i1, i2));
e--;
s++;
}
}
}
}
}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    
    int tc, i, k, flag, x, y, sum, C, L, Li[10009];
    scanf("%d", &tc);
    while( tc-- )
    {
        flag=0;
        scanf("%d %d %d", &C, &L, &Li[0]);
        for(i=1; i<L; i++)
        {
            scanf("%d", &Li[i]);
            sum=Li[0]+Li[i];
            if(sum==C)
            {
                flag=1;
                y=i+1;
            }
        }
        if(flag) printf("1 %dn", y);
        else
        {
            for(i=1; i<L; i++)
            {
                for(k=i+1; k<L; k++)
                {
                    sum=Li[i]+Li[k];
                    if(sum==C)
                    {
                        flag=1;
                        x=i+1;
                        y=k+1;
                        break;
                    }
                }
                if(flag) break;
            }
            printf("%d %dn", x, y);
        }

    }
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int n,m;
    scanf("%d",&n);
    int i=0;
    for(;i<n;i++)
    {
        int c ;
        scanf("%d",&c);
        scanf("%d",&m);
        int data[m];
        int j=0;
        for(;j<m;j++)
        {
            scanf("%d",&data[j]);
            //printf("%d ",data[j]);
        }
        int k=0;
        for(j=0;j<m;j++)
        {
            for(k=j+1;k<m;k++)
            {
                if(data[j]+data[k]==c)
                {
                    printf("%d %dn",j+1,k+1);
                    goto flag;
                }
            }
        }
       flag:;
    }
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes