Sie sind auf Seite 1von 54

1.MEMORISE ME!

(ARRAY)

PROBLEM STATEMENT:

Arijit is a brilliant boy. He likes memory games. He likes to participate alone but this time he has
to have a partner. So he chooses you.

In this Game , your team will be shown N numbers for few minutes . You will have to memorize
these numbers.

Now, the questioner will ask you Q queries, in each query He will give you a number , and you
have to tell him the total number of occurrences of that number in the array of numbers shown
to your team . If the number is not present , then you will have to say “NOT PRESENT” (without
quotes).

INPUT And OUTPUT

The first line of input will contain N, an integer, which is the total number of numbers shown to
your team.

The second line of input contains N space separated integers .

The third line of input contains an integer Q , denoting the total number of integers.

The Next Q lines will contain an integer denoting an integer, , for which you have to print the
number of occurrences of that number () in those N numbers on a new line.

If the number isn’t present then Print “NOT PRESENT” (without quotes) on a new line.

SAMPLE INPUT

111220

1
1

SAMPLE OUTPUT

NOT PRESENT

NOT PRESENT

SOURCE CODE:

#include <stdio.h>

int main()

int A[10000]={0}, i, j, Q, N, a, b;

scanf("%d", &N);

for(i=0; i<N; i++){

scanf("%d", &a);

A[a]++;

scanf("%d", &Q);

for(i=0; i<Q; i++){

2
scanf("%d", &b);

if(A[b]==0){

printf("NOT PRESENT\n");

else

printf("%d\n", A[b]);

return 0;

RESULT:

Sample Test Cases Passed

Time (sec)

0.108613

Input

111220

3
Your Code's Output

NOT PRESENT

NOT PRESENT

Expected Correct Output

Compilation Log

Compiled successfully.

Execution Log

No execution log!

4
5
2.BAG OF NUMBERS

(STACK)

PROBLEM STATEMENT:

Vikas is given a bag which consists of numbers (integers) blocks,Vikas has to organize the
numbers again in the same order as he has inserted it into the bag, i.e. the first number inserted
into the bag by Vikas should be picked up first followed by other numbers in series. Help Vikas
to complete this work in O(n) time complexity with the condition to use one extra bag to
complete the work (assume that the bags are compact and is in the form of a stack structure
and has the same width as that of the number blocks and is large enough to fill the bag to the
top and the number taken from bag is in reverse order).

Hint: You may use the concept of Stacks.

SAMPLE INPUT

input: 15 21 39 390 392 380.

SAMPLE OUTPUT

output: 15 21 39 390 392 380.

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

char s[7];

scanf("%s", s);

printf("output");

scanf("%d", &n);

if (n == 1)

printf(" %d", n);

else

6
printf(": %d", n);

while(scanf("%d", &n))

printf(" %d", n);

printf(".");

RESULT:

Sample Test Cases Passed

Time (sec)

0.113485

Input

input: 15 21 39 390 392 380.

Your Code's Output

output: 15 21 39 390 392 380.

Expected Correct Output

output: 15 21 39 390 392 380.

Compilation Log

Compiled successfully.

Execution Log

No execution log!

7
3.MONK AND CHAMBER OF SECRETS

(QUEUE)

PROBLEM STATEMENT:

Hagrid says "follow the spiders" and so Harry and Ron head to the Forbidden Forest. There they
meet Aragog, a giant spider who tells them about the innocence of Hagrid. But Aragog only
allows Hagrid to go back. These boys have got into a serious trouble now.

The only way to escape as Aragog says is to answer a question.

Aragog shows them a queue of N spiders of which only X spiders are to be selected. Each spider
has some power associated with it. There are X iterations on the queue.

In each iteration, X spiders are dequeued (if queue has less than X entries, all of them will be
dequeued) and the one with maximum power is selected and remaining spiders are enqueued
back to the queue (in the order they were dequeued) but their power is decreased by one unit.
If there are multiple spiders with maximum power in those dequeued spiders, the one which

8
comes first in the queue is selected. If at any moment , power of any spider becomes 0, it can't
be decremented any further, it remains the same.

Now, Aragog asks the boys to tell him the positions of all the selected spiders (positions in the
initial given queue) in the order they are selected. As the boys are frightened and can't think of
anything , they call Monk for the rescue. Help Monk to get the answer fast and save the boys.

Input Format:

The first line consists of two space separated integers N and X, denoting the number of spiders
in the queue and the number of spiders that have to be selected respectively.

The next line consists of an array A, denoting the power of spider at position i ().

Output Format:

For each of the X iterations, output the position of the selected spider in that iteration. Position
refers to the index at which the spider was present in the initial given queue (1 based indexing).

Constraints:

1<=X<=316

X<=N<=X*X

1<=A[i]<=X:1<=i<=N

SAMPLE INPUT

65

122345

SAMPLE OUTPUT

9
56412

SOURCE CODE:

#include<stdio.h>

#include<string.h>

#include<math.h>

#include<string.h>

int main()

int idx,l,n,flag,y,x,d[1000],e[1000],max,c[100010],a[100010],i,j;

scanf("%d%d",&n,&x);

for(i=1;i<=n;i++) {

scanf("%d",&a[i]);

c[i]=i;

for(j=1;j<=x;j++)

max=-1;

if(n<x)

y=n;

else

y=x;

for(i=1;i<=y;i++)

if(a[i]>max)

10
{

max=a[i];

idx=c[i];

flag=0;

for(i=1;i<=x;i++)

if(idx==c[i])

flag=1;

continue;

if(a[i]>0)

d[i-flag]=a[i]-1;

else

d[i-flag]=a[i];

e[i-flag]=c[i];

for(i=1;i<=n-x;i++)

a[i]=a[i+x];

c[i]=c[i+x];

l=1;

11
for(;i<n;i++)

a[i]=d[l];

c[i]=e[l++];

n--;

printf("%d ",idx);

printf("\n");

return 0;}

RESULT:

Sample Test Cases Passed

Time (sec)

0.101661

Input

65

122345

Your Code's Output

56412

Expected Correct Output

56412

Compilation Log

Compiled successfully.

12
Execution Log

No execution log!

13
4.HACKER AND TRAFFIC LIGHTS

(QUEUE)

PROBLEM STATEMENT:

Zolo is stuck in a traffic due to dysfunctional traffic light. Zolo is a professional hacker and he can
get into the system and change the state of the light. His planet has different types of traffic
lights such that there are N bulbs on the traffic board and only when all of them are green(G)
the cars can pass. there are 2 other states also which the bulb can show; i.e. Red(R) & Yellow(Y).
Note that the lights are designed such that they follow a state change cyclic pattern as follows:

R------>Y------>G------->R

Once Zolo gets into the system he can select any position i and update all elements between i to
min(N, i + K - 1) by increasing their state by 1.This whole process takes 1 sec and he can repeat
this process any no. of times until he gets all lights = G . Find the minimum time to do the
process as Zolo is getting late for work.

INPUT:

The first line contains N K

The second line describes the current status of each bulb as an array whose each element can
either be G or Y or R

OUTPUT:

print the minimum amount of time required to clear the traffic jam.

CONSTRAINTS:

1<=N, K<=100000

14
SAMPLE INPUT

42

RYGY

SAMPLE OUTPUT

SOURCE CODE:

#include<stdio.h>

int n, k;

int arr[100005];

int temp[100005];

int answer = 0;

int sum = 0;

int a = 0;

int b = 0;

int main() {

scanf("%d %d", &n, &k);

for (int i = 1; i <= n; i++) {

char bulb;

scanf(" %c", &bulb);

if (bulb == 'G') arr[i] = 0;

if (bulb == 'R') arr[i] = 1;

15
if (bulb == 'Y') arr[i] = 2;

for (int i = 1; i<=n; i++) {

arr[i] += sum;

if ((arr[i] + 1) % 3 == 0) {

temp[b++] = 1;

answer+=1;

sum+=1;

else if ((arr[i] + 2) % 3 == 0) {

temp[b++] = 2;

answer += 2;

sum += 2;

else temp[b++] = 0;

if (i >= k) sum = sum-temp[a++];

printf("%d\n", answer);

return 0;

RESULT:

Sample Test Cases Passed

Time (sec)

16
0.10213

Input

42

RYGY

Your Code's Output

Expected Correct Output

Compilation Log

Compiled successfully.

Execution
Log

No
execution
log!

17
5.REMOVE FRIENDS

(LINKED LIST)

PROBLEM STATEMENT:

After getting her PhD, Christie has become a celebrity at her university, and her facebook profile
is full of friend requests. Being the nice girl she is, Christie has accepted all the requests.

Now Kuldeep is jealous of all the attention she is getting from other guys, so he asks her to
delete some of the guys from her friend list.

To avoid a 'scene', Christie decides to remove some friends from her friend list, since she knows
the popularity of each of the friend she has, she uses the following algorithm to delete a friend.

Algorithm Delete(Friend):
DeleteFriend=false
for i = 1 to Friend.length-1
if (Friend[i].popularity < Friend[i+1].popularity)
delete i th friend
DeleteFriend=true
break
if(DeleteFriend == false)
delete the last friend

Input:
First line contains T number of test cases. First line of each test case contains N, the number of
friends Christie currently has and K ,the number of friends Christie decides to delete. Next lines
contains popularity of her friends separated by space.

Output:
For each test case print N-K numbers which represent popularity of Christie friend's after
deleting K friends.

Constraints:
1<=T<=1000
1<=N<=100000

18
0<=K< N
0<=popularity_of_friend<=100

NOTE:
Order of friends after deleting exactly K friends should be maintained as given in input.

SAMPLE INPUT:

31

3 100 1
52
19 12 3 4 17
53
23 45 11 77 18
100 1

SAMPLE OUTPUT:

19 12 17

77 18

SOURCE CODE:

#include<stdio.h>

int a[100000];

int z = -1;

int empty() {

return (z == -1);

void push(int x) {

a[++z] = x;

int peek() {

19
return a[z];

int pop() {

return a[z--];

int main() {

int x;

scanf("%d",&x);

while(x--) {

int n,k;

scanf("%d %d",&n,&k);

int a[n];

for(int i=0;i<n;i++)

scanf("%d",a+i);

z = -1;

int c=0;

for(int i=0;i<n;i++) {

while(!empty() && (a[i] > peek()) && (c < k)){

pop();

c++;

push(a[i]);

while(!empty() && c<k){

pop();

20
c++;

int b[n-k],i=n-k-1;

while(!empty()) {

b[i--]=peek();

pop();

for(int i=0;i<n-k;i++)

printf("%d ",b[i]);

printf("\n");

return 0;

RESULT:
Sample Test Cases passed
Time (sec)
0.101253

Input
3
31
3 100 1
52
19 12 3 4 17
53
23 45 11 77 18

Your Code's Output


100 1
19 12 17
77 18
Expected Correct Output
100 1

21
19 12 17
77 18

Compilation Log
Compiled successfully.

22
6.CRICKET BALLS

(HASH TABLE)

PROBLEM STATEMENT:

Daksh has 'N' boxes of cricket balls and each box has certain number of cricket balls (non-zero)
in it. The boxes are numbered from 1 to N.

Now, Daksh has to go for practicing to cricket ground to compete in the upcoming fest URJA.
Daksh wants to carry exactly K number of cricket balls to ground and he can carry only 2 boxes.

23
Can you tell him the number of ways in which he can select 2 boxes such that total number of
cricket balls in them is K.

Input :

First line of the input is the number of test cases T.

Each test case has 3 lines. First line is the number of boxes N.

Next line has N space separated integers where the ith integer is the number of cricket balls in
the ith box.

3rd line of each test case contains value of K.

Output:

For each test case, output a single number, the number of ways in which Daksh can select the
boxes.

Constraints:

1<=T<=100

1<=N<=105

1<=Number of Balls in ith box<=100

SAMPLE INPUT

12343

24
5

15333

SAMPLE OUTPUT

SOURCE CODE:

#include <stdio.h>

#define MAX_TAM 100

int main()

int Tcases,numberBoxes,i,K,temp,result,temp2;

scanf("%d",&Tcases);

int arr[MAX_TAM+1];

while (Tcases--)

result = 0;

for (i = 1; i <= MAX_TAM; ++i)

arr[i] = 0;

scanf("%d",&numberBoxes);

for (i = 1; i <= numberBoxes; ++i)

scanf("%d",&temp);

25
arr[temp]++;

scanf("%d",&K);

temp2 = K /2 ;

for (i = 1; i <= temp2; ++i)

temp = K - i;

if (temp<= MAX_TAM && temp>i)

result += (arr[i]*arr[temp]);

else if (temp == i)

result +=(arr[i]*(arr[i]-1))/2;

printf("%d\n",result);

return 0;}

RESULT:

Sample Test Cases Passed

Time (sec)

0.101832

26
Input

12343

15333

Your Code's Output

Expected Correct Output

Compilation Log

Compiled successfully.

Execution Log

No execution log!

27
7.MIRROR IMAGE

(BINARY TREE)

PROBLEM STAEMENT:

You are given a binary tree rooted at 1. You have to find the mirror image of any node qi about
node 1. If it doesn't exist then print -1.

28
Input:

First line of input is N and Q.

Next N-1 line consists of two integers and one character first of whose is parent node , second is
child node and character "L" representing Left child and "R" representing right child.

Next Q lines represents qi.

Output:

For each qi print it mirror node if it exists else print -1.

Constraints:

1 <= N <= 103

1<= Q <= 103

SAMPLE INPUT

10 8

12R

13L

24R

25L

36R

37L

58R

59L

7 10 R

29
1

10

SAMPLE OUTPUT

-1

-1

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdbool.h>

struct Node

int data;

char *path;

30
struct Node *lChild;

struct Node *rChild;

};

struct Node *createNode(int data)

struct Node *temp = malloc(sizeof(struct Node));

temp->lChild = temp->rChild = NULL;

temp->data = data;

return temp;

char *concatenate(char *a, char *b)

char *path = malloc(strlen(a) + strlen(b) + 1);

strcpy(path, a);

strcat(path, b);

return path;

int main()

int i;

int N, Q;

int index, child;

char state;

struct Node *head, *head1;

31
struct Node *ar[1001];

scanf("%d %d", &N, &Q);

head = createNode(1);

head->path = "";

ar[1] = head;

while(--N)

scanf("%d %d %c", &index, &child, &state);

struct Node *temp = ar[index];

switch(state)

case 'L':

temp->lChild = createNode(child);

ar[child] = temp->lChild;

temp->lChild->path = concatenate(temp->path, "L");

brake;

case 'R':

temp->rChild = createNode(child);

ar[child] = temp->rChild;

temp->rChild->path = concatenate(temp->path, "R");

break;

while(Q--)

32
{

scanf("%d", &child);

char *path = ar[child]->path;

struct Node *temp = head;

bool present = true;

for(i = 0; path[i] && temp; i++){

if(path[i] == 'R') {

temp = temp->lChild;

else {

temp = temp->rChild;

if(temp == NULL) {

printf("-1\n");

present = false;

break;

if(present)

printf("%d\n", temp->data);

inorder(head);

33
printf("\n");

return 0;

RESULT:

Sample Test Cases Passed

Time (sec)

0.107677

Input

10 8

12R

13L

24R

25L

36R

37L

58R

59L

7 10 R

34
10

Your Code's Output

-1

-1

Expected Correct Output

-1

-1

Compilation Log

Compiled successfully.

35
Execution Log

No execution log!

8.DISTINCT COUNT

36
(BINARY SEARCH TREE)

PROBLEM STATEMENT:

Given an array A of N integers, classify it as being Good Bad or Average. It is called Good, if it
contains exactly X distinct integers, Bad if it contains less than X distinct integers and Average if
it contains more than X distinct integers.

Input format:

First line consists of a single integer T denoting the number of test cases.

First line of each test case consists of two space separated integers denoting N and X.

Second line of each test case consists of N space separated integers denoting the array
elements.

Output format:

Print the required answer for each test case on a new line.

SAMPLE INPUT

41

1425

42

4215

43

5241

44

1245

SAMPLE OUTPUT

37
Average

Average

Average

Good

SOURCE CODE:

#include <stdio.h>

int partition(int arr[],int l,int h)

int pivot=arr[l];

int i=l+1;

int j=h;

while(i<=j)

while(i<=h&&arr[i]<pivot)

i++;

while(j>l&&arr[j]>pivot)

j--;

if(i<j)

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

i++;

38
j--;

else

i++;

arr[l]=arr[j];

arr[j]=pivot;

return j;

void sort(int arr[],int l,int h)

if(l>=h)

return;

int pivot=partition(arr,l,h);

sort(arr,l,pivot-1);

sort(arr,pivot+1,h);

int main()

int t;

scanf("%d",&t);

while(t>0)

int n,x;

39
scanf("%d",&n);

scanf("%d",&x);

int i,arr[n];

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

sort(arr,0,n-1);

int ans=1;

i=1;

while(i<n)

if(arr[i]!=arr[i-1])

ans++;

i++;

if(ans==x)

printf("Good\n");

else

if(ans<x)

printf("Bad\n");

else

printf("Average\n");

t--;

return 0;

40
}

RESULT:

Sample Test Cases Passed

Time (sec)

0.102599

Input

41

1425

42

4215

43

5241

44

1245

Your Code's Output

Average

Average

Average

Good

Expected Correct Output

41
Average

Average

Average

Good

Compilation Log

Compiled successfully.

Execution Log

No execution log!

9.HOSTEL VISIT

42
(HEAPS)

PROBLEM STATEMENT:

Dean of NIT Silchar is going to visit Hostels of NIT Silchar. As you know that he is a very busy
person so he decided to visit only first "K" nearest Hostels. Hostels are situated in 2D plane. You
are given the coordinates of hostels and you have to answer the Rocket distance of Kth nearest
hostel from origin ( Dean's place ) .

Input:

First line of input contains Q Total no. of queries and K

There are two types of queries:

first type : 1 x y

For query of 1st type, you came to know about the co-ordinates ( x , y ) of newly constructed
hostel.

second type : 2

For query of 2nd type, you have to output the Rocket distance of Kth nearest hostel till now.

The Dean will always stay at his place ( origin ).

It is gauranted that there will be atleast k queries of type 1 before first query of type 2.

Rocket distance between two points ( x2 , y2 ) and ( x1 , y1 ) is defined as (x2 - x1)2 + (y2 - y1)2

Output:

For each query of type 2 output the Rocket distance of Kth nearest hostel from Origin.

Constraints:

1 < = k < = Q < = 105

-106 < = x , y < = 106

SAMPLE INPUT

43
93

1 10 10

199

1 -8 -8

177

166

155

SAMPLE OUTPUT

200

162

98

SOURCE CODE:

#include <stdio.h>

void restore_down(long long int *, int, int);

void restore_up(long long int *, int);

void insert(long long int *, long long int, int *);

long long int extract(long long int *, int *);

void build_heap(int *);

int parent(int x){

44
return (x-1)/2;

int first_child(int x){

return 2*x-1;

void swap(long long int *a, long long int *b){

long long x = *a;

*a = *b;

*b = x;

int main()

int i,q,k,size=0;

scanf("%d%d",&q,&k);

long long arr[k];

while(q--){

int type;

long long x,y;

scanf("%d",&type);

if(type == 1){

scanf("%lld%lld",&x,&y);

long long dis = x*x + y*y;

if(size < k)insert(arr,dis,&size);

else{

45
long long max = extract(arr,&size);

if(dis < max)max = dis;

insert(arr,max,&size);

else {

long long ans = extract(arr, &size);

printf("%lld\n",ans);

insert(arr,ans,&size);

return 0;

void restore_down(long long arr[], int pos, int arr_size){

int max = first_child(pos);

if(max+1 < arr_size && arr[max] < arr[max+1])max = max+1;

if(max < arr_size && arr[max] > arr[pos]){

swap(&arr[pos], &arr[max]);

restore_down(arr, max, arr_size);

void restore_up(long long arr[], int pos){

if(pos == 0)return;

if(arr[pos] > arr[parent(pos)]){

46
swap(&arr[pos], &arr[parent(pos)]);

restore_up(arr,parent(pos));

void insert(long long arr[], long long x, int *size){

arr[*size] = x;

restore_up(arr, *size );

(*size)++;

long long int extract(long long arr[], int *size){

(*size)--;

long long int value = arr[0];

arr[0] = arr[*size];

restore_down(arr, 0, *size);

return value;

RESULT:

Sample Test Cases Passed

Time (sec)

0.102653

Input

93

1 10 10

47
199

1 -8 -8

177

166

155

Your Code's Output

200

162

98

Expected Correct Output

200

162

98

Compilation Log

Compiled successfully.

Execution Log

No execution log!

48
49
10.YOUR SOCIAL NETWORK

(GRAPH)

PROBLEM STATEMENT:

Influenced by Social Networking sites, Rahul launches his own site Friendsbook. Each user in
Friendsbook is given a unique number, first user being numbered 1. There are N users in
Friendsbook numbered from 1to N. In Friendsbook, i th user can make j th user his friend
without becoming his friend himself, i.e. in Friendsbook, there is a one-way link rather than a
two-way link as in Facebook. Moreover i th user can make j th user his friend iff i>j. Also one
user should have no more and no less than one friend except user 1 who will have no friend.
Rahul wants to modify Friendsbook and find out whether one user is somehow linked to some
other user. Help Rahul do so.

Input:

The first line of input takes the number of test cases, T. Then T test cases follow.Each test case
has 2 input lines.

The first line of each test case takes the number of users, N. The second line of each test case
consists of N - 1 space

separated integers. Here, i th integer denotes the friend of (i+1)th user.

Output:

Print all possible connections between the users in the following format:

4 2 2 means 4 is linked to 2 via 2 connections.

5 2 3 means 5 is linked to 2 via 3 connections, and so on.

The order of display should be as follows:

Print all possible connections starting from user 2 to user N with other users starting from 1 to
The Current User Number - 1.

In case one user is not connected at all with another user, that connection should not be
printed.

50
Constraints:

1<=T<=200

1<=N<=500

Example:

Input:

12

11

122

Output:

211312321

211311

211312321412421

SOURCE CODE:

int min(int a,int b){

return a>b?b:a;

int main(){

int t;

51
scanf("%d",&t);

while(t--){

int n;

scanf("%d",&n);

int A[n+1];

A[0]=A[1]=0;

for(int i=2;i<=n;i++){

scanf("%d",&A[i]);

int F[n+1][n+1];

for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++){

F[i][j]=9999;

for(int i=2;i<=n;i++){

F[i][A[i]]=1;

for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++){

for(int k=1;k<=n;k++){

F[i][j] = min(F[i][j],F[i][k]+F[k][j]);

52
}

for(int i=1;i<=n;i++){

for(int j=1;j<=n;j++){

if(F[i][j]!=9999)

printf("%d %d %d ",i,j,F[i][j]);

printf("\n");

return 0;

RESULT:

For Input:

12

11

Your Output is:

211312321

211311

53
54

Das könnte Ihnen auch gefallen