TCS Digital Coding Solve | Code 1 | Codewindow.in

Question 1:

There are M number of boys, N number of girls and P number of pets given. A pet race is held for a fun-filled Sunday in a town. Each pet animal is accompanied by two persons for the race. The organiser planning for the seating arrangments by making groups such that each group has three members, and among these members, there is at least one boy and at least one girl, the third member being a boy, a girl, or a pet. One bench is required for each such group. The task is to find the maximum number of benches required for this.

Example 1:

Input:
5->Value of M
4->Value of N
2->Value of P

Output:
3

Explanation:
From this inputs given above:
Number of boys: 5
Number of girls: 4
Number of pets: 2

Group 1
Boy: 1
Girl: 1
Pet: 1

Group 2
Boy: 1
Girl: 1
Pet: 1

Group 3
Boy: 2
Girl: 1

Group 4
Boy: 1
Girl: 1

Among the above groups, only groups 1, 2 and 3 are valid groups. Whereas group 4 is invalid because it consist of only 2 members. Hence the output is 3.

Example 2:

Input:
3->Value of M
2->Value of N
0->Value of P

Output:
1

Explanation:
From this inputs given above:
Number of boys: 3
Number of girls: 2
Number of pets: 0

Group 1
Boy: 2
Girl: 1

Group 2
Boy: 1
Girl: 1

Among the above groups, only groups 1 is valid groups. Whereas group 2 is invalid because it consist of only 2 members. Hence the output is 1.

Constrains:
0<=M<=1000
0<=N<=1000
0<=p<=1000

Solution: In Python3

m=int(input())
n=int(input())
p=int(input())
#print(m,n,p)
bench=0
while m>0 or n>0 or p>0:
    #print(bench)
    if m-1>=0 and n-1>=0 and p-1>=0:
        m-=1 
        n-=1 
        p-=1 
        bench+=1
        #print(m,n,p)
        #print(bench)
        
    elif p==0:
        if m-2>=0 and n-1>=0:
            m-=2 
            n-=1 
            bench+=1
            #print(m,n,p)
            #print(bench)
        elif m-1>=0 and n-2>=0:
            m-=1 
            n-=2 
            bench+=1
            #print(m,n,p)
            #print(bench)
        else:
            m-=1 
            n-=1 
            if m==0 or n==0 or p==0:
                pass
            else:
                bench+=1
            #print(m,n,p)
            #print(bench)
print(bench)

Also Checkout

Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *