Join Regular Classroom : Visit ClassroomTech

Array | Zombie World | Codewindow.in

Zoya has developed a new game called Zombie World. The objective of the game is to kill all zombies in given amount of time. More formally,
– N represents the total number of zombies in the current level
– T represents the maximum time allowed for the current level
– P represents the initial energy level a player starts with
– Ei defines the energy of the i-th zombie
– D defines the minimum energy the player needs, to advance to the next level

When a player energy is greater than or equal to the i-th zombie’s energy, the player wins. Upon winning, the player will be awarded with an additional energy equal to the difference between current zombie energy and the player energy. One unit of time will be taken to complete the fight with a single zombie.

Rules of the game:
– At any given time, a player can fight with only one zombie
– Player is allowed to choose any one zombie to fight with.

Your task is to determine whether the player will advance to the next level or not, if he plays optimally.

Input Format:
The first line contains the number of test cases (K). Each test case consists of three parts:
1. The total number of zombies (N) and the maximum time allowed (T)
2. Array of size N, which represents the energy of zombies (E)
3. The initial energy level a player (P) and the minimum energy required to advance (D)

Output Format:
Print “Yes” if a player can advance to the next level else print “No”.

Constraints:
1<=K<=10
1<=N<=50
1<=Ei<=500
1<=T<=100
1<=D<=2000
1<=P<=500

Example
Input:
1
2 3
4 5
5 7

Output:
Yes

Solution: In C

#include<stdio.h>

int main()
{
    int n,t,e[20],i,pe,me,k;
    scanf("%d",&k);
    while(k)
    {
        scanf("%d",&n);
        scanf("%d",&t);
        for(i=0;i<n;i++)
            scanf("%d",&e[i]);
        scanf("%d",&pe);
        scanf("%d",&me);
        if(t<n)
            goto x;
        else
        {
            for(i=0;i<n;i++)
            {
                if(pe>=e[i])
                {
                    pe=pe+(pe-e[i]);
                }
            }
            if(pe<=me)
                printf("Yes\n");
            else
                x: printf("No\n");
        }
        k--;
    }
    return 0;
}

Solution: In Java

import java.util.Scanner;

class Zombie{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n,t,e[] = new int[20],i,pe,me,k;
        k = sc.nextInt();
        while(k>0)
        {
            n = sc.nextInt();
            t = sc.nextInt();
            for(i=0;i<n;i++)
                e[i] = sc.nextInt();
            pe = sc.nextInt();
            me = sc.nextInt();
            if(t<n){
                System.out.printf("No\n");
                k--;
                continue;
            }
            else
            {
                for(i=0;i<n;i++)
                {
                    if(pe>=e[i])
                    {
                        pe=pe+(pe-e[i]);
                    }
                }
                if(pe<=me)
                    System.out.printf("Yes");
                else
                    System.out.printf("No\n");
            }
            k--;
        }
    }
}

Solution: In Python 3

e = []
k = int(input())
while k>0:
    n, t = [int(x) for x in input().split()]
    e = list(map(int, input().split()))
    pe, me = [int(x) for x in input().split()]
    if (t<n):
        print("No")
        k = k-1
        continue
    else:
        for i in range(0,n):
            if (pe>=e[i]):
                pe=pe+(pe-e[i])
        if (pe<=me):
            print("Yes")
        else:
            print("No\n")
    k = k-1

Also Checkout

Recent Posts
Categories
Pages