Wednesday, 29 March 2023

Minimum Integer 30/03/2023 -- GFG Problem of the day

 You are 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.


Example 1:


Input:

N = 3,

A = { 1, 3, 2}

Output:

2

Explanation:

Sum of integers in the array is 6.

since 6 ≤ 3*2, therefore 2 is the answer.

Example 2:


Input:

N = 1,

A = { 3 }

Output:

3

Explanation:

3 is the only possible answer

Your Task:

The task is to complete the function minimumInteger() which takes an integer N and an integer array A as input parameters and returns the minimum integer which satisfies the condition.


Expected Time Complexity: O(N)

Expected Auxiliary Space: O(1)


Constraints:

1 ≤  N ≤ 105

1 ≤  Ai ≤ 109


SOLUTION IN C++

from typing import List



class Solution:

    def minimumInteger(self, N : int, A : List[int]) -> int:

        # code here

        s = sum(A)

        ans = max(A)

        for a in A:

            if N * a >= s and a < ans:

                ans = a

        return ans



#{ 

 # Driver Code Starts

class IntArray:

    def __init__(self) -> None:

        pass

    def Input(self,n):

        arr=[int(i) for i in input().strip().split()]#array input

        return arr

    def Print(self,arr):

        for i in arr:

            print(i,end=" ")

        print()



if __name__=="__main__":

    t = int(input())

    for _ in range(t):


        N = int(input())



        A=IntArray().Input(N)


        obj = Solution()

        res = obj.minimumInteger(N, A)


        print(res)



# } Driver Code Ends


No comments:

Post a Comment

GeeksforGeeks and LeetCode - Problem Of The Day

Welcome to our Problem of the Day Solutions page, where we provide comprehensive solutions to daily programming challenges from two renowned...