Friday, 31 March 2023

Make Array Elements Equal

 #User function Template for python3


class Solution:

    def minOperations(self, N):

        # Code here

        if N == 1:

            return 0


        return N * N // 4


#{ 

 # Driver Code Starts

#Initial Template for Python 3


if __name__ == '__main__':


    T = int(input())

    while T > 0: 

        N =int(input())

        ob = Solution()

        print(ob.minOperations(N))


        T -= 1


Thursday, 30 March 2023

🗓️ geeksforgeeks problem-of-the-day March, Day 31

 #User function Template for python3


class Solution():

    def lexicographicallyLargest(self, a, n):

        #your code here

        ans = []

        j = 0

        for i in range(len(a)):

            if (a[i] - a[j]) % 2:

                t = a[j:i]

                t.sort(reverse=True)

                ans.extend(t)

                j = i


        t = a[j:len(a)]

        t.sort(reverse=True)

        ans.extend(t)


        return ans

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


Tuesday, 28 March 2023

Count the Substrings [29/03/2023]

Count the Substrings.py

Python Code !! 

#{ 

 # Driver Code Starts

#Initial Template for Python 3

from collections import Counter


class Solution:

    def countSubstring(self, S): 

        #code here

        n = len(S)

        d = Counter()

        d[0] = 1

        cnt = 0

        ans = 0

        for c in S:

            if c.isupper():

                cnt += 1

            else:

                cnt -= 1

            ans += d[cnt];

            d[cnt] += 1

        return ans


#{ 

 # Driver Code Starts.

if __name__ == '__main__': 

    t = int(input())

    for _ in range(t):

        S = input()

        ob = Solution()

        ans = ob.countSubstring(S)

        print(ans)


# } Driver Code Ends

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