# Find the number of operations required for all array elements to be equal

| | |

Examples :

Input : arr = [1, 6, 1, 1, 1];
Output : 4
Explanation : Since arr becomes [2, 2, 2, 2, 2] after distribution from max element.

Input : arr = [2, 2, 3];
Output : -1
Explanation : Here arr becomes [3, 3, 1] after distribution.

Algorithm :

• Declare a temporary variable for store the number of operations performed.
• Find the maximum element of the given array and store its index value.
• Check if all elements are equal to the maximum element after n subtractions.
• Again check that each element is equal to the other elements and return n.

Below is the implementation of the above approach:

C++

 ` // C++ program for finding a number ` ` // operations required to perform ` ` // all array elements are equal ` ` # include "bits / stdC++. h" ` ` using ` ` namespace ` ` std; `   ` // Function for finding the maximum ` ` // element of this array ` ` int ` ` find_n (` ` int ` ` a [], ` ` int ` ` n) ` ` { ` ` int ` ` j = 0, k = 0, s = 0; `   ` int ` ` x = * max_element (a, a + n); ` ` int ` ` y = * min_element (a, a + n); ` ` for ` ` (` ` int ` ` i = 0; i "n; i ++) ` ` ` ` { ` ` ` ` if ` ` (a [i] == x) ` ` { ` ` s = i; ` ` break ` `; ` `} `   ` ` `} ` ` for ` ` (` ` int ` ` i = 0; i "n; i ++) ` ` { ` ` if ` ` (a [i]! = x & amp; & amp; a [i] "= y & amp; & amp; a [i]! = 0) ` ` { ` ` a [j] + = 1; ` ` a [s] - = 1; ` ` x - = 1; ` ` k + = 1; ` ` j + = 1; ` `} ` ` else ` ` if ` ` (a [i]! = 0) ` ` { ` ` j + = 1; ` `} ` `} ` ` `  ` for ` ` (` ` int ` ` i = 0; i "n; i ++) ` ` { ` ` ` ` if ` ` ( a [i]! = x) ` ` { ` ` k = -1; ` ` break ` `; ` `} ` `} ` ` ` ` return ` ` k; ` `} `   ` // Driver code ` ` int ` ` main () ` ` { `   ` int ` ` a [] = { 1, 6, 1, 1, 1}; ` ` int ` ` n = ` ` sizeof ` ` (a) / ` ` sizeof ` ` (a ); ` ` cout ""(find_n (a, n)); `   ` return ` ` 0; ` `} `   ` // This code is provided by princiraj1992 `

Java

 ` // Java program for finding the number ` ` // operations required to perform ` ` // all array elements are equal `   ` import ` ` java.util.Arrays; `   ` class ` ` GFG {` ` `  ` // Function for finding the maximum ` ` // element of this array ` ` static ` ` int ` ` find_n (` ` int ` ` [] a) {` ` ` ` int ` ` j = ` ` 0 ` `, k = ` ` 0 ` `, s = ` ` 0 ` `; `   ` int ` ` x = Arrays.stream (a) .max (). getAsInt (); ` ` int ` ` y = Arrays.stream (a) .min (). getAsInt (); ` ` for ` ` (` ` int ` ` i: a) {` ` if ` ` (a [i] == x) {` ` ` ` s = i; ` ` break ` `; ` `} `   ` ` `} ` ` for ` ` (` ` int ` ` i: a) {` ` if ` ` (i! = x & amp; & amp; i "= y & amp; & amp; i! = ` ` 0 ` `) {` ` ` ` a [j] + = ` ` 1 ` `; ` ` a [s] - = ` ` 1 ` `; ` ` x - = ` ` 1 ` `; ` ` k + = ` ` 1 ` `; ` ` j + = ` ` 1 ` `; ` `} ` ` else ` ` if ` ` (i! = ` ` 0 ` ` ) {` ` j + = ` ` 1 ` `; ` `} ` `} ` ` `  ` for ` ` (` ` int ` ` i: a) {` ` if ` ` (a [i]! = x) {` ` k = - ` ` 1 ` `; ` ` break ` `; ` `} ` `} ` ` ` ` return ` ` k; ` `} ` ` // Driver code ` ` `  ` ` ` public ` ` static ` ` void ` ` main (String [] args) {`   ` int ` ` [] a = {` ` 1 ` ` , ` ` 6 ` `, ` ` 1 ` `, ` ` 1 ` `, ` ` 1 ` `}; ` ` System.out.println (find_n (a)); ` `} `   `} `

python3

 ` # Python program to find the number ` ` # operations required to perform ` ` # all array elements are equal `   ` # Maximum search function ` ` # element of this array ` ` def ` ` find_n (a): ` ` j, k ` ` = ` ` 0 ` `, ` ` 0 ` ` `  ` ` ` x ` ` = ` ` max ` ` (a) ` ` for ` ` i ` ` in ` ` range ` ` (` ` len ` ` (a)): ` ` if ` ` (a [i] ` ` = ` ` = ` ` x): ` ` ` ` s ` ` = ` ` i ` ` break `   ` for i in a: `` if (i! = x and i " = min (a) and i! = ’’ ): a [j] + = 1 a [s] - = 1 x - = 1   k + = 1 j + = 1 elif (i! = ’’ ): j + = 1   for i in range ( len (a)):  if (a [i]! = x): k = - 1 break   return k   Driver code a = [ 1 , 6 , 1 , 1 , 1 ] print (find_n (a)) `

