
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the number of operations required to make all array elements Equal in C++
In this problem, we are given an array arr of size n. Our task is to Find the number of operations required to make all array elements Equal
The operation is defined as distribution of equal weights from the element with maximum weight to all the elements of the array.
If it is not possible to make array elements equal, print -1.
Let’s take an example to understand the problem,
Input : arr[] = {7, 3, 3, 3} Output : 3
Explanation
Array after distribution is {4, 4, 4, 4}
Solution Approach
A simple solution to the problem is by finding the largest value of the array. And then using this largest value check if all elements of the array are equal and the value is equal to the subtraction of n (or its multiples) from the maximum value of the array. If yes, return n, if No, return -1 (denoting not possible).
Example
Let’s take an example to understand the problem
#include<bits/stdc++.h> using namespace std; int findOperationCount(int arr[],int n){ int j = 0, operations = 0; int maxVal = arr[0]; int minVal = arr[0]; int maxValInd = 0; for (int i = 1; i < n; i++){ if(arr[i] > maxVal){ maxVal = arr[i]; maxValInd = i; } if(arr[i] < minVal){ minVal = arr[i]; } } for (int i =0;i<n;i++){ if (arr[i] != maxVal && arr[i] <= minVal && arr[i] != 0){ arr[j] += 1; arr[maxValInd] -= 1; maxVal -= 1; operations += 1; j += 1; } else if (arr[i] != 0){ j += 1; } } for (int i = 0; i < n; i++){ if (arr[i] != maxVal){ operations = -1; break; } } return operations; } int main(){ int arr[] = {4, 4, 8, 4}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"The number of operations required to make all array elements Equal is "<<findOperationCount(arr, n); return 0; }
Output
The number of operations required to make all array elements Equal is 3
- Related Articles
- Number of operations required to make all array elements Equal in Python
- Minimum operations required to make all the array elements equal in C++
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum number of given operations required to make two strings equal using C++.
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum delete operations to make all elements of array same in C++.
- Minimum operations of given type to make all elements of a matrix equal in C++
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum operations to make array equal using Python
- Minimum operation to make all elements equal in array in C++
- Minimum number of moves to make all elements equal using C++.
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Minimum operations required to set all elements of binary matrix in C++
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to find minimum number of operations required to make one string substring of other in Python
