Created
January 19, 2018 04:32
-
-
Save oliverwreath/60255a2566cfd1ef5a1e750eaed22c7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| import java.util.*; | |
| import java.text.*; | |
| import java.math.*; | |
| import java.util.regex.*; | |
| public class Solution { | |
| /** | |
| * @param arr | |
| * @param money | |
| */ | |
| static void solve(int[] arr, int money) { | |
| // Complete this function | |
| if (arr == null || arr.length < 1) { | |
| System.out.println(-1 + " " + -1); | |
| return; | |
| } | |
| // core logic | |
| HashMap<Integer, Integer> hashMap = new HashMap<>(); | |
| for (int i = 0; i < arr.length; i++) { | |
| if (arr[i] < money) { | |
| if (hashMap.containsKey(money - arr[i])) { | |
| int index = hashMap.get(money - arr[i]); | |
| System.out.println((index + 1) + " " + (i + 1)); | |
| return; | |
| } | |
| if (!hashMap.containsKey(arr[i])) { | |
| hashMap.put(arr[i], i); | |
| } | |
| } | |
| } | |
| } | |
| 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(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment