Minimum integer required to do S ≤ N*X
Last Updated :
19 Oct, 2023
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++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void minimumInt(vector< int >& A, int N)
{
int sm = 0;
for ( int i = 0; i < N; i++) {
sm += A[i];
}
vector< int > ans;
for ( int x = 0; x < N; x++) {
if (sm <= N * A[x]) {
ans.push_back(A[x]);
}
}
int result = *min_element(ans.begin(), ans.end());
cout << result << endl;
}
int main()
{
int N = 3;
vector< int > A = { 1, 2, 3 };
minimumInt(A, N);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
public class Main {
static void minimumInt(ArrayList<Integer> A, int N) {
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++) {
if (sm <= N * A.get(x)) {
ans.add(A.get(x));
}
}
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 );
minimumInt(A, N);
}
}
|
Python3
import sys
def minimumInt(A, N):
sm = 0
for i in range (N):
sm + = A[i]
ans = []
for x in range (N):
if sm < = N * A[x]:
ans.append(A[x])
result = min (ans)
print (result)
if __name__ = = '__main__' :
N = 3
A = [ 1 , 2 , 3 ]
minimumInt(A, N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static void minimumInt( int [] A, int N)
{
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++) {
if (sm <= N * A[x]) {
ans.Add(A[x]);
}
}
int result = ans.Min();
Console.WriteLine(result);
}
static public void Main()
{
int N = 3;
int [] A = { 1, 2, 3 };
minimumInt(A, N);
}
}
|
Javascript
function minimumInt(A, N) {
let sm = A.reduce((acc, val) => acc + val, 0);
let ans = [];
for (let x = 0; x < N; x++) {
if (sm <= N * A[x]) {
ans.push(A[x]);
}
}
let result = Math.min(...ans);
console.log(result);
}
let N = 3;
let A = [1, 2, 3];
minimumInt(A, N);
|
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:-
C++
#include <bits/stdc++.h>
using namespace std;
int minimumInteger( int N,vector< int > &A)
{
long long int sum=0;
int ans = INT_MAX;
sum = accumulate(A.begin(),A.end(),sum);
for ( int i=0;i<N;i++){
if (sum<=( long long int )N*A[i])
{
ans=min(ans,A[i]);
}
}
return ans;
}
int main() {
int N = 3;
vector< int > A = {1,2,3};
cout<<minimumInteger(N,A)<<endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int minimumInteger( int N, List<Integer> A) {
long sum = 0 ;
int ans = Integer.MAX_VALUE;
for ( int num : A) {
sum += num;
}
for ( int i = 0 ; i < N; i++) {
if (sum <= ( long ) N * A.get(i)) {
ans = Math.min(ans, A.get(i));
}
}
return ans;
}
public static void main(String[] args) {
int N = 3 ;
List<Integer> A = new ArrayList<>(Arrays.asList( 1 , 2 , 3 ));
System.out.println(minimumInteger(N, A));
}
}
|
Python3
import sys
def minimumInteger(N, A):
Sum = 0
ans = sys.maxsize
Sum = sum (A)
for i in range (N):
if Sum < = N * A[i]:
ans = min (ans, A[i])
return ans
N = 3
A = [ 1 , 2 , 3 ]
print (minimumInteger(N, A))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int MinimumInteger( int N, List< int > A)
{
long sum = 0;
int ans = int .MaxValue;
sum = A.Sum();
for ( int i = 0; i < N; i++)
{
if (sum <= ( long )N * A[i])
{
ans = Math.Min(ans, A[i]);
}
}
return ans;
}
static void Main( string [] args)
{
int N = 3;
List< int > A = new List< int > { 1, 2, 3 };
Console.WriteLine(MinimumInteger(N, A));
}
}
|
Javascript
function minimumInteger(N, A) {
let sum = 0;
let ans = Number.MAX_SAFE_INTEGER;
sum = A.reduce((acc, current) => acc + current, 0);
for (let i = 0; i < N; i++) {
if (sum <= N * A[i]) {
ans = Math.min(ans, A[i]);
}
}
return ans;
}
const N = 3;
const A = [1, 2, 3];
console.log(minimumInteger(N, A));
|
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.
Share your thoughts in the comments
Please Login to comment...