Minimize operations to reduce Array by replacing pair with their sum
Last Updated :
14 Mar, 2023
Given an array X[] of length N. In each operation, the following task is performed:
- Decrement any value by 1.
- Replace any pair with their sum and the cost of replacing is the same as the reduced value (i.e., 1).
Examples:
Input: N = 4, X[] = {4, 2, 1, 3}
Output: 2
Explanation: Operations are performed as:
First operation: Select X[3] = 1, reduced that value by one, and used it to merge X[1] and X[2], Formally, X[1] + X[2] + 1 = 4 + 2 + 1 = 7. Now updated X[] is = {7, 3}
Second operation: Seclect element X[2]=3, reduced it by one, then X[2]=2, Now merge X[1] and X[2] with that reduced value. Formally, X[1] + X[2] + 1 = 7 + 2 + 1 = 10. Updated X[] is: X[]={10}. No need to proceed with further operations as only one element is there in X[].
So, in total 2 operations were required.
Input: N = 7, X[] = {4, 2, 2, 4, 1, 2, 7}, Z=3
Output: 4
Explanation: Operations are performed as:
First operation: Select X[5], reduced it by one, Now A[5]=0 and use the reduced value to merge X[2] and X[3]. Formally, X[2] + X[3] + 1 = 2 + 2 +1 = 5. Now updated X[] is: {4, 5, 4, 2, 7}
Second operation: Select X[1] = 4, reduced it by one, Now X[1]=3 and use reduced value to merge X[2] and X[3]. Formally, X[2] + X[3]+1 = 5 + 4 + 1 = 10. Now updated X[] is: {3, 10, 2, 7}
Third operation: Select X[3] = 2, reduce it by one, Now X[3]=1, and use reduced value to merge X[1] and X[4]. Formally, X[1] + X[4] + 1 = 3 + 7 + 1 = 11. Now updated X[] is: {10, 1, 11}
Fourth operation: Select X[2]=1, reduce it by one, Now X[2]=0, and use reduced value to merge X[1] and X[3]. Formally, X[1] + X[3] + 1 = 10 + 11 + 1 = 22. Now updated X[] is: {22}
Approach: Implement the idea below to solve the problem
The problem is observation based and can be solved by using Greedy Technique. The problem required to be perform sorting then start reducing the lowest elements because this will result in minimum number of operations as some elements may disappear during the process.
Follow the below steps to implement the idea:
- Create a variable (say operations = 0) for holding the minimum number of operations.
- Sort the array.
- Run a loop from i = 0 to N-1:
- If (N – i – 1 >operations) then:
- If (N – i – 2 > operations + X[i]) then add X[i] to operations.
- Otherwise, if (N – i – 2 = operations + X[i]) then add X[i] to operations and break
- Else set operations as (N – i – 1).
- Otherwise, break from the loop.
- Return the value of operations.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Method for obtaining minimum cost
void min_cuts(int N, int X[])
{
// Variable to store number of operations
int operations = 0;
// Sorting array
sort(X, X + N);
// Loop for traversing
for (int i = 0; i < N; i++) {
if (N - i - 1 > operations) {
if (N - i - 2 > operations + X[i])
operations += X[i];
else if (N - i - 2 == operations + X[i]) {
operations += X[i];
break;
}
else
operations = N - i - 1;
}
else
break;
}
// Printing the minimum cost of total operations
cout << operations << endl;
}
int main() {
int X[] = { 4, 2, 2, 4, 1, 2, 7 };
int N = sizeof(X)/sizeof(X[0]);
// Function call
min_cuts(N, X);
}
Java
// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
throws java.lang.Exception
{
int X[] = { 4, 2, 2, 4, 1, 2, 7 };
int N = X.length;
// Function call
min_cuts(N, X);
}
// Method for obtaining minimum cost
static void min_cuts(int N, int[] X)
{
// Variable to store number of
// operations
int operations = 0;
// Sorting array
Arrays.sort(X);
// Loop for traversing
for (int i = 0; i < N; i++) {
if (N - i - 1 > operations) {
if (N - i - 2 > operations + X[i])
operations += X[i];
else if (N - i - 2 == operations + X[i]) {
operations += X[i];
break;
}
else
operations = N - i - 1;
}
else
break;
}
// Printing the minimum cost of total operations
System.out.println(operations);
}
}
C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
public class GFG {
static public void Main()
{
// Code
int[] X = { 4, 2, 2, 4, 1, 2, 7 };
int N = X.Length;
// Function call
min_cuts(N, X);
}
// Method for obtaining minimum cost
static void min_cuts(int N, int[] X)
{
// Variable to store number of operations
int operations = 0;
// Sorting array
Array.Sort(X);
// Loop for traversing
for (int i = 0; i < N; i++) {
if (N - i - 1 > operations) {
if (N - i - 2 > operations + X[i])
operations += X[i];
else if (N - i - 2 == operations + X[i]) {
operations += X[i];
break;
}
else
operations = N - i - 1;
}
else
break;
}
// Printing the minimum cost of total operations
Console.WriteLine(operations);
}
}
// This code is contributed by sankar.
Python3
def min_cuts(N, X):
# Variable to store number of operations
operations = 0
# Sorting array
X.sort()
# Loop for traversing
for i in range(N):
if N - i - 1 > operations:
if N - i - 2 > operations + X[i]:
operations += X[i]
elif N - i - 2 == operations + X[i]:
operations += X[i]
break
else:
operations = N - i - 1
else:
break
# Printing the minimum cost of total operations
print(operations)
X = [4, 2, 2, 4, 1, 2, 7]
N = len(X)
# Function call
min_cuts(N, X)
Javascript
// Javascript program for the above approach
function min_cuts(N, X) {
// Variable to store number of operations
let operations = 0;
// Sorting array
X.sort((a, b) => a - b);
// Loop for traversing
for (let i = 0; i < N; i++) {
if (N - i - 1 > operations) {
if (N - i - 2 > operations + X[i]) {
operations += X[i];
} else if (N - i - 2 === operations + X[i]) {
operations += X[i];
break;
} else {
operations = N - i - 1;
}
} else {
break;
}
}
// Printing the minimum cost of total operations
console.log(operations);
}
let X = [4, 2, 2, 4, 1, 2, 7];
let N = X.length;
// Function call
min_cuts(N, X);
// This code is contributed by rishab
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...