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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 通過 k 次運算的最小增量以使所有元素相等 > 原文: [https://www.geeksforgeeks.org/minimum-increment-k-operations-make-elements-equal/](https://www.geeksforgeeks.org/minimum-increment-k-operations-make-elements-equal/) 給您一個 n 元素數組,您必須找到使數組的所有元素相等所需的操作數。 單個操作可以將元素增加 k。 如果不可能使所有元素相等,則打印-1。 **示例**: ``` Input : arr[] = {4, 7, 19, 16}, k = 3 Output : 10 Input : arr[] = {4, 4, 4, 4}, k = 3 Output : 0 Input : arr[] = {4, 2, 6, 8}, k = 3 Output : -1 ``` 為了解決這個問題,我們需要檢查所有元素是否可以相等,也只能通過從元素值中增加 k 來檢查。 為此,我們必須檢查任何兩個元素的差應始終被 k 整除。 如果是這樣,那么所有元素都可以變得相等,否則無論如何都不能通過使它們遞增 k 來變得相等。 同樣,可以通過找到所有元素的(max – Ai)/ k 值來計算所需的操作次數。 其中 max 是數組的最大元素。 算法: ``` // iterate for all elements for (int i=0; i<n; i++) { // check if element can make equal to max // or not if not then return -1 if ((max - arr[i]) % k != 0 ) return -1; // else update res for required operations else res += (max - arr[i]) / k ; } return res; ``` ## C++ ```cpp // Program to make all array equal #include <bits/stdc++.h> using namespace std; // function for calculating min operations int minOps(int arr[], int n, int k) { ????// max elements of array ????int max = *max_element(arr, arr + n); ????int res = 0; ????// iterate for all elements ????for (int i = 0; i < n; i++) { ????????// check if element can make equal to ????????// max or not if not then return -1 ????????if ((max - arr[i]) % k != 0) ????????????return -1; ????????// else update res for required operations ????????else ????????????res += (max - arr[i]) / k; ????} ????// return result ????return res; } // driver program int main() { ????int arr[] = { 21, 33, 9, 45, 63 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????int k = 6; ????cout << minOps(arr, n, k); ????return 0; } ``` ## Java ```java // Program to make all array equal import java.io.*; import java.util.Arrays; class GFG { ????// function for calculating min operations ????static int minOps(int arr[], int n, int k) ????{ ????????// max elements of array ????????Arrays.sort(arr); ????????int max = arr[arr.length - 1]; ????????int res = 0; ????????// iterate for all elements ????????for (int i = 0; i < n; i++) { ????????????// check if element can make equal to ????????????// max or not if not then return -1 ????????????if ((max - arr[i]) % k != 0) ????????????????return -1; ????????????// else update res for required operations ????????????else ????????????????res += (max - arr[i]) / k; ????????} ????????// return result ????????return res; ????} ????// Driver program ????public static void main(String[] args) ????{ ????????int arr[] = { 21, 33, 9, 45, 63 }; ????????int n = arr.length; ????????int k = 6; ????????System.out.println(minOps(arr, n, k)); ????} } // This code is contributed by vt_m ``` ## Python3 ```py # Python3 Program to make all array equal # function for calculating min operations def minOps(arr, n, k): ????# max elements of array ????max1 = max(arr) ????res = 0 ????# iterate for all elements ????for i in range(0, n):? ????????# check if element can make equal to ????????# max or not if not then return -1 ????????if ((max1 - arr[i]) % k != 0): ????????????return -1 ????????# else update res fo ????????# required operations ????????else: ????????????res += (max1 - arr[i]) / k ????# return result ????return int(res) # driver program arr = [21, 33, 9, 45, 63]? n = len(arr) k = 6 print(minOps(arr, n, k)) # This code is contributed by? # Smitha Dinesh Semwal ``` ## C# ```cs // C# program Minimum increment by // k operations to make all elements equal. using System; class GFG { ????// function for calculating min operations ????static int minOps(int[] arr, int n, int k) ????{ ????????// max elements of array ????????Array.Sort(arr); ????????int max = arr[arr.Length - 1]; ????????int res = 0; ????????// iterate for all elements ????????for (int i = 0; i < n; i++) { ????????????// check if element can make ????????????// equal to max or not if not ????????????// then return -1 ????????????if ((max - arr[i]) % k != 0) ????????????????return -1; ????????????// else update res for required ????????????// operations ????????????else ????????????????res += (max - arr[i]) / k; ????????} ????????// return result ????????return res; ????} ????// Driver program ????public static void Main() ????{ ????????int[] arr = { 21, 33, 9, 45, 63 }; ????????int n = arr.Length; ????????int k = 6; ????????Console.Write(minOps(arr, n, k)); ????} } // This code is contributed by nitin mittal. ``` ## PHP ```php <?php // Program to make all array equal // function for calculating? // min operations function minOps($arr, $n, $k) { ????// max elements of array ????$max = max($arr); ????$res = 0; ????// iterate for all elements ????for ($i = 0; $i < $n; $i++)? ????{ ????????// check if element can? ????????// make equal to max or ????????// not if not then return -1 ????????if (($max - $arr[$i]) % $k != 0) ????????????return -1; ????????// else update res for ????????// required operations ????????else ????????????$res += ($max -? ?????????????????????$arr[$i]) / $k; ????} ????// return result ????return $res; } // Driver Code $arr = array(21, 33, 9, 45, 63); $n = count($arr); $k = 6; print (minOps($arr, $n, $k)); // This code is contributed? // by Manish Shaw(manishshaw1) ?> ``` **輸出**: ``` 24 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看