Given an array arr[], an integer k, and an integer x, find the first index of x in the array. If x is not present, return -1.
The given array is a K-Step array, where the absolute difference between every pair of adjacent elements is at most k. In other words, for every valid index i: |arr[i] - arr[i - 1]| ≤ k
Examples:
Input: arr[] = [4, 5, 6, 7, 6], k = 1, x = 6
Output: 2
Explanation: The absolute difference between every two adjacent elements is at most 1. The first occurrence of 6 is at index 2.Input: arr[] = [20, 40, 50], k = 20, x = 70
Output: -1
Explanation: The array is a K-Step array, but 70 is not present. Hence, return -1.
This problem can also be solved using Linear Search by checking every element one by one. However, we can optimize the search using the K-Step property.
Since adjacent elements differ by at most k, if the current element is far from x, we skip positions where x cannot occur.
Suppose we are currently at index i and arr[i] != x.
Let the difference between the current element and x be: diff = |arr[i] - x|
- Since adjacent elements differ by at most k, the value can move towards x by at most k in one step.
- Therefore, we can skip diff / k positions instead of checking every element individually.
- If diff < k, then diff / k becomes 0. To ensure that we always move forward, take at least one step: jump = max(1, diff / k)
- Move to the next possible index by adding this jump to i and continue the search.
We continue this process until x is found or the index reaches the end of the array.
Consider: arr[] = [4, 5, 6, 7, 6], k = 1, x = 6
- At index 0, arr[0] = 4, so diff = |4 - 6| = 2.
- Calculate the jump as max(1, (2 / 1)) = 2.
- Move from index 0 to index 2.
- At index 2, arr[2] = 6, which matches x.
Therefore, return 2
#include <bits/stdc++.h>
using namespace std;
int findStepKeyIndex(vector<int>& arr, int k, int x) {
int n = arr.size();
int i = 0;
// Jump to next possible index using step property
while (i < n) {
if (arr[i] == x)
return i;
// Minimum jump should be 1 to avoid infinite loop
i += max(1, abs(arr[i] - x) / k);
}
return -1;
}
int main() {
vector<int> arr = {4, 5, 6, 7, 6};
int k = 1;
int x = 6;
cout << findStepKeyIndex(arr, k, x) << endl;
return 0;
}
class GFG {
static int findStepKeyIndex(int[] arr, int k, int x) {
int n = arr.length;
int i = 0;
// Jump to next possible index using step property
while (i < n) {
if (arr[i] == x)
return i;
// Minimum jump should be 1 to avoid infinite loop
i += Math.max(1, Math.abs(arr[i] - x) / k);
}
return -1;
}
public static void main(String[] args) {
int[] arr = {4, 5, 6, 7, 6};
int k = 1;
int x = 6;
System.out.println(findStepKeyIndex(arr, k, x));
}
}
def findStepKeyIndex(arr, k, x):
n = len(arr)
i = 0
# Jump to next possible index using step property
while i < n:
if arr[i] == x:
return i
# Minimum jump should be 1 to avoid infinite loop
i += max(1, abs(arr[i] - x) // k)
return -1
if __name__ == "__main__":
arr = [4, 5, 6, 7, 6]
k = 1
x = 6
print(findStepKeyIndex(arr, k, x))
using System;
class GFG {
static int findStepKeyIndex(int[] arr, int k, int x) {
int n = arr.Length;
int i = 0;
// Jump to next possible index using step property
while (i < n) {
if (arr[i] == x)
return i;
// Minimum jump should be 1 to avoid infinite loop
i += Math.Max(1, Math.Abs(arr[i] - x) / k);
}
return -1;
}
public static void Main() {
int[] arr = {4, 5, 6, 7, 6};
int k = 1;
int x = 6;
Console.WriteLine(findStepKeyIndex(arr, k, x));
}
}
function findStepKeyIndex(arr, k, x)
{
let n = arr.length;
let i = 0;
// Jump to next possible index using step property
while (i < n) {
if (arr[i] === x)
return i;
// Minimum jump should be 1 to avoid infinite loop
i += Math.max(1, Math.floor(Math.abs(arr[i] - x) / k));
}
return -1;
}
// Driver code
let arr = [ 4, 5, 6, 7, 6 ];
let k = 1;
let x = 6;
console.log(findStepKeyIndex(arr, k, x));
Output
2