Anda di halaman 1dari 13

Arab Academy for Science Technology and Maritime Transport

College of compouting and Information Technology

Course Computing Algorithms


Lecturer Dr Nahla Belal
TA Maram Shouman

Assignment 1: Intro to Computing Algorithms


Question 1:
Given two numbers A and B. The task is to compute the last digit of the resulting F, where
F= B! / A!
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test
cases follow. Each test case contains two number A and B as input.
Output:
For each test case, print last digit of B! / A! (B factorial/ A factorial).
Constraints:
1<=T<=100
1<=A<=B<=1018
Example:
Input:
2
24
107 109
Output:
2
2
Explanation:

Input: A = 2, B = 4
Output: 2
Explanation: A! = 2 and B! = 24.
F = 24/2 = 12 --> last digit = 2
Question 2(Save Winterfell):
As White Walkers have attacked Winterfell, Cersei has to send her army to Winterfell but
through tunnels.
Unwilling to do so, she wants to delay as much as she can so she take longest route to
Winterfell.
You are given no of tunnels (n) followed by tunnel 'u' connecting tunnel 'v' having distance 'w'.
Now, you being fan of Cersie, is keen to find the longest route.

Input:
The first line of input contains the number of test cases t.
The first line of each test case contains the number of tunnels (n). Each 'n-1' lines contain 'u','v'
and 'w' denoting that tunnel 'u' and 'v' are connected and distance between them is 'w'.

Output:

For each test case print the length of longest route.

Constraints:
1<=t<=1000
1<=n<=7000
1<=u, v<=n
1<=w<=500

Example:
Input:
1
8
153
354
542
245
476
782
761
Output:
14
Question 3(Save Gotham!)

Gotham has been attacked by Joker. Bruce Wayne has deployed automatic machine gun at each
tower of Gotham.
All the towers in Gotham are in straight line.
You are given no of towers 'n' followed by height of 'n' towers.
For every tower (p), find the height of the closest tower (towards the right), greater than the
height of tower (p).
Now, Print sum of all such heights (mod 1000000001).
Note: If for a tower (k), no such tower exists then take its height as 0.
Input:
First line of the input contains t, the number of test cases. First line of each test case contains
'n' denoting no of towers.
This is followed by 'n' spaced integers h1, h2.....h (n) representing height of towers.

Output:
On a single line, output the sum (mod 1000000001).

Constraints:
1<=t<=100
1<=n<=18000
0<=a[i] <=100000

Example:
Input:
1
9
112 133 161 311 122 512 1212 0 19212

Output:
41265
Explanation :
nextgreater(112) : 133
nextgreater(133) : 161
nextgreater(161) : 311
nextgreater(311) : 512
nextgreater(122) : 512
nextgreater(512) : 1212
nextgreater(1212) : 19212
nextgreater(0) : 19212
nextgreater(19212) : 0
Add = 133+161+311+512+512+1212+19212+19212+0 = 41265.
Question 4(Buying Vegetables)

Rahul wanted to purchase vegetables mainly brinjal, carrot and tomato. There are N different
vegetable sellers in a line. Each vegetable seller sells all three vegetable items, but at different
prices. Rahul, obsessed by his nature to spend optimally, decided not to purchase same
vegetable from adjacent shops. Also, Rahul will purchase exactly one type of vegetable item (only
1 kg) from one shop. Rahul wishes to spend minimum money buying vegetables using this
strategy. Help Rahul determine the minimum money he will spend.
Input:
First line indicates number of test cases T. Each test case in its first line contains N denoting the
number of vegetable sellers in Vegetable Market. Then each of next N lines contains three space
separated integers denoting cost of brinjal, carrot and tomato per kg with that particular
vegetable seller.

Output:
For each test case, output the minimum cost of shopping taking the mentioned conditions into
account in a separate line.

Constraints
1 <= T <= 10
1 <= N <= 100000
Cost of each vegetable (brinjal/carrot/tomato) per kg does not exceed 10^4

