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++ Implementation #include <algorithm> #include <iostream> #include <vector> using namespace std;
void minimumInt(vector< int >& A, int N)
{ // Total sum of array
int sm = 0;
for ( int i = 0; i < N; i++) {
sm += A[i];
}
vector< int > ans;
for ( int x = 0; x < N; x++) {
// Checking the condition
if (sm <= N * A[x]) {
// Appending the value if the
// condition is true
ans.push_back(A[x]);
}
}
// Finding the minimum element
int result = *min_element(ans.begin(), ans.end());
cout << result << endl;
} // Driver code int main()
{ int N = 3;
vector< int > A = { 1, 2, 3 };
// Function call
minimumInt(A, N);
return 0;
} |
import java.util.ArrayList;
import java.util.Collections;
public class Main {
static void minimumInt(ArrayList<Integer> A, int N) {
// Total sum of array
int sm = 0 ;
for ( int i = 0 ; i < N; i++) {
sm += A.get(i);
}
ArrayList<Integer> ans = new ArrayList<Integer>();
for ( int x = 0 ; x < N; x++) {
// Checking the condition
if (sm <= N * A.get(x)) {
// Appending the value if the condition is true
ans.add(A.get(x));
}
}
// Finding the minimum element
int result = Collections.min(ans);
System.out.println(result);
}
public static void main(String[] args) {
int N = 3 ;
ArrayList<Integer> A = new ArrayList<Integer>();
A.add( 1 );
A.add( 2 );
A.add( 3 );
// Function call
minimumInt(A, N);
}
} |
import sys
def minimumInt(A, N):
# Total sum of array
sm = 0
for i in range (N):
sm + = A[i]
ans = []
for x in range (N):
# Checking the condition
if sm < = N * A[x]:
# Appending the value if the
# condition is true
ans.append(A[x])
# Finding the minimum element
result = min (ans)
print (result)
# Driver code if __name__ = = '__main__' :
N = 3
A = [ 1 , 2 , 3 ]
# Function call
minimumInt(A, N)
# This code is contributed by tushar rokade
|
// C# code implementation for the above approach using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static void minimumInt( int [] A, int N)
{
// Total sum of array
int sm = 0;
for ( int i = 0; i < N; i++) {
sm += A[i];
}
List< int > ans = new List< int >();
for ( int x = 0; x < N; x++) {
// Checking the condition
if (sm <= N * A[x]) {
// Appending the value if the condition is
// true
ans.Add(A[x]);
}
}
// Finding the minimum element
int result = ans.Min();
Console.WriteLine(result);
}
static public void Main()
{
// Code
int N = 3;
int [] A = { 1, 2, 3 };
// Function call
minimumInt(A, N);
}
} // This code is contributed by karthik. |
function minimumInt(A, N) {
// Total sum of array
let sm = A.reduce((acc, val) => acc + val, 0);
let ans = [];
for (let x = 0; x < N; x++) {
// Checking the condition
if (sm <= N * A[x]) {
// Appending the value if the condition is true
ans.push(A[x]);
}
}
// Finding the minimum element
let result = Math.min(...ans);
console.log(result);
} // Driver code let N = 3; let A = [1, 2, 3]; // Function call minimumInt(A, N); // This code is contributed by rambabuguphka |
2
Time Complexity: O(N)
Auxiliary Space: O(N)
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 do sum of the array.
- After that we will take a ans variable which will store the minimum answer
- After it we will iterate over array and check if sum less than or equal to (≤) N*array[x] (sm ≤ n*arra[x]).
- If this condition is true we will take minimum in ans variable.
Implementation:-
#include <bits/stdc++.h> using namespace std;
//function to compute answer int minimumInteger( int N,vector< int > &A)
{ //variable to store sum
long long int sum=0;
//variable to store answer
int ans = INT_MAX;
//doing sum of all the elements
sum = accumulate(A.begin(),A.end(),sum);
//iterating over array
for ( int i=0;i<N;i++){
//checking condition
if (sum<=( long long int )N*A[i])
{
//taking minimum
ans=min(ans,A[i]);
}
}
return ans;
} int main() {
//size of array
int N = 3;
//array
vector< int > A = {1,2,3};
cout<<minimumInteger(N,A)<<endl;
return 0;
} //code contributed by shubhamrajput6156 |
import java.util.*;
public class GFG {
// Function to compute the answer
public static int minimumInteger( int N, List<Integer> A) {
// Variable to store sum
long sum = 0 ;
// Variable to store the answer
int ans = Integer.MAX_VALUE;
// Doing the sum of all the elements
for ( int num : A) {
sum += num;
}
// Iterating over the array
for ( int i = 0 ; i < N; i++) {
// Checking condition
if (sum <= ( long ) N * A.get(i)) {
// Taking the minimum
ans = Math.min(ans, A.get(i));
}
}
return ans;
}
// Driver code
public static void main(String[] args) {
// Size of array
int N = 3 ;
// Array
List<Integer> A = new ArrayList<>(Arrays.asList( 1 , 2 , 3 ));
System.out.println(minimumInteger(N, A));
}
} |
import sys
# function to compute answer def minimumInteger(N, A):
# variable to store sum
Sum = 0
# variable to store answer
ans = sys.maxsize
# doing sum of all the elements
Sum = sum (A)
# iterating over array
for i in range (N):
# checking condition
if Sum < = N * A[i]:
# taking minimum
ans = min (ans, A[i])
return ans
# size of array N = 3
# array A = [ 1 , 2 , 3 ]
print (minimumInteger(N, A))
|
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{ //function to compute answer
static int MinimumInteger( int N, List< int > A)
{
//variable to store sum
long sum = 0;
//variable to store answer
int ans = int .MaxValue;
//doing sum of all the elements
sum = A.Sum();
//iterating over array
for ( int i = 0; i < N; i++)
{
//checking condition
if (sum <= ( long )N * A[i])
{
//taking minimum
ans = Math.Min(ans, A[i]);
}
}
return ans;
}
static void Main( string [] args)
{
//size of array
int N = 3;
//array
List< int > A = new List< int > { 1, 2, 3 };
Console.WriteLine(MinimumInteger(N, A));
}
} |
// Function to compute the answer function minimumInteger(N, A) {
// Variable to store the sum
let sum = 0;
// Variable to store the answer, initialize it to a large value
let ans = Number.MAX_SAFE_INTEGER;
// Calculate the sum of all elements in the array
sum = A.reduce((acc, current) => acc + current, 0);
// Iterate over the array
for (let i = 0; i < N; i++) {
// Checking the condition
if (sum <= N * A[i]) {
// Update the answer with the minimum value
ans = Math.min(ans, A[i]);
}
}
return ans;
} // Main function const N = 3; // Size of the array
const A = [1, 2, 3]; // Array
// Call the function and print the result to the console console.log(minimumInteger(N, A)); |
2
Time Complexity:- O(N) ( As we are only traversing the array)
Auxiliary Space:- O(1) we are not using any vector to store answer and find minimum from that.