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++
// 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;
}
Java
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);
}
}
Python3
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#
// 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.
JavaScript
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
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;
//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
Java
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));
}
}
Python3
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))
C#
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));
}
}
JavaScript
// 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));
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.
Similar Reads
Minimum count of digits required to obtain given Sum Given an integer N, the task is to find the minimum number of digits required to generate a number having the sum of digits equal to N. Examples: Input: N = 18 Output: 2 Explanation: The number with smallest number of digits having sum of digits equal to 18 is 99. Input: N = 28 Output: 4 Explanation
3 min read
Minimum time required to produce m items Given n machines represented by an integer array arr[], where arr[i] denotes the time (in seconds) taken by the i-th machine to produce one item. All machines work simultaneously and continuously. Additionally, we are also given an integer m, representing the total number of items required. The task
11 min read
Minimum number operations required to convert n to m | Set-2 Given two integers n and m and a and b, in a single operation n can be multiplied by either a or b. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1. Examples: Input: n = 120, m = 51840, a = 2, b = 3
5 min read
Minimum number operations required to convert n to m | Set-2 Given two integers n and m and a and b, in a single operation n can be multiplied by either a or b. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1. Examples: Input: n = 120, m = 51840, a = 2, b = 3
5 min read
Minimum squares to cover a rectangle Given a rectangle with length l and breadth b, we need to find the minimum number of squares that can cover the surface of the rectangle, given that each square has a side of length a. It is allowed to cover the surface larger than the rectangle, but the rectangle has to be covered. It is not allowe
9 min read
Minimum squares to cover a rectangle Given a rectangle with length l and breadth b, we need to find the minimum number of squares that can cover the surface of the rectangle, given that each square has a side of length a. It is allowed to cover the surface larger than the rectangle, but the rectangle has to be covered. It is not allowe
9 min read