-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pair_with_Difference.c
55 lines (42 loc) · 926 Bytes
/
Pair_with_Difference.c
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
// Given an array A of sorted integers and another non negative integer k, find if there exists 2 indices i and j such that A[j] - A[i] = k, i != j.
// Input Format:
// First Line n - Number of elements in an array
// Next n Lines - N elements in the array
// k - Non - Negative Integer
// Output Format:
// 1 - If pair exists
// 0 - If no pair exists
// Explanation for the given Sample Testcase:
// YES as 5 - 1 = 4
// So Return 1.
// For example:
// Input Result
// 3 1
// 1 3 5
// 4
#include<stdio.h>
int find(int arr[],int k,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[j]-arr[i]==k && i!=j)
return 1;
}
}
return 0;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n],k;
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
scanf("%d",&k);
if(find(arr,k,n))
printf("1");
else
printf("0");
}