-
Notifications
You must be signed in to change notification settings - Fork 154
/
sum
51 lines (39 loc) · 1.04 KB
/
sum
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
class sum {
// Function to count pairs in array
// with sum less than x.
static int findPairs(int arr[], int n, int x)
{
int l = 0, r = n - 1;
int result = 0;
while (l < r)
{
// If current left and current
// right have sum smaller than x,
// the all elements from l+1 to r
// form a pair with current l.
if (arr[l] + arr[r] <= x)
{
System.out.println(arr[l]+" "+arr[r]);
result += (r - l);
l++;
}
// Move to smaller value
else
r--;
}
return result;
}
// Driver method
public static void main(String[] args)
{
int n1 = 100;
int arr[] = new int[n1];
for(int i =0;i<n1;i++)
arr[i] = i+1;
for(int i =0;i<n1;i++)
System.out.println(arr[i]);
int n = arr.length;
int x = n1+1;
System.out.print(findPairs(arr, n, x));
}
}