Quizzes and Assignments PDF
Quizzes and Assignments PDF
Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal
to the square of the third.
For example, 3, 5 and 4 form a Pythagorean triple, since 3*3 + 4*4 = 25 = 5*5
You are given three integers, a, b, and c. They need not be given in increasing order. If
they form a Pythagorean triple, then print "yes", otherwise, print "no". Please note that
the output message is in small letters.
Sample Input
3
5
4
Sample Output
yes
Test Case 2 5 no
8
2
Test Case 7 5 no
8
12
Sums of Powers of Numbers
Due on 2014-09-16, 23:59 IST
Level: Easy
In this program, you are given an input N, which is a positive integer less than or equal
to 40. Write a program to find the sums of fourth powers of the first N numbers.
Sample Input
2
Sample Output
17
Sample Test Cases
Input Output
Test Case 1 2 17
Test Case 2 1 1
You are given a positive integer N. You have to print N rows as follows. The first row
consists of one 0, the second row 2 zeroes, and so on, until the Nth row, which consists
of N zeroes.
Sample Test Cases
Input Output
Test Case 1 2 0
00
Test Case 2 6 0
00
000
0000
00000
000000
Test Case 3 22 0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000
00000000000
000000000000
0000000000000
00000000000000
000000000000000
0000000000000000
00000000000000000
000000000000000000
0000000000000000000
00000000000000000000
000000000000000000000
0000000000000000000000
Test Case 4 40 0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000
00000000000
000000000000
0000000000000
00000000000000
000000000000000
0000000000000000
00000000000000000
000000000000000000
0000000000000000000
00000000000000000000
000000000000000000000
0000000000000000000000
00000000000000000000000
000000000000000000000000
0000000000000000000000000
00000000000000000000000000
000000000000000000000000000
0000000000000000000000000000
00000000000000000000000000000
000000000000000000000000000000
0000000000000000000000000000000
00000000000000000000000000000000
000000000000000000000000000000000
0000000000000000000000000000000000
00000000000000000000000000000000000
000000000000000000000000000000000000
0000000000000000000000000000000000000
00000000000000000000000000000000000000
000000000000000000000000000000000000000
0000000000000000000000000000000000000000
Triangular Matrix
Due on 2014-09-16, 23:59 IST
Level:Medium
In this assignment, you will be given an NxN matrix. You have to determine whether the
matrix is a triangular matrix.
The diagonal of the matrix M of size NxN is the set of entries M(0,0), M(1,1), M(2,2), ...,
M(N,N).
A matrix is upper triangular if every entry below the diagonal is 0. For example,
111
001
002
is an upper triangular matrix. (The diagonal itself, and the entries above and below the
diagonals can be zeroes or non-zero integers.)
A matrix is lower triangular if every entry above the diagonal is 0. For example,
200
310
422
is a lower triangular matrix.
Input
First, you will be given N, which is the size of the matrix.
Then you will be given N rows of integers, where each row consists of N integers
separated by spaces.
Output
If the input matrix is triangular, then print yes. Otherwise, print no.
Test Case 3 3 no
1 0 1
0 1 0
1 1 2
Test Case 6 7 no
1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 1 1
You are given a sequence of integers as input, terminated by a -1. (That is, the input integers
may be positive, negative or 0. A -1 in the input signals the end of the input.)
-1 is not considered as part of the input.
Find the second largest number in the input. You may not use arrays.
Sample Test Cases
Input Output
Test -840 -288 -261 -337 -335 488 -1 -261
Case 1
Test -840 -335 -1 -840
Case 2
Test 471 -288 -366 488 -335 -420 -840 465 -337 413 -261 458 -1 471
Case 3
Test 593 -588 -93 -288 512 -513 168 -146 -366 -335 180 -49 -266 -840 129 512
Case 4 423 458 64 488 32 475 -295 413 193 471 -586 -337 133 -488 394 -420
465 -128 497 -261 55 -615 -40 -888 -507 -1
Test -146 -288 -266 133 -840 423 -93 64 -335 -513 -366 471 413 488 -420 512
Case 5 497 475 -507 129 -615 465 -295 55 -261 -128 593 180 -40 512 458 -
488 193 168 -586 -49 -337 32 -888 -588 -1
Quiz
Due on 2014-09-26, 23:59 IST
The due date for submitting this assignment has passed.
State whether the following expression is valid. If the expression is valid, evaluate the value of the
expression; if the expression is not valid state the reason.
(3>5-2)
Expression is Valid Expression evaluates to 1
Expression is Valid Expression evaluates to 0
Expression is Valid Expression evaluates to -2
Expression is Valid Expression evaluates to -1
Expression is In-Valid Operation is not clearly defined
1 point
State whether the following expressions is valid. If the expression is valid, evaluate the value of the
expression; if the expression is not valid state the reason. Assume i=3 , j=2
Tick all that apply
((i>j)&&(i=j))
Expression is Valid Expression evaluates to 0
Expression is In-Valid Expression gives a compiler-error
Expression is Valid Expression evaluates to 1
Expression is In-Valid Single = can't be used in the expression
1 point
State whether the following expression is valid. If the expression is valid, evaluate the value of the
expression; if the expression is not valid state the reason.
Tick all that apply
(++3*5)
Expression is valid Expression evaluates to 16
Expression is valid Expression evaluates to 20
Expression is valid Expression evaluates to 25
Expression is valid Expression evaluates to 17
Expression is invalid Increment operator requires a variable to perform its operation
1 point
State whether the following expressions is valid.If the expression is valid, evaluate the value of the
expression; if the expression is not valid state the reason. Assume i=3 , j=2
Tick all that apply
(! i | | j)
Expression is valid Expression evaluates to 1
Expression is valid Expression evaluates to 0
Expression is in-valid ! is wrongly used
Expression is in-valid Expression evaluation is compiler-dependent
1 point
State whether the following expression is valid. If the expression is valid, evaluate the value of the
expression; if the expression is not valid state the reason. Assume i=3 , j=2
Tick all that apply
( i=2,-j)
Expression is valid Expression evaluates to 1
Expression is valid Expression evaluates to 2
Expression is valid Expression evaluates to -2
Expression is valid Expression evaluates to 0
Expression is in-valid Expression evaluation is compiler dependent
Expression is in-valid Assignment operation evaluates to nothing
2 points
This code takes a number num as input, and outputs the sum of the odd numbers in the range 0,1,...,num,
including num if it is odd. Please complete the code.
int main()
{
int i = 1;
int num; /*number of elements */
int oddsum=0; /* sum of odd numbers */
scanf("%d",&num);
while(i<=num)
{ oddsum=oddsum+i;
i = ____1___
}
printf("%d\n",oddsum);
return 0;
}
1 point
Write down the the output of the code when the input is
10 30
2 points
Question : AlphaNumeric
Complete the following function that returns 1 if the input is alphanumeric else it returns 0 to the calling
function
int isalphanum(char c)
{
int result;
if (c>= ____1_____ && c <= 'z')
return 1;
if (c>=___2_____ && c<= 'Z')
return 1;
if (c>=___3____ && c<=____4____)
return ___5____;
return ____6____;
}
1 point
1 point
1 point
Response to Alphanumeric (Question 8) Blank 4
1 point
1 point
1 point
Below is a program that checks whether a given number is an Armstrong number or not.
An Armstrong number is the one in which the sum of cubes of its digit is equal to the number itself.
The program takes a number as input and output whether the given number is an Armstrong number or
not.
For example:
(a) 371 is an Armstrong Number as 27+343+1=371
(b) 153 is an Armstrong Number (1+125+27=153)
(c) 42 is not an Armstrong Number (64+8=72)
Complete the program by filling in the blanks. Please note that the syntax has to be fully correct including
semicolons (the ; symbol). [There are EIGHT missing components.]
int main()
{
int number, total;
int finalDigit;
printf("Enter the number: ");
scanf("%d",___1____);
int n = number;
total =___2___
while( n ___3___ 0)
{
finalDigit = ___4___
total = total + __5____;
n = ___6___
}
printf("The total is __7__\n",total);
if(total _____8____ number)
printf("%d is a Armstrong number \n",number);
else
printf("%d is not a Armstrong number \n",number);
return 0;
}
Response to Armstrong Number (Question 14) Blank 1
1 point
1 point
1 point
1 point
1 point
1 point
1 point
Write a function called divide that takes two non-negative integers : a and b and
returns the quotient of a divided by b, if b is a factor of a, else it returns -1.
Note: In this assignment the main() function is given to you. The given code for main()
cannot be changed by you and divide() is being called from inside that. You only need
to write the divide function.
Also note that the #include line is also provided so you do not need to add it while
writing the divide() function.
Sample Test Cases
Input Output
Test Case 1 4 2
2
Test Case 2 2 -1
4
Test Case 3 2 -1
3
Test Case 4 4 2
2
Test Case 5 4 -1
0
Test Case 6 0 0
6
Write a C program that calculates the number of ways to choose k objects from n
distinct objects. 'k' and 'n' both are integers.
Input Format:
First line contains the value of n, where 0<=n<=10
Second line contains the value of k, where k>=0
Output Format:
One line containing the number of ways to chose the objects
Note: In this question you are not given main() so you have to write the complete
program.
Note: In the below given Sample Test Cases, the second test case is incorrect.
The course staff is fixing it but the expected answer will be 2 and not 1. If you
think your code is correct, try submitting it and you should get 100% score.
Test Case 2 2 2
1
Test Case 3 5 10
2
Test Case 4 7 1
0
Test Case 5 4 0
100000
Test Case 7 10 1
10
You are given a sequence of n-1 distinct positive integers, all of which are less than or
equal to a integer n. You have to find the integer that is missing from the range
[1,2,...,n]. Solve the question without using arrays.
Input Format:
One line containing the integer n where 2<=n<=10,000
First line is followed by a sequence of n-1 distinct positive integers. Note that the
sequence may not be in any particular order.
Output Format:
One line containing the missing number
Test 2 1
Case 2
7
LCM of n numbers
Due on 2014-09-28, 23:59 IST
Level: Medium
Write a C program that calculates the least common multiple (LCM) of 'n' numbers.
Input Format:
First line contains the number of numbers that are input 'n', where n>1
Second line contains 'n' positive integers whose LCM is to be calculated
Output Format:
One line containing the LCM of the 'n' numbers
Sample Test Cases
Input Output
Test Case 1 4 924
3 4 7 11
Test Case 2 3 60
4 6 10
Test Case 3 4 30
3 2 10 15
Test Case 4 3 10
1 10 1
Test Case 5 1 4
4
Write a C program that given an integer n, prints the number of integers that are less
than or equal to n and co-prime to n
Two integers a and b are said to be relatively prime or co-prime if the only positive
integer that evenly divides both of them is 1. That is, the only common positive factor of
the two numbers is 1. This is equivalent to their greatest common divisor being 1.
Input Format:
One line containing the value of 'n' , where 1<=n<=10,000
Output Format:
One line containing the number of integers that are co-prime to n and less than or equal
to 'n'
Sample Test Cases
Input Output
Test Case 1 10 4
Test Case 2 23 22
Test Case 3 11 10
Test Case 4 30 8
Test Case 5 1 1
Input Format:
First line is the value of integer n: 1 <= n <= 1,000,000
Second line is a space separated list of integers 1 2 ... n permuted in some random
order
Output Format: Output a single line containing a space separated list of integers which
is the lexicographically preceding permutation of the input permutation.
Sample Test Cases
Input Output
Test Case 1 3 1 2 3
1 3 2
Test Case 2 4 2 4 3 1
3 1 2 4
Test Case 3 7 1 2 3 4 5 6 7
1 2 3 4 5 6 7
Test Case 4 5 5 1 2 3 4
5 1 2 4 3
Test Case 5 12 2 12 11 10 9 8 7 6 5 4 3 1
3 1 2 4 5 6 7 8 9 10 11 12
Given a source string S and a pattern string P, count the number of times the pattern
string P occurs in the source string S.
Note: Overlapping sequences are counted as separate occurrences.
Input Format:
First line is the source string S s.t. 1 <= |S| <= 8192 characters
Second line is the pattern string P s.t. 1 <= |P| <= 8192 characters
Output Format:
Output a single integer containing the number of occurrences of pattern string P in
source string S.
Sample Test Cases
Input Output
Test Case 1 mississippi 2
issi
Test Case 4 a 0
banana
Print Subarray
Due on 2014-10-19, 23:59 IST
Level: Easy
Given an input character array of A and start index S and end index E, write a function
that prints the sub array starting from S (including S) and ending at index E (including
E). The character array may contain spaces and tabs.
Note: You are given the main function. Just write the subroutine 'void
printSubarray(char *a, int start, int end)'.
Input Format:
First line is the input array A s.t. 1 <= |A| <= 8192
Second line is the start index S s.t. 0 <= S <= |A|-1
Third line is the ending index E s.t. 0 <= E <= |A|-1
Output Format:
A single line containing the sub array of A from start index S to end index E.
Sample Test Cases
Input Output
Test It was a nice, sunny day. t wa
Case 1
4
1
Test boneywasawarrior boneywasawarr
Case 0
12
2
Test Sankore Madrasah, The University of Sankore, or Sankore Madrasah, The
Case Sankore Masjid is one of three ancient centers of University of Sankore,
learning located in Timbuktu, Mali, West Africa. The or Sankore Masjid is
3 three mosques of Sankore, Djinguereber Mosque and Sidi one of three ancient
Yahya compose the famous University of Timbuktu. centers of learning
Madrasah means school/university in Arabic and also in
other languages associated with Muslim people.
0
105
Given an input string S, center justify the string by using the character '_' to align the
string. There will be neither preceding spaces before the string S nor suffix spaces after
the string S. The output should be center-justified in a line of width 64 characters,
followed by a newline.
Note:
1. If S has an odd number of characters then the number of preceding '_' should be one
more than the number of trailing '_'
2. If S has an even number of characters then the number of preceding '_' should be
equal to the number of trailing '_'
Input Format:
First line is the input string S s.t. 1 <= |S| <= 64
Output Format:
Output the center justified string S followed by a newline.
Sample Test Cases
Input Output
Te I love programming _______________________I love
st programming_______________________
Ca
se
1
Te Pneumonoultramicroscopicsilicovolcanoco __________Pneumonoultramicroscopicsilic
st niosis ovolcanoconiosis_________
Ca
se
2
Te 123456789012345678901234567890123456789 123456789012345678901234567890123456789
st 0123456789012345678901234 0123456789012345678901234
Ca
se
3
Te 1 ________________________________1______
st _________________________
Ca
se
4
Te This is too easy ________________________This is too
st easy________________________
Ca
se
5
Te P=NP ? _____________________________P=NP
st ?_____________________________
Ca
se
6
Te Always _____________________________Always____
st _________________________
Ca
se
7
There are 'n' ants on a 'n+1' length rod. The ants are numbered from 1 to n and are
initially placed at positions starting from position 1 till position n. They are moving either
in left direction (denoted by '-1') or in the right direction (denoted by '1'). Whenever an
ant crosses the boundary of the rod it falls off the rod. You are given the initial direction
of the ants. Now, whenever two ants collide their direction switches, i.e. the ant going in
left direction ('-1) changes it's direction towards right ('1') and the ant going in the right
direction ('1') changes it's direction towards left ('-1'). Find last ant to fall off the rod.
Note: In case two ants are falling simultaneously in the end print the index of the lower
indexed ant.
Input Format:
First line contains the integer 'n' denoting the total number of ants s.t. 1 <= n <= 1,000
Second line contains 'n' space separated numbers (either '1' or '-1') denoting the initial
directions of the ants.
Output Format:
Output a single integer which is the index (lower index in case two ants are falling
simultaneously in the end) of the last ant to fall off the table.
Sample Test Cases
Input Output
Test 2 1
Case 1 1
1
Test 3 2
Case 1 -1 -1
2
Test 2 2
Case -1 -1
3
Test 8 3
Case 1 1 -1 1 1 1 -1 1
4
Test 10 5
Case 1 -1 1 1 -1 1 -1 1 -1 -1
5
Test 100 52
Case -1 -1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 1 -1 1 -1
1 -1 1 1 1 -1 -1 1 -1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 1 1 1 1 -1 1 1 -1 -1
6 -1 -1 -1 1 -1 -1 -1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1
-1 -1 1 1 1 1 -1 1 1 -1 -1 -1 -1 1 1 -1
You are given a collection of words, say as in a dictionary. You can represent it in the
following compressed form: the first word will be followed by a sequence of a pair of
number and a word. The number in the pair is the position till which the previous words'
characters are included in the new word and the tail is the remaining trailing word which
is the different than the previous word.
Example:
Suppose successive words in our dictionary are:
color
comma
commatose
dot
Input Format:
First line contains the integer 'n' denoting the number of words in the dictionary s.t. 1 <=
n <= 1,000
Second line would contain the first word.
It will be followed by 'n-1' lines each containing an integer and a trailing string.
Note: The input is designed such that the integer will always be <= size of previous
word formed
Example Input:
4
zebra
3u
2 nith
1 ggurat
Output Format:
Output a single string that is the last resulting word of the given dictionary
Example Output:
zggurat
Explanation:
The dictionary actually is:
zebra
zebu (3 first characters are common with zebra)
zenith (2 first characters are common with zebu)
zggurat (1 first character is common with zenith)
Sample Test Cases
Input Output
Test Case 1 4 ziggurat
zebra
3 u
2 nith
1 iggurat
#include <stdio.h>
int main()
{
int a[]={1,2,3};
foo(a);
printf("%d",a[0]);
return 0;
}
11
101
1010
110
1 point
What is the output of the following program?
#include<stdio.h>
int main()
{
int size, i;
size=5;
int arr[size]={1,2,3,4,5};
for(i=1; i<=size;i++)
{
printf("%d", arr[i]);
}
return 0;
}
12345
2345
23456
None of These
1 point
#include<stdio.h>
void fun(int * a)
{
int ** x=&a;
printf("%u\n",x);
return ;
}
int main(){
int a[5]={1,2,3,4,5};
int * b = a;
int ** y=&b;
printf("%u\n",y);
fun(b);
return 0;
}
Both the printed values are necessarily the same
Both the printed values are necessarily different
Both the printed values may be same
None of these
1 point
11
20
44
80
1 point
#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
2 2 15
3 2 20
3 1 20
3 2 15
1 point
Which of the following are correct ways to initialize an array? (There may be multiple answers.)
int num[]={1,2,3,4};
int num{}={1,2,3,4};
int num[4]={1,2,3,4};
int num()={1,2,3,4};
1 point
#include<stdio.h>
int main(){
int a = 330;
char *p;
p =( char *)&a;
printf("%d ",*p);
return 0;
}
330
74
54
331
1 point
Collatz function
Due on 2014-11-03, 23:59 IST
Difficulty: Easy
We consider the repeated application of the Collatz function starting with a given integer n, as
follows:
f(n), f(f(n)), f(f(f(n))),
It is conjectured that no matter which positive integer n you start from, this sequence
eventually will have 1 in it. It has been verified to hold for numbers up to 5 260 [Wikipedia:
Collatz Conjecture].
In this question, you will be given a positive number <= 32,000. You have to output how many
times f has to be applied repeatedly in order to first reach 1.
Sample Test Cases
Input Output
Test Case 1 1 0
Test Case 2 8 3
Test Case 3 7 16
You are given 'k' eggs and a 'n' storey building. The eggs MIGHT break if thrown down
from a specific height (Note: It is NOT necessary that the eggs have to break; they
might not even break from the topmost floor). What is the minimum number of steps in
which you can find (using 'k' eggs) the minimum height of the floor in the building from
which the eggs will start breaking ?
Note: You have to output the minimum number of steps required; NOT the floor of the
building from which eggs will break;
Input Format:
First line of the input is an integer 'q':
1 <= q <= 1,000, which is the number of queries.
Second line of the input has two space separated integers: the height of the building 'n'
and the number of eggs which you can use 'k':
1 <= n <= 1,000
1 <= k <= 10
Output Format:
For each q output a single line denoting the minimum number of trials required to find
the height from which the eggs will start breaking.
Example:
For n = 151 and k = 1 the minimum number of steps in which we can find the height
from which eggs can break(considering the worst case) is 151. This is because since
we have only 1 egg we have no choice but to start from the first floor and throw the egg
from each floor and check whether it breaks or not. In worst case this might require 151
steps.
For n = 100 and k = 2 the minimum number of steps in which we can find the height
from which eggs can break(considering again the worst case) is 14. This is because
suppose we throw the FIRST egg from 14th floor and it breaks then we will have to try
each of the remaining 13 floors using the remaining egg. So in this case number of
trials required is 14. Suppose the first egg doesn't break then we can drop it from 27th
floor (13 + 14). We have chosen 27th floor because suppose if the first egg breaks from
27th floor then we will have to test floors from 15-26 (=12). So, the total number of trials
required in this case is: 12 + 1(from 14th floor) + 1(from 27th floor) = 14 trials. Suppose
the first egg doesn't break even now, drop it from 39(12 + 13 + 14) floor for same
reason.
Sample Test Cases
Input Output
Test Case 1 4 10
10 1 4
10 2 14
100 2 9
104 3
Test Case 2 3 10
923 8 5
23 9 14
101 2
Test Case 3 5 6
42 8 35
35 1 7
70 5 7
79 9 7
63 5
Test Case 4 10 6
42 8 335
335 1 8
170 5 9
479 9 11
963 5 10
706 6 9
282 8 44
962 2 19
996 3 10
828 7
Test Case 5 20 6
42 8 335
335 1 8
170 5 9
479 9 11
963 5 10
706 6 9
282 8 44
962 2 19
996 3 10
828 7 10
392 5 13
903 4 12
293 3 9
422 7 10
719 6 9
448 7 10
772 9 18
870 3 10
668 10 6
36 5
MinInsertPalindorme
Due on 2014-11-03, 23:59 IST
MinInsert-Palindrome Problem
Difficulty : Medium
You are given a string of characters, or numbers. Find the minimum number of
characters to be inserted into the string in order to obtain a palindrome.
A palindrome is a word, phrase, number, or other sequence of symbols or elements
that reads the same forward or reversed.
For example, the string abcbd can be transformed into a palindrome ("dabcbad" or
"adbcbda"). However, inserting fewer than 2 characters will not produce a palindrome.
Input Format:
First line contains an integer 'n' specifying the length of the string, where 3<=n<=20
Second line contains a string of length n.
Output Format
One line containing the minimum number of insertions required to make the string a
palindrome
Sample Test Cases
Input Output
Test Case 1 5 0
nitin
Test Case 2 7 1
aabbaab
Test Case 3 20 17
WbHRKhSxoS18CnGxola3
Test Case 4 20 0
eeeeeeeeeeeeeeeeeeee
Test Case 5 20 17
0ziG30WBD79ow1E0fu1X
Test Case 6 20 15
OGJ2gg5J5VXLRc4fye7g
Test Case 7 20 15
Yka081R3gN0O5XK00xN2
Coin Counting
Due on 2014-11-03, 23:59 IST
Curious Coin-Counting Problem
Difficulty : Medium
Given an amount A, we want you to compute the number of ways in which you
can gather A rupees if you have an infinite supply of each of C = {1, 3, 5} valued rupee
coins.
Input:
First line contains T, the number of test-cases. This is followed by T lines, where each
line consists of the amount A.
Output:
For each test case, print the number of ways in which A can be formed using an infinite
supply of 1, 3 and 5 rupee coins.
Sample Input:
2
5
10
Sample Output:
3
7
Constraints
T < 100
A < 101
Test Case 4 1 1
0
PA5_Student_Database
Due on 2014-11-17, 23:59 IST
Create a database of students using structures, where in each entry of the database
will have the following fields:
1. if a student 'A' has lower marks in physics than a student 'B', then A's data is listed
before B.
2. If A and B have the same physics marks and A has lower chemistry marks than B,
then A is listed before B.
3. If A and B have the same marks in physics and chemistry, and A has lower marks in
mathematics than B, then A is listed before B.
4. If all marks are equal and A's name precedes B's name in the dictionary order, then
A is listed before B.
.
Input Format :
First line contains the number of students n, where 1<=n<=100.
In following n lines each line contains(space separated) a name and their respective
marks in physics, chemistry, maths, where 0<=marks<=100.
Output Format :
Sorted database of n lines.
PA5_Matrix_Exponentiation
Due on 2014-11-17, 23:59 IST
Level: Difficult
In this problem, you will be given a square matrix M and a ppsitive integer power N.
You will have to compute M raised to the power N. (that is, M multiplied with itself N
times.)
Input
First line of input if T ( number of test-cases) First line of each test-case contains two
integer M , N where M is size of square array that we have to exponent and N is the
power to which we have to exponent
Next M lines describe the input matrix. Each line contains exactly M elements
corresponding to each array
Limits
1<=T<=10
1<=M<=50
1<=N<=100000
Output
Output M line corresponding to each row of resultant matrix Each line must have M
integers where jth element of ith line is jth element of resultant matrix taken modulo with
1000000007 (10^9+7)
Test 4 8 0
Case 2 3 28 64
2 0 27 9
2 1 4 0 0
2 3 64 102 54
3 1 136 171 98
0 0 108 147 82
3 3 13 12
0 3 1 12 13
4 3 2
2 3 2
2 2
2 3
3 2
PA5_MazeProblem
Due on 2014-11-17, 23:59 IST
Maze Problem
You are provided a matrix of size N*N with source position at (0,0) and
destination at (N-1,N-1) in a 2D array. Some of the positions in the array are marked as
0 which are blocked cells, rest being marked 1.
Note that cells at (0,0) and (N-1,N-1) are always 1. You can either make
movement towards right or down, i.e., from position (x,y), you can go to either the
position (x,y+1) or (x+1,y).
Input
First line consists of the size of the input array N (<=50), following that would
be the state of NxN maze which would consist of 0s and 1s.
Output
You have to print "POSSIBLE" if there exists a path between the source and the
destination otherwise print "NOT POSSIBLE".
Sample Test Cases
Input Output
Test Case 1 4 POSSIBLE
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Adjacency List
Due on 2014-11-17, 23:59 IST
A graph is abstractly a collection of vertices which are labelled by
non-negative integers, and a collection of edges. A graph called an
undirected graph if we talk of merely the presence of an edge between
vertices i and j, rather than its direction.
Input
You have to output the list of nodes to which n has an edge, in the
order in which the edges were input, one line for each vertex.
Test Case 2 10 2
1 2 3
1 3 4
1 4 5
2 3 6
2 4
5 1
5 2
5 3
5 4
6 1
1
Test Case 3 13 1
0 4 4
0 7 10
0 10
1 8
1 9
3 6
3 10
4 8
4 10
5 9
6 9
7 10
8 10
8
Dec 14 Question 1
Due on 2014-12-23, 05:30 IST
A directed graph is a graph where the edges have a direction - The edge (i,j) represents an edge from i to j only. To
specify an edge in the backward direction, there must be an edge of the form (j,i). If this is absent, then there is an
edge only in the first direction.
You will be given a directed graph as a sequence of edges. You have to see whether for every vertex, whether the
number of incoming edges is the same as the number of outgoing edges.
If every vertex has equal number of incoming and outgoing edges, then print yes, otherwise print the smallest vertex
index which has unequal number of incoming and outgoing edges.
Input
Output
If every vertex has equal number of incoming and outgoing edges, then print yes. Otherwise, print the smallest
vertex which has unequal number of incoming and outgoing edges.
Sample Input 1
401021220
Sample Output 1
<i<j<="" body=""></i
Sample Test Cases
Input Output
Test 142 0 1 0 2 0 4 0 5 0 6 0 8 0 9 0 10 0 11 0 12 1 2 1 3 1 4 1 5 1 6 1 7 1
Case 1 9 1 10 1 12 2 0 2 1 2 3 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 3 1 3 4 3
5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 4 0 4 1 4 2 4 3 4 5 4 6 4 7 4 8 4 9 4
1 10 4 11 4 12 5 0 5 1 5 2 5 3 5 4 5 8 5 9 5 10 5 11 5 12 6 0 6 1 6 2 6
3 6 4 6 5 6 7 6 8 6 9 6 10 6 11 6 12 7 0 7 1 7 2 7 3 7 5 7 6 7 8 7 9 7
10 7 11 7 12 8 0 8 1 8 2 8 3 8 4 8 6 8 7 8 10 8 11 8 12 9 0 9 1 9 2 9
3 9 4 9 5 9 6 9 7 9 8 9 10 9 11 9 12 10 0 10 1 10 2 10 3 10 4 10 5 10
6 10 7 10 9 10 11 10 12 11 0 11 1 11 2 11 3 11 4 11 5 11 6 11 7 11 8
11 9 11 10 11 12 12 0 12 1 12 2 12 3 12 4 12 5 12 6 12 7 12 8 12 9 12
10 12 11
Test 868 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 27
Case 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29
1 0 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16
3 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 2 0 2
1 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17
2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 2 26 2 27 2 28 2 29 3 0 3 1 3
2 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18
3 19 3 20 3 21 3 22 3 23 3 24 3 25 3 26 3 27 3 28 3 29 4 0 4 1 4 2 4 3
4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19
4 20 4 21 4 22 4 23 4 24 4 25 4 26 4 27 4 28 4 29 5 0 5 1 5 2 5 3 5 4
5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20
5 21 5 22 5 23 5 24 5 25 5 26 5 27 5 28 5 29 6 0 6 1 6 2 6 3 6 4 6 5 6
7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21
6 22 6 23 6 24 6 25 6 26 6 27 6 28 6 29 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7
8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22
7 23 7 24 7 25 7 26 7 27 7 28 7 29 8 0 8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 9
8 10 8 11 8 12 8 13 8 14 8 15 8 16 8 17 8 18 8 19 8 20 8 21 8 22 8 23
8 24 8 25 8 26 8 27 8 28 8 29 9 0 9 1 9 2 9 3 9 4 9 5 9 6 9 7 9 8 9 10
9 11 9 12 9 13 9 14 9 15 9 16 9 17 9 18 9 19 9 20 9 21 9 22 9 23 9 24
9 25 9 26 9 27 9 28 9 29 10 0 10 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8
10 9 10 11 10 12 10 13 10 14 10 15 10 16 10 17 10 18 10 19 10 20 10 21
10 22 10 23 10 24 10 25 10 26 10 27 10 28 10 29 11 0 11 1 11 2 11 3 11
4 11 5 11 6 11 7 11 8 11 9 11 10 11 12 11 13 11 14 11 15 11 16 11 17
11 18 11 19 11 20 11 21 11 22 11 23 11 24 11 25 11 26 11 27 11 28 11
29 12 0 12 1 12 2 12 3 12 4 12 5 12 6 12 7 12 8 12 9 12 10 12 11 12 13
12 14 12 15 12 16 12 17 12 18 12 19 12 20 12 21 12 22 12 23 12 24 12
25 12 26 12 27 12 28 12 29 13 0 13 1 13 2 13 3 13 4 13 5 13 6 13 7 13
8 13 9 13 10 13 11 13 12 13 14 13 15 13 16 13 17 13 18 13 19 13 20 13
21 13 22 13 23 13 24 13 25 13 26 13 27 13 28 13 29 14 0 14 1 14 2 14 3
14 4 14 5 14 6 14 7 14 8 14 9 14 10 14 11 14 12 14 13 14 15 14 16 14
17 14 18 14 19 14 20 14 21 14 22 14 23 14 24 14 25 14 26 14 27 14 28
14 29 15 0 15 1 15 2 15 3 15 4 15 5 15 6 15 7 15 8 15 9 15 10 15 11 15
12 15 13 15 14 15 16 15 17 15 18 15 19 15 20 15 21 15 22 15 23 15 24
15 25 15 26 15 27 15 28 15 29 16 0 16 1 16 2 16 3 16 4 16 5 16 6 16 7
16 8 16 9 16 10 16 11 16 12 16 13 16 14 16 15 16 17 16 18 16 19 16 20
16 21 16 22 16 23 16 24 16 25 16 26 16 27 16 28 16 29 17 0 17 1 17 2
17 3 17 4 17 5 17 6 17 7 17 8 17 9 17 10 17 11 17 12 17 13 17 14 17 15
17 16 17 18 17 19 17 20 17 21 17 22 17 23 17 24 17 25 17 26 17 27 17
28 17 29 18 0 18 1 18 2 18 3 18 4 18 5 18 6 18 7 18 8 18 9 18 10 18 11
18 12 18 13 18 14 18 15 18 16 18 17 18 19 18 20 18 21 18 22 18 23 18
24 18 25 18 26 18 27 18 28 18 29 19 0 19 1 19 2 19 3 19 4 19 5 19 6 19
7 19 8 19 9 19 10 19 11 19 12 19 13 19 14 19 15 19 16 19 17 19 18 19
20 19 21 19 22 19 23 19 24 19 25 19 26 19 27 19 28 19 29 20 0 20 1 20
2 20 3 20 4 20 5 20 6 20 7 20 8 20 9 20 10 20 11 20 12 20 13 20 14 20
15 20 16 20 17 20 18 20 19 20 21 20 22 20 23 20 24 20 25 20 26 20 27
20 28 20 29 21 0 21 1 21 2 21 3 21 4 21 5 21 6 21 7 21 8 21 9 21 10 21
11 21 12 21 13 21 14 21 15 21 16 21 17 21 18 21 19 21 20 21 22 21 23
21 24 21 25 21 26 21 27 21 28 21 29 22 0 22 1 22 2 22 3 22 4 22 5 22 6
22 7 22 8 22 9 22 10 22 11 22 12 22 13 22 14 22 15 22 16 22 17 22 18
22 19 22 20 22 21 22 23 22 24 22 25 22 26 22 27 22 28 22 29 23 0 23 1
23 2 23 3 23 4 23 5 23 6 23 7 23 8 23 9 23 10 23 11 23 12 23 13 23 14
23 15 23 16 23 17 23 18 23 19 23 20 23 21 23 22 23 24 23 25 23 26 23
27 23 28 23 29 24 0 24 1 24 2 24 3 24 4 24 5 24 6 24 7 24 8 24 9 24 10
24 11 24 12 24 13 24 14 24 15 24 16 24 17 24 18 24 19 24 20 24 21 24
22 24 23 24 25 24 26 24 27 24 28 24 29 25 0 25 1 25 2 25 3 25 4 25 5
25 6 25 7 25 8 25 9 25 10 25 11 25 12 25 13 25 14 25 15 25 16 25 17 25
18 25 19 25 20 25 21 25 22 25 23 25 24 25 26 25 27 25 28 25 29 26 0 26
1 26 2 26 3 26 4 26 5 26 6 26 7 26 8 26 9 26 10 26 11 26 12 26 13 26
14 26 15 26 16 26 17 26 18 26 19 26 20 26 21 26 22 26 23 26 24 26 25
26 27 26 28 26 29 27 0 27 1 27 2 27 3 27 4 27 5 27 6 27 7 27 8 27 9 27
10 27 11 27 12 27 13 27 14 27 15 27 16 27 17 27 18 27 19 27 20 27 21
27 22 27 23 27 24 27 25 27 26 27 28 27 29 28 0 28 1 28 2 28 3 28 4 28
5 28 6 28 7 28 8 28 9 28 10 28 11 28 12 28 13 28 14 28 15 28 16 28 17
28 18 28 19 28 20 28 21 28 22 28 23 28 24 28 25 28 26 28 27 28 29 29 0
29 1 29 2 29 3 29 4 29 5 29 6 29 7 29 8 29 9 29 10 29 11 29 12 29 13
29 14 29 15 29 16 29 17 29 18 29 19 29 20 29 21 29 22 29 23 29 24 29
25 29 26
Test 98 0 24 0 31 0 37 0 38 0 39 1 26 1 29 1 32 1 40 2 14 2 28 2 29 2 37 2 0
Case 38 2 39 3 22 3 24 3 32 3 37 3 40 4 12 4 38 4 39 5 20 5 32 5 36 5 39 7
22 7 31 7 40 8 29 8 32 8 38 8 39 9 21 9 33 10 18 10 36 10 40 11 14 11
4 31 11 36 11 38 11 39 12 23 12 34 12 39 14 38 14 39 15 23 15 32 15 38
15 39 16 40 17 23 17 27 17 35 17 40 18 21 19 26 19 39 20 34 20 38 20
40 21 23 21 34 21 39 22 40 23 31 23 33 23 35 23 38 23 40 25 37 25 38
25 39 26 33 26 40 27 31 27 37 27 39 28 32 28 39 29 30 29 38 29 40 30
32 30 35 30 38 30 40 31 32 31 36 31 40 32 39 33 37 33 40 34 40 35 36
Test 129 0 44 0 47 0 50 1 28 1 41 1 45 1 47 1 49 2 50 3 16 3 29 3 38 3 45 3 0
Case 47 3 50 4 18 4 42 4 47 4 50 5 49 6 35 6 45 6 50 7 47 7 49 8 18 8 43 8
50 9 13 9 46 9 50 10 41 10 42 10 47 10 48 10 50 11 43 11 49 13 30 13
5 36 13 42 13 49 14 41 14 42 14 49 15 16 15 28 15 37 15 47 16 25 16 39
16 45 16 50 17 21 17 45 17 50 18 34 18 44 18 47 18 48 18 50 19 32 20
36 20 42 20 45 21 36 21 45 21 46 21 50 22 40 22 41 22 48 22 50 23 43
23 50 24 44 24 49 25 44 25 49 26 41 26 47 26 49 27 29 27 32 27 46 27
47 27 48 27 50 28 30 28 49 29 45 29 48 29 50 30 46 30 49 31 40 31 43
31 50 32 44 32 46 32 49 33 41 33 49 34 42 34 48 34 49 35 49 36 38 36
48 36 49 37 45 37 47 37 48 37 49 38 46 39 44 39 50 41 44 41 50 43 45
43 48 43 49 44 45 44 46 44 47 45 46 45 47 46 47 46 50
December 14 Question 2
Due on 2014-12-23, 05:30 IST
Given a number, print it in the Indian way of
writing the numbers with commas. In the
Indian system the rightmost comma is placed
after three rightmost digits, and then a comma
is placed after every two digits from the right.
For example:
1001921900000 -----> Indian System:
10,01,92,19,00,000
31000 -----> Indian System: 31,000
100 -----> Indian System: 100
12000000 -----> Indian System: 1,20,00,000
Input:
A number with at most 50 digits.
Output:
A single line with the Indian style of writing
the number with commas.
Sample Input 1
1021111
Sample Output 1
10,21,111
Sample Test Cases
Input Output
Test 21323141124124 2,13,23,14,11,24,124
Cas
e1
Test 93184915910001 9,31,84,91,59,10,001
Cas
e2
Test 3241738742310515910510951059105 32,41,73,87,42,31,05,15,91,05,10,95,10,59,10,
Cas 10 510
e3
Test 2143413415151513515 21,43,41,34,15,15,15,13,515
Cas
e4
Test 1000 1,000
Cas
e5
December 14 Question 3
Due on 2014-12-23, 05:30 IST
n prisoners, numbered 1 to n, are standing in a circle to be executed by the Roman army. The centurion will start with
start with a prisoner, skip the current one, and execute the next. The execution proceeds in a clockwise manner. At
every point, the next live prisoner is skipped, and the prisoner after that is executed. You have to print the prisoner
who will survive at the end.
When n=4, the execution proceeds as follows. First 2 will be executed, then 4. After this, the next prisoner, 1 will be
skipped and 3 will be executed. So the survivor is 1.
Input
Output
Sample Input 1
Sample Output 1
Sample Input 2
Sample Output 2
Test Case 2 4 1
Test Case 4 89 51
December 14 Question 4
Due on 2014-12-23, 05:30 IST
A number n is called a primitive root of a prime p if n^1 mod p, n^2 mod p, ... , n^{p-1} mod p visits all numbers {1, 2,
, p-1}.
(Important: Note that a^b is "a raised to the power b", and not the "^" operator in C. You will have to program this
operation yourself, or use appropriate <math.h> functions.)
Input
The input consists of a single line with two positive integers n and p. 1<n<p and p is a prime number less than 1000.
Output
If n is a primitive root of p, then print yes.
Otherwise, print the smallest positive number which does not occur in the sequence n^1 mod p, n^2 mod p, ... , n^{p-
1} mod p.
Sample Input 1
25
Sample Output 1
yes
Sample Input 2
27
Sample Output 2
3