Starters-84 Contest CodeChef

 All the Problem Statements and their Solution Codes are mentioned Below:-

                                           Me After Winning a Game with an FM after 10 Consecutive Loss

The Last Problem of the Contest, Sum OR is still being processed by my Obtuse Brain, well, CODE SPEAKS FOR ITSELF!! ;)

Digit Operation:

Problem Statement:-

You are given arrays � and �, each consisting of � strings, and a positive integer �.
For all 1≤�≤�, both �� and �� consist of characters from 0 to 9 (both inclusive).

You can perform the following types of operations on array �:

  • Type 1: Choose two indices � and � (�≠�) and swap any character of �� with any character of ��. The cost of this operation is 0.
  • Type 2: Choose an index � and replace one character of �� with any character from 0 to 9 (both inclusive). The cost of this operation is 1.

For example, let �=[24,30,51]. A valid sequence of operations can be:

  • Swap 4 in �1=24 with 0 in �2=30 to obtain [20,34,51].
  • Swap 0 in �1=20 with 5 in �3=51 to obtain [25,34,01].
  • Replace 0 in �3=01 with 1 to obtain [25,34,11].
    The cost of the above sequence of operations is 1.

Find whether we can convert the array � to array � using any (possibly zero) number of operations with cost ≤�.

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains two space-separated integers � and �, the size of array and the maximum cost of operations.
    • The second line of each test case contains � space-separated strings �1,�2,…,��.
    • The third line of each test case contains � space-separated strings �1,�2,…,��.

Output Format

For each test case, print YES if it is possible to convert array � to � using any (possibly zero) number of operations with cost ≤�. Else, print NO.

You may print each character of the string in uppercase or lowercase (for example, the strings yEs, yes, Yes, and YES will all be treated as identical).

Constraints

  • 1≤�≤1000
  • 2≤�≤105
  • 1≤�≤1018
  • 1≤∣��∣,∣��∣≤19
  • �� and �� consist of characters from 0 to 9 (both inclusive).
  • The sum of � over all test cases won't exceed 105.

Sample 1:

Input
Output
3
2 2
1 9
9 1
2 2
1 11
11 1
4 1
22 13 12 89
28 98 21 31
YES
NO
YES

Explanation:

Test case 1: We can use one operation of type 1 to swap 1 in �1 with 9 in �2. Thus, � becomes [9,1], which is equal to �. We are able to do this with 0 cost, which is ≤2.

Test case 2: It is impossible to convert � to � with at most 2 cost.

Code Implementation:-

for _ in range(int(input())):
    n,k=map(int,input().split())
    A=list(input().split())
    B=list(input().split())
    # print(A)
    # print(B)
    bad_c=False
    for i,st in enumerate(A):
        if(len(st)!=len(B[i])):
            bad_c=True
            break
        # for j,c in enumerate(st):
        #     if(B[i][j]!=)
           
    if(bad_c==True):
        print("NO")
    else:
        print("YES")


Chef and Adjacent Sums:

Problem Statement:-

You are given an array � of size �.
Consider an array � of size � formed by sorting � in non-decreasing order.
Let �=(��+�(�−1)).

Find whether there exists any rearrangement of the array �, such that, for all (1≤�<�),
(��+�(�+1))<�.
If such a rearrangement exists, print YES, otherwise print NO.

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains a integer � — the number of elements in the array.
    • The next line contains � space-separated integers, denoting the elements of the array.

Output Format

For each test case, output YES, if a rearrangement exists that satisfies the conditions. Otherwise, output NO.

You may print each character in uppercase or lowercase. For example, NO, no, No, and nO are all considered the same.

Constraints

  • 1≤�≤1000
  • 2≤�≤105
  • 1≤��≤105
  • The sum of � over all test cases won't exceed 105.

Sample 1:

Input
Output
4
3
3 4 4
4
1 2 3 4
2
2 2
2
1 2
YES
YES
NO
NO

Explanation:

Test case 1: Here �=[3,4,4] and �=(4+4)=8.
We can rearrange the array to [4,3,4] such that (�1+�2)=7<8 and (�2+�3)=7<8. Thus, the rearrangement is valid.

