# Make all elements of an array equal with the given operation

Given an array arr[] of n integers and an integer k. The task is to make all the elements of arr[] equal with the given operation. In a single operation, any non-negative number x ≤ k (can be a floating point value) can be added to any element of the array and k will be updated as k = k – x. Print Yes is possible else print No.

Examples:

Input: k = 8, arr[] = {1, 2, 3, 4}
Output: Yes
1 + 3.5 = 4.5
2 + 2.5 = 4.5
3 + 1.5 = 4.5
4 + 0.5 = 4.5
3.5 + 2.5 + 1.5 + 0.5 = 8 = k

Input: k = 2, arr[] = {1, 2, 3, 4}
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Since the task is to make all elements of the array equal and the total of additions has to be exactly k. There is only a single value at which we can make them all of these elements equal i.e. (sum(arr) + k) / n. If there is an element in the array which is already greater than this value then the answer does not exist otherwise print Yes.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if all the elements ` `// of the array can be made equal ` `// with the given operation ` `bool` `isPossible(``int` `n, ``int` `k, ``int` `arr[]) ` `{ ` ` `  `    ``// To store the sum of the array elements ` `    ``// and the maximum element from the array ` `    ``int` `sum = arr, maxVal = arr; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``sum += arr[i]; ` `        ``maxVal = max(maxVal, arr[i]); ` `    ``} ` ` `  `    ``if` `((``float``)maxVal > (``float``)(sum + k) / n) ` `        ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `k = 8; ` `    ``int` `arr[] = { 1, 2, 3, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``if` `(isPossible(n, k, arr)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `//Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `// Function that returns true if all  ` `// the elements of the array can be  ` `// made equal with the given operation ` `static` `boolean` `isPossible(``int` `n, ``int` `k, ``int` `arr[]) ` `{ ` ` `  `    ``// To store the sum of the array elements ` `    ``// and the maximum element from the array ` `    ``int` `sum = arr[``0``]; ` `    ``int` `maxVal = arr[``0``]; ` ` `  `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `    ``{ ` `        ``sum += arr[i]; ` `        ``maxVal = Math.max(maxVal, arr[i]); ` `    ``} ` ` `  `    ``if` `((``float``)maxVal > (``float``)(sum + k) / n) ` `        ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `     `  `        ``int` `k = ``8``; ` `        ``int` `arr[] = { ``1``, ``2``, ``3``, ``4` `}; ` `        ``int` `n = arr.length; ` ` `  `        ``if` `(isPossible(n, k, arr)) ` `            ``System.out.println (``"Yes"``); ` `        ``else` `            ``System.out.println( ``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by @Tushil.  `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function that returns true if all  ` `# the elements of the array can be  ` `# made equal with the given operation ` `def` `isPossible(n, k, arr): ` `     `  `    ``# To store the sum of the array elements ` `    ``# and the maximum element from the array ` `    ``sum` `=` `arr[``0``] ` `    ``maxVal ``=` `arr[``0``]; ` ` `  `    ``for` `i ``in` `range``(``1``, n): ` `        ``sum` `+``=` `arr[i] ` `        ``maxVal ``=` `max``(maxVal, arr[i]) ` ` `  ` `  `    ``if` `(``int``(maxVal)> ``int``((``sum` `+` `k) ``/` `n)): ` `        ``return` `False` ` `  `    ``return` `True` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``k ``=` `8` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``] ` `    ``n ``=` `len``(arr) ` ` `  `    ``if` `(isPossible(n, k, arr)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Function that returns true if all  ` `    ``// the elements of the array can be  ` `    ``// made equal with the given operation  ` `    ``static` `bool` `isPossible(``int` `n,  ` `                        ``int` `k, ``int` `[]arr)  ` `    ``{  ` `     `  `        ``// To store the sum of the array elements  ` `        ``// and the maximum element from the array  ` `        ``int` `sum = arr;  ` `        ``int` `maxVal = arr;  ` `         `  `        ``for` `(``int` `i = 1; i < n; i++)  ` `        ``{  ` `            ``sum += arr[i];  ` `            ``maxVal = Math.Max(maxVal, arr[i]);  ` `        ``}  ` `     `  `        ``if` `((``float``)maxVal > (``float``)(sum + k) / n)  ` `            ``return` `false``;  ` `     `  `        ``return` `true``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `         `  `        ``int` `k = 8;  ` `        ``int` `[]arr = { 1, 2, 3, 4 };  ` `        ``int` `n = arr.Length;  ` `     `  `        ``if` `(isPossible(n, k, arr))  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``else` `            ``Console.WriteLine( ``"No"``);  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Ryuga `

## PHP

 ` (float)(``\$sum` `+ ``\$k``) / ``\$n``) ` `        ``return` `false; ` ` `  `    ``return` `true; ` `} ` ` `  `    ``// Driver code ` `    ``\$k` `= 8; ` `    ``\$arr` `= ``array``( 1, 2, 3, 4 ); ` `    ``\$n` `= sizeof(``\$arr``) / sizeof(``\$arr``); ` ` `  `    ``if` `(isPossible(``\$n``, ``\$k``, ``\$arr``)) ` `        ``echo` `"Yes"``; ` `    ``else` `        ``echo` `"No"``; ` ` `  `# This code is contributed by akt_miit. ` `?> `

Output:

```Yes
```

Time Complexity: O(n) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.