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:
- Initialize a variable, say secMax, to store the second largest element of the array.
- Initialize a variable, say totalSum, to store the sum of the array elements.
- Traverse the array and calculate the sum of the array elements and find the second largest array element.
- If totalSum < (N – 1) * secMax or totalSum % (N – 1) != 0, then print “NO”.
- Otherwise, print “YES”.
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)) if ((secMax * (N - 1)) > totalSum) { return false ; } // If totalSum is not // divisible by (N - 1) if (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)) if ((secMax * (N - 1 )) > totalSum) { return false ; } // If totalSum is not // divisible by (N - 1) if (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)) if ((secMax * (N - 1 )) > totalSum): return False # If totalSum is not # divisible by (N - 1) if (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 contributd 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)) if ((secMax * (N - 1)) > totalSum) { return false ; } // If totalSum is not // divisible by (N - 1) if (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 |
YES
Time complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.