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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 使兩個數組的元素相同,最小增減 > 原文: [https://www.geeksforgeeks.org/making-elements-of-two-arrays-same-with-minimum-incrementdecrement/](https://www.geeksforgeeks.org/making-elements-of-two-arrays-same-with-minimum-incrementdecrement/) 給定兩個相同大小的數組,我們需要使用最少的操作將第一個數組轉換為另一個數組。 在一個操作中,我們可以將元素增加或減少一個。 請注意,元素的出現順序不必相同。 為了將一個數字轉換為另一個數字,我們可以對其加減 1。 **示例**: > **輸入**: a = {3,1,1},b = {1,2,2} > **輸出**: 2 > **說明**: 在這里,我們可以通過 1 運算將任何 1 變成 2,并以一個減數運算將 3 變成 2。 因此 a []變成{2,2,1},這是 b []的排列。 > > **輸入**: a = {3,1,1},b = {1,1,2} > **輸出**: 1 **算法**: 1.首先對兩個數組進行排序。 2.排序后,我們將運行一個循環,在該循環中,我們將比較第一個和第二個數組元素,并計算使第一個數組等于第二個數組所需的操作。 下面是上述方法的實現 ## C++ ```cpp // CPP program to find minimum increment/decrement // operations to make array elements same. #include <bits/stdc++.h> using namespace std; int MinOperation(int a[], int b[], int n) { ????// sorting both arrays in ????// ascending order ????sort(a, a + n); ????sort(b, b + n); ????// variable to store the ????// final result ????int result = 0; ????// After sorting both arrays ????// Now each array is in non- ????// decreasing order. Thus, ????// we will now compare each ????// element of the array and ????// do the increment or decrement ????// operation depending upon the ????// value of array b[]. ????for (int i = 0; i < n; ++i) { ????????result = result + abs(a[i] - b[i]); ????} ????return result; } // Driver code int main() { ????int a[] = { 3, 1, 1 }; ????int b[] = { 1, 2, 2 }; ????int n = sizeof(a) / sizeof(a[0]); ????cout << MinOperation(a, b, n); ????return 0; } ``` ## Java ```java // Java program to find minimum? // increment/decrement operations // to make array elements same. import java.util.Arrays; import java.io.*; class GFG? { static int MinOperation(int a[], ????????????????????????int b[],? ????????????????????????int n) { ????// sorting both arrays? ????// in ascending order ????Arrays.sort(a); ????Arrays.sort(b); ????// variable to store? ????// the final result ????int result = 0; ????// After sorting both arrays ????// Now each array is in non- ????// decreasing order. Thus, ????// we will now compare each ????// element of the array and ????// do the increment or decrement ????// operation depending upon the ????// value of array b[]. ????for (int i = 0; i < n; ++i)? ????{ ????????if (a[i] > b[i]) ????????????result = result + ?????????????????????Math.abs(a[i] - b[i]); ????????else if (a[i] < b[i]) ????????????result = result +? ?????????????????????Math.abs(a[i] - b[i]); ????} ????return result; } // Driver code public static void main (String[] args)? { ????int a[] = {3, 1, 1}; ????int b[] = {1, 2, 2}; ????int n = a.length; ????System.out.println(MinOperation(a, b, n)); } } // This code is contributed // by akt_mit ``` ## Python3 ```py # Python 3 program to find minimum? # increment/decrement operations to # make array elements same. def MinOperation(a, b, n): ????# sorting both arrays in ascending order ????a.sort(reverse = False) ????b.sort(reverse = False) ????# variable to store the final result ????result = 0 ????# After sorting both arrays. Now each? ????# array is in non-decreasing order.? ????# Thus, we will now compare each element ????# of the array and do the increment or? ????# decrement operation depending upon? ????# the value of array b[]. ????for i in range(0, n, 1): ????????if (a[i] > b[i]): ????????????result = result + abs(a[i] - b[i]) ????????elif(a[i] < b[i]): ????????????result = result + abs(a[i] - b[i]) ????return result # Driver code if __name__ == '__main__': ????a = [3, 1, 1] ????b = [1, 2, 2] ????n = len(a) ????print(MinOperation(a, b, n)) # This code is contributed by # Sahil_Shelangia ``` ## C# ```cs //C# program to find minimum?? // increment/decrement operations? // to make array elements same.? using System; public class GFG {? static int MinOperation(int []a,? ????????????????????????int []b,?? ????????????????????????int n)? {? ????// sorting both arrays?? ????// in ascending order? ????Array.Sort(a);? ????Array.Sort(b);? ????// variable to store?? ????// the final result? ????int result = 0;? ????// After sorting both arrays? ????// Now each array is in non-? ????// decreasing order. Thus,? ????// we will now compare each? ????// element of the array and? ????// do the increment or decrement? ????// operation depending upon the? ????// value of array b[].? ????for (int i = 0; i < n; ++i)?? ????{? ????????if (a[i] > b[i])? ????????????result = result +? ?????????????????????Math.Abs(a[i] - b[i]);? ????????else if (a[i] < b[i])? ????????????result = result +?? ?????????????????????Math.Abs(a[i] - b[i]);? ????}? ????return result;? }? // Driver code? public static void Main ()?? {? ????int []a = {3, 1, 1};? ????int []b = {1, 2, 2};? ????int n = a.Length;? ????Console.WriteLine(MinOperation(a, b, n));? }? }? /*This C# code is contributed by 29AjayKumar*/ ``` ## PHP ```php <?php // PHP program to find minimum? // increment/decrement operations? // to make array elements same. function MinOperation($a, $b, $n) { ????// sorting both arrays in ????// ascending order ????sort($a); ????sort($b); ????// variable to store? ????// the final result ????$result = 0; ????// After sorting both arrays ????// Now each array is in non- ????// decreasing order. Thus, ????// we will now compare each ????// element of the array and ????// do the increment or decrement ????// operation depending upon the ????// value of array b[]. ????for ($i = 0; $i < $n; ++$i)? ????{ ????????if ($a[$i] > $b[$i]) ????????????$result = $result + abs($a[$i] -? ????????????????????????????????????$b[$i]); ????????else if ($a[$i] < $b[$i]) ????????????$result = $result + abs($a[$i] -? ????????????????????????????????????$b[$i]); ????} ????return $result; } // Driver code $a = array ( 3, 1, 1 ); $b = array ( 1, 2, 2 ); $n = sizeof($a); echo MinOperation($a, $b, $n); // This code is contributed by ajit ?> ``` **輸出**: ``` 2 ``` **時間復雜度**:`O(N log N)` [](https://practice.geeksforgeeks.org/courses/competitive-programming-live?utm_source=geeksforgeeks&utm_medium=article&utm_campaign=gfg_article_cp) * * * * * *
                  <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>

                              哎呀哎呀视频在线观看