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 element exist return the first occurrence of the key.

Examples:

Input : arr[] = {4, 5, 6, 7, 6} k = 1 x = 6 Output : 2 The first index of 6 is 2. Input : arr[] = {20, 40, 50, 70, 70, 60} k = 20 x = 60 Output : 5 The index of 60 is 5

This problem is mainly an extension of Search an element in an array where difference between adjacent elements is 1.

A **Simple Approach** is to traverse the given array one by one and compare every element with given element ‘x’. If matches, then return index.

The above solution can be **Optimized **using the fact that difference between all adjacent elements is at most k. The idea is to start comparing from the leftmost element and find the difference between current array element and x. Let this difference be ‘diff’. From the given property of array, we always know that x must be at-least ‘diff/k’ away, so instead of searching one by one, we jump ‘diff/k’.

Below is the implementation of above idea.

## C++

`// 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) ` `{ ` ` ` `// Traverse the given array starting from ` ` ` `// leftmost element ` ` ` `int` `i = 0; ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `// If x is found at index i ` ` ` `if` `(arr[i] == x) ` ` ` `return` `i; ` ` ` ` ` `// Jump the difference between current ` ` ` `// array element and x divided by k ` ` ` `// We use max here to make sure that i ` ` ` `// moves at-least one step ahead. ` ` ` `i = i + max(1, ` `abs` `(arr[i]-x)/k); ` ` ` `} ` ` ` ` ` `cout << ` `"number is not present!"` `; ` ` ` `return` `-1; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `arr[] = {2, 4, 5, 7, 7, 6}; ` ` ` `int` `x = 6; ` ` ` `int` `k = 2; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Element "` `<< x << ` `" is present at index "` ` ` `<< search(arr, n, x, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to search an element in ` `// an array where difference between all ` `// elements is 1 ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// x is the element to be searched ` ` ` `// in arr[0..n-1] such that all ` ` ` `// elements differ by at-most k. ` ` ` `static` `int` `search(` `int` `arr[], ` `int` `n, ` ` ` `int` `x, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// Traverse the given array starting ` ` ` `// from leftmost element ` ` ` `int` `i = ` `0` `; ` ` ` ` ` `while` `(i < n) { ` ` ` ` ` `// If x is found at index i ` ` ` `if` `(arr[i] == x) ` ` ` `return` `i; ` ` ` ` ` `// Jump the difference between ` ` ` `// current array element and x ` ` ` `// divided by k We use max here ` ` ` `// to make sure that i moves ` ` ` `// at-least one step ahead. ` ` ` `i = i + Math.max(` `1` `, Math.abs(arr[i] ` ` ` `- x) / k); ` ` ` `} ` ` ` ` ` `System.out.println(` `"number is "` `+ ` ` ` `"not present!"` `); ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `arr[] = { ` `2` `, ` `4` `, ` `5` `, ` `7` `, ` `7` `, ` `6` `}; ` ` ` `int` `x = ` `6` `; ` ` ` `int` `k = ` `2` `; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(` `"Element "` `+ x + ` ` ` `" is present at index "` ` ` `+ search(arr, n, x, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to search an element in an array ` `# where difference between all elements is 1 ` ` ` `# x is the element to be searched in arr[0..n-1] ` `# such that all elements differ by at-most k. ` `def` `search(arr, n, x, k): ` ` ` ` ` `# Traverse the given array starting from ` ` ` `# leftmost element ` ` ` `i ` `=` `0` ` ` `while` `(i < n): ` ` ` ` ` `# If x is found at index i ` ` ` `if` `(arr[i] ` `=` `=` `x): ` ` ` `return` `i ` ` ` ` ` `# Jump the difference between current ` ` ` `# array element and x divided by k ` ` ` `# We use max here to make sure that i ` ` ` `# moves at-least one step ahead. ` ` ` `i ` `=` `i ` `+` `max` `(` `1` `, ` `int` `(` `abs` `(arr[i] ` `-` `x) ` `/` `k)) ` ` ` ` ` ` ` `print` `(` `"number is not present!"` `) ` ` ` `return` `-` `1` ` ` ` ` `# Driver program to test above function ` `arr ` `=` `[` `2` `, ` `4` `, ` `5` `, ` `7` `, ` `7` `, ` `6` `] ` `x ` `=` `6` `k ` `=` `2` `n ` `=` `len` `(arr) ` `print` `(` `"Element"` `, x, ` `"is present at index"` `,search(arr, n, x, k)) ` ` ` `# This code is contributed ` `# by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to search an element in ` `// an array where difference between ` `// all elements is 1 ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// x is the element to be searched ` ` ` `// in arr[0..n-1] such that all ` ` ` `// elements differ by at-most k. ` ` ` `static` `int` `search(` `int` `[]arr, ` `int` `n, ` ` ` `int` `x, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// Traverse the given array starting ` ` ` `// from leftmost element ` ` ` `int` `i = 0; ` ` ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// If x is found at index i ` ` ` `if` `(arr[i] == x) ` ` ` `return` `i; ` ` ` ` ` `// Jump the difference between ` ` ` `// current array element and x ` ` ` `// divided by k We use max here ` ` ` `// to make sure that i moves ` ` ` `// at-least one step ahead. ` ` ` `i = i + Math.Max(1, Math.Abs(arr[i] ` ` ` `- x) / k); ` ` ` `} ` ` ` ` ` `Console.Write(` `"number is "` `+ ` ` ` `"not present!"` `); ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `[]arr = { 2, 4, 5, 7, 7, 6 }; ` ` ` `int` `x = 6; ` ` ` `int` `k = 2; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.Write(` `"Element "` `+ x + ` ` ` `" is present at index "` `+ ` ` ` `search(arr, n, x, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to search an ` `// element in an array where ` `// difference between all ` `// elements is 1 ` ` ` `// x is the element to be ` `// searched in arr[0..n-1] ` `// such that all elements ` `// differ by at-most k. ` `function` `search(` `$arr` `, ` `$n` `, ` `$x` `, ` `$k` `) ` `{ ` ` ` ` ` `// Traverse the given array ` ` ` `// starting from leftmost element ` ` ` `$i` `= 0; ` ` ` `while` `(` `$i` `< ` `$n` `) ` ` ` `{ ` ` ` `// If x is found at index i ` ` ` `if` `(` `$arr` `[` `$i` `] == ` `$x` `) ` ` ` `return` `$i` `; ` ` ` ` ` `// Jump the difference between current ` ` ` `// array element and x divided by k ` ` ` `// We use max here to make sure that i ` ` ` `// moves at-least one step ahead. ` ` ` `$i` `= ` `$i` `+ max(1, ` `abs` `(` `$arr` `[` `$i` `] - ` `$x` `) / ` `$k` `); ` ` ` `} ` ` ` ` ` `echo` `"number is not present!"` `; ` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `{ ` ` ` `$arr` `= ` `array` `(2, 4, 5, 7, 7, 6); ` ` ` `$x` `= 6; ` ` ` `$k` `= 2; ` ` ` `$n` `= sizeof(` `$arr` `)/sizeof(` `$arr` `[0]); ` ` ` `echo` `"Element $x is present"` `. ` ` ` `"at index "` `, ` ` ` `search(` `$arr` `, ` `$n` `, ` `$x` `, ` `$k` `); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Element 6 is present at index 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Minimize the maximum difference of adjacent elements after at most K insertions
- Maximum subsequence sum of at most K-distant adjacent elements
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Array range queries for searching an element
- m-WAY Search Trees | Set-1 ( Searching )
- Real time optimized KMP Algorithm for Pattern Searching
- Rabin-Karp algorithm for Pattern Searching in Matrix
- Searching Algorithms in Java
- Difference between Searching and Sorting Algorithms
- Maximize Array sum by swapping at most K elements with another array
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1
- Lexicographically smallest array after at-most K consecutive swaps
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Most frequent word in an array of strings
- Check whether an array can be made strictly decreasing by modifying at most one element

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.