# Find K such that changing all elements of the Array greater than K to K will make array sum N

Given an array arr[] and an integer N, the task is to find a number K from the given array such that if all the elements in the arr greater than K are changed to K then the sum of all the elements in the resulting array will be N. Print -1 if it is not possible.

Examples:

Input: arr[] = {3, 1, 10, 8, 4}, N = 16
Output: 4
If all the elements greater than 4 are changed to 4 then the resulting array will be {3, 1, 4, 4, 4}
Hence the sum will be 16 which is the required value of N.

Input: arr[] = {3, 1, 10, 8, 4}, N = 11
Output: -1

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

Approach: The idea is to use sorting to solve the above problem.

• First, sort the array in ascending order.
• Then traverse the array and for each element arr[i].
• Assume that all the elements after it has been changed to arr[i] and calculate the sum.
• If it is equal to N then return arr[i].
• If the end of the array is reached then print -1.

Below is the implementation of the above approach:

## CPP

 `// C++ implementation of the approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return K such that changing ` `// all elements greater than K to K will ` `// make array sum N otherwise return -1 ` `int` `findK(``int` `arr[], ``int` `size, ``int` `N) ` `{ ` ` `  `    ``// Sorting the array in increasing order ` `    ``sort(arr, arr + size); ` `    ``int` `temp_sum = 0; ` ` `  `    ``// Loop through all the elements of the array ` `    ``for` `(``int` `i = 0; i < size; i++) { ` `        ``temp_sum += arr[i]; ` ` `  `        ``// Checking if sum of array equals N ` `        ``if` `(N - temp_sum == arr[i] * (size - i - 1)) { ` `            ``return` `arr[i]; ` `        ``} ` `    ``} ` `    ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 1, 10, 4, 8 }; ` `    ``int` `size = ``sizeof``(arr) / ``sizeof``(``int``); ` `    ``int` `N = 16; ` ` `  `    ``cout << findK(arr, size, N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return K such that changing  ` `    ``// all elements greater than K to K will  ` `    ``// make array sum N otherwise return -1  ` `    ``static` `int` `findK(``int` `arr[], ``int` `size, ``int` `N)  ` `    ``{  ` `     `  `        ``// Sorting the array in increasing order  ` `        ``Arrays.sort(arr);  ` `        ``int` `temp_sum = ``0``;  ` `     `  `        ``// Loop through all the elements of the array  ` `        ``for` `(``int` `i = ``0``; i < size; i++) ` `        ``{  ` `            ``temp_sum += arr[i];  ` `     `  `            ``// Checking if sum of array equals N  ` `            ``if` `(N - temp_sum == arr[i] * (size - i - ``1``)) ` `            ``{  ` `                ``return` `arr[i];  ` `            ``}  ` `        ``}  ` `        ``return` `-``1``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `[]arr = { ``3``, ``1``, ``10``, ``4``, ``8` `};  ` `        ``int` `size = arr.length;  ` `        ``int` `N = ``16``;  ` `     `  `        ``System.out.print(findK(arr, size, N));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python

 `# Python3 implementation of the approach ` ` `  `# Function to return K such that changing ` `# all elements greater than K to K will ` `# make array sum N otherwise return -1 ` `def` `findK(arr, size, N): ` ` `  `    ``# Sorting the array in increasing order ` `    ``arr ``=` `sorted``(arr) ` `    ``temp_sum ``=` `0` ` `  `    ``# Loop through all the elements of the array ` `    ``for` `i ``in` `range``(size): ` `        ``temp_sum ``+``=` `arr[i] ` ` `  `        ``# Checking if sum of array equals N ` `        ``if` `(N ``-` `temp_sum ``=``=` `arr[i] ``*` `(size ``-` `i ``-` `1``)): ` `            ``return` `arr[i] ` `    ``return` `-``1` ` `  `# Driver code ` `arr ``=` `[``3``, ``1``, ``10``, ``4``, ``8``] ` `size ``=` `len``(arr) ` `N ``=` `16` ` `  `print``(findK(arr, size, N)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# implementation of the above approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return K such that changing  ` `    ``// all elements greater than K to K will  ` `    ``// make array sum N otherwise return -1  ` `    ``static` `int` `findK(``int` `[]arr, ``int` `size, ``int` `N)  ` `    ``{  ` `     `  `        ``// Sorting the array in increasing order  ` `        ``Array.Sort(arr);  ` `        ``int` `temp_sum = 0;  ` `     `  `        ``// Loop through all the elements of the array  ` `        ``for` `(``int` `i = 0; i < size; i++) ` `        ``{  ` `            ``temp_sum += arr[i];  ` `     `  `            ``// Checking if sum of array equals N  ` `            ``if` `(N - temp_sum == arr[i] * (size - i - 1)) ` `            ``{  ` `                ``return` `arr[i];  ` `            ``}  ` `        ``}  ` `        ``return` `-1;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main() ` `    ``{  ` `        ``int` `[]arr = { 3, 1, 10, 4, 8 };  ` `        ``int` `size = arr.Length;  ` `        ``int` `N = 16;  ` `     `  `        ``Console.Write(findK(arr, size, N));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```4
```

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.

My Personal Notes arrow_drop_up 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.

Improved By : mohit kumar 29, AnkitRai01