Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Ice Cream Parlor problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Ice Cream Parlor problem solution, we have given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.

HackerRank Ice Cream Parlor problem solution

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

{“mode”:”full”,”isActive”:false}

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++;
                }
            }
        }
    }
}

{“mode”:”full”,”isActive”:false}

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;
}

{“mode”:”full”,”isActive”:false}

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;
}

{“mode”:”full”,”isActive”:false}

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 *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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