The Wayback Machine - https://web.archive.org/web/20240818163938/https://www.geeksforgeeks.org/minimum-increment-k-operations-make-elements-equal/
Open In App

Minimum increment by k operations to make all elements equal

Last Updated : 19 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

You are given an array of n-elements, you have to find the number of operations needed to make all elements of array equal. Where a single operation can increment an element by k. If it is not possible to make all elements equal print -1.

Example : 

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

To solve this question we require to check whether all elements can became equal or not and that too only by incrementing k from elements value. For this we have to check that the difference of any two elements should always be divisible by k. If it is so, then all elements can become equal otherwise they can not became equal in any case by incrementing k from them. Also, the number of operations required can be calculated by finding value of (max – Ai)/k for all elements. where max is maximum element of array.

Algorithm : 

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

Implementation:

C++




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




// 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
        int max = Integer.MIN_VALUE;
          for(int i=0; i<n; i++) {
            max = Math.max(max, arr[i]);
        }
        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




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




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


Javascript




<script>
 
// Program to make all array equal
 
// function for calculating min operations
function minOps(arr, n, k)
{
    // max elements of array
    var max = arr[0];
     
    for(var i=0; i<arr.length; i++)
    {
        if(arr[i]>max)
            max = arr[i];
    }
    var res = 0;
 
    // iterate for all elements
    for (var 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
var arr = [ 21, 33, 9, 45, 63 ];
var n = arr.length;
var k = 6;
document.write( minOps(arr, n, k));
 
// This code is contributed by rutvik_56.
</script>


Output

24

Time Complexity: O(n)
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Minimize increment or increment and decrement of Pair to make all Array elements equal
Given an array arr[] of size N, the task is to minimize the number of steps to make all the array elements equal by performing the following operations: Choose an element of the array and increase it by 1.Select two elements simultaneously (arr[i], arr[j]) increase arr[i] by 1 and decrease arr[j] by 1. Examples: Input: arr = [4, 2, 4, 6]Output: 2Ex
8 min read
Minimum number of increment-other operations to make all array elements equal.
We are given an array consisting of n elements. At each operation you can select any one element and increase rest of n-1 elements by 1. You have to make all elements equal performing such operation as many times you wish. Find the minimum number of operations needed for this. Examples: Input : arr[] = {1, 2, 3} Output : Minimum Operation = 3 Expla
8 min read
Minimum increment operations to make K elements equal
Given an array arr[] of N elements and an integer K, the task is to make any K elements of the array equal by performing only increment operations i.e. in one operation, any element can be incremented by 1. Find the minimum number of operations required to make any K elements equal.Examples: Input: arr[] = {3, 1, 9, 100}, K = 3 Output: 14 Increment
11 min read
Count minimum decrement prefix or suffix or increment all operations to make Array equal to 0
Given an array arr[] of size N. The task is to make all the array elements equal to zero by applying the minimum number of operations. Following operations are allowed: Select an index i and decrease each element by 1 for the prefix up to that index.Select an index i and decrease each element by 1 for the suffix starting from that index.Increase al
7 min read
Minimum increment and decrement by K of each pair elements required to make all array elements equal
Given an array arr[], the task is to check if it is possible to make all array elements equal by repeatedly choosing a triplet (i, j, k), where i and j are different, and subtract k from arr[i] and add k to arr[j]. Examples: Input: arr[] = {1, 5, 6, 4}Output: YesExplanation:Operations performed: Choose i = 2, j = 0, k = 2 and perform the given oper
10 min read
Minimum number of increment operations required to make two Strings equal
Given two strings S1 and S2 of the same length containing characters ranging from '0' to '9', the task is to return the minimum number of operations required to make two strings equal by following the below operations: Increment any two characters from the string by 1.If it is not possible to make two strings equal with above condition then return
8 min read
Minimum Increment or Bitwise OR operations to make two integers equal
Given two integers X and Y (X &lt; Y), we can perform the following operations any number of times: Increment X by 1, X = X + 1Increment Y by 1, Y = Y + 1 Change X to X | Y, X = X | YThe task is to determine the minimum number of operations required to make X = Y. Input: X = 1, Y = 3 Output: 1 Explanation: It takes only one operation to make X = Y,
8 min read
Minimum Increment / decrement to make array elements equal
Given an array of integers where [Tex]1 \leq A[i] \leq 10^{18} [/Tex]. In one operation you can either Increment/Decrement any element by 1. The task is to find the minimum operations needed to be performed on the array elements to make all array elements equal. Examples: Input : A[] = { 1, 5, 7, 10 } Output : 11 Increment 1 by 4, 5 by 0. Decrement
7 min read
Minimum cost to make Array equal by increment/decrementing elements
Given an array, arr[], and the cost array cost[], the task is to find the minimum cost to make all the array elements equal by incrementing or decrementing the element at any index by 1. The cost of the increment or decrement operation for the element at index i is the cost[i]. Examples: Input: arr[] = {1, 3, 5, 2}, cost[] = {2, 3, 1, 14}Output: 8E
8 min read
Minimum number of increment/decrement operations such that array contains all elements from 1 to N
Given an array of N elements, the task is to convert it into a permutation (Each number from 1 to N occurs exactly once) by using the following operations a minimum number of times: Increment any number.Decrement any number. Examples: Input: arr[] = {1, 1, 4} Output: 2 The array can be converted into [1, 2, 3] by adding 1 to the 1st index i.e. 1 +
4 min read
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements
Given an array arr[] of size N, the task is to make all array elements equal to 0 by replacing all elements of a subsequences of equal elements by any integer, minimum number of times. Examples: Input: arr[] = {3, 7, 3}, N = 3Output: 2Explanation:Selecting a subsequence { 7 } and replacing all its elements by 0 modifies arr[] to { 3, 3, 3 }. Select
5 min read
Minimum increment operations to make the array in increasing order
Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array. Examples: Input : a = { 1, 3, 3, 2 }, X = 2 Output : 3 Explanation : Modified array is { 1, 3, 5, 6 } Input : a = { 3, 5, 6 }, X = 5 Output : 0 Observation: Let's take two numbers a and b. a &gt;
7 min read
Minimum increment or decrement operations required to make the array sorted
Given an array arr[] of N integers, the task is to sort the array in non-decreasing order by performing the minimum number of operations. In a single operation, an element of the array can either be incremented or decremented by 1. Print the minimum number of operations required.Examples: Input: arr[] = {1, 2, 1, 4, 3} Output: 2 Add 1 to the 3rd el
15+ min read
Minimum increment/decrement operations required to make Median as X
Given an array A[] of n odd integers and an integer X. Calculate the minimum number of operations required to make the median of the array equal to X, where, in one operation we can either increase or decrease any single element by one. Examples: Input: A[] = {6, 5, 8}, X = 8 Output: 2 Explanation: Here 6 can be increased twice. The array will beco
6 min read
Maximize length of subarray of equal elements by performing at most K increment operations
Given an array A[] consisting of N integers and an integer K, the task is to maximize the length of the subarray having equal elements after performing at most K increments by 1 on array elements. Note: Same array element can be incremented more than once. Examples: Input: A[] = {2, 4, 8, 5, 9, 6}, K = 6Output: 3Explanation:Subarray [8, 5, 9] can b
8 min read
Minimize increment or decrement operations to make given Array elements consecutive
Given an array, arr[] of size N., The task is to perform minimum increment or decrement operations on the elements of the array, to make all the elements consecutive, Output the minimum sum of all the possible changes(addition and subtractions) required to do the same. Examples: Input: N = 5, arr[] = {13, 6, 11, 18, 4}Output: 15Explanation: Convert
5 min read
Minimum steps to make X equal to Y by performing increment or bitwise OR operation
Given two integers X and Y, the task is to find the minimum number of operations required to make X equal to Y by performing the given operations such that: X = X + 1Y = Y + 1X = X | Y Examples: Input: X = 4, Y = 5Output: 1Explanation: Applying 1st operation one time X = X + 1 =&gt; 4 + 1 =&gt; 5 (Thus, both numbers become equal). Input: X = 2, Y =
10 min read
Minimum operations required to make all the array elements equal
Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets deleted (the size of the array remains same). If th
6 min read
Minimum operations to make all elements equal using the second array
Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of: A[i] = A[i] - B[i], 0 &lt;= i &lt;= n-1 Note: If it's not possible to make array elements equal print -1.Examples: Input: A[] = {5, 7, 10, 5, 15}, B[] = {2, 2, 1, 3, 5} Output:
7 min read
Minimum Cost to make all array elements equal using given operations
Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is to find the minimum total cost to make all the elem
12 min read
Minimum operations required to make all elements in an array of first N odd numbers equal
Given an array consisting of first N odd numbers, the task is to find the minimum number of operations required to make all the array elements equal by repeatedly selecting a pair and incrementing one element and decrementing the other element in the pair by 1. Examples: Input: N = 3Output: 2Explanation:Initially, the array is {1, 3, 5}. Following
10 min read
Minimum operations required to make all elements of Array less than equal to 0
Given an array arr[] consisting of N positive numbers, the task is to find the minimum number of operations required to make all elements of the array less than or equal to 0. In each operation, one has to pick the minimum positive element from the array and subtract all the elements of the array from that number. Examples: Input: arr[] = {1, 2, 4,
5 min read
Find the minimum number of operations required to make all array elements equal
Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent
6 min read
Minimum number of operations required to make all elements equal
Given an array arr[] of length N along with an integer M. All the elements of arr[] are in the range [1, N]. Then your task is to output the minimum number of operations required to make all elements equal given that in one operation you can select at most M elements and increment all of them by 1. Examples: Input: N = 3, M = 2, arr[] = {1, 2, 3}Ou
5 min read
Minimize absolute difference between the smallest and largest array elements by minimum increment decrement operations
Given an array arr[] consisting of N positive integers, the task is to minimize the number of operations required to minimize the absolute difference between the smallest and largest elements present in the array. In each operation, subtract 1 from an array element and increment 1 to another array element. Examples: Input: arr[] = {1, 6}Output: 2Ex
8 min read
Check if Array sum can be made equal to X using K triplet increment operations
Given an array arr of length N and two integers K and X. For every element arri in the array, if arri &gt; 0 then the elements which are adjacent to it and the element itself will get incremented by 2 for all 1â‰Īiâ‰ĪN. Check if the sum of all array elements after performing the K operation is equal to X or not. Examples: Input: arr[] = { 0, 0, 1, 0, 0
8 min read
Minimize increment operations to make Array non-decreasing
Given an array arr[] of n integers. Modify the array such that every element is at least as large as the previous element. This can be done by increasing the value of any element by 1. The task is to find the minimum number of moves required to make the array non-decreasing. Examples: Input: n = 5, arr[] = {8, 9, 2, 7, 7}Output: 11Explanation: The
5 min read
Minimum Bitwise AND operations to make any two array elements equal
Given an array of integers of size 'n' and an integer 'k', We can perform the Bitwise AND operation between any array element and 'k' any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two elements of the array equal after performing
10 min read
Minimum Bitwise OR operations to make any two array elements equal
Given an array arr[] of integers and an integer K, we can perform the Bitwise OR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two elements of the array equal after performing the above-
9 min read
Minimum Bitwise XOR operations to make any two array elements equal
Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise XOR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two elements of the array equal after performin
9 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg