<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之旅 廣告
                # 每個數組元素中另一個數組中最接近的較大元素 > 原文: [https://www.geeksforgeeks.org/closest-greater-element-every-array-element-another-array/](https://www.geeksforgeeks.org/closest-greater-element-every-array-element-another-array/) 給定兩個數組 a []和 b [],我們需要構建一個數組 c [],使得 c []的每個元素 c [i]都包含 a []中的一個值,該值大于 b [i]并且最接近 到 b [i]。 如果 a []的元素不大于 b [i],則 c [i]的值為-1。 所有數組的大小相同。 **示例**: ``` Input : a[] = [ 2, 6, 5, 7, 0] b[] = [1, 3, 2, 5, 8] Output : c[] = [2, 5, 5, 7, -1] Input : a[] = [ 2, 6, 5, 7, 0] b[] = [0, 2, 3, 5, 1] Output : c[] = [2, 5, 5, 6, 2] ``` **樸素的方法**:對于 b []中的每個元素,我們遍歷整個 a []并嘗試查找最接近的較大元素,并保存每次搜索的結果。 這將花費 O(n ^ 2)的時間復雜度。 **有效方法**:對數組 a []進行排序,并對每個 b [i]在排序后的數組 a []中應用[二分搜索](https://www.geeksforgeeks.org/binary-search/)。 對于這種方法,我們的時間復雜度將為 O(nlogn)。 **注意**:對于最接近的較大元素,我們可以使用 [upper_bound()](https://www.geeksforgeeks.org/stdupper_bound-in-cpp/)。 下面是實現。 ## C++ ```cpp // CPP to find result from target array // for closest element #include <bits/stdc++.h> using namespace std; // Function for printing resultant array void closestResult(int a[], int b[], int n) { ????// change arr[] to vector ????vector<int> vect(a, a + n); ????// sort vector for ease ????sort(vect.begin(), vect.end()); ????// iterator for upper_bound ????vector<int>::iterator up; ????// vector for result ????vector<int> c; ????// calculate resultant array ????for (int i = 0; i < n; i++) { ????????// check upper bound element ????????up = upper_bound(vect.begin(), vect.end(), b[i]); ????????// if no element found push -1 ????????if (up == vect.end()) ????????????c.push_back(-1); ????????// Else push the element ????????else ????????????c.push_back(*up); // add to resultant ????} ????cout << "Result = "; ????for (auto it = c.begin(); it != c.end(); it++) ????????cout << *it << " "; } // driver program int main() { ????int a[] = { 2, 5, 6, 1, 8, 9 }; ????int b[] = { 2, 1, 0, 5, 4, 9 }; ????int n = sizeof(a) / sizeof(a[0]); ????closestResult(a, b, n); ????return 0; } ``` ## Java ```java // Java to find result from target array? // for closest element import java.util.*; class GFG? { ????// Function for printing resultant array ????static void closestResult(Integer[] a,? ??????????????????????????????int[] b, int n)? ????{ ????????// change arr[] to Set ????????TreeSet<Integer> vect = new TreeSet<>(Arrays.asList(a)); ????????// vector for result ????????Vector<Integer> c = new Vector<>(); ????????// calculate resultant array ????????for (int i = 0; i < n; i++)? ????????{ ????????????// check upper bound element ????????????Integer up = vect.higher(b[i]); ????????????// if no element found push -1 ????????????if (up == null) ????????????????c.add(-1); ????????????// Else push the element ????????????else ????????????????c.add(up); // add to resultant ????????} ????????System.out.print("Result = "); ????????for (int i : c) ????????????System.out.print(i + " "); ????} ????// Driver Code ????public static void main(String[] args)? ????{ ????????Integer[] a = { 2, 5, 6, 1, 8, 9 }; ????????int[] b = { 2, 1, 0, 5, 4, 9 }; ????????int n = a.length; ????????closestResult(a, b, n); ????} } // This code is contributed by // sanjeev2552 ``` ## Python3 ```py # Python implementation to find result # from target array for closest element import bisect # Function for printing resultant array def closestResult(a, b, n): ????# sort list for ease ????a.sort() ????# list for result ????c = [] ????# calculate resultant array ????for i in range(n): ????????# check location of upper bound element ????????up = bisect.bisect_right(a, b[i]) ????????# if no element found push -1 ????????if up == n: ????????????c.append(-1) ????????# else puch the element ????????else: ????????????c.append(a[up]) # add to resultant ????print("Result = ", end = "") ????for i in c: ????????print(i, end = " ") # Driver code if __name__ == "__main__": ????a = [2,5,6,1,8,9] ????b = [2,1,0,5,4,9] ????n = len(a) ????closestResult(a, b, n) # This code is contributed by # sanjeev2552 ``` **輸出**: ``` Result = 5 2 1 6 5 -1 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看