Example
Input:
1
3
1 50 50
50 50 50
1 50 50
Output:
52
Explanation:
There are two ways, each one gives 52 as minimum cost. One way is buy brinjals from first
shop, carrots from second shop and brinjals from third shop or he can buy brinjals from first
shop, tomatoes from second shop and brinjals from third shop.
Both ways, cost comes up to 1 + 50 + 1 = 52
Question 5(Print Binary Tree levels in sorted order)
Given a Complete Binary tree, print the level order traversal in sorted order.
Input:
The first line of the input contains integer T denoting the number of test cases. For each test
case, the first line takes an integer n denoting the size of array i.e. number of nodes followed
by n-space separated integers denoting the nodes of the tree in level order fashion.

Output:
For each test case, the output is the level order sorted tree.
Note: For every level, we only print distinct elements.

Constraints
1<=T<=100
1<=n<=10^5

Example:
Input:
2
7
7654321
6
564921
Output:
7
56
1234
5
46
129
Explanation:
Tree looks like this
7
/ \
6 5
/\ /\
43 2 1
Sorted order:
7
56
1234
Question 6(String Conversion)

Given two strings X and Y (call letters in uppercase). Check if it is possible to convert X to Y by
performing following operations.

 Make some lowercase letters uppercase.


 Delete all the lowercase letters.

Input:
First line of the input contains no of test cases T, the T test cases follow.
Each test case consist of 2 space separated integers A and B denoting the size of string X and Y
respectively
The next two lines contain the 2 string X and Y.

Output:
For each test case print the "Yes" if it's possible to convert X to Y by performing following
operations else "No".

Constraints
1<=T<=200
1<=size(X),size(Y)<=100
'a'<=X[i]<='z'
'A'<=X[i] , Y[i]<='Z'
Example:
Input :

2
53
daBcd ABC
43
ABcd BCD

Output:
Yes
No

Explanation:
Test Case 1: daBcd -> dABCd -> ABC Covert a and b at index 1 and 3 to upper case, delete the
rest those are lowercase. We get the string Y so the output is: Yes.
Question 7(Container with most water)
Given N non-negative integers a1, a2,…., an where each represents a point at coordinate (i, ai). N
vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two
lines, which together with x-axis forms a container, such that the container contains the most
water.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case
contains an integer N followed by N space separated integers.

Output:
For each test case, the output is the integer denoting the maximum area of water that can be
contained ( maximum area instead of maximum volume as working with 2D)

Constraints
1<=T<=100
2<=N<=50

Example:
Input:
2
4
1543
5
31245
Output
6
12
Explanation:
1. 5 and 3 are distance 2 apart. So the size of the base = 2. Height of container = min (5, 3) = 3.
So total area = 3 * 2 = 6.
2. 5 and 3 are distance 4 apart. So the size of the base = 4. Height of container = min (5, 3) = 3.
So total area = 4 * 3 = 12.
Question 8(Geek and Strings)
Geek recently read about strings, and he got interested in them. Geek has a list containing N
words (not necessarily distinct)-denoted by Li. Now, he will ask Q queries. Each query will
consist of a string x. You need to tell How many strings in the List has the string x as its prefix.
Input:
The First line contains an integer T, the number of test cases. The first line of each test
case contains an integer N, the total number of Words. Each of the next N lines contains a
string consisting of only lowercase alphabets -denoting Li.
The next Line contains Q- denoting the number of queries. Each of the next Q lines contain a
string -denoting the string x.

Output:
For each query Output an integer denoting the total number of strings in the list that have
string x as their prefix.

Constraints
1<=T<=10
1<= N<=30000
1<=Q<=10000
1<=Li, x<=100

Example:
Input:
1
5
abracadabra
geeksforgeeks
abracadabra
geeks
geeksthrill
5
abr
geeks
geeksforgeeks
ge
gar
Output:
2
3
1
3
0
Explanation:
There are 2 strings that have prefix "abr" - "abracadabra" and "abracadabra"
There are 3 strings that have prefix "geeks" - "geeksforgeeks" and "geeks" and "geeksthrill"
There is 1 string that have prefix "geeksforgeeks" - "geeksforgeeks".
There are 3 strings that have prefix "ge" - "geeksforgeeks" and "geeks" and "geeksthrill"
There is No string that have prefix as "gar".
Question 9(Water Connection Problem)
Every house in the colony has at most one pipe going into it and at most one pipe going out of
it. Tanks and taps are to be installed in a manner such that every house with one outgoing pipe
but no incoming pipe gets a tank installed on its roof and every house with only an incoming
pipe and no outgoing pipe gets a tap. Find the efficient way for the construction of the network
of pipes.
Input:
The first line contains an integer T denoting the number of test cases. For each test case, the
first line contains two integer n & p denoting the number of houses and number of pipes
respectively. Next, p lines contain 3 integer inputs a, b & d, d denoting the diameter of the pipe
from the house ato house b.

Output:
For each test case, the output is the number of pairs of tanks and taps installed i.e. n followed
by n lines that contain three integers: house number of tank, house number of tap and the
minimum diameter of pipe between them.

Constraints
1<=T<=50
1<=n<=20
1<=p<=50
1<=a, b<=20
1<=d<=100

Example:
Input:
1
96
7 4 98
5 9 72
4 6 10
2 8 22
9 7 17
3 1 66
Output:
3
2 8 22
3 1 66
5 6 10

Explanation:
Connected components are 3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3 followed by 2 8 22, 3 1 66, 5 6 10.
Question 10(Longest Sub-Array with Sum K)
Given an array containing N integers and an integer K. Your task is to find the length of the
longest Sub-Array with sum of the elements equal to the given value K.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test
cases follow. Each test case consists of two lines. First line of each test case contains two
Integers N and K and the second line contains N space separated elements of the array.

Output:
For each test case, print the required length of the longest Sub-Array in new line. If no such sub
array can be formed print 0.

Constraints
1<=T<=500
1<=N,K<=105
-105<=A[i]<=105

Example:
Input:
3
6 15
10 5 2 7 1 9
6 -5
-5 8 -14 2 4 12
36
-1 2 3
Output:
4
5
0
Explanation:
Test Case 1:

Input: arr [] = {10, 5, 2, 7, 1, 9},


K = 15
Output: 4
The sub-array is {5, 2, 7, 1}.
Question 11(Grouping Of Numbers)

One day Jim came across array of N numbers (a [N]).He decided to divide these N
numbers into different groups. Each group contains numbers in which sum of any
two numbers should not be divisible by an integer K. Print the size of the group
containing maximum numbers.
Input:

First line of the input contains an integer T, denoting the number of test cases. Then
T test cases follows. The first line of each test case contains 2 space-separated
integers N and K respectively.
The second line contains N space-separated integers denoting the N numbers which
Jim came across.
Output:

Print the size of the group containing maximum numbers in a new line.

Constraints

1<=T<=100
1<=N<=100000
1<=K<=100000
1<=a[i] <=100000; 1<=i<=N
Example:
Input:
1
48
1726
Output:
2
Explanation:
K=8
The group of numbers which can be formed are:
(1),(2),(7),(6),(1,2),(1,6),(7,2),(7,6),So, the maximum possible size if the group is 2.
Question 12(Pass the semester)
Satish wants to prepare for tomorrow’s exam. He knows the distribution of marks for the
subject along with time to learn the concepts.
You are given remaining time for the exam along with marks for each topic and passing marks
for the subject.
Find the max marks Satish can attain by studying max no of topic in max no hours not
exceeding (p).
Input:

The first line of input contains the number of test cases t.


The first line of each test case contains the no of topics (n), time remaining for exam (m) in
minutes and passing marks (p).
Each 'n' lines contain u (time to learn topic) and v (weightage of topic in exam).

Output:

For each test case print "YES" along with time taken or "NO".

Constraints

1<=t<=100
1<=n<=300
1<=m<=150
1<=p<=35
1<=u, v<=25

Example:
Sample Input 0
1
5 40 21
12 10
16 10
20 10
24 10
83

Sample Output 0
YES 36

Anda mungkin juga menyukai