📌  相关文章
📜  在相邻元素之间的差为1的数组中搜索元素

📅  最后修改于: 2021-05-04 09:57:00             🧑  作者: Mango

给定一个数组,其中相邻元素之间的差为1,编写一种算法来搜索数组中的元素并返回该元素的位置(返回第一个出现的元素)。
例子 :

Let element to be searched be x

Input: arr[] = {8, 7, 6, 7, 6, 5, 4, 3, 2, 3, 4, 3}     
       x = 3
Output: Element 3 found at index 7

Input: arr[] =  {1, 2, 3, 4, 5, 4}
       x = 5
Output: Element 5 found at index 4 

一种简单的方法是一个遍历给定数组,然后将每个元素与给定元素“ x”进行比较。如果匹配,则返回索引。
可以使用所有相邻元素之间的差异为1的事实来优化上述解决方案。其思想是从最左边的元素开始进行比较,并找到当前数组元素与x之间的差异。将此差异设为“ diff”。从数组的给定属性中,我们始终知道x必须至少相距’diff’,所以我们不跳一个一跳地搜索’diff’。
感谢RajnishKrJha提出了此解决方案。
以下是上述想法的实现。

C++
// C++ program to search an element in an array where
// difference between all elements is 1
#include
using namespace std;
 
// x is the element to be searched in arr[0..n-1]
int search(int arr[], int n, int x)
{
    // Traverse the given array starting from
    // leftmost element
    int i = 0;
    while (i


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]
    static int search(int arr[], int n, int x)
    {
         
        // 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
            i = i + Math.abs(arr[i]-x);
        }
     
        System.out.println ("number is not" +
                                     " present!");
 
        return -1;
    }
 
    // Driver program to test above function
    public static void main (String[] args) {
         
        int arr[] = {8 ,7, 6, 7, 6, 5, 4, 3,
                                   2, 3, 4, 3 };
        int n = arr.length;
        int x = 3;
        System.out.println("Element " + x +
                        " is present at index "
                            + search(arr,n,3));
    }
}
 
//This code is contributed by vt_m.


Python 3
# 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]
def search(arr, n, x):
 
    # 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
        i = i + abs(arr[i] - x)
     
    print("number is not present!")
    return -1
 
# Driver program to test above function
arr = [8 ,7, 6, 7, 6, 5, 4, 3, 2, 3, 4, 3 ]
n = len(arr)
x = 3
print("Element" , x , " is present at index ",
                             search(arr,n,3))
                              
# This code is contributed by Smitha


C#
// C# program to search an element
// in an array where difference
// between all elements is 1
using System;
 
public class GFG
{
     
    // in arr[0..n-1]
    static int search(int []arr, int n,
                      int x)
    {
         
        // 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
            i = i + Math.Abs(arr[i] - x);
        }
     
        Console.WriteLine ("number is not" +
                           " present!");
 
        return -1;
    }
 
    // Driver code
    public static void Main()
    {
         
        int []arr = {8 ,7, 6, 7, 6, 5,
                     4,3, 2, 3, 4, 3 };
        int n = arr.Length;
        int x = 3;
        Console.WriteLine("Element " + x +
                        " is present at index "
                        + search(arr, n, 3));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :

Element 3 is present at index 7

时间复杂度: O(n)

辅助空间: O(1)