Codevita: Mockvita Problems

993 views
Skip to first unread message

kluvs1311

unread,
Aug 3, 2015, 4:32:49 AM8/3/15
to EncapsulateIdeas

Problem : Super ASCII String Checker

In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.
In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26. 
Your task is to find out whether the given string is a super ascii string or not.

Input Format: 

First line contains number of test cases T, followed by T lines, each containing a string "S".

Output Format: 

For each test case print "Yes" if the String "S" is super ascii, else print "No" 

Constraints:

1<=T<=100

1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').

Sample Input and Output

SNo.InputOutput
1
2
bba
scca


Yes
No



Explanation: 

In case 1, viz. String "bba" - 
The count of character 'b' is 2. Ascii value of 'b' is also 2. 
The count of character 'a' is 1. Ascii value of 'a' is also 1.
Hence string "bba" is super ascii. 



Problem : Zombie World

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

In order to defeat a zombie, player energy must be at least equal to the i-th zombie's energy. If the player succeeded in defeating the zombie, then he 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


Sample Input and Output

SNo.InputOutput
1
1
2 3
4 5
5 7





Problem : Trace the Rats

Given a square maze (A) of dimension N, every entry (Aij) in the maze is either an open cell 'O' or a wall 'X'. A rat can travel to its adjacent locations (left, right, top and bottom), but to reach a cell, it must be open. Given the locations of R rats, can you find out whether all the rats can reach others or not.

Input Format: 

Input will consist of three parts, viz.
1. Size of the maze (N)
2. The maze itself (A = N * N)
3. Number of rats (R)
4. Location of R rats (Xi, Yi)

Note:
(Xi,Yi) will represents the location of the i-th rat.
Locations are 1-index based. 

Output Format: 

Print "Yes" if the rats can reach each other, else print "No"

Constraints:

1<=N<=350

Aij = {'O','X'}

1<=X<=N*N

1<=Xi<=N

1<=Yi<=N


Sample Input and Output

SNo.InputOutput
1
3
O O X
O X O
O O X
4
1 1
1 2
2 1
3 2


Yes

2
3
O O X
O X O
O O X
4
1 1
1 2
2 1
2 3


No

Problem : Online Communities - Even Groups

In a social network, online communities refer to the group of people with an interest towards the same topic.
People connect with each other in a social network. A connection between Person I and Person J is represented as C I J. When two persons belonging to different communities connect, the net effect is merger of both communities which I and J belonged to.
We are only interested in finding out the communities with the member count being an even number. Your task is to find out those communities.

Input Format: 

First line starts with N, and then each line of input either represents a connection between two people or represents a query. In case of a connection between two people it will be represented by C I J, which will represent I and J are connected to each other. In case of a query it will be represented by Q. 
-1 will represents the end of input.

Input will consist of three parts, viz.
1. The total number of people on the social network (N)
2.Queries
C I J, connect I and J
Q 0 0, print the number of communities with even member-count

-1 will represent end of input. 

Output Format: 

For each query Q, output the number of communities with even member-count

Constraints:

1<=N<=1000000

1<=I, J<=N


Sample Input and Output


SNo.InputOutput
1
5
Q 0 0
C 1 2
Q 0 0
C 2 3
Q 0 0
C 4 5
Q 0 0
-1


0
1
0
1




Yes

mailmevivek17

unread,
Aug 3, 2015, 5:08:49 AM8/3/15
to EncapsulateIdeas
Zombie Solution

#include<stdio.h>

int main()
{
 int n, time, zombie[20],player, min,i, test,flag=0;
 scanf("%d",&test);
 while(test)
 {
  scanf("%d %d",&n,&time);
  for(i=0;i<n;i++)
   scanf("%d",&zombie[i]);
  scanf("%d %d",&player,&min);
  if(min>0 && min<=2000 && player>0 && player<=500 && n>0 && n<=50 && time>0 && time<=100)
  {
  if(time<n)
   flag++;
  for(i=0;i<n;i++)
  {
   if(player>=zombie[i])
   {
    player+=(player-zombie[i]);
   }
   else
   {
    flag++;
    break;
   }
  }
   if(player>=min && !flag)
    printf("Yes");
    else
     printf("No");
     }
 flag=0;
 test--;
 }
 return 0;
 }


