<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 在相鄰元素之間的差為 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,編寫一種算法來搜索數組中的元素并返回該元素的位置(返回第一個出現的元素)。 **示例**: ``` 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”。 從 array 的給定屬性中,我們始終知道 x 必須至少相距'diff',因此,我們不逐一搜索,而是跳了'diff'。** 感謝 RajnishKrJha 建議此解決方案。 以下是上述想法的實現。 ## C++ ```cpp // 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 ```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# ```cs // 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 ```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] 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. ?> ``` **輸出**: ``` Element 3 is present at index 7 ``` [**在相鄰相差最多 k 的數組中搜索**](https://www.geeksforgeeks.org/searching-array-adjacent-differ-k/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看