Test case 2: Here �=[1,2,3,4] and �=(4+3)=7.
We can rearrange the array to [4,2,3,1] such that (�1+�2)=6<7, (�2+�3)=5<7, and (�3+�4)=4<7. Thus, the rearrangement is valid.

Test case 3: The exists no possible rearrangement that satisfies the conditions.

Test case 4: The exists no possible rearrangement that satisfies the conditions.

Code Implementation:-

for _ in range(int(input())):
    n=int(input())
    nums=sorted(list(map(int,input().split())))
    mx_e=nums[-1]+nums[-2]
    for i in range(n//2):
        if((nums[i]+nums[n-1-i])>=mx_e):
            print("NO")
            break
    else:
        print("YES")


Max Count of  1:

Problem Statement:-

You are given a binary string � of length �.

You have to construct a binary string �, of length �, such that:

  • For each (1≤�<�), �(�+1)=(��⊕��), where ⊕ denotes the bitwise XOR operation;
  • The count of 1s in the string � is maximised.

Find the maximum count of 1 in string �.

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • The first line of each test case contains a single integer �, the length of the string �.
  • The second line of each test case contains a binary string �.

Output Format

For each test case, output on a new line, the maximum count of 1 in string �.

Constraints

  • 1≤�≤105
  • 1≤�≤3⋅105
  • � consists of 0 and 1 only.
  • The sum of � over all test cases won't exceed 3⋅105.

Sample 1:

Input
Output
3
4
0011
4
1100
4
1010
3
3
2

Explanation:

Test case 1: Conside the string �=1110.
Here, �2=�1⊕�1=1⊕0, �3=�2⊕�2=1⊕0, �4=�3⊕�3=1⊕1. It can be shown that no other value of � exists that satisfies the conditions and has more than three 1s.

Test case 2: Conside the string �=1011.
Here, �2=�1⊕�1=1⊕1, �3=�2⊕�2=0⊕1, �4=�3⊕�3=1⊕0. It can be shown that no other value of � exists that satisfies the conditions and has more than three 1s.

Test case 3: The string �=0110, which has two 1s.

Code Implementation:-

for _ in range(int(input())):
    n=int(input())
    s=input()
    def buzz(be):
        bit_elem=be
        if(bit_elem=='1'):
            count=1
        else:
            count=0
        for i in range(n-1):
            c=s[i]
            if(c==bit_elem):
                bit_elem='0'
            else:
                count+=1
                bit_elem='1'
        return count
    ans1=buzz('1')
    ans2=buzz('0')
    if(ans1>ans2):
        print(ans1)
    else:
        print(ans2)
    # if(s[0]=='0'):
    #     bit_elem='1'
    #     count=1
    # else:
    #     bit_elem='0'
    #     count=0
   
    # print(count)


Max Binary:

Problem Statement:-

Chef has a binary strings � of length �, and an integer �.
Hitesh wants to maximize the decimal representation of � using � operations of the following type:

  • Type 1: Insert 0 at any position in the string.
  • Type 2: Change any 0 to 1.

Help Hitesh find the modified string with maximum possible decimal representation after performing at most � operations.

Note that the decimal representation of a binary string refers to the numeric value it represents when converted to the decimal number system. For instance, the decimal representation of 101 will be 5 (22+20), and that of 000110 will be 6 (22+21)

Input Format

  • First line will contain �, number of test cases. Then the test cases follow.
  • The first line of each test case contains two integers � and �.
  • The second line contains the string �.

Output Format

For each test case, output on a new line, the modified string with maximum possible decimal representation after performing at most � operations.

Constraints

  • 1≤�≤1000
  • 1≤�≤106
  • 1≤�≤106
  • � consists of 0 and 1 only.
  • The sum of � and � over all test cases won't exceed 5⋅106.

Sample 1:

Input
Output
4
4 2
1101
6 3
001110
5 4
00110
3 1
000
110100
10111000
10110000
100

Explanation:

Test case 1: We are allowed to perform two operations. We can perform both operations of type 1 to obtain 110100, having decimal value 52.

Test case 2: We are allowed to perform three operations. We can perform two operations of type 1 to obtain 00111000, and one operation of type 2 to obtain 10111000, having decimal value 184.

Code Implementation:-

for _ in range(int(input())):
    n,k=map(int,input().split())
    s=input()
    if(s[0]=='1'):
        s=s+'0'*k
    else:
        if(len(s)>1):
            s='1'+s[1:]+'0'*(k-1)
        else:
            s='1'+'0'*(k-1)
    print(s)


Melt Gold:

Problem Statement:-

Chef has an ore with melting point of � degrees.
Chef’s kiln has a initial temperature of � degrees. The temperature of the kiln increases by � degrees after the ��ℎ minute.

Find the minimum time in minutes after which the ore starts melting.

Note:

  • We are only concerned about the temperature at the end of each minute and not during a minute.
  • The ore starts melting if the temperature of the kiln becomes greater than or equal to the melting point.

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • Each test case consists of two space-separated integers � and � — the melting point of the ore and the initial temperature of kiln.

Output Format

For each test case, output on a new line, the minimum time in minutes after which the ore starts melting.

Constraints

  • 1≤�≤105
  • 1≤�<�≤105

Sample 1:

Input
Output
3
3 2
5 3
10 5
1
2
3

Explanation:

Test case 1: The initial temperature of the kiln is 2 and the melting point of ore is 3.
After first minute, the temperature of kiln increases by 1.
Thus, after 1 minute the temperature of kiln becomes 2+1=3, which is equal to the melting point. Thus, the ore starts melting.

Test case 2: The initial temperature of the kiln is 3 and the melting point of ore is 5.
After first minute, the temperature of kiln increases by 1, and becomes 4.
After second minute, the temperature of kiln increases by 2, and becomes 6.

Thus, after 2 minutes the temperature of kiln becomes 6, which is greater than the melting point. Thus, the ore starts melting.

Test case 3: The initial temperature of the kiln is 5 and the melting point of ore is 10.
After first minute, the temperature of kiln increases by 1, and becomes 6.
After second minute, the temperature of kiln increases by 2, and becomes 8.
After third minute, the temperature of kiln increases by 3, and becomes 11.

Thus, after 3 minutes the temperature of kiln becomes 11, which is greater than the melting point. Thus, the ore starts melting.

Code Implementation:-

for _ in range(int(input())):
    x,y=map(int,input().split())
    diff=x-y
    temp=0
    count=1
    # for i in range(1,)
    while(True):
       
        temp+=count
        if(temp>=diff):
            break
       
        count+=1
    print(count)
   


Elections in ChefLand:

Problem Statement:-

Election season has started in Chefland and the election commission wants to know the count of eligible voters.

There are � people in Chefland where the age of the ��ℎ person in ��.
Given that a person needs to be at least � years old to vote, find the number of eligible voters.

Input Format

  • The first line of input will contain a single integer �, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains two space-separated integers � and � — the number of people in Chefland, and the minimum age required for a person to vote in Chefland.
    • The next line contains � space-separated integers, where the ��ℎ integer denotes the age of the ��ℎ person.

Output Format

For each test case, output on a new line, the number of eligible voters in Chefland.

Constraints

  • 1≤�≤200
  • 1≤�≤100
  • 1≤��,�≤100

Sample 1:

Input
Output
4
4 3
5 3 1 2
3 2
1 3 4
4 2
2 1 2 4
5 6
1 2 3 4 5
2
2
3
0

Explanation:

Test case 1: The minimum age to vote in Chefland is 3 years. There are 2 people with age greater than equal to 3 and thus, there are 2 eligible voters.

Test case 2: The minimum age to vote in Chefland is 2 years. There are 2 people with age greater than equal to 2 and thus, there are 2 eligible voters.

Test case 3: The minimum age to vote in Chefland is 2 years. There are 3 people with age greater than equal to 2 and thus, there are 3 eligible voters.

Test case 4: The minimum age to vote in Chefland is 6 years. There are no people with age greater than equal to 6 and thus, there are no eligible voters.

Code Implementation:-

for _ in range(int(input())):
    n,x=map(int,input().split())
    nums=list(map(int,input().split()))
    count=0
    for i in nums:
        if(i>=x):
            count+=1
    print(count)

Comments

Popular Posts