Array Manipulation and Sum - GeeksforGeeks

02 Aug, 2021
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++
Java
Python 3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the required elementint 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 codeint 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 implementation of the approachimport 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.
# Python3 implementation of the approach # Function to return the required elementdef getElement(a, n, S) :         # Sort the array    a.sort();     sum = 0;     for i in range(n) :                 # 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 Codeif __name__ == "__main__" :         S = 5;    a = [ 1, 3, 2, 5, 8 ];    n = len(a) ;     print(getElement(a, n, S)) ;     # This code is contributed by Ryuga
// C# implementation of the approachusing 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 implementation of the approach // Function to return the required elementfunction 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?>
<script> //Javascript implementation of the approach // Function to return the required elementfunction getElement(a, n, S){    // Sort the array    a.sort();     var sum = 0;     for (var 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;} var S = 5;var a = [ 1, 3, 2, 5, 8 ];var n = a.length;document.write(getElement(a, n, S));  // This code is contributed by SoumikMondal</script>
1
 
Time Complexity: O(N*logN)Auxiliary Space: O(1)
Akanksha_Rai
Code_Mech
ankthon
SoumikMondal
pankajsharmagfg
Sorting Quiz
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
DSA Sheet by Love Babbar
Difference between Informed and Uninformed Search in AI
SCAN (Elevator) Disk Scheduling Algorithms
Quadratic Probing in Hashing
K means Clustering - Introduction
Arrays in Java
Arrays in C/C++
Program for array rotation
Stack Data Structure (Introduction and Program)
Largest Sum Contiguous Subarray