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

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

给定一个由n个整数组成的数组。每个数组元素都是通过将+1或-1加到上一个元素而获得的,即,任意两个连续元素之间的绝对差为1。任务是以最小的比较次数搜索元素索引(少于逐个元素进行的简单搜索) 。如果该元素多次出现,则打印最小的索引。如果不存在该元素,则打印-1。
例子:

Input : arr[] = {5, 4, 5, 6, 5, 4, 3, 2} 
        x = 4.
Output : 1
The first occurrence of element x is at 
index 1.

Input : arr[] = { 5, 4, 5, 6, 4, 3, 2, 3 } 
        x = 9.
Output : -1
Element x is not present in arr[]

令要搜索的元素是x。在任何索引i处,如果arr [i]!= x,则x出现的可能性在位置i + abs(arr [i] – a)处,因为每个元素都是通过将+1或-1加到上一个元素。在i和i + abs(arr [i] – a)之间不可能存在el。因此,如果arr [i]!= x,则直接跳转到i + abs(arr [i] – a)。

Algorithm to solve the problem:
1. Start from index = 0.
2. Compare arr[index] and x.
   a) If both are equal, return index.
   b) If not, set index = index + abs(arr[index] - x).
3. Repeat step 2.

下面是上述想法的实现:

C++
// C++ program to search an element in an array
// where each element is obtained by adding
// either +1 or -1 to previous element.
#include
using namespace std;
 
// Return the index of the element to be searched.
int search(int arr[], int n, int x)
{
    // Searching x in arr[0..n-1]
    int i = 0;
    while (i <= n-1)
    {
        // Checking if element is found.
        if (arr[i] == x)
            return i;
 
        // Else jumping to abs(arr[i]-x).
        i += abs(arr[i]-x);
    }
 
    return -1;
}
 
// Driven Program
int main()
{
    int arr[] =  {5, 4, 5, 6, 5, 4, 3, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    int x = 4;
 
    cout << search(arr, n, x) << endl;
 
    return 0;
}


Java
// Java program to search an element
// in an array where each element is
// obtained by adding either +1 or
// -1 to previous element.
class GFG
{
     
// Return the index of the
// element to be searched.
static int search(int arr[], int n, int x)
{
    // Searching x in arr[0..n-1]
    int i = 0;
    while (i <= n-1)
    {
        // Checking if element is found.
        if (arr[i] == x)
            return i;
 
        // Else jumping to abs(arr[i]-x).
        i += Math.abs(arr[i]-x);
    }
 
    return -1;
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = {5, 4, 5, 6, 5, 4, 3, 2};
    int n = arr.length;
    int x = 4;
 
    System.out.println(search(arr, n, x));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to search an element in
# an array where each element is obtained
# by adding either +1 or -1 to previous element
 
# Return the index of the element to be searched
def search(arr, n, x):
 
    # Searching x in arr[0..n-1]
    i = 0
    while (i <= n-1):
     
        # Checking if element is found.
        if (arr[i] == x):
            return i
 
        # Else jumping to abs(arr[i]-x).
        i += abs(arr[i] - x)
     
    return -1
 
# Driver code
arr = [5, 4, 5, 6, 5, 4, 3, 2]
n = len(arr)
x = 4
 
print(search(arr, n, x))
 
# This code is contributed by Anant Agarwal.


C#
// C# program to search an element in
// an array where each element is
// obtained by adding either + 1 or
// -1 to previous element.
using System;
 
class GFG
{
     
// Return the index of the
// element to be searched.
static int search(int []arr, int n,
                  int x)
{
     
    // Searching x in arr[0.. n - 1]
    int i = 0;
    while (i <= n - 1)
    {
        // Checking if element is found
        if (arr[i] == x)
            return i;
 
        // Else jumping to abs(arr[i] - x)
        i += Math.Abs(arr[i] - x);
    }
 
    return -1;
}
 
// Driver code
public static void Main ()
{
    int []arr = {5, 4, 5, 6, 5, 4, 3, 2};
    int n = arr.Length;
    int x = 4;
 
    Console.WriteLine(search(arr, n, x));
}
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

1