# Make all array elements equal to K by repeatedly incrementing subsequences

Last Updated : 06 Oct, 2022

Given an array arr[] consisting of N integers and an integer K, the task is to make all array elements equal to K by repeatedly incrementing all elements of subsequences by 1
Note: The value of K is at least the maximum element of the array.

Examples:

Input: arr[] = {2, 3, 3, 4}, K = 5
Output:
Explanation:
Operation 1: Select the subsequence {2, 3, 4}. After incrementing each element, the subsequence modifies to {3, 4, 5}. The array modifies to {3, 3, 4, 5}.
Operation 2: Select the subsequence {3, 4}. After incrementing each element, the subsequence modifies to {4, 5}. The array modifies to {3, 4, 5, 5}.
Operation 3: Select the subsequence {3, 4}. After incrementing each element, the subsequence modifies to {4, 5}. The array modifies to {4, 5, 5, 5}.
Operation 4: Select the subsequence {4}. After incrementing each element, the subsequence modifies to {5}.The array modifies to {5, 5, 5, 5}.

Input: arr[] = {1, 1, 1, 1}, K = 3
Output: 5

Approach: The idea is to use Hashing to keep track of the elements in the subsequences. When an element in a subsequence is increased by 1, its frequency reduces by 1 and the frequency of its modified value increases by 1
Follow the steps below to solve the problem:

• Initialize a variable, say ans, that stores the minimum number of operations required.
• Initialize a Hashmap, say mp, and store the frequency of array elements.
• While the frequency of K is less than N, i.e., mp[K] < N, perform the following operations:
• Iterate through a range [1, K – 1] using the variable i
• If mp[i] is greater than 0, decrease the frequency of the current value and increase the frequency of the next group (i + 1) elements by 1.
• If (i + 1) is not part of any previous value, skip it and continue traversing the loop.
• Increment the value of ans by 1.
• After completing the above steps, print the value of ans as the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the minimum number` `// of operations required to make all` `// elements equal to k` `void` `minOperations(``int` `arr[], ``int` `n, ``int` `k)` `{` `    ``// Initialize a hashmap` `    ``map<``int``, ``int``> mp;`   `    ``// Store frequency of array elements` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``mp[arr[i]]++;` `    ``}`   `    ``// Store the minimum number of` `    ``// operations required` `    ``int` `ans = 0;`   `    ``// Iterate until all array elements` `    ``// becomes equal to K` `    ``while` `(mp[k] < n) {`   `        ``// Iterate through range [1, k - 1]` `        ``// since only one element can be` `        ``// increased from each group` `        ``for` `(``int` `i = 1; i <= k - 1; i++) {`   `            ``// Check if the current number` `            ``// has frequency > 0, i.e.,` `            ``// it is a part of a group` `            ``if` `(mp[i]) {`   `                ``// If true, decrease the` `                ``// frequency of current` `                ``// group of element by 1` `                ``mp[i]--;`   `                ``// Increase the frequency` `                ``// of the next group of` `                ``// elements by 1` `                ``mp[i + 1]++;`   `                ``// If the next element is` `                ``// not the part of any` `                ``// group, then skip it` `                ``if` `(mp[i + 1] == 1) {` `                    ``i++;` `                ``}` `            ``}` `        ``}`   `        ``// Increment count of operations` `        ``ans++;` `    ``}`   `    ``// Print the count of operations` `    ``cout << ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `arr[] = { 2, 3, 3, 4 };` `    ``int` `K = 5;` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``// Function Call` `    ``minOperations(arr, N, K);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG` `{`   `  ``// Function to find the minimum number` `  ``// of operations required to make all` `  ``// elements equal to k` `  ``static` `void` `minOperations(``int` `arr[], ``int` `n, ``int` `k)` `  ``{`   `    ``// Initialize a hashmap` `    ``Map mp = ``new` `HashMap<>();`   `    ``// Store frequency of array elements` `    ``for` `(``int` `i = ``0``; i < n; i++)` `    ``{` `      ``if` `(mp.containsKey(arr[i])) ` `      ``{` `        ``mp.put(arr[i], mp.get(arr[i]) + ``1``);` `      ``}` `      ``else` `      ``{` `        ``mp.put(arr[i], ``1``);` `      ``}` `    ``}`   `    ``// Store the minimum number of` `    ``// operations required` `    ``int` `ans = ``0``;`   `    ``// Iterate until all array elements` `    ``// becomes equal to K` `    ``while` `(mp.containsKey(k) == ``false` `           ``|| mp.get(k) < n) {`   `      ``// Iterate through range [1, k - 1]` `      ``// since only one element can be` `      ``// increased from each group` `      ``for` `(``int` `i = ``1``; i <= k - ``1``; i++) {`   `        ``// Check if the current number` `        ``// has frequency > 0, i.e.,` `        ``// it is a part of a group` `        ``if` `(mp.containsKey(i) && mp.get(i) > ``0``) {`   `          ``// If true, decrease the` `          ``// frequency of current` `          ``// group of element by 1` `          ``mp.put(i, mp.get(i) - ``1``);`   `          ``// Increase the frequency` `          ``// of the next group of` `          ``// elements by 1` `          ``if` `(mp.containsKey(i + ``1``))` `            ``mp.put(i + ``1``, mp.get(i + ``1``) + ``1``);` `          ``else` `            ``mp.put(i + ``1``, ``1``);`   `          ``// If the next element is` `          ``// not the part of any` `          ``// group, then skip it` `          ``if` `(mp.containsKey(i + ``1``)` `              ``&& mp.get(i + ``1``) == ``1``) {` `            ``i++;` `          ``}` `        ``}` `      ``}`   `      ``// Increment count of operations` `      ``ans++;` `    ``}`   `    ``// Print the count of operations` `    ``System.out.print(ans);` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``int` `arr[] = { ``2``, ``3``, ``3``, ``4` `};` `    ``int` `K = ``5``;` `    ``int` `N = arr.length;`   `    ``// Function Call` `    ``minOperations(arr, N, K);` `  ``}` `}`   `// This code is contributed by Dharanendra L V.`

## Python3

 `class` `GFG :` `  `  `    ``# Function to find the minimum number` `    ``# of operations required to make all` `    ``# elements equal to k` `    ``@staticmethod` `    ``def` `minOperations( arr,  n,  k) :` `      `  `        ``# Initialize a hashmap` `        ``mp ``=`  `dict``()` `        `  `        ``# Store frequency of array elements` `        ``i ``=` `0` `        ``while` `(i < n) :` `            ``if` `((arr[i] ``in` `mp.keys())) :` `                ``mp[arr[i]] ``=` `mp.get(arr[i]) ``+` `1` `            ``else` `:` `                ``mp[arr[i]] ``=` `1` `            ``i ``+``=` `1` `            `  `        ``# Store the minimum number of` `        ``# operations required` `        ``ans ``=` `0` `        `  `        ``# Iterate until all array elements` `        ``# becomes equal to K` `        ``while` `((k ``in` `mp.keys()) ``=``=` `False` `or` `mp.get(k) < n) :` `          `  `            ``# Iterate through range [1, k - 1]` `            ``# since only one element can be` `            ``# increased from each group` `            ``i ``=` `1` `            ``while` `(i <``=` `k ``-` `1``) :` `              `  `                ``# Check if the current number` `                ``# has frequency > 0, i.e.,` `                ``# it is a part of a group` `                ``if` `((i ``in` `mp.keys()) ``and` `mp.get(i) > ``0``) :` `                  `  `                    ``# If true, decrease the` `                    ``# frequency of current` `                    ``# group of element by 1` `                    ``mp[i] ``=` `mp.get(i) ``-` `1` `                    `  `                    ``# Increase the frequency` `                    ``# of the next group of` `                    ``# elements by 1` `                    ``if` `((i ``+` `1` `in` `mp.keys())) :` `                        ``mp[i ``+` `1``] ``=` `mp.get(i ``+` `1``) ``+` `1` `                    ``else` `:` `                        ``mp[i ``+` `1``] ``=` `1` `                        `  `                    ``# If the next element is` `                    ``# not the part of any` `                    ``# group, then skip it` `                    ``if` `((i ``+` `1` `in` `mp.keys()) ``and` `mp.get(i ``+` `1``) ``=``=` `1``) :` `                        ``i ``+``=` `1` `                ``i ``+``=` `1` `            ``# Increment count of operations` `            ``ans ``+``=` `1` `            `  `        ``# Print the count of operations` `        ``print``(ans, end ``=``"")` `        `  `    ``# Driver Code` `    ``@staticmethod` `    ``def` `main( args) :` `        ``arr ``=` `[``2``, ``3``, ``3``, ``4``]` `        ``K ``=` `5` `        ``N ``=` `len``(arr)` `        `  `        ``# Function Call` `        ``GFG.minOperations(arr, N, K)` `    `    `if` `__name__``=``=``"__main__"``:` `    ``GFG.main([])` `    `  `    ``# This code is contributed by adityaburujwale.`

