[Hackerrank] ice cream parlor

Source: Internet
Author: User

Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. among n flavors available, they have to choose two distinct flavors whose cost equals m. given a list of cost of N flavors, output the indices of two items whose sum equals m. the cost of a flavor (CI) will be no more than 10000.

Input Format

The first line of the input contains t, t test cases follow.
Each test case follows the format: the first line contains M. The second line contains the number n. The third line contains n single space separated integers denoting the price of each flavor CI.

Output Format

Output two integers, each of which is a valid index of the flavor. The lower index must be printed first. Indices are indexed from 1 to n.

Constraints

1 ≤ T ≤ 50
2 ≤ m ≤ 10000
2 ≤ n ≤ 10000
1 ≤ CI ≤10000
The prices of two items may be same and each test case has a unique solution.

 

It is also a problem of finding two numbers in the array and M, similar to the two sum problem in leetcode.

In this case, the number in the array may be repeated, so the value in the map should be saved as an arraylist.

In addition, make sure that I is smaller than the coordinates found in map (as shown in the judgment of 26 lines of code) to avoid duplication.

The Code is as follows:

 1 import java.util.*; 2  3 public class Solution { 4     public static void main(String[] args) { 5         Scanner in = new Scanner(System.in); 6         int t = in.nextInt(); 7         while(t-- >0){ 8             int m = in.nextInt(); 9             int n = in.nextInt();10             int[] prices = new int[n];11             HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();12             13             for(int i = 0;i < n;i++){14                 prices[i]=in.nextInt();15                 if(!map.containsKey(prices[i]))16                     map.put(prices[i], new ArrayList<Integer>());17                 map.get(prices[i]).add(i);18             }19             20             for(int i = 0;i < n;i++){21                 int has = prices[i];22                 int toFind = m-prices[i];23                 24                 if(map.containsKey(toFind)){25                     for(int temp:map.get(toFind)){26                         if(i < temp){27                             System.out.printf("%d %d",i+1,temp+1);28                             System.out.println();29                         }30                     }31                 }32             }33         }       34     }35 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.