Minimum integer required to do S ≤ N*X
Given an array A[] of size N. Let us denote S as the sum of all integers present in the array. Among all integers present in the array, find the minimum integer X such that S ≤ N*X.
Examples:
Input: N = 3, A[] = [1, 2, 3]
Output: 2
Explanation: Total sum of the array is 6, let’s check for all the numbers present in the array:
- array[0] = 1, 6 ≤ 1*3 => 6 ≤ 4 (Yep the number 4 is less than the total sum of the array but it is not equal, so we can check more).
- array[1] = 2, 6 ≤ 2*3 => 6 ≤ 6 (Yep it is basically equal to the sum of the array)
- array[2] = 3, 6 ≤ 3*3 => 6 !≤ 9 (No this condition get false which is greater than the sum of number)
In the following condition, we have a check that 1 and 2 stratified the condition. in both of them, we have seen that the number 2 is equal to the sum of the array. So, last we will output 2.
Approach: Steps involved in the implementation of code:
- Declarer the sum var with the sum of all numbers in the array
- After that, we can iterate through the array.
- And check if sum less than or equal to (≤) N*array[x] (sm ≤ n*arra[x])
- If this condition is will true then we push/append the value of the array(array[x]) in the list/vector.
- Done!. We can print out the minimum value of the result. (remember we need a minimum integer)
Below is the implementation of the code:
C++
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Given an array A[] of size N. Let us denote S as the sum of all integers present in the array. Among all integers present in the array, find the minimum integer X such that S ≤ N*X.
Examples:
Input: N = 3, A[] = [1, 2, 3]
Output: 2
Explanation: Total sum of the array is 6, let’s check for all the numbers present in the array:
- array[0] = 1, 6 ≤ 1*3 => 6 ≤ 4 (Yep the number 4 is less than the total sum of the array but it is not equal, so we can check more).
- array[1] = 2, 6 ≤ 2*3 => 6 ≤ 6 (Yep it is basically equal to the sum of the array)
- array[2] = 3, 6 ≤ 3*3 => 6 !≤ 9 (No this condition get false which is greater than the sum of number)
In the following condition, we have a check that 1 and 2 stratified the condition. in both of them, we have seen that the number 2 is equal to the sum of the array. So, last we will output 2.
Approach: Steps involved in the implementation of code:
- Declarer the sum var with the sum of all numbers in the array
- After that, we can iterate through the array.
- And check if sum less than or equal to (≤) N*array[x] (sm ≤ n*arra[x])
- If this condition is will true then we push/append the value of the array(array[x]) in the list/vector.
- Done!. We can print out the minimum value of the result. (remember we need a minimum integer)
Below is the implementation of the code:
C++
|
Time Complexity: O(N)
Auxiliary Space: O(N)