C #

PHP

 ` // C # program for finding the number ` ` // operations required to perform ` ` // all array elements are equal ` ` using ` ` System; ` ` using ` ` System.Linq; `   ` class ` ` GFG ` ` {` ` `  ` // Function for finding the maximum ` ` // element of this array ` ` static ` ` int ` ` find_n (` ` int ` ` [] a) ` ` {` ` int ` ` j = 0, k = 0, s = 0; `   ` int ` ` x = a.Max (); ` ` int ` ` y = a.Min (); ` ` foreach ` ` (` ` int ` ` i ` ` in ` ` a) ` ` {` ` if ` ` (a [i] == x) ` ` {` ` s = i; ` ` break ` `; ` `} `   ` ` `} `   ` foreach ` ` (` ` int ` ` i ` ` in ` ` a) ` ` ` ` {` ` if ` ` (i! = x & amp; & amp; i "= y & amp; & amp ; i! = 0) ` ` {` ` a [j] + = 1; ` ` a [s] - = 1; ` ` x - = 1; ` ` k + = 1; ` ` j + = 1; ` `} `   ` ` ` else ` ` if ` ` (i! = 0) ` ` { ` ` j + = 1; ` `} ` `} ` ` `  ` foreach ` ` (` ` int ` ` i ` ` in ` ` a) ` ` {` ` if ` ` (a [i]! = x) ` ` {` ` k = -1; ` ` break ` `; ` `} ` `} ` ` ` ` return ` ` k; ` `} `   ` // Driver code ` ` public ` ` static ` ` void ` ` Main () ` ` {` ` int ` ` [] a = {1, 6, 1, 1, 1 }; ` ` Console.Write (find_n (a)); ` `} ` `} `   ` // This code is provided by 29AjayKumar `
 ` & lt;? Php ` ` / / PHP program for finding the number ` ` // operations required to perform all ` ` // array elements Equal `   ` // Function to find the maximum element ` ` // this array ` ` function ` ` find_n (& amp; ` ` \$ a ` `) ` ` {` ` \$ j ` ` = 0; ` ` \$ k ` ` = 0; `   ` \$ x ` ` = max (` ` \$ a ` `); ` ` for ` ` (` ` \$ i ` ` = 0; ` ` \$ i ` ` "sizeof (` ` \$ a ` `); ` ` \$ i ` ` ++) ` ` {` ` if ` ` (` ` \$ a ` ` [` ` \$ i ` `] == ` ` \$ x ` `) ` ` {` ` \$ s ` ` = ` ` \$ i ` `; ` ` break ` `; ` `} ` `} ` ` `  ` for ` ` (` ` \$ i ` ` = 0; ` ` \$ i ` ` "sizeof (` ` \$ a ` `); ` ` \$ i ` ` ++) ` ` {` ` ` ` if ` ` (` ` \$ a ` ` [` ` \$ i ` `]! = ` ` \$ x ` ` and ` ` \$ a ` ` [ \$ i ] "= min ( \$ a ) and  ```` \$ a [ \$ i ]! = 0) {   \$ a [ \$ j ] + = 1; \$ a [ \$ s ] - = 1; code class = "plain">) { \$ j = 0;  \$ k = 0;   \$ x = max ( \$ a ); for ( \$ i = 0; \$ i "sizeof ( \$ a ); \$ i ++) { if ( \$ a [ \$ i ] == \$ x ) { \$ s = \$ i ; break ; } } (adsbygoogle = window.adsbygoogle || []).push({}); Shop Learn programming in R: courses\$ Best Python online courses for 2022\$ Best laptop for Fortnite\$ Best laptop for Excel\$ Best laptop for Solidworks\$ Best laptop for Roblox\$ Best computer for crypto mining\$ Best laptop for Sims 4\$ (adsbygoogle = window.adsbygoogle || []).push({}); Latest questions NUMPYNUMPY psycopg2: insert multiple rows with one query 12 answers NUMPYNUMPY How to convert Nonetype to int or string? 12 answers NUMPYNUMPY How to specify multiple return types using type-hints 12 answers NUMPYNUMPY Javascript Error: IPython is not defined in JupyterLab 12 answers All questions (adsbygoogle = window.adsbygoogle || []).push({}); News 13/08/2022 W4 Games called for DirectX12 support in the new version of the Godot game engine 13/08/2022 SK Hynix wants to build a chip testing and packaging plant in the US 13/08/2022 The developer showed that DDR5 memory diagnostics in the not yet presented version of MemTest86 was taken to a new level Wiki __del__ Python OpenCV | cv2.putText () method __del__ numpy.arctan2 () in Python __del__ Python | os.path.realpath () method around Python OpenCV | cv2.circle () method cvtcolor Python OpenCV cv2.cvtColor () method Python functions Python - Move item to the end of the list Counters time.perf_counter () function in Python __dict__ Check if one list is a subset of another in Python __del__ Python os.path.join () method © 2017—2022 Python Engineering Hub EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Python Loops Counters NumPy NLP Regular Expressions Wiki Tech news Python Wiki StackOverflow PHP JavaScript Books All books Computations Development Cryptography For dummies Big Data document.addEventListener("DOMContentLoaded", () => { let arrayCode = document.querySelectorAll('pre'); arrayCode.forEach(element => { element.classList.add("prettyprint"); }); }); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q022WLXW4X'); ```