TASK Java Lab
TASK Java Lab
1. Decision Making
Task 1
Tags – Easy
Problem Statement
Given a positive integer denoting, do the following: If , then print the lowercase English word
corresponding to the number (e.g., one for , two for , etc.). If , print Greater than 9 .
Input Format
Constraints
1<=n<1=0^9
Output Format
If 1<=n<=10^9 , then print the lowercase English word corresponding to the number (e.g., one
for , two for , etc.); otherwise, print Greater than 9 instead.
Sample Input 1
Sample Output 1
Five
Sample Input 2
44
Sample Output 2
Greater 9
Task 2
Tag: Easy
Problem Statement
India is playing a cricket match. The predictor said, if India scores more than 300 runs today,
then india will win the match. If they are scoring less than 200, then India will lose the match.
When India score is from 200 to 300, then check luck, if luck is yes means, India wins. If luck is
no means India lose the match.
Input Format
Constraints
Output Format
Sample Input 1
105
Sample Output
‘INDIA LOSE’
Sample Input 2
300
Sample Output 2
‘INDIA WIN’
Task 3
Tag: Easy
Problem Statement
A number will be given to you. You want to check the number is odd or even.
If it is odd number, then check whether the given number is divisible by 5, if so print ‘ODD 5’.
If it is odd and not divisible by ‘5’ then check the number is divisible by 3, if so print ‘ODD 3’.
If the number is odd and not divisible by ‘5’ and ‘3’, then print ‘ODD’
If the number is even and divisible by ‘5’ means print ‘EVEN 5’.
If the given number is even and not divisible by ‘5’ means, then check the number is divisible by
‘2’, if so
If the number is even and not divisible by ‘5’ and ‘2’, then print ‘EVEN’
Input Format
Constraints
Output Format
Print any one which is matching ODD 5 ODD 3 EVEN 5 EVEN 2 ODD EVEN
Sample Input 1
15
Sample Output
EVEN 5
2. Array 1D
Task 1
Tags – Easy
Problem Statement
Example: If array, arr=[1,2,3,45], after reversing it, the array should be, arr= [5,4,3,2,1] .
Input Format
The first line contains an integer,n , denoting the size of the array. The next line contains n
Constraints
1<=n<=1000
Output Format
The output is handled by the code given in the editor, which would print the array.
Sample Input 1
16 13 7 2 1 12
Sample Output 1
12 1 2 7 13 16
Task 2
Tags – Easy
Problem Statement
Given an array, rotate the array to the right by k steps, where k is non-negative.
Input Format
[1,2,3,4,5,6,7] and k = 3
Constraints
1<=n<=10
Output Format
[5,6,7,1,2,3,4]
Sample Input 1
Sample Output 1
[3,99,-1,-100
Task 1
Tags – Medium
Problem Statement
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the
same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Input Format
[2,0,2,1,1,0]
Constraints
0<=n<=2
Output Format
[0,0,1,1,2,2]
Sample Input 1
[0,1,0,1,2,2]
Sample Output 1
[2,2,1,1,0,0]
3. Prime Number Based Problems
Task 1
Tags – Easy
Problem Statement
Given two numbers and as interval range, the task is to find the prime numbers in between
this interval.
Input Format
a = 1, b = 10
Constraints
1<=a<=100
1<=b<=100
Output Format
2, 3, 5, 7
Sample Input 1
Sample Output 1
Tags – Easy
Problem Statement
Given a number N. Find number of almost primes from 1 to n. A number is called almost if it has
exactly two distinct prime factors.
Note: The numbers can have any number of non-prime factors but should have exactly two
prime factors.
Input Format
N = 10
Constraints
1<=n<=100
Output Format
Sample Input 1
21
Sample Output 1
Task 3
Tags – Easy
Problem Statement
Given a number N (greater than 2 ). The task is to find two distinct prime numbers whose
product will be equal to the given number. There may be several combinations possible. Print
only first such pair.
If it is not possible to express N as a product of two distinct primes, print “Not Possible”.
Input Format
N = 15
Constraints
1<=n<=100
Output Format
3,5
39
Sample Output 1
3,13
3 and 13 are both primes and,
3*13 = 39
Task 1
Tags – Easy
Problem Statement
Let's consider a square grid with N rows and N columns, both numbered 1 through N. Each cell
contains one integer. Let ri denote the minimum value in the i-th row, and let Ci denote the
maximum value in the i-th column. A grid is called nice if and only if the following condition
holds true:
Chef has a square grid and can change some of its elements (numbers in the cells). Find the
minimum possible number of elements Chef should change, so that the new grid is nice.
Input Format
The first line of the input contains an integer N, denoting the size of the grid.
The following N lines describe the grid. The i-th of those lines contains N integers, denoting the
numbers in the i-th row.
Constraints
1 ≤ N ≤ 1000
Each number in the initial grid is between 1 and 106, inclusive.
Output Format
Print one integer, denoting the minimum number of changes required to make the grid nice.
Sample Input 1
10 20 30
20 10 30
10 5 35
Sample Output 1
Task 2
Tags – Easy
Problem Statement
Chef loves to play with arrays by himself. Today, he has an array A consisting of Ndistinct
integers. He wants to perform the following operation on his array A.
Select a pair of adjacent integers and remove the larger one of these two. This decreases the
array size by 1. Cost of this operation will be equal to the smaller of them.
Find out minimum sum of costs of operations needed to convert the array into a single element.
Input Format
First line of input contains a single integer T denoting the number of test cases. First line of each
test case starts with an integer N denoting the size of the array A. Next line of input
contains N space separated integers, where the ith integer denotes the value Ai.
Constraints
1 ≤ T ≤ 10
2 ≤ N ≤ 50000
1 ≤ Ai ≤ 105
Output Format
For each test case, print the minimum cost required for the transformation.
Sample Input 1
34
425
Sample Output 1
5. Number Theory
Task 1
Tags – Easy
Problem Statement
The Head Chef has received his id from the Association of Byteland . He wants to know the
numerical rank of his number among the numbers that can be formed by the exact same digits (
i.e. among numbers having same number of 0's , 1's ... 9's as his id ) . Note that id's can start with
0 . Given a number, find the rank of the number .
Input Format
The first line of the input contains an integer T denoting the number of test cases. The
description of T test cases follows.
Each test case contains a single integer N denoting the id the chef has received .
Constraints
1 ≤ T ≤ 10000
1 ≤ number of digits in N ≤ 18
Output Format
For each test case, output a single line containing the answer to the given test case .
Sample Input 1
276
762
Sample Output 1
6
Task 2
Tags – Easy
Problem Statement
You are given a 1-indexed array of size N. Initially, all elements of the array are zeros. You need
to perform U updates to the array.
Each Update asks you to add 1 to the elements of the array at the indices specified by 'ax+b' for
all non-negative integers x, where a and b are positive integers which specify an update query.
After performing all the updates to the array, print the array with every entry separated by space
Input Format
Each of the next U lines contains 2 space-separated integers: a and b, specifying an update.
Constraints
1 ≤ N ≤ 105
1 ≤ a, b ≤ N
1 ≤ U ≤ 2*105
Output Format
Sample Input 1
74
13
77
73
22
Sample Output 1
0122122
Task 3
Tags – Easy
Problem Statement
Rajesh is a very brilliant student. He likes sequences a lot specially the fibonacci sequence.
The fibonacci sequence is shown below
1 1 2 3 5 8 ...
Today Rajesh friend Saket gave him a problem on fibonacci sequences.
Since Rajesh is a genious at fibonacci sequences, he considers this as a pretty easy problem and
asks you to solve it as he is feeling sleepy.
Input Format
First line of the input contains an integer T, denoting the number of inputs.
T lines follow each containing an integer N
Constraints
Should contain all the constraints on the input data that you may have. Format it like:
Subtask 1: 1 points
1 ≤ T ≤ 10
1 ≤ N ≤ 20
Subtask 2: 29 points
1 ≤ T ≤ 1000
1 ≤ N ≤ 100000
Subtask 3: 70 points
1 ≤ T ≤ 100000
1 ≤ N ≤ 1000000000
Output Format
Sample Output 1
Task 1
Tags – Easy
Problem Statement
You want to build a temple for snakes. The temple will be built on a mountain range, which can
be thought of as n blocks, where height of i-th block is given by hi. The temple will be made on a
consecutive section of the blocks and its height should start from 1 and increase by exactly 1
each time till some height and then decrease by exactly 1 each time to height 1, i.e. a consecutive
section of 1, 2, 3, .. x-1, x, x-1, x-2, .., 1 can correspond to a temple. Also, heights of all the
blocks other than of the temple should have zero height, so that the temple is visible to people
who view it from the left side or right side.
You want to construct a temple. For that, you can reduce the heights of some of the blocks. In a
single operation, you can reduce the height of a block by 1 unit. Find out minimum number of
operations required to build a temple.
Input Format
The first line of the input contains an integer T denoting the number of test cases. The
description of T test cases follows.
The next line contains n integers, where the i-th integer denotes hi
Constraints
1 ≤ T ≤ 10
2 ≤ n ≤ 105
1 ≤ hi ≤ 109
Output Format
For each test case, output a new line with an integer corresponding to the answer of that testcase.
Sample Input 1
121
1121
12621
Sample Output 1
Task 2
Tags – Easy
Problem Statement
All submissions for this problem are available.Chef has an array AA which is a permutation of
natural numbers till nn. He defines value of an array as no of elements in the array whose
neighboring elements have sum at least kk.(Note that first and last element of an array have only
1 neighboring elements where as remaining elements have 2 neighboring elements each.)
You don't know the permutation that chef has, so you decide to find the sum of the values of all
possible permutations. Given nn and kk you need to find this sum and output it modulo
1000000007 (109+7109+7).
Input Format
First line will contain TT, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, two integers n,kn,k.
Constraints
1≤T≤1000001≤T≤100000
2≤n≤1052≤n≤105
2≤k≤105
Output Format
For each testcase, output in a single line sum of value of all possible permutations modulo
1000000007.
Sample Input 1
22
33
Sample Output 1
10
Task 3
Tags – Easy
Problem Statement
Chef is playing a game with his friend Misha. They have a pile containg N coins. Players take
alternate turns, removing some coins from the pile. On each turn, a player can remove either one
coin or coins equal to some prime power (i.e. pxcoins, where p - prime number and x - positive
integer). Game ends when the pile becomes empty. The player who can not make a move in his
turn loses.
Chef plays first. Your task is to find out who will win the game, provided that both of the player
play optimally.
Input Format
The first line of the input contains an integer T denoting the number of test cases. The
description of T test cases follows.
The only line of each test case contains one integer N.
Constraints
1 ≤ T ≤ 1000
1 ≤ N ≤ 109
Output Format
For each test case, output a single line containing one word - the name of the winner of the
game. Print "Chef" (without quotes) if Chef wins the game, print "Misha" (without quotes)
otherwise.
Sample Input 1
Sample Output 1
Chef
Chef
Task 1
Tags – Easy
Problem Statement
All submissions for this problem are available.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, aa and bb, that may or may not be of the same length, determine the minimum
number of character deletions required to make aa and bb anagrams. Any characters can be
deleted from either of the strings.
Input Format
The first line contains a single string, a.
The second line contains a single string, b.
Constraints
1≤Length of a and b≤10001≤Length of a and b≤1000
The strings a and b consist of lowercase English alphabetic letters ascii[a-z]
Output Format
Print a single integer denoting the number of characters you must delete to make the two strings
anagrams of each other.
Sample Input 1
cde
abc
Sample Output 1
Task 2
Tags – Easy
Problem Statement
Check whether two strings contain same characters in same order Given two strings s1 and s2,
the task is to find whether the two string contain the same characters that occur in the same
order. For example: string “COLOR” and string “COLOUR” contain the same charcters in same
order.
Input Format
s1 = “COLOR”, s2 = “COLOUR”
Constraints
Alphabets
Output Format
Yes or No
Sample Input 1
s1 = “COLOR”, s2 = “COLOUR”
Sample Output 1
Yes
Task 3
Tags – Easy
Problem Statement
All submissions for this problem are available.Roy protected his confidential information by
encrypting it using a secret method. He rotates the alphabet in the following manner:
Original stringOriginal string - abcdefghijklmnopqrstuvwxyz
Rotation by 3Rotation by 3 – defghijklmnopqrstuvwxyzabc
Input Format
The first line contains the integer, nn, the length of the unencrypted string.
The second line contains the unencrypted string, ss.
The third line contains kk, the number of letters to rotate the alphabet by.
Constraints
1≤n≤1001≤n≤100
0≤k≤1000≤k≤100
s is a valid ASCII string without any spaces.
Output Format
.
Sample Input 1
11
middle-Outz
2
Sample Output 1
okffng-Qwvb
8. Pattern Forming
****
** * * * *
* *
* *
* *
* *
9. Display Order
Task1
Task 2
Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given
array. The functions should put all 0s first, then all 1s and all 2s in last.
Examples:
Input: {0, 1, 2, 0, 1, 2}
Output: {0, 0, 1, 1, 2, 2}
Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}
Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}
Task 3
Write program user would be asked to enter a set of Strings and the program would sort and
display them in ascending alphabetical order.
Examples:
Input : 3
Jesi
Arun
Ram
Output :
Arun
Jesi
Ram
Task 1
struct
int si;
double d;
float cp;
} s;
void
main ()
printf ("%d, %d, %d", sizeof (s.si), sizeof (s.d), sizeof (s.cp));
Task 2
int main ()
{
int a, b;
a = b = 4;
b = a++;
Task 3
int main()
int x;
if(x < 5)
continue;
else
break;
printf("Placement Question");
}
return 0;
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0:
}
Task 2
1. #include <stdio.h>
2. int main()
3. {
4. double number;
5.
6. printf("Enter a number: ");
7. scanf("%d", &number);
8.
9. if (number = 0.0)
10. {
11. if (number == 0.0)
12. printf("You entered 0.")
13. else
14. printf("You entered a negative number.");
15.
16. else
17. printf("You entered a positive number.");
18. 0;
19. }
Task 3
1. #include <stdio.h>
2. int checkPrime(int n);
3. int main()
4. {
5. int n, i, flag = 0;
6.
7. printf("Enter a positive integer: ");
8. scanf("%d", n);
9.
10. for(i = 2; i <= n/2; i)
11. {
12. // condition for i to be a prime number
13. if (checkPrime(i) 1)
14. {
15. // condition for n-i to be a prime number
16. if (checkPrime(n-i) == 1)
17. {
18. // n = primeNumber1 + primeNumber2
19. printf("%d = %d + %d\n", n, i, n - i);
20. flag = 1;
21. }
22.
23. }
24. }
25.
26. if (== 0)
27. printf("%d cannot be expressed as the sum of two prime numbers.", n);
28.
29. return 0;
30. }
31.
32. // Function to check prime number
33. int checkPrime(int n)
34. {
35. int i, isPrime = 1;
36.
37. for(i = 2; i <= n/2; ++i)
38. {
39. if(n % i == 0)
40. {
41. isPrime = 0;
42. break;
43. }
44. }
45.
46. return isPrime;
47. }