-
Notifications
You must be signed in to change notification settings - Fork 2
/
Interesting_Array.java
70 lines (66 loc) · 1.92 KB
/
Interesting_Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.company;
import java.io.IOException;
import java.util.Scanner;
public class Interesting_Array {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
int k = s.nextInt();
intrestingArray(n, arr, k);
}
}
private static void intrestingArray(int n, int[] arr, int k) {
//Brute-Force Approach
// for (int i = 0; i < n - 1; i++) {
// for (int j = i + 1; j < n; j++) {
// if (arr[i] + arr[j] == k) {
// System.out.println(i + " " + j);
// return;
// }
// }
// }
// System.out.println("no answer");
//Optimized Approach
int y = 0, x = 0;
for (int i = n - 1; i >= 0; i--) {
x = k % arr[i];
y = check(x, arr, n);
if (x != -1 && arr[i] + arr[y] == k && y < i) {
System.out.println(y + " " + i);
return;
}
}
System.out.println("no answer");
}
private static int check(int x, int[] arr, int n) {
//Using Linear Search
// for (int i = 0; i < n; i++) {
// if (arr[i] == x) {
// return i;
// }
// if(x < arr[i]){
// break;
// }
// }
//Using Binary Search
int front = 0;
int last = n;
while (front <= last){
int mid = (front + last) / 2;
if(arr[mid] == x){
return mid;
}else if(x > arr[mid]){
front = mid + 1;
}else {
last = mid - 1;
}
}
return -1;
}
}