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 = 5Output: 4 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 = 3Output: 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++
#include <bits/stdc++.h>
using namespace std;
void minOperations(int arr[], int n, int k)
{
map<int, int> mp;
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
}
int ans = 0;
while (mp[k] < n) {
for (int i = 1; i <= k - 1; i++) {
if (mp[i]) {
mp[i]--;
mp[i + 1]++;
if (mp[i + 1] == 1) {
i++;
}
}
}
ans++;
}
cout << ans;
}
int main()
{
int arr[] = { 2, 3, 3, 4 };
int K = 5;
int N = sizeof(arr) / sizeof(arr[0]);
minOperations(arr, N, K);
return 0;
}
Java
import java.io.*;
import java.util.*;
class GFG
{
static void minOperations(int arr[], int n, int k)
{
Map<Integer, Integer> mp = new HashMap<>();
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);
}
}
int ans = 0;
while (mp.containsKey(k) == false
|| mp.get(k) < n) {
for (int i = 1; i <= k - 1; i++) {
if (mp.containsKey(i) && mp.get(i) > 0) {
mp.put(i, mp.get(i) - 1);
if (mp.containsKey(i + 1))
mp.put(i + 1, mp.get(i + 1) + 1);
else
mp.put(i + 1, 1);
if (mp.containsKey(i + 1)
&& mp.get(i + 1) == 1) {
i++;
}
}
}
ans++;
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 2, 3, 3, 4 };
int K = 5;
int N = arr.length;
minOperations(arr, N, K);
}
}
Python3
class GFG :
@staticmethod
def minOperations( arr, n, k) :
mp = dict()
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
ans = 0
while ((k in mp.keys()) == False or mp.get(k) < n) :
i = 1
while (i <= k - 1) :
if ((i in mp.keys()) and mp.get(i) > 0) :
mp[i] = mp.get(i) - 1
if ((i + 1 in mp.keys())) :
mp[i + 1] = mp.get(i + 1) + 1
else :
mp[i + 1] = 1
if ((i + 1 in mp.keys()) and mp.get(i + 1) == 1) :
i += 1
i += 1
ans += 1
print(ans, end ="")
@staticmethod
def main( args) :
arr = [2, 3, 3, 4]
K = 5
N = len(arr)
GFG.minOperations(arr, N, K)
if __name__=="__main__":
GFG.main([])
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void minOperations(int []arr, int n, int k)
{
Dictionary<int, int> mp = new Dictionary<int, int>();
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);
}
}
int ans = 0;
while (mp.ContainsKey(k) == false
|| mp[k] < n) {
for (int i = 1; i <= k - 1; i++) {
if (mp.ContainsKey(i) && mp[i] > 0) {
mp[i] = mp[i] - 1;
if (mp.ContainsKey(i + 1))
mp[i + 1] = mp[i + 1] + 1;
else
mp.Add(i + 1, 1);
if (mp.ContainsKey(i + 1)
&& mp[i + 1] == 1) {
i++;
}
}
}
ans++;
}
Console.Write(ans);
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 3, 4 };
int K = 5;
int N = arr.Length;
minOperations(arr, N, K);
}
}
Javascript
<script>
function minOperations(arr, n, k)
{
let mp = new Map();
for (let i = 0; i < n; i++)
{
if (mp.has(arr[i]))
{
mp.set(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.set(arr[i], 1);
}
}
let ans = 0;
while (mp.has(k) == false
|| mp.get(k) < n) {
for (let i = 1; i <= k - 1; i++) {
if (mp.has(i) && mp.get(i) > 0) {
mp.set(i, mp.get(i) - 1);
if (mp.has(i + 1))
mp.set(i + 1, mp.get(i + 1) + 1);
else
mp.set(i + 1, 1);
if (mp.has(i + 1)
&& mp.get(i + 1) == 1) {
i++;
}
}
}
ans++;
}
document.write(ans);
}
let arr = [ 2, 3, 3, 4 ];
let K = 5;
let N = arr.length;
minOperations(arr, N, K);
</script>
Time Complexity: O(N*K)Auxiliary space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...