-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBeautifulTriplets.java
More file actions
76 lines (62 loc) · 1.43 KB
/
BeautifulTriplets.java
File metadata and controls
76 lines (62 loc) · 1.43 KB
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
71
72
73
74
75
76
//O(nlogn) currently, using binary search, as sorted order is given
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
public int search(int[] arr,int left,int right,int value)
{
int found=0,copy;
while(left<=right)
{
int mid = (left+right)/2;
if(value==arr[mid])
{
found=1;
copy = mid;
return mid;
}
else
{
if(value<arr[mid])
right=mid-1;
else
left=mid+1;
}
}
if(found==0)
{
return -1;
}
return -1;
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int n,d;
Scanner reader = new Scanner(System.in);
n= reader.nextInt();
d=reader.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=reader.nextInt();
}
int count=0,value1,value2,index1,index2;
for(int i=0;i<n;i++)
{
value1 = arr[i]+d;
index1 = search(arr,i+1,n-1,value1);
if(index1!=-1 && index1!=n-1)
{
value2 = arr[index1]+d;
index2= search(arr,index1+1,n-1,value2);
if(index2!=-1)
count++;
}
}
System.out.println(count);
}
}