Professional Documents
Culture Documents
PSR Vijay
PSR Vijay
(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).
The first line of input will contain N, an integer, which is the total number of numbers shown to
your team.
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);
scanf("%d", &a);
A[a]++;
scanf("%d", &Q);
2
scanf("%d", &b);
if(A[b]==0){
printf("NOT PRESENT\n");
else
printf("%d\n", A[b]);
return 0;
RESULT:
Time (sec)
0.108613
Input
111220
3
Your Code's Output
NOT PRESENT
NOT PRESENT
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).
SAMPLE INPUT
SAMPLE OUTPUT
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
char s[7];
scanf("%s", s);
printf("output");
scanf("%d", &n);
if (n == 1)
else
6
printf(": %d", n);
while(scanf("%d", &n))
printf(".");
RESULT:
Time (sec)
0.113485
Input
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.
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:
Time (sec)
0.101661
Input
65
122345
56412
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 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() {
char bulb;
15
if (bulb == 'Y') arr[i] = 2;
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;
printf("%d\n", answer);
return 0;
RESULT:
Time (sec)
16
0.10213
Input
42
RYGY
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++) {
pop();
c++;
push(a[i]);
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
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 :
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.
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
SAMPLE INPUT
12343
24
5
15333
SAMPLE OUTPUT
SOURCE CODE:
#include <stdio.h>
int main()
int Tcases,numberBoxes,i,K,temp,result,temp2;
scanf("%d",&Tcases);
int arr[MAX_TAM+1];
while (Tcases--)
result = 0;
arr[i] = 0;
scanf("%d",&numberBoxes);
scanf("%d",&temp);
25
arr[temp]++;
scanf("%d",&K);
temp2 = K /2 ;
temp = K - i;
result += (arr[i]*arr[temp]);
else if (temp == i)
result +=(arr[i]*(arr[i]-1))/2;
printf("%d\n",result);
return 0;}
RESULT:
Time (sec)
0.101832
26
Input
12343
15333
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:
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.
Output:
Constraints:
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;
};
temp->data = data;
return temp;
strcpy(path, a);
strcat(path, b);
return path;
int main()
int i;
int N, Q;
char state;
31
struct Node *ar[1001];
head = createNode(1);
head->path = "";
ar[1] = head;
while(--N)
switch(state)
case 'L':
temp->lChild = createNode(child);
ar[child] = temp->lChild;
brake;
case 'R':
temp->rChild = createNode(child);
ar[child] = temp->rChild;
break;
while(Q--)
32
{
scanf("%d", &child);
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:
Time (sec)
0.107677
Input
10 8
12R
13L
24R
25L
36R
37L
58R
59L
7 10 R
34
10
-1
-1
-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 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;
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:
Time (sec)
0.102599
Input
41
1425
42
4215
43
5241
44
1245
Average
Average
Average
Good
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 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.
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:
SAMPLE INPUT
43
93
1 10 10
199
1 -8 -8
177
166
155
SAMPLE OUTPUT
200
162
98
SOURCE CODE:
#include <stdio.h>
44
return (x-1)/2;
return 2*x-1;
*a = *b;
*b = x;
int main()
int i,q,k,size=0;
scanf("%d%d",&q,&k);
while(q--){
int type;
scanf("%d",&type);
if(type == 1){
scanf("%lld%lld",&x,&y);
else{
45
long long max = extract(arr,&size);
insert(arr,max,&size);
else {
printf("%lld\n",ans);
insert(arr,ans,&size);
return 0;
swap(&arr[pos], &arr[max]);
if(pos == 0)return;
46
swap(&arr[pos], &arr[parent(pos)]);
restore_up(arr,parent(pos));
arr[*size] = x;
restore_up(arr, *size );
(*size)++;
(*size)--;
arr[0] = arr[*size];
restore_down(arr, 0, *size);
return value;
RESULT:
Time (sec)
0.102653
Input
93
1 10 10
47
199
1 -8 -8
177
166
155
200
162
98
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
Output:
Print all possible connections between the users in the following format:
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:
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
211312321
211311
53
54