Techno Blender
Digitally Yours.

Minimum integer required to do S ≤ N*X

0 41


Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

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;

}

Time Complexity: O(N)
Auxiliary Space: O(N)

Like Article

Save Article


Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

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;

}

Time Complexity: O(N)
Auxiliary Space: O(N)

Like Article

Save Article

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment