Department of Basic Engineering Sciences C Programming & Data Structures Lab List of Experiments For The A.Y. 2016-17
Department of Basic Engineering Sciences C Programming & Data Structures Lab List of Experiments For The A.Y. 2016-17
Ex.
Topic Experiment Name
No.
1.) a.) Objective: Hello, World
https://www.hackerrank.com/challenges/30-hello-world
In this challenge, we review some basic concepts that will get you started with this series.
You will need to use the same (or similar) syntax to read input and write output in
challenges throughout HackerRank.
Task
To complete this challenge, you must save a line of input from stdin to a variable, print
Hello, World. On a single line, and finally print the value of your variable on a second
line.
Note: The instructions support submissions in many popular languages. You can switch
languages using the drop-down menu above your editor, and the variable may be written
differently depending on the best-practice conventions of your submission language.
Input Format
Basic
Concepts 1 A single line of text denoting inputString (the variable whose contents must be printed).
Introduction
Output Format
Print Hello, World. On the first line, and the contents of on the second line.
Sample Input
Sample Output
Hello, World.
Welcome to 30 Days of Code!
Explanation
On the first line, we print the string literal Hello, World.. On the second line, we print
the contents of the variable which, for this sample case, happens to be Welcome to 30
Days of Code!. If you do not print the variable's contents to stdout, you will not pass the
hidden test case.
1.) b.) Objective: Data Types
https://www.hackerrank.com/challenges/30-data-types
Today, we're discussing data types. Check out the Tutorial tab for learning materials and
an instructional video!
Task
Complete the code in the editor below. The variables i, d and s are already declared and
initialized for you. You must declare 3 variables: one of type int, one of type double, and
one of type String. Then you must read 3 lines of input from STDIN and initialize your
variables. Finally, you must use the + operator to perform the following operations:
Note: If you are using a language that doesn't support using + for string concatenation
(e.g:C), you can just print one variable immediately following the other on the same line.
The string provided in your editor must be printed first, immediately followed by the string
Basic you read as input.
Concepts 1
Introduction Input Format
Output Format
Print the sum of both integers on the first line, the sum of both doubles on the second line,
and then the two concatenated strings on the third line.
Sample Input
12
4.0
is the best place to learn and practice coding!
Sample Output
16
8.0
HackerRank is the best place to learn and practice coding!
https://www.hackerrank.com/challenges/time-conversion
Basic
Concepts 1
Given a time in AM/PM format, convert it to military (24-hour) time.
Introduction
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock.
Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Input Format
Output Format
Convert and print the given time in 24-hour format, where. 00 <= hh <= 23
Sample Input
07:05:45PM
Sample Output
19:05:45
2.) a.) Objective: Intro to conditional Statements
In this challenge, we're getting started with conditional statements.
https://www.hackerrank.com/challenges/30-conditional-statements
Task
Given an integer, n, perform the following conditional actions:
Complete the stub code provided in your editor to print whether or not n is weird.
Input Format
Output Format
Sample Input 0
3
Sample Output 0
Weird
Sample Input 1
24
Sample Output 1
Not Weird
2.) b.) Objective: Plus Minus
https://www.hackerrank.com/challenges/plus-minus
Given an array of integers, calculate which fraction of its elements are positive, which
fraction of its elements are negative, and which fraction of its elements are zeroes,
respectively. Print the decimal value of each fraction on a new line.
Note: This challenge introduces precision problems. The test cases are scaled to six
decimal places, though answers with absolute error of up to 10-4 are acceptable.
Input Format
The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers describing an array of numbers.
(a0,a1,a2,…………,an-1)
Output Format
Sample Input
6
-4 3 -9 0 4 1
Sample Output
0.500000
0.333333
0.166667
Explanation
There are 3 positive numbers, 2 negative numbers, and 1 zero in the array.
The respective fractions of positive numbers, negative numbers and zeroes are,
3/6=0.500000, 2/6=0.333333 and 1/6=0.166667, respectively.
2.) c.) Objective: Angry Professor
https://www.hackerrank.com/challenges/angry-professor
A Discrete Mathematics professor has a class of N students. Frustrated with their lack of
discipline, he decides to cancel class if fewer than K students are present when class starts.
Given the arrival time of each student, determine if the class is canceled.
Input Format
Each test case consists of two lines. The first line has two space-separated integers, N
(students in the class) and K (the cancelation threshold).
The second line contains N space-separated integers (a1,a2,…………,aN) describing the
arrival times for each student.
Note: Non-positive arrival times (ai <= 0) indicate the student arrived early or on time;
positive arrival times (ai > 0) indicate the student arrived ai minutes late.
Output Format
Conditional For each test case, print the word YES if the class is canceled or NO if it is not.
2
statements I
Constraints
1 <= T <= 10
1<= N <=1000
1 <= K<=N
-100 <= ai <= 100 Where i belongs to [1,N]
Note
If a student arrives exactly on time (ai = 0), the student is considered to have entered before
the class started.
Sample Input
2
43
-1 -3 4 2
42
0 -1 2 1
Sample Output
YES
NO
3.) a.) Objective: Utopian Tree
https://www.hackerrank.com/challenges/utopian-tree
The Utopian Tree goes through 2 cycles of growth every year. Each spring, it doubles in
height. Each summer, its height increases by 1 meter.
Laura plants a Utopian Tree sapling with a height of 1 meter at the onset of spring. How
tall will her tree be after N growth cycles?
Input Format
Constraints
1 <= T <= 10
0 <= N <=60
Output Format
For each test case, print the height of the Utopian Tree after N cycles. Each height must be
printed on a new line.
Sample Input
3
0
1
4
Sample Output
1
2
7
Explanation
In the first case (N=0), the initial height (H=1) of the tree remains unchanged.
In the second case (N=1), the tree doubles in height and is 2 meters tall after the spring
cycle.
In the third case (N=4), the tree doubles its height in spring (H=2), then grows a meter in
summer (H=3), then doubles after the next spring (H=6), and grows another meter after
summer (H=7). Thus, at the end of 4 cycles, its height is 7 meters.
3.) b.) Objective: Chocolate Feast
https://www.hackerrank.com/challenges/chocolate-feast
Little Bobby loves chocolate, and he frequently goes to his favorite 5&10 store, Penny
Auntie, with n dollars to buy chocolates. Each chocolate has a flat cost of c dollars, and the
store has a promotion where they allow you to trade in m chocolate wrappers in exchange
for 1 free piece of chocolate.
For example, if m=2 and Bobby has n=4 dollars that he uses to buy 4 chocolates at c=1
dollar apiece, he can trade in the 4 wrappers to buy 2 more chocolates. Now he has 2 more
wrappers that he can trade in for 1 more chocolate. Because he only has 1 wrapper left at
this point and 1<m, he was only able to eat a total of 5 pieces of chocolate.
Given n, c, and m for t trips to the store, can you determine how many chocolates Bobby
eats during each trip?
Input Format
The first line contains an integer, t, denoting the number of trips Bobby makes to the store.
Each line i of the t subsequent lines contains three space-separated integers describing the
respective n, c, and m values for one of Bobby's trips to the store.
Conditional Constraints
statements 3 1 <= T <= 1000
II
2 <= n <= 105
1 <= c <= n
2 <= m <= n
Output Format
For each trip to Penny Auntie, print the total number of chocolates Bobby eats on a new
line.
Sample Input
3
10 2 5
12 4 4
622
Sample Output
6
3
5
Explanation
In this challenge, we're going to use loops to help us do some simple math. Check out the
Tutorial tab to learn more.
Task
Given an integer, N, print its first 10 multiples. Each multiple N x i (where 1<=i<=10)
should be printed on a new line in the form: N x i = result.
Input Format
A single integer, N.
Constraints
2 <= N <= 20
Output Format
Loops 4
Print 10 lines of output; each line i (where 1<=i<=10) contains the result of N x i in the
form:
N x i = result.
Sample Input
Sample Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
4.) b.) Objective: Stair Case
https://www.hackerrank.com/challenges/staircase
Consider a staircase of size n = 4:
#
##
###
####
Observe that its base and height are both equal to n, and the image is drawn using #
symbols and spaces. The last line is not preceded by any spaces.
Input Format
Sample Input
6
Sample Output
#
##
###
####
#####
######
4.) c.) Objective: Sherlock and Divisors
https://www.hackerrank.com/challenges/sherlock-and-divisors
Watson gives an integer N to Sherlock and asks him: What is the number of divisors of N
that are divisible by 2?
Input Format
First line contains T, the number of testcases. This is followed by T lines each containing
an integer N.
Output Format
For each testcase, print the required answer in one line.
Constraints
1 <= T <= 100
1 <= N <= 109
Sample Input
2
9
8
Sample Output
0
3
Explanation
9 has three divisors 1, 3 and 9 none of which is divisible by 2.
8 has four divisors 1, 2, 4 and 8, out of which three are divisible by 2.
5.) a.) Objective: Sherlock and Squares
https://www.hackerrank.com/challenges/sherlock-and-squares
Watson gives two integers (A and B) to Sherlock and asks if he can count the number of
square integers between A and B (both inclusive).
Note: A square integer is an integer which is the square of any integer. For example, 1, 4,
9, and 16 are some of the square integers as they are squares of 1, 2, 3, and 4, respectively.
Input Format
The first line contains T, the number of test cases. T test cases follow, each in a new line.
Each test case contains two space-separated integers denoting A and B.
Output Format
For each test case, print the required answer in a new line.
Constraints
Sample Input
2
39
17 24
Sample output
2
0
Explanation
Test Case #00: In range[3,9], 4 and 9 are the two square numbers.
Test Case #01: In range[17,24], there are no square numbers.
5.) b.) Objective: Solve Me First
https://www.hackerrank.com/challenges/solve-me-first
Welcome to HackerRank! The purpose of this challenge is to familiarize you with reading
input from stdin (the standard input stream) and writing output to stdout (the standard
output stream) using our environment.
Review the code provided in the editor below, then complete the solve Me First function
so that it returns the sum of two integers read from stdin. Take some time to understand
this code so you're prepared to write it yourself in future challenges.
Input Format
Code that reads input from stdin is provided for you in the editor. There are lines of input,
and each line contains a single integer.
Output Format
Code that prints the sum calculated and returned by solve Me First is provided for you in
the editor.
Objective
Today, we're learning and practicing an algorithmic concept called Recursion.
Task: Write a factorial function that takes a positive integer, N as a parameter and prints
Functions 5 the result of N! (N factorial).
Note: If you fail to use recursion or fail to name your recursive function factorial or
Factorial, you will get a score of 0.
Input Format
Constraints
2 <= N <= 12
Your submission must contain a recursive function named factorial.
Output Format
Sample Input
Sample Output
Explanation
1. factorial(3) = 3 x factorial(2)
2. factorial(2) = 2 x factorial(1)
3. factorial(1) = 1
From steps 2 and 3, we can say factorial (2) = 2 x 1 = 2; then when we apply the value
from factorial (2) to step 1, we get factorial (3) = 3 x 2 x 1 = 6. Thus, we print 6 as our
answer.
Given an array of N integers, can you find the sum of its elements?
Input Format
The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers representing the array's elements.
Output Format
Arrays I 6
Print the sum of the array's elements as a single integer.
Sample Input
6
1 2 3 4 10 11
Sample Output
31
6.) b.) Objective: A Very Big Sum
https://www.hackerrank.com/challenges/a-very-big-sum
You are given an array of integers of size N. You need to print the sum of the elements in
the array, keeping in mind that some of those integers may be quite large.
Input
The first line of the input consists of an integer N. The next line contains N space-
separated integers contained in the array.
Constraints
1 <= N <= 10
0 <= A[i] <= 1010
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005
Output
Print a single value equal to the sum of the elements in the array. In the above sample, you would
Arrays I 6 print
5000000015
Input Format
The first line contains a single integer, N. The next N lines denote the matrix's rows, with
each line containing N space-separated integers describing the columns.
Output Format
Print the absolute difference between the two sums of the matrix's diagonals as a single
integer.
Sample Input
Arrays II 7
3
11 2 4
456
10 8 -12
Sample Output
15
Explanation
Given an integer, N, traverse its digits (d1,d2,...,dn) and determine how many digits evenly
divide N (i.e.: count the number of times N divided by each digit di has a remainder of 0).
Print the number of evenly divisible digits.
Note: Each digit is considered to be unique, so each occurrence of the same evenly
divisible digit should be counted (i.e.: for N = 111, the answer is 3).
Input Format
Constraints
1 <= T <= 15
0 < N < 109
Output Format
For every test case, count and print (on a new line) the number of digits in N that are able
to evenly divide N.
Sample Input
2
12
1012
Sample Output
2
3
7.) c.) Objective: The Time in Words
https://www.hackerrank.com/challenges/the-time-in-words
Given the time in numerals we may convert it into words, as shown below:
Write a program which prints the time in words for the input given in the format
mentioned above.
Constraints
1 <= H < 12
0 <= M < 60
Output Format
Display the time in words.
Sample Input
5
47
Sample Output
thirteen minutes to six
7.) d.) Objective: Lonely Integer
https://www.hackerrank.com/challenges/lonely-integer
There are N integers in an A array. All but one integer occur in pairs. Your task is to find
the number that occurs only once.
Arrays II 7
Input Format
The first line of the input contains an integer N, indicating the number of integers. The
next line contains N space-separated integers that form the array A.
Constraints
1 <= N < 100
N % 2 = 1 (N is an odd Number)
% ( is an odd number)
Output Format
Sample Input 1
1
1
Sample Output 1
1
Sample Input 2
3
112
Sample Output 2
2
Sample Input 3
5
00121
Sample Output 3
2
Explanation
In the first input, we see only one element (1) and that element is the answer.
In the second input, we see three elements; 1 occurs at two places and 2 only once. Thus,
the answer is 2.
In the third input, we see five elements. 1 and 0 occur twice. The element that occurs only
once is 2.
8.) a.) Objective: Pangrams
https://www.hackerrank.com/challenges/pangrams
Roy wanted to increase his typing speed for programming contests. So, his friend advised
him to type the sentence "The quick brown fox jumps over the lazy dog" repeatedly,
because it is a pangram. (Pangrams are sentences constructed by using every letter of the
alphabet at least once.)
After typing the sentence several times, Roy became bored with it. So he started to look
for other pangrams.
Input Format
Constraints
Length of can be at most 103 and it may contain spaces, lower case and upper case letters.
Lower-case and upper-case instances of a letter are considered the same.
Output Format
Input #1
Input #2
Sample Output
Output #1
pangram
Output #2
not pangram
Explanation
In the first test case, the answer is pangram because the sentence contains all the letters of
the English alphabet.
8.) b.) Objective: Alternating Characters
https://www.hackerrank.com/challenges/alternating-characters
Shashank likes strings in which consecutive characters are different. For example, he likes
ABABA, while he doesn't like ABAA. Given a string containing characters and only, he
wants to change it into a string he likes. To do this, he is allowed to delete the characters in
the string.
Input Format
The first line contains an integer T, i.e. the number of test cases.
The next T lines contain a string each.
Output Format
For each test case, print the minimum number of deletions required.
Constraints
1 <= T <= 10
1 <= length of string <= 105
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4
Explanation
Alice is taking a cryptography class and finding anagrams to be very useful. We consider
two strings to be anagrams of each other if the first string's letters can be rearranged to
form the second string. In other words, both strings must contain the same exact letters in
the same exact frequency For example, bacdc and dcbac are anagrams, but bacdc and
dcbad are not.
Alice decides on an encryption scheme involving two large strings where encryption is
dependent on the minimum number of character deletions required to make the two strings
anagrams. Can you help her find this number?
Given two strings, a and b, that may or may not be of the same length, determine the
minimum number of character deletions required to make a and b anagrams. Any
characters can be deleted from either of the strings.
Chinese
Russian
Input Format
The first line contains a single string, a.
The second line contains a single string, b.
Constraints
1 <= |a|,|b| <= 104
It is guaranteed that and consist of lowercase English letters.
Output Format
Print a single integer denoting the number of characters which must be deleted to make the
two strings anagrams of each other.
Sample Input
cde
abc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of
each other:
Remove d and e from cde to get c.
Remove a and b from abc to get c.
We had to delete characters to make both strings anagrams, so we print on a new line.
9.) a.) Objective: Caesar Cipher
https://www.hackerrank.com/challenges/caesar-cipher-1
Note: The cipher only encrypts letters; symbols, such as - , remain unencrypted.
Input Format
The first line contains an integer, N, which is the length of the unencrypted string.
The second line contains the unencrypted string, S.
The third line contains the integer encryption key, K, which is the number of letters to
rotate.
Constraints
1 <= N <= 100
0 <= K <= 100
S is a valid ASCII string and doesn't contain any spaces.
Output Format
Sample Input
11
middle-Outz
2
Sample Output
okffng-Qwvb
Explanation
Each unencrypted letter is replaced with the letter occurring K spaces after it when listed
alphabetically. Think of the alphabet as being both case-sensitive and circular; if K rotates
past the end of the alphabet, it loops back to the beginning (i.e.: the letter after z is a, and
the letter after Z is A).
Selected Examples:
m (ASCII 109) becomes o (ASCII 111).
i (ASCII 105) becomes k (ASCII 107).
- remains the same, as symbols are not encoded.
O (ASCII 79) becomes Q (ASCII 81).
z (ASCII 122) becomes b (ASCII 98); because z is the last letter of the alphabet, a (ASCII
97) is the next letter after it in lower-case rotation.
9.) b.) Objective: Palindrome Index
https://www.hackerrank.com/challenges/palindrome-index
Given a string, S, of lowercase letters, determine the index of the character whose removal
will make S a palindrome. If S is already a palindrome or no such character exists, then
print -1. There will always be a valid solution, and any correct answer is acceptable. For
example, if S = "bcbc", we can either remove 'b' at index 0 or 'c' at index 3.
Input Format
The first line contains an integer, T, denoting the number of test cases.
Each line i of the T subsequent lines describes a test case in the form of a single string, Si.
Constraints
1 <= T <= 20
All characters are lowercase English letters.
Output Format
Print an integer denoting the zero-indexed position of the character that makes S not a
palindrome; if S is already a palindrome or no such character exists, print -1.
Sample Input
Strings II 9
3
aaab
baa
aaa
Sample Output
3
0
-1
Explanation
https://www.hackerrank.com/challenges/gem-stones
John has discovered various rocks. Each rock is composed of various elements, and each
element is represented by a lower-case Latin letter from 'a' to 'z'. An element can be
present multiple times in a rock. An element is called a gem-element if it occurs at least
once in each of the rocks.
Given the list of N rocks with their compositions, display the number of gem-elements that
exist in those rocks.
Input Format
Each of the next N lines contains a rock's composition. Each composition consists of
lower-case letters of English alphabet.
Constraints
Print the number of gem-elements that are common in these rocks. If there are none, print
0.
Sample Input
abcdde
baccd
eeabg
Sample Output
Explanation
Only "a" and "b" are the two kinds of gem-elements, since these are the only characters
that occur in every rock's composition.
https://www.hackerrank.com/challenges/sherlock-and-array
Watson gives Sherlock an array of length. Then he asks him to determine if there exists an
element in the array such that the sum of the elements on its left is equal to the sum of the
elements on its right. If there are no elements to the left/right, then the sum is considered to
be zero.
Input Format
The first line contains T, the number of test cases. For each test case, the first line contains
N, the number of elements in the array A. The second line for each test case contains N
space-separated integers, denoting the array A.
Output Format
For each test case print YES if there exists an element in the array, such that the sum of the
elements on its left is equal to the sum of the elements on its right; otherwise print NO.
Constraints
1 <= T <= 10
Sample Input
2
3
1 2 3
4
1 2 3 3
Sample Output
NO
YES
Explanation
For the first test case, no such index exists.
For the second test case, A[1] + A[2] = A[4], therefore index 3 satisfies the given conds.
11.) Objective: Maximum Element
https://www.hackerrank.com/challenges/maximum-element
You have an empty sequence, and you will be given queries. Each query is one of these
three types:
Input Format
The first line of input contains an integer, N. The next N lines each contain an above
mentioned query. (It is guaranteed that each query is valid.)
Constraints
1 <= N <= 105
1 <= x <= 109
Output Format
For each type query, print the maximum element in the stack on a new line.
Sample Input
10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3
Sample Output
26
91
12.) a.) Objective: Reverse a linked list
https://www.hackerrank.com/challenges/reverse-a-linked-list
You’re given the pointer to the head node of a linked list. Change the next pointers of
the nodes so that their order is reversed. The head pointer given may be null meaning that
the initial list is empty.
Input Format
You have to complete the Node* Reverse (Node* head) method which takes one
argument - the head of the linked list. You should NOT read any input from stdin/console.
12.) b.) Objective: Delete duplicate-value nodes from a sorted linked list
https://www.hackerrank.com/challenges/delete-duplicate-value-nodes-from-a-sorted-linked-list
You're given the pointer to the head node of a sorted linked list, where the data in the
nodes is in ascending order. Delete as few nodes as possible so that the list does not
contain any value more than once. The given head pointer may be null indicating that the
list is empty.
For now do not be concerned with the memory deallocation. In common abstract data
structure scenarios, deleting an element might also require deallocating the memory
occupied by it. For an initial intro to the topic of dynamic memory please consult:
http://www.cplusplus.com/doc/tutorial/dynamic/
Input Format
You have to complete the Node* RemoveDuplicates (Node* head) method which takes
one argument - the head of the sorted linked list. You should NOT read any input from
stdin/console.
Output Format
Linked List 12
Delete as few nodes as possible to ensure that no two nodes have the same data. Adjust the
next pointers to ensure that the remaining nodes form a single sorted linked list. Then
return the head of the sorted updated linked list. Do NOT print anything to stdout/console.
Sample Input
1 -> 1 -> 3 -> 3 -> 5 -> 6 -> NULL
NULL
Sample Output
1 -> 3 -> 5 -> 6 -> NULL
NULL
Explanation
1. 1 and 3 are repeated, and are deleted.
2. Empty list remains empty.