Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
Last Updated :
16 Feb, 2023
Given an array arr[] of size N and an integer K, the task is to check if all array elements can be made equal by removing an array element and incrementing the value of all other array elements such that the total sum of the array elements remains the same. If found to be true, then print "YES". Otherwise, print "NO".
Examples :
Input: arr[] = { 2, 2, 2, 3 }
Output: YES
Explanation:
Removing arr[2] ( 0-based indexing ) from the array and incrementing all other elements by 1 modifies arr[] to { 3, 3, 3 }
Since all array elements are equal. Therefore, the required output is "YES".
Input: arr[] = { 0, 3, 0 }, K = 3
Output:: NO
Removing arr[1] ( 0-based indexing ) from the array and incrementing the value arr[0] by 1 and arr[2] by 2 modifies arr[] to { 1, 2 }
Since all array elements are not equal. Therefore, the required output is "NO".
Approach: The problem can be solved using Greedy technique. The idea is to remove the largest array element and increment the value of other elements such that the total sum remains same. Follow the steps below to solve the problem:
Below is the implementation of above approach
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
bool CheckAllarrayEqual(int arr[], int N)
{
// Base case
if (N == 1) {
return true;
}
// Stores sum of
// array elements
int totalSum = arr[0];
// Stores second largest
// array element
int secMax = INT_MIN;
// Stores the largest
// array element
int Max = arr[0];
// Traverse the array
for (int i = 1; i < N; i++) {
if (arr[i] >= Max) {
// Update secMax
secMax = Max;
// Update Max
Max = arr[i];
}
else if (arr[i] > secMax) {
// Update secMax
secMax = arr[i];
}
// Update totalSum
totalSum += arr[i];
}
// If totalSum is less than
// secMax * (N - 1))
// or totalSum is not
// divisible by (N - 1)
if (((secMax * (N - 1)) > totalSum) or (totalSum % (N - 1))) {
return false;
}
return true;
}
// Driver Code
int main()
{
int arr[] = { 6, 2, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
if (CheckAllarrayEqual(arr, N)) {
cout << "YES";
}
else {
cout << "NO";
}
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
static boolean CheckAllarrayEqual(int[] arr,
int N)
{
// Base case
if (N == 1)
{
return true;
}
// Stores sum of
// array elements
int totalSum = arr[0];
// Stores second largest
// array element
int secMax = Integer.MIN_VALUE;
// Stores the largest
// array element
int Max = arr[0];
// Traverse the array
for(int i = 1; i < N; i++)
{
if (arr[i] >= Max)
{
// Update secMax
secMax = Max;
// Update Max
Max = arr[i];
}
else if (arr[i] > secMax)
{
// Update secMax
secMax = arr[i];
}
// Update totalSum
totalSum += arr[i];
}
// If totalSum is less than
// secMax * (N - 1))
// or totalSum is not
// divisible by (N - 1)
if (((secMax * (N - 1)) > totalSum) || (totalSum % (N - 1) != 0))
{
return false;
}
return true;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 6, 2, 2, 2 };
int N = arr.length;
if (CheckAllarrayEqual(arr, N))
{
System.out.print("YES");
}
else
{
System.out.print("NO");
}
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
# Function to check if an array of
# equal elements with sum equal to
# the given array can be obtained or not
def CheckAllarrayEqual(arr, N):
# Base case
if (N == 1):
return True
# Stores sum of
# array elements
totalSum = arr[0]
# Stores second largest
# array element
secMax = -10**19
# Stores the largest
# array element
Max = arr[0]
# Traverse the array
for i in range(1,N):
if (arr[i] >= Max):
# Update secMax
secMax = Max
# Update Max
Max = arr[i]
elif (arr[i] > secMax):
# Update secMax
secMax = arr[i]
# Update totalSum
totalSum += arr[i]
# If totalSum is less than
# secMax * (N - 1))
# or totalSum is not
# divisible by (N - 1)
if (((secMax * (N - 1)) > totalSum) or (totalSum % (N - 1))):
return False
return True
# Driver Code
if __name__ == '__main__':
arr=[6, 2, 2, 2]
N = len(arr)
if (CheckAllarrayEqual(arr, N)):
print("YES")
else:
print("NO")
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
static bool CheckAllarrayEqual(int[] arr, int N)
{
// Base case
if (N == 1) {
return true;
}
// Stores sum of
// array elements
int totalSum = arr[0];
// Stores second largest
// array element
int secMax = Int32.MinValue;
// Stores the largest
// array element
int Max = arr[0];
// Traverse the array
for (int i = 1; i < N; i++) {
if (arr[i] >= Max) {
// Update secMax
secMax = Max;
// Update Max
Max = arr[i];
}
else if (arr[i] > secMax) {
// Update secMax
secMax = arr[i];
}
// Update totalSum
totalSum += arr[i];
}
// If totalSum is less than
// secMax * (N - 1))
// or totalSum is not
// divisible by (N - 1)
if (((secMax * (N - 1)) > totalSum) || (totalSum % (N - 1) != 0)) {
return false;
}
return true;
}
// Driver Code
public static void Main()
{
int[] arr = { 6, 2, 2, 2 };
int N = arr.Length;
if (CheckAllarrayEqual(arr, N)) {
Console.Write("YES");
}
else {
Console.Write("NO");
}
}
}
// This code is contributed by sanjoy_62
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to check if an array of
// equal elements with sum equal to
// the given array can be obtained or not
function CheckAllarrayEqual(arr,N)
{
// Base case
if (N == 1)
{
return true;
}
// Stores sum of
// array elements
let totalSum = arr[0];
// Stores second largest
// array element
let secMax = Number.MIN_VALUE;
// Stores the largest
// array element
let Max = arr[0];
// Traverse the array
for(let i = 1; i < N; i++)
{
if (arr[i] >= Max)
{
// Update secMax
secMax = Max;
// Update Max
Max = arr[i];
}
else if (arr[i] > secMax)
{
// Update secMax
secMax = arr[i];
}
// Update totalSum
totalSum += arr[i];
}
// If totalSum is less than
// secMax * (N - 1))
// or totalSum is not
// divisible by (N - 1)
if (((secMax * (N - 1)) > totalSum) || (totalSum % (N - 1) != 0))
{
return false;
}
return true;
}
// Driver Code
let arr = [ 6, 2, 2, 2 ];
let N = arr.length;
if (CheckAllarrayEqual(arr, N))
{
document.write("YES");
}
else
{
document.write("NO");
}
// This code is contributed by sravan kumar Gottumukkala
</script>
Time complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal Given an array arr[] consisting of N elements, the task is to find the minimum number of increments required to be performed on the given array such that after selecting any array element at any index and splitting its value to the other array elements makes all other N - 1 elements equal. Examples:
6 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 we can select any one element and increase rest of n-1 elements by 1. We 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[] =
5 min read
Minimum sum of values subtracted from array elements to make all array elements equal Given an array arr[] consisting of N positive integers, the task is to find the sum of all the array elements required to be subtracted from each array element such that remaining array elements are all equal. Examples: Input: arr[] = {1, 2}Output: 1Explanation: Subtracting 1 from arr[1] modifies ar
4 min read
Minimum removals required to make frequency of all remaining array elements equal Given an array arr[] of size N, the task is to find the minimum number of array elements required to be removed such that the frequency of the remaining array elements become equal. Examples : Input: arr[] = {2, 4, 3, 2, 5, 3}Output: 2Explanation: Following two possibilities exists:1) Either remove
13 min read
Minimum removals required to make frequency of each array element equal to its value Given an array arr[] of size N, the task is to find the minimum count of array elements required to be removed such that frequency of each array element is equal to its value Examples: Input: arr[] = { 2, 4, 1, 4, 2 } Output: 2 Explanation: Removing arr[1] from the array modifies arr[] to { 2, 1, 4,
11 min read
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
8 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 f
7 min read
Minimum decrements to make an Array at most 0 such that all array elements are cyclically decremented after a number is reduced to 0 Given a circular array arr[] of N integers and an array cost[], the task is to calculate the minimum number of operations required to make all elements of the array equal to 0, where at each operation decrement the value of an index i by 1. If the value of an index becomes 0, decrement the value of
6 min read
Modify array to another given array by replacing array elements with the sum of the array | Set-2 Given an Array input[] consisting only of 1s initially and an array target[] of size N, the task is to check if the array input[] can be converted to target[] by replacing input[i] with the sum of array elements in each step. Examples: Input: input[] = { 1, 1, 1 }, target[] = { 9, 3, 5 } Output: YES
10 min read