<!--# test cases are failing against it, please chaeck-->

kluvs1311

unread,
Aug 3, 2015, 5:20:09 AM8/3/15
to EncapsulateIdeas
Super ASCII
 
#include<stdio.h>
#include<string.h>
int main()
{
int test, flag=0,length=0;
int j,count[26]={0,};
char arr[400];
scanf("%d",&test);
if(test>0 && test<=100)
{
while(test)
{
 scanf("%s",arr);
   length=strlen(arr);
 if(length<=400)
 {
  for(j=0;arr[j]!='\0';j++)
  {
   if(arr[j]<97 || arr[j]>122)
   {
    flag++;
    break;
   }
    count[(arr[j]-97)]++;
   }
 for(j=0;j<26;j++)
 {
  if(count[j]!=0)
  {
   if(count[j]!=j+1)
    {
     flag++;
     break;
    }
   }
 }
 if(flag)
  printf("No");
  else
  printf("Yes");
 }
  for(j=0;j<26;j++)
  {
  count[j]=0;
  }
  flag=0;
 test--;
 }
 }
return 0;
}




Utkarsh Bajpai

unread,
Aug 3, 2015, 5:46:45 AM8/3/15
to EncapsulateIdeas
Here is the solution for Super Ascii String Checker Complexity of this can be improved

here is code


import java.io.*;
class SuperAscii
{
static int count[]=new int[27];
public static void main(String args[])throws IOException
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the No of Test case");
int no=Integer.parseInt(br.readLine());
if(no>=1 && no<=100)
{
while(no!=0)
{
System.out.println("Enter the String");
String str=br.readLine();
int len=str.length();
if(strcheck(str,len))
{
for(int i=0;i<len;i++)
{
int value=((int)str.charAt(i))-96;
count[value]+=1;
}
if(check())
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
no=no-1;
}
else
{
System.out.println("Wrong String-->1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').\nre-enter");
}
}
}
else
{
System.out.println("Test case Exceeded");
}
}
catch(Exception e)
{
}
}
public static boolean check()
{
for(int i=1;i<=26;i++)
{
if(count[i]!=i && count[i]!=0)
{
return false;
}
}
return true;
}
public static boolean strcheck(String str,int len)
{
if(len<=1 || len >=400)
{
return false;
}
else
{
for(char c : str.toCharArray()) {
if(Character.isLetter(c) && Character.isUpperCase(c)) {
return false;
}
}
}
return true;

Utkarsh Bajpai

unread,
Aug 3, 2015, 11:20:55 AM8/3/15
to EncapsulateIdeas
here is the solution of Zombie World

CODE

import java.util.*;
class Zombie
{
public static void main(String args[])
{
try
{
int energy[];
boolean flag=true;
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
if(test>=1 && test<=10 )
{
while(test!=0)
{
int no_of_zombie=sc.nextInt();
if(no_of_zombie>=1 && no_of_zombie <=50)
{
int time=sc.nextInt();
if(time>=1 && time <= 100)
{
energy=new int[no_of_zombie];
for(int loopIndex=0;loopIndex<no_of_zombie;loopIndex++)
{
energy[loopIndex]=sc.nextInt();
if(energy[loopIndex]<=1 || energy[loopIndex]>=500)
{
System.out.println("Energy is out of range");
flag=false;
break;
}
}
if(flag)
{
int player=sc.nextInt();
if(player>=1 && player<=500)
{
int advance=sc.nextInt();
if(advance>=1 && advance <=2000)
{
for(int loopindex2=0;loopindex2<no_of_zombie;loopindex2++)
{
player+=Math.abs(player-energy[loopindex2]);
}
if(player<=advance)
{
System.out.println("yes");
}
else
{
System.out.println("NO");
}
}
}
else
{
System.out.println("Player Energy is Out of range");
}
}
}
else
{
System.out.println("Time Is Out of Range");
}
}
else
{
System.out.println("no of Zombies Exceeded");
}
test=test-1;
}
}
else
{
System.out.println("Test Case Exceeded");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

//check this if any doubt let me know

Utkarsh Bajpai

unread,
Aug 3, 2015, 11:22:52 AM8/3/15
to EncapsulateIdeas
Reply all
Reply to author
Forward
0 new messages