Searching in an array where adjacent differ by at most k | Practice | GeeksforGeeks

A step array is an array of integer where each element has a difference of at most k with its neighbor. Given a key x, we need to find the index value of x if multiple elements exist, return the first occurrence of the key. 

Example 1:
â€‹Input : arr[ ] = {4, 5, 6, 7, 6}, K = 1 
        and X = 6
Output : 2
Explanation:
In an array arr 6 is present at index 2.
So, return 2.


â€‹Example 2:
Input : arr[ ] = {20 40 50}, K = 20 
        and X = 70
Output :  -1 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function search() that takes an array (arr), sizeOfArray (n), an integer value X, another integer value K, and return an integer displaying the index of the element X in the array arr. If the element is not present in the array return -1. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 105
1 ≤ K ≤ 102
1 ≤ arr[i], X ≤ 105
0
amritkumar79791 week ago
int search(int arr[], int n, int x, int k){   for(int i=0; i<n; i++)   {       if(arr[i]==x)       {           return i;       }    }   return -1;}   
0
vishalsavade2 weeks ago
//IDK what this question wanted to convey but it is working
//for simple search 
int search(int arr[], int n, int x, int k)
{
    // Complete the function
    for(int i = 0; i < n; i++){
        if(arr[i] == x)
            return i;
    }
    
    return -1;
}
0
itsmemritu
This comment was deleted.
-3
ravisharmaaaa4 weeks ago
//code 
for(int i=0;i<n;i++)   {       if(arr[i]==x)       {           return i;           break;       }   }   return -1;
-1
omuksa0072 months ago
What is the meaning of K here 😒
0
chiragagrawal7652 months ago
why is binary search not working over here?
please answer some1
-4
2019sushilkumarkori2 months ago
int search(int arr[], int n, int x, int k)
{
    // Complete the function
    for(int i=0;i<n;i++){
        if(arr[i] == x){
            return i;
        }
    }
    return -1;
}    	
+1
sagar9760dharvan2 months ago
#Java
 
int search(int a[], int n, int x, int k){   int nextse=0;   while(nextse<n)   {       if(a[nextse]==x)       {           return nextse;       }              int low=a[nextse]-k;       int high=a[nextse]+k;       if(x>=low && x<=high)       nextse++;       else       nextse+=2;      }   return -1;}     
0
anushkay00921
This comment was deleted.
0
allenvrghs20073 months ago
c++ solution
 
   vector <int> v (arr, arr +n);     auto it = find(v.begin(), v.end(), x);   // If element was found   if (it != v.end())   {           // calculating the index       // of K       int index = it - v.begin();       // cout << index << endl;       return index;   }   else {       // If the element is not       // present in the vector       // cout << "-1" << endl;       return -1;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.