## C#

 `// C# program for the above approach`   `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG` `{`   `  ``// Function to find the minimum number` `  ``// of operations required to make all` `  ``// elements equal to k` `  ``static` `void` `minOperations(``int` `[]arr, ``int` `n, ``int` `k)` `  ``{`   `    ``// Initialize a hashmap` `    ``Dictionary<``int``, ``int``> mp = ``new` `Dictionary<``int``, ``int``>();`   `    ``// Store frequency of array elements` `    ``for` `(``int` `i = 0; i < n; i++)` `    ``{` `      ``if` `(mp.ContainsKey(arr[i])) ` `      ``{` `        ``mp[arr[i]] =  mp[arr[i]] + 1;` `      ``}` `      ``else` `      ``{` `        ``mp.Add(arr[i], 1);` `      ``}` `    ``}`   `    ``// Store the minimum number of` `    ``// operations required` `    ``int` `ans = 0;`   `    ``// Iterate until all array elements` `    ``// becomes equal to K` `    ``while` `(mp.ContainsKey(k) == ``false` `           ``|| mp[k] < n) {`   `      ``// Iterate through range [1, k - 1]` `      ``// since only one element can be` `      ``// increased from each group` `      ``for` `(``int` `i = 1; i <= k - 1; i++) {`   `        ``// Check if the current number` `        ``// has frequency > 0, i.e.,` `        ``// it is a part of a group` `        ``if` `(mp.ContainsKey(i) && mp[i] > 0) {`   `          ``// If true, decrease the` `          ``// frequency of current` `          ``// group of element by 1` `          ``mp[i] = mp[i] - 1;`   `          ``// Increase the frequency` `          ``// of the next group of` `          ``// elements by 1` `          ``if` `(mp.ContainsKey(i + 1))` `            ``mp[i + 1] = mp[i + 1] + 1;` `          ``else` `            ``mp.Add(i + 1, 1);`   `          ``// If the next element is` `          ``// not the part of any` `          ``// group, then skip it` `          ``if` `(mp.ContainsKey(i + 1)` `              ``&& mp[i + 1] == 1) {` `            ``i++;` `          ``}` `        ``}` `      ``}`   `      ``// Increment count of operations` `      ``ans++;` `    ``}`   `    ``// Print the count of operations` `    ``Console.Write(ans);` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(String[] args)` `  ``{` `    ``int` `[]arr = { 2, 3, 3, 4 };` `    ``int` `K = 5;` `    ``int` `N = arr.Length;`   `    ``// Function Call` `    ``minOperations(arr, N, K);` `  ``}` `}`     `// This code is contributed by 29AjayKumar`

## Javascript

 `   `

Output:

`4`

Time Complexity: O(N*K)
Auxiliary space: O(N)

Previous
Next