Array Manipulation and Sum
Given an array arr[] of N integers and an integer S. The task is to find an element K in the array such that if all the elements from the array > K are made equal to K then the sum of all the elements of the resultant array becomes equal to S. If it is not possible to find such an element then print -1 .
Examples:
Input: M = 15, arr[] = {12, 3, 6, 7, 8}
Output: 3
Resultant array = {3, 3, 3, 3, 3}
Sum of the array elements = 15 = SInput: M = 5, arr[] = {1, 3, 2, 5, 8}
Output: 1
Approach: Sort the array. Traverse the array considering that the value of K is equal to the current element and then check if sum of the previous elements + (K * number of remaining elements) = S. If yes then print the value of K, if no such element found then print -1 in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the required element int getElement(int a[], int n, int S) { // Sort the array sort(a, a + n); int sum = 0; for (int i = 0; i < n; i++) { // If current element // satisfies the condition if (sum + (a[i] * (n - i)) == S) return a[i]; sum += a[i]; } // No element found return -1; } // Driver code int main() { int S = 5; int a[] = { 1, 3, 2, 5, 8 }; int n = sizeof(a) / sizeof(a[0]); cout << getElement(a, n, S); return 0; } |
Java
//Java implementation of the approach import java.util.Arrays; class GFG { // Function to return the required element static int getElement(int a[], int n, int S) { // Sort the array Arrays.sort(a); int sum = 0; for (int i = 0; i < n; i++) { // If current element // satisfies the condition if (sum + (a[i] * (n - i)) == S) return a[i]; sum += a[i]; } // No element found return -1; } // Driver code public static void main(String[] args) { int S = 5; int a[] = { 1, 3, 2, 5, 8 }; int n = a.length; System.out.println(getElement(a, n, S)); } } // This code is contributed by Mukul singh. |
C#
// C# implementation of the approach using System; class GFG { // Function to return the required element static int getElement(int[] a, int n, int S) { // Sort the array Array.Sort(a); int sum = 0; for (int i = 0; i < n; i++) { // If current element // satisfies the condition if (sum + (a[i] * (n - i)) == S) return a[i]; sum += a[i]; } // No element found return -1; } // Driver code public static void Main() { int S = 5; int[] a = { 1, 3, 2, 5, 8 }; int n = a.Length; Console.WriteLine(getElement(a, n, S)); } } // This code is contributed by Mukul singh. |
PHP
<?php // PHP implementation of the approach // Function to return the required element function getElement($a, $n, $S) { // Sort the array sort($a, 0); $sum = 0; for ($i = 0; $i < $n; $i++) { // If current element // satisfies the condition if ($sum + ($a[$i] * ($n - $i)) == $S) return $a[$i]; $sum += $a[$i]; } // No element found return -1; } // Driver code $S = 5; $a = array(1, 3, 2, 5, 8); $n = sizeof($a); echo getElement($a, $n, $S); // This code is contributed // by Akanksha Rai ?> |
1
Recommended Posts:
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Check whether an array can be fit into another array rearranging the elements in the array
- Generate original array from an array that store the counts of greater elements on right
- Print modified array after multiple array range increment operations
- Find minimum value to assign all array elements so that array product becomes greater
- Longest alternating sub-array starting from every index in a Binary Array
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Sort an array where a subarray of a sorted array is in reverse order
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Maximum subarray sum in array formed by repeating the given array k times
- Check if an array can be Arranged in Left or Right Positioned Array
- Insert minimum number in array so that sum of array becomes prime
- Construct an array from GCDs of consecutive elements in given array
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.



