292.md 7.9 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6
# 在相邻元素之间的差为 1 的数组中搜索元素

> 原文: [https://www.geeksforgeeks.org/search-an-element-in-an-array-where-difference-between-adjacent-elements-is-1/](https://www.geeksforgeeks.org/search-an-element-in-an-array-where-difference-between-adjacent-elements-is-1/)

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

W
wizardforcel 已提交
7
**示例**
W
init  
wizardforcel 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

```
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

```

## [推荐:在继续进行解决之前,请先在 ***<u>{IDE}</u>*** 上尝试您的方法。](https://ide.geeksforgeeks.org/)

**简单方法**可以一次遍历给定数组,并将每个元素与给定元素“ x”进行比较。 如果匹配,则返回索引。

可以使用所有相邻元素之间的差异为 1 的事实对上述解决方案进行**优化。[想法]是从最左侧的元素开始进行比较,然后找出当前数组元素与 x 之间的差异。 将此差异设为“ diff”。 从 array 的给定属性中,我们始终知道 x 必须至少相距'diff',因此,我们不逐一搜索,而是跳了'diff'。**

感谢 RajnishKrJha 建议此解决方案。

以下是上述想法的实现。

W
wizardforcel 已提交
32
## C++ 
W
init  
wizardforcel 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

```

// 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] 
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 + abs(arr[i]-x); 
    } 

    cout << "number is not present!"; 
    return -1; 
} 

// Driver program to test above function 
int main() 
{ 
    int arr[] = {8 ,7, 6, 7, 6, 5, 4, 3, 2, 3, 4, 3 }; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    int x = 3; 
    cout << "Element " << x  << " is present at index "
         << search(arr,n,3); 
    return 0; 
} 

```

## 爪哇

```

// 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 

```

W
wizardforcel 已提交
169
## C# 
W
init  
wizardforcel 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

```

// 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 

```

W
wizardforcel 已提交
225
## PHP
W
init  
wizardforcel 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

```

<?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] 
function 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); 
    } 

    echo "number is not present!"; 
    return -1; 
} 

// Driver Code 
$arr = array(8 ,7, 6, 7, 6, 5, 
             4, 3, 2, 3, 4, 3); 
$n = sizeof($arr); 
$x = 3; 
echo "Element " , $x , " is present " , 
      "at index ", search($arr, $n, 3); 

// This code is contributed 
// by nitin mittal. 
?> 

```

W
wizardforcel 已提交
273
**输出**
W
init  
wizardforcel 已提交
274 275 276 277 278 279 280 281 282 283

```
Element 3 is present at index 7
```

**[在相邻相差最多 k 的数组中搜索](https://www.geeksforgeeks.org/searching-array-adjacent-differ-k/)**

本文由 **Rishabh** 提供。 如果发现任何不正确的地方,或者想分享有关上述主题的更多信息,请发表评论

现在不要停下来,将您的学习提高到一个新的水平。 借助最受信任的课程,学习数据结构和算法的所有重要概念: [DSA Self Paced](https://practice.geeksforgeeks.org/courses/dsa-self-paced?utm_source=geeksforgeeks&utm_medium=article&utm_campaign=gfg_article_dsa_content_bottom) 。 以对学生友好的价格准备好行业。