Given two integers N, K, and an array arr[] consisting of positive integers. The task is to find any possible array of size N such that the mean of arr[] and the array to be found together is K.
Examples:
Input: arr[] = {1, 5, 6}, N = 4, K = 3
Output: {1, 2, 3, 3}
Explanation: Mean of {1, 5, 6} and {1, 2, 3, 3} together is the following:
(1 + 5 + 6 + 1 + 2 + 3 + 3) / (3+4) = 3.
Therefore {1, 2, 3, 3} is possible array to make the mean = 3.Input: arr[] = {7, 8, 6}, N = 2, K = 3
Output: Not Possible
Approach: This problem can be solved by using simple Number System Average concepts. Let’s say sumArr be the sum of array new_arr[] and M to be the size of new_arr[]. Then according to average concept the equation can be formed as:
(sumArr + X) / (N+M) = K, where X is sum of required array.
X = K * (N + M) – sumArr.
Therefore, sum of the required array should be X to make the mean equal to K. If X is less than N, then there is no way to form the array.
The simplest way to form the required array is
1, 1, 1, …(M-1 times), X-(M-1)
.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Find the required array such that mean of both // the arrays is equal to K vector< int > findArray(vector< int > arr, int N, int K) { // To store sum of elements in arr[] int sumArr = 0; int M = int (arr.size()); // Iterate to find sum for ( int i = 0; i < M; i++) { sumArr += arr[i]; } // According to the formula derived above int X = K * (N + M) - sumArr; // If requiredSum if less than N if (X < N) { cout << "Not Possible" ; return {}; } // Otherwise create an array to store answer vector< int > res(N); // Putting all 1s till N-1 for ( int i = 0; i < N - 1; i++) { res[i] = 1; } res[N - 1] = X - (N - 1); // Return res as the final result return res; } // Driver Code int main() { vector< int > arr = { 1, 5, 6 }; int N = 4, K = 3; vector< int > ans = findArray(arr, N, K); for ( int i = 0; i < ans.size(); i++) cout << ans[i] << " " ; } |
Java
// Java program for the above approach public class GFG { // Find the required array such that mean of both // the arrays is equal to K static void findArray( int []arr, int N, int K) { // To store sum of elements in arr[] int sumArr = 0 ; int M = arr.length; // Iterate to find sum for ( int i = 0 ; i < M; i++) { sumArr += arr[i]; } // According to the formula derived above int X = K * (N + M) - sumArr; // If requiredSum if less than N if (X < N) { System.out.println( "Not Possible" ); } // Otherwise create an array to store answer int []res = new int [N]; // Putting all 1s till N-1 for ( int i = 0 ; i < N - 1 ; i++) { res[i] = 1 ; } res[N - 1 ] = X - (N - 1 ); // Return res as the final result for ( int i = 0 ; i < res.length; i++) System.out.print(res[i] + " " ); } // Driver Code public static void main (String[] args) { int []arr = { 1 , 5 , 6 }; int N = 4 , K = 3 ; findArray(arr, N, K); } } // This code is contributed by AnkThon |
Python3
# python program for the above approach # Find the required array such that mean of both # the arrays is equal to K def findArray(arr, N, K): # To store sum of elements in arr[] sumArr = 0 M = len (arr) # Iterate to find sum for i in range ( 0 , M): sumArr + = arr[i] # According to the formula derived above X = K * (N + M) - sumArr # If requiredSum if less than N if (X < N): print ( "Not Possible" ) return [] # Otherwise create an array to store answer res = [ 0 for _ in range (N)] # Putting all 1s till N-1 for i in range ( 0 , N - 1 ): res[i] = 1 res[N - 1 ] = X - (N - 1 ) # Return res as the final result return res # Driver Code if __name__ = = "__main__" : arr = [ 1 , 5 , 6 ] N = 4 K = 3 ans = findArray(arr, N, K) for i in range ( 0 , len (ans)): print (ans[i], end = " " ) # This code is contributed by rakeshsahni |
C#
// C# program for the above approach using System; public class GFG { // Find the required array such that mean of both // the arrays is equal to K static void findArray( int []arr, int N, int K) { // To store sum of elements in arr[] int sumArr = 0; int M = arr.Length; // Iterate to find sum for ( int i = 0; i < M; i++) { sumArr += arr[i]; } // According to the formula derived above int X = K * (N + M) - sumArr; // If requiredSum if less than N if (X < N) { Console.WriteLine( "Not Possible" ); } // Otherwise create an array to store answer int []res = new int [N]; // Putting all 1s till N-1 for ( int i = 0; i < N - 1; i++) { res[i] = 1; } res[N - 1] = X - (N - 1); // Return res as the final result for ( int i = 0; i < res.Length; i++) Console.Write(res[i] + " " ); } // Driver Code public static void Main ( string [] args) { int []arr = { 1, 5, 6 }; int N = 4, K = 3; findArray(arr, N, K); } } // This code is contributed by AnkThon |
Javascript
<script> // Javascript program for the above approach // Find the required array such that mean of both // the arrays is equal to K function findArray(arr, N, K) { // To store sum of elements in arr[] let sumArr = 0; let M = (arr.length); // Iterate to find sum for (let i = 0; i < M; i++) { sumArr += arr[i]; } // According to the formula derived above let X = K * (N + M) - sumArr; // If requiredSum if less than N if (X < N) { document.write( "Not Possible" ); return []; } // Otherwise create an array to store answer let res = new Array(N); // Putting all 1s till N-1 for (let i = 0; i < N - 1; i++) { res[i] = 1; } res[N - 1] = X - (N - 1); // Return res as the final result return res; } // Driver Code let arr = [1, 5, 6]; let N = 4, K = 3; let ans = findArray(arr, N, K); for (let i = 0; i < ans.length; i++) document.write(ans[i] + " " ); // This code is contributed by gfgking. </script> |
1 1 1 6
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!