Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
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++
#include <bits/stdc++.h>
using namespace std;
bool CheckAllarrayEqual( int arr[], int N)
{
if (N == 1) {
return true ;
}
int totalSum = arr[0];
int secMax = INT_MIN;
int Max = arr[0];
for ( int i = 1; i < N; i++) {
if (arr[i] >= Max) {
secMax = Max;
Max = arr[i];
}
else if (arr[i] > secMax) {
secMax = arr[i];
}
totalSum += arr[i];
}
if (((secMax * (N - 1)) > totalSum) or (totalSum % (N - 1))) {
return false ;
}
return true ;
}
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
import java.util.*;
class GFG{
static boolean CheckAllarrayEqual( int [] arr,
int N)
{
if (N == 1 )
{
return true ;
}
int totalSum = arr[ 0 ];
int secMax = Integer.MIN_VALUE;
int Max = arr[ 0 ];
for ( int i = 1 ; i < N; i++)
{
if (arr[i] >= Max)
{
secMax = Max;
Max = arr[i];
}
else if (arr[i] > secMax)
{
secMax = arr[i];
}
totalSum += arr[i];
}
if (((secMax * (N - 1 )) > totalSum) || (totalSum % (N - 1 ) != 0 ))
{
return false ;
}
return true ;
}
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" );
}
}
}
|
Python3
def CheckAllarrayEqual(arr, N):
if (N = = 1 ):
return True
totalSum = arr[ 0 ]
secMax = - 10 * * 19
Max = arr[ 0 ]
for i in range ( 1 ,N):
if (arr[i] > = Max ):
secMax = Max
Max = arr[i]
elif (arr[i] > secMax):
secMax = arr[i]
totalSum + = arr[i]
if (((secMax * (N - 1 )) > totalSum) or (totalSum % (N - 1 ))):
return False
return True
if __name__ = = '__main__' :
arr = [ 6 , 2 , 2 , 2 ]
N = len (arr)
if (CheckAllarrayEqual(arr, N)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool CheckAllarrayEqual( int [] arr, int N)
{
if (N == 1) {
return true ;
}
int totalSum = arr[0];
int secMax = Int32.MinValue;
int Max = arr[0];
for ( int i = 1; i < N; i++) {
if (arr[i] >= Max) {
secMax = Max;
Max = arr[i];
}
else if (arr[i] > secMax) {
secMax = arr[i];
}
totalSum += arr[i];
}
if (((secMax * (N - 1)) > totalSum) || (totalSum % (N - 1) != 0)) {
return false ;
}
return true ;
}
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" );
}
}
}
|
Javascript
<script>
function CheckAllarrayEqual(arr,N)
{
if (N == 1)
{
return true ;
}
let totalSum = arr[0];
let secMax = Number.MIN_VALUE;
let Max = arr[0];
for (let i = 1; i < N; i++)
{
if (arr[i] >= Max)
{
secMax = Max;
Max = arr[i];
}
else if (arr[i] > secMax)
{
secMax = arr[i];
}
totalSum += arr[i];
}
if (((secMax * (N - 1)) > totalSum) || (totalSum % (N - 1) != 0))
{
return false ;
}
return true ;
}
let arr = [ 6, 2, 2, 2 ];
let N = arr.length;
if (CheckAllarrayEqual(arr, N))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...