We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I also can't understand why to use binarySearch for this problem, it is kind of twoSum problems. I watched the video to introduce the binarySearch method, it is so complicated, since we need the indices of the value, I think it's better to keep the orignal array. Below is my Java solution, it works well.
public class Solution {
static void solve(int[] arr, int money) {
// Complete this function
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < arr.length; i++){
int target = money-arr[i];
if(arr[i] < money){
if(map.containsKey(target))
System.out.println(map.get(target) + " " + (i+1));
break;
else
map.put(arr[i], i+1);
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
int money = in.nextInt();
int n = in.nextInt();
int[] arr = new int[n];
for(int arr_i = 0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
}
solve(arr, money);
}
in.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
I also can't understand why to use binarySearch for this problem, it is kind of twoSum problems. I watched the video to introduce the binarySearch method, it is so complicated, since we need the indices of the value, I think it's better to keep the orignal array. Below is my Java solution, it works well.
public class Solution {
}