forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG_SearchWhereAdjDifferAtmostK.cpp
55 lines (46 loc) · 1.12 KB
/
GFG_SearchWhereAdjDifferAtmostK.cpp
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
/*
Searching in an array where adjacent differ by at most k
https://practice.geeksforgeeks.org/problems/searching-in-an-array-where-adjacent-differ-by-at-most-k0456/1/#
*/
// { Driver Code Starts
// C++ program to search an element in an array
// where difference between all elements is 1
#include<bits/stdc++.h>
using namespace std;
// x is the element to be searched in arr[0..n-1]
// such that all elements differ by at-most k.
int search(int arr[], int n, int x, int k);
// Driver program to test above function
int main()
{
int t,n,k,x;
cin>>t;
while(t--)
{
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cin>>x;
cout<<search(arr, n, x, k)<<endl;
}
return 0;
}
// } Driver Code Ends
// x is the element to be searched in arr[0..n-1]
// such that all elements differ by at-most k.
int search(int arr[], int n, int x, int k)
{
int i=0;
while(i<n)
{
if(arr[i] == x) return i;
// else
// {
// int diff = x-arr[i];
// jmp = diff/k;
// }
i+= max(1, (x-arr[i])/k);
}
return -1;
}