67% found this document useful (3 votes)
2K views71 pages

New CTS Sample Questions With Solutions

The document describes a game conducted during a school summer vacation outing where students are given an integer and must rearrange the digits in increasing order from right to left. It provides an input/output sample where the integer 231 is rearranged to 321. A Java code solution is given that takes the integer as input, converts it to a string, sorts the characters in increasing order, and converts it back to an integer for output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
2K views71 pages

New CTS Sample Questions With Solutions

The document describes a game conducted during a school summer vacation outing where students are given an integer and must rearrange the digits in increasing order from right to left. It provides an input/output sample where the integer 231 is rearranged to 321. A Java code solution is given that takes the integer as input, converts it to a string, sorts the characters in increasing order, and converts it back to an integer for output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Mock1

Model: https://www.geeksforgeeks.org/find-two-numbers-with-sum-and-product-both-same-as-n/
The Two Numbers
Ram and Mohan are two brothers. They are not good in Maths. So their father decided to give some
assignment as a game so that they can enjoy as well as they can learn the concept of number
systems. So he gave two numbers to them. One is the sum of two number, x and y, and another is
the product of same two numbers. Help them to write a code to find x and y.
Note:
The two numbers should be printed in ascending order.
Input format
The input contains two integers in the same line separated by space that denotes the sum of x and y
and the product of s and y respectively.
Output format
The output consists of two numbers separated by space which corresponds to x and y in ascending
order.
Sample input 1:
56
Sample output 1:
23

Sample input 2:
15 50
Sample output 2:
5 10

Solution:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int x,y;
x=in.nextInt();
y=in.nextInt();
int r1,r2;
r2=(int)(x+Math.sqrt(x*x-4*y))/2;
r1=x-r2;
System.out.println(r1+" "+r2);
}
}
Skill testing
Amar’s team is assigned with a project that deals with the digital signals. He is aware that the digital
signals will be represented as arrays. He wants to test his team with respect to their strength in
arrays. As part of the skill testing the task is as follows:
You are given an array of integers of size N.
Consider all its contiguous subarrays of length k and find the maximum sum.
Do this for all k from 1 to the length of the input array.
Write a program to evaluate the task done by the kids.
Input format:
The first line contains a single integer denotes the size of the array N.
The second line contains N-space separated integer that corresponds to the values of the array.
Output format:
The output consists of N-space separated integer that corresponds to the values of the array.
Sample input 1:
5
-1 2 1 3 -2
Sample output 1:
34653

Explanation:
For inputArray=[-1,2,1,3,-2], the output should be [3,2,1,0,0].
The contiguous subarray
Of k=1,each subarray will have 1 element, the sub-array with maximum sum is [3]. So result[0]=3.
Of k=2,each subarray will have 2 elements, the sub-array with maximum sum is [1,3]. So result[1]=4.
Of k=3,each subarray will have 3 elements, the sub-array with maximum sum is [2,1,3]. So
result[2]=6.
Of k=4,each subarray will have 4 elements, the sub-array with maximum sum is [-1,2,1,3]. So
result[4]=5.
Of k=5,each subarray will have 5 elements, the sub-array with maximum sum is [-1,2,1,3,-2]. So
result[5]=3.

Sample input 2:
5
2 3 2 -2 3
Sample output 2:
35768
Solution:

//Skill testing
/*
Sample input 1:
5
-1 2 1 3 -2
Sample output 1:
3 4 6 5 3

Sample input 2:
5
2 3 2 -2 3
Sample output 2:
3 5 7 6 8
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
int i, j, k;
int sas = 0;
int[] sum1=new int[n];
int[] sum2=new int[n];
int ind=1;
for (i = 0; i < n; i++)
a[i] = sc.nextInt();

for (i = 0; i < n; i++) {


sum2 = new int[n];
if (sas == 0) {
for (j = 0; j < n; j++) {
if(sum1[i]<a[j])sum1[i]=a[j];
}
//System.out.println(sas);
//for(int e:sum1)System.out.print(e+" ");
}else {//if(sas<=5)
int sp=0;
int ep=(sp+1 + sas>n)?n:sp+sas;
//System.out.println("\n\nsas="+sas);
for (j = 0; j < n; j++) {
int sum=0;
for (k = sp; k <= ep; k++) {
sum+=a[k];
}
sum2[j]=sum;
sp=(j+1==n-1)?Integer.MAX_VALUE:j+1;//(sp==n-1)?
99:sp+1;
ep=(sp+sas>=n-1)?n-1:sp+sas;
ep=ep;
}
//for(int e:sum2)System.out.print(e+" ");
//System.out.print("\ni="+i);
for(int e:sum2) {
if(sum1[i]<e)sum1[i]=e;
}
//System.out.println();
//for(int e:sum1)System.out.print(e+" ");
//System.out.println();
}
sas++;
}
//System.out.println(sum1.length);
for(int e:sum1)System.out.print(e+" ");
}
}
Anuska’s instruction
Anuska has created a robot that will take instructions and work accordingly. Due to budget problem,
she has set the memory low. To overcome that, she has to encode the instruction. Given the
instruction s, count the consecutive characters and each character will be followed by its frequency.
For example, if the instruction is “aaaabb”, the encoded instruction is “a4b2”.
Note:
If the frequency is one, the count need not be printed.
Input format:
The first input is a string that denotes the instruction, s.
Output format:
Output is a string that denotes the encoded string.
Sample input 1:
aaaabb
Sample output 1:
a4b2

sample input 2:
sggvvvgss
sample output 2:
sg2v3gs2
Solution:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next(),res="";
int c=1;
for(int i=0;i<str.length()-1;){
c=1;
for(int j=i+1;j<str.length();j++){
if(str.charAt(i)==str.charAt(j)){
c++;
i++;
}
else {
res=res+str.charAt(i)+c;
i=j;
break;
}
}
}
res=res+str.charAt(str.length()-1)+c;
res=res.replace("1","");
System.out.println(res);

}
}

Team Assessment
The Team Leader of Testing team of XYZ company decided to assess the team members by
organising a programming event. So he gave the team a task.
The task is given an array of size , find the number of distinct elements in the array and print them in
ascending order. Those members who answer, will get some reward.
Write a program to help the Team Leader to evaluate the solution of the team members.
Input format:
The first line contains s single integer that denotes the size of the array, N.
The second line contains N space separated integer values of the array.
Output format:
The first line is an integer that denotes the number of the distinct elements in the array.
The second line is a series of integers separated by space that denotes the distinct elements
Sample input 1:
5
12121
Sample output 1:
2
12
Sample input 2:
5
145 25 21 25 36
Sample output 2:
4
21 25 36 145
Solution:
//Team Assessment
/*
Sample input 1:
5
1 2 1 2 1
Sample output 1:
2
1 2
Sample input 2:
5
145 25 21 25 36
Sample output 2:
4
21 25 36 145

*/
import java.util.*;
public class Main2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
String res="";
for(int i=0;i<n;i++) {
String s=String.valueOf(a[i]);
if(res.indexOf(s)==-1)res+=a[i]+" ";
}
String[] r=res.split(" ");
System.out.println(r.length);
int[] f=new int[r.length];
for(int i=0;i<r.length;i++)f[i]=new Integer(r[i]);
for(int i=0;i<r.length-1;i++) {
for(int j=i+1;j<r.length;j++) {
if(f[i]>f[j]) {
int t=f[i];
f[i]=f[j];
f[j]=t;
}
}
}
for(int i=0;i<r.length;i++)System.out.print(f[i]+" ");
}
}

Summer Vacation Game


In summer vacation time, the students of ABC school went for a team outing. A game was conducted
by the Incharge for the student so that they can enjoy. So he told them to take one integer N and
arrange the digits of the integer in incremental order from right to left. Help students by writing a
code.
Input format:
The first line contains s single integer that denotes the given number, N.
Output format:
The output is an integer in which the digits of N are arranged in incremental order from right to left.
Sample input 1:
231
Sample output 1:
321
Solution:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int nd=0;
int nn=n;
while(nn>0){
if(nn/10>0)nd++;
nn=nn/10;
}
int[] d=new int[++nd];
int ind=0;
while(n>0){
d[ind++]=n%10;
n=n/10;
}
for(int i=0;i<nd-1;i++){
for(int j=i+1;j<nd;j++){
if(d[i]<=d[j]){
int t=d[i];
d[i]=d[j];
d[j]=t;
}
}
}
for(int e:d)System.out.print(e);
}
}

Picnic Event
As part of the picnic events, the families from ABC apartment plan to organize same games for their
kids so that they can learn something during leisure time also. The task is as follows:
Given a string S, find the string which has only the distinct character in S. the characters should be
presented in the descending order of its occurrence. If the occurrence count of two characters is
same, then the order for those two characters should be the same as its is in the input string.
Input Format:
The input is a string that denotes the S.
Note:
The String is case-sensitive.
Output format:
The output is a string that denotes the modified string.
Sample input 1:
HelloWorld
Sample output 1:
loHeWrd

sample input 2:
Entertainment
Sample Output 2:
nteEraim

Solution
//Picnic Event
/*
Sample input 1:
HelloWorld
Sample output 1:
loHeWrd

sample input 2:
Entertainment
Sample Output 2:
nteEraim

*/
import java.util.*;
public class Main3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2="",s3="",f="";
int n=s1.length();

//find duplicate elements


for(int i=0;i<n;i++) {
boolean isDuplicate=false;
for(int j=i+1;j<n;j++) {
if(s1.charAt(i)==s1.charAt(j)) {
isDuplicate=true;
}
}
if(isDuplicate&&s2.indexOf(s1.charAt(i))==-1)s2+=s1.charAt(i);
}
//System.out.println(s2);

//order distint elements in descending order of occurence


int[] a=new int[s2.length()];
int ind=0;
for(char ch1:s2.toCharArray()) {
int c=0;
for(char ch2:s1.toCharArray()) {
if(ch1==ch2)c++;
}
a[ind++]=c;
f=f+ch1+c;
}

for(int i=0;i<s1.length()-1;i++) {
for(int j=i+1;j<s2.length();j++) {
if(a[i]<a[j]) {
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
//System.out.println(f);
//Entertainment
for(int i=0;i<a.length;i++) {
String ch1=String.valueOf(a[i]);
int j=0;
while(j<f.length()) {
if(ch1.equals(f.charAt(j)+"")) {
System.out.print(f.charAt(j-1));
String rs=f.charAt(j-1)+""+f.charAt(j)+"";
f=f.replace(rs, "");
break;
}
j++;
}
}
//System.out.println();
//find distinct elements

for(int i=0;i<n;i++) {
boolean isDuplicate=false;
for(int j=0;j<n;j++) {
if(i==j)continue;
if(s1.charAt(i)==s1.charAt(j)) {
isDuplicate=true;
break;
}
}
if(!isDuplicate)s3+=s1.charAt(i);
}
System.out.println(s3);
}
}
Mock 2

Practical Exam Preparation


Vel is a first year computer science student. The next day he is having his practical’s lab exam. His
sister (Priya) is working in a top IT firm. To test vel, she gives him a problem to solve. The problem is
to find the N-th special number, given he value of N. she also defines a number to be special, if it’s
units place digit only consist of 0,1,2,3,4 or 5.
Write a program to help vel.
Input format:
First input is an integer that denotes the N value.
Output format:
Output is an integer that denotes the N-th special number.
Sample input 1:
5
Sample output 1
4
Explanation:
Here N=5
The first 5 special numbers are 0,1,2,3 and 4. So the output is 4.

Sample input 2:
60
Sample output 2:
95
Explanation:
Here N=60.
The first 60 special numbers are as follows:
0,1,2,3,4,5,10,11,12,13,14,15,20,21,22,23,24,25,30,31,32,33,34,35,40,41,42,43,44,45,50,51,52,53,54
,55,60,61,62,63,64,65,70,71,72,73,74,75,80,81,82,83,84,85,90,91,92,93,94 and 95
So the output is 95.
Solution:

//Practical Exam Preparation


/*
Sample input 1:
5
Sample output 1
4
Sample input 2:
60
Sample output 2:
95

*/
import java.util.*;

public class Main4 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
int num = 0;
for (int i = 0; i < n; i++) {
boolean flag=true;
while (flag) {
if (num % 10 == 0 || num % 10 == 1 || num % 10 == 2 ||
num % 10 == 3 || num % 10 == 4 || num % 10 == 5) {
a[i] = num++;
flag=false;
}
else {
num++;
}
}
}
//for (int i : a)System.out.print(i + " ");
//System.out.println();
System.out.println(a[n - 1]);
}
}

Sai’s Programming Skills


th
Sai is doing his 5 grade in VIBGYOR International School. He has already started to do programming.
Priya, being a programming geeks, tests her son’s knowledge by asking him to code for her
requirement. The requirement is as follows:
Given two arrays A and B each containing N numbers, choose exactly one number from A and
exactly one number from B such that the index of the two chosen numbers is not same and the sum
of the 2 chosen values is minimum. Formally, if you choose ith element from A whose value is x and
jth element from B whose value is y, you need to minimize the value of (x+y) such that i is not equal
to j.
Help Sai in writing the program.
Input format:
First line of the input is an integer that denotes the N value, size of the array.
Second line of the input is a series of n integers separated by a space that denotes the array values
of A.
Third line of the input is a series of n integers separated by a space that denotes the array values of
B.
Assume:
All the elements in each array are distinct.
Output format:
Output is an integer that denotes the minimum value as per the requirement.
Sample input 1:
5
12345
54321
Sample output 1:
2
Explanation:
Here x is 1 (the first element of A) and y is also 1 the last element of B). their sum is 2.
Sample input 2:
6
2 5 4 8 6 96
4 5 6 9 8 10
Sample output 2:
7
Explanation
Here x is 2(the first element of A) at index 0 and y is 4 at index 0. Since the index of j is also the same,
we choose the next smallest element which is 5 at index 1.
Solution:
//Sai’s Programming Skills
/*
Sample input 1:
5
1 2 3 4 5
5 4 3 2 1
Sample output 1:
2

Sample input 2:
6
2 5 4 8 6 96
4 5 6 9 8 10
Sample output 2:
7

*/
import java.util.*;
public class Main5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
int[] b=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
for(int i=0;i<n;i++)b[i]=sc.nextInt();
int min1=Integer.MAX_VALUE,min2=Integer.MAX_VALUE,ind1=-1,ind2=-1;
for(int i=0;i<n;i++) {
if(min1>a[i]) {
min1=a[i];
ind1=i;
}
}
for(int i=0;i<n;i++) {
if(min2>b[i]&&i!=ind1) {
min2=b[i];
ind2=i;
}
}
// System.out.printf("min1=%d,ind1=%d,min2=%d,ind2=
%d\n",min1,ind1,min2,ind2);
System.out.println(min1+min2);
}
}

Bangles gift
Vel bought 2 bangles for his elder sister Priya, as a birthday gift. Before presenting the gift, vel
challenges his sister to answer his question blindfolded. He places both the bangles on the table and
asks priya to answer two questions:

1. Are the two bangles intersecting or touching or not touching?


2. What is the distance between the centers of the bangles?
To answer these questions, Vel gives the centers c1(x1,y1) and c2(x2,y2) and radius r1 and r2 of the
bangles.
Now write a program to help priya to answer her brother’s questions.
Input format:
First line of the input consists of two integers separated by a space which corresponds to x1 and y1
values, respectively.
Second line of the input consists of two integers separated by a space which corresponds to x2 and
y2 values, respectively.
Third line of the input consists of two integers separated by a space which corresponds to r1 and r2
values, respectively.
Output format:
First line of the output is a string, “intersects” or “Touches” or “Do not Touch”.
Second line of the output is a float value which corresponds to the distance between the two centers
of the bangles, rounded off to 2 decimal points.
Sample input 1:
34
14 18
58
Sample output 1:
Do not Touch
17.80
Sample input 2:
34
46
45
Sample output 2:
Intersects
2.24
Solution:
package Mock2;

//Bangles gift
/*
Sample input 1:
3 4
14 18
5 8
Sample output 1:
Do not Touch
17.80
Sample input 2:
3 4
4 6
4 5
Sample output 2:
Intersects
2.24

*/
import java.util.*;

public class Main1 {


static int circle(int x1, int y1, int x2, int y2, int r1, int r2) {
int distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
int radSumSq = (r1 + r2) * (r1 + r2);
if (distSq == radSumSq)
return 1;
else if (distSq > radSumSq)
return -1;
else
return 0;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt(), y1 = sc.nextInt(), x2 = sc.nextInt(), y2 =
sc.nextInt(), r1 = sc.nextInt(),
r2 = sc.nextInt();
int t = circle(x1, y1, x2, y2, r1, r2);
if (t == 1)
System.out.println("Touches");
else if (t < 0)
System.out.println("Do not Touch");
else
System.out.println("Intersects");
int distSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
int radSumSq = (r1 + r2) * (r1 + r2);
System.out.printf("%.2f",Math.sqrt(distSq));
}

Choclolate box
Priya returned to India from the onsite trip to Singapore. She bought lots of chocolates for his son sai
and younger brother vel. The chocolate box is square shaped and contains identical compartments.
Each compartment contains several number of chocolates. Now comes the fight between sai and vel
for the chocolates. To stop the fight, priya gives a condition to both of them that, the compartments
in the upper triangular part of the box are for sai and that of the lower triangular part is for vel, and
the compartments in the main diagonal will be shared by priya and her husband.
Now help sai and vel to find how many chocolates did each get, by writing a program.
Note:
In this problem, upper triangle consists of elements above the main diagonal, and the lower triangle
consists of elements below the main diagonal.
Input format:
First input is an integer that denotes the N value, size of the box. The range of n is [1 – 15], both
inclusive.
Next N lines of the input consists of N integers separated by space in each line, that denotes the
number of chocolates in each compartment.
Output format:
Output is two integers separated by a space that denotes the number of chocolates for sai and
number of chocolates for vel respectively.
Note:
If N=1, there is no upper triangle and lower triangle. The single element present corresponds to the
main diagonal.
Sample input 1:
4
1234
8596
1 2 3 15
5716
Sample output 1:
39 24
Solution:
//Choclolate box
/*
Sample input 1:
4
1 2 3 4
8 5 9 6
1 2 3 15
5 7 1 6
Sample output 1:
39 24

*/
package Mock2;

import java.util.*;

public class Main2 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
int up=0,low=0;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
up+=a[i][j];
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
low+=a[i][j];
}
}
System.out.print(up+" "+low);
}

Reorder the students


Sai is doing his 5th grade in VIBGYOR international school. He is the most intelligent student in his
class, and his class teacher has made him the representative for his class. Now its play time. There
are n students in his class and each student is given a card that has a number written in it. Sai, being
the representative, has to reorder those kids as per the order given to him.
Help sai in reordering the kids, by writing a program.
Input format:
First input is an integer that denotes the n value, the number of students.
Second input is a series of n integers separated by a space that denotes the numbers given to each
student.
Third input is a series of n integers separated by a space that denotes the array which contains the
order in which the students have to be reordered.
Assume:
The elements of the 2nd array are unique and always greater than or equal to 0 and less than n.
Output format:
Output is a series of n integers separated by a space that denotes the reordered array.
Sample input 1:
3
14 15 16
120
Sample output 1:
16 14 15
Explanation:
The first array: 16 14 15
The order array: 1 2 0
Here the first element 16 should be moved to index 1.
The second element 14 should be moved to index 2.
The third element 15 should be moved to index 0.
So, after moving, the final order is 16 14 15
Sample input 2:
5
12345
12034
Sample output 2:
31245
Solution:
//Reorder the students
/*
Sample input 1:
3
14 15 16
1 2 0
Sample output 1:
16 14 15
Sample input 2:
5
1 2 3 4 5
1 2 0 3 4
Sample output 2:
3 1 2 4 5

*/
package Mock2;
import java.util.*;
public class Main3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
int[] p=new int[n];
int[] r=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
for(int i=0;i<n;i++)p[i]=sc.nextInt();
for(int i=0;i<n;i++) {
int ind=p[i];
r[ind]=a[i];
}
for(int i:r)System.out.print(i+" ");
}

Sliding number
nd
Priya’s son sai is doing his 2 grade. He started learning about number. To test his intelligence, priya
asks him to solve a problem. The problem is to find the number of sliding numbers within a given
range. A number is called sliding number if all adjacent digits have an absolute difference of 1.
For e.g. ‘432’ is a sliding number while 542 is not.
Write a program to help sai to find the number of sliding number in range [m,n](both inclusive)
Input format:
First input is an integer that denotes the m value.
Second input is an integer that denotes the n value.
Assume:
n>M>=10
output format:
output is an integer that denotes the number of sliding numbers.
Sample input 1:
10
25
Sample output 1:
4
Explanation:
Sliding numbers are 10 12 21 23
Sample input 2:
10
16
Sample output 2:
2
Solution:
//Sliding number
/*
Sample input 1:
10
25
Sample output 1:
4
Explanation:
Sliding numbers are 10 12 21 23
Sample input 2:
10
16
Sample output 2:
2

*/
package Mock2;

import java.util.*;

public class Main4 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(), n = sc.nextInt();
int c = 0;
for (int i = m; i <= n; i++) {
int x = i, dc = 0;
while (x > 0) {
dc++;
int d1 = x % 10;
x = x / 10;
}
int[] a = new int[dc];
x = i;
for (int j = 0; j < dc; j++) {
a[j] = x % 10;
x = x / 10;
}
boolean flag = true;
for (int j = 0; j < dc - 1; j++) {
if (Math.abs(a[j] - a[j + 1]) != 1) {
flag = false;
break;
}
}
if(flag)c++;
}
System.out.println(c);
}

Mock 3
Boat travel
You’re travelling in boat, the water currents sometimes they slow you down. You need to calculate
the time to reach. Knowing the boat speed and water current speed.
A boat moves at a constant speed of x kmph without water flow. the water flow also affects the
speed of the boat.
Given the ideal boat speed and the water flow speed, write a program to find the time required to
reach a given distance.
Input format: :
First LIne of the input contains a single integer value that denotes the ideal boat speed in kmph.
Second Line of the input contains a single integer value that denotes the water flow speed in
kmph(+ve when it favors the boat and -ve when it disfavours.
Third Line of the input contains a single integer value that denotes the distance to be covered in km.
Output format::
A single integer value that denotes the time taken to reach the given distance.
Assume::
The time taken will always be an integer.
sample input 1::
20
5
100
sample output 1::
4
sample input 2::
25
-5
100
Solution:
//Boat travel
/*
sample input 1::
20
5
100
sample output 1::
4
sample input 2::
25
-5
100
sample output2:
5
*/
package Mock3;
import java.util.*;
public class Main1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt(),y=sc.nextInt(),z=sc.nextInt();
System.out.println(z/(x+y));
}

Bigger Number
Suriya loves playing with numbers. He comes up with a new game idea of creating a greater number
than the given number by changing exactly one digit. Now he has to find the number of ways in
which this is possible. Help Suriya to develop a program to find the number of ways to obtain a
bigger number.
input format::
Input contains a single integer that denotes the given number.
output format::
the output contains a single integer that denotes the number of ways.
sample input 1::
10
sample output1 ::
17

sample input 2::


32310
sample output 2::
36
Explanation:
First digit 3 can be changed into 4,5,6,7,8,9 so 6 posiibilities
second dight 2 can be changed into 3,4,5,6,7,8,9 sp 7 possibilities
Third digit 3 can be changed into 4,5,6,7,8,9 so 6 posiibilities
Fourth digit 1 can be changed into 2,3,4,5,6,7,8,9 so 8 posiibilities
Fifth digit 0 can be changed into 1,2,3,4,5,6,7,8,9 so 9 posiibilities
Hence Result=6+7+6+8+9=36
Solution:
//Bigger Number
/*
sample input 1::
10
sample output1 ::
17

sample input 2::


32310
sample output 2::
36

*/
package Mock3;
import java.util.*;
public class Main2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int s=0;
while(n>0) {
int d=n%10;
s=s+(9-d);
n=n/10;
}
System.out.println(s);
}

--------------------------------------------------------------------------------------------------------------------------------------
Greatest common prime divisor
Sapna has learnt about prime numbers and Greatest common divisor. Now shw experiments by
finding the greatest prime divisor.
Help her develop a program by combining her knowledge in both the concepts to find the greatest
common prime divisor.
Given two numbers, write a program to find the greatest common prime divisor.
Note:
If there is not such numbers, print -1.
input format:
First Line of the input contains a single integer that denotes the second number.
Second Line of the input contains a single integer that denotes the second number.
Ouptut format:
The output contains a single integer that denotes the gcpd of the two inputs.
sample input 1:
12
18
sample output 1:
3
Solution:
//Greatest common prime divisor
/*
sample input 1:
12
18
sample output 1:
3
*/
package Mock3;

import java.util.*;

public class Main3 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt(), n2 = sc.nextInt(),gcpd=-1;
int sn = (n1 < n2) ? n1 : n2;
for (int i = 2; i < n1; i++) {
boolean isPrime = true;
for (int j = 3; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if(isPrime&&n1%i==0&&n2%i==0)gcpd=i;
}
System.out.println(gcpd);
}
}

--------------------------------------------------------------------------------------------------------------------------------------
Exchange positions in array
Nikitha is very interested in programming with arrays, she tries to exchange the positions of the
elements by swapping the elements and the number at the place which it occupies.
For Example,
Consider the array elements.
1342
The element 1 in the given array, is at position 1, so 1 is places at position 1 in output array.
The element 3 in the given array, is at position 2, so 2 is places at position 3 in output array.
The element 4 in the given array, is at position 3, so 3 is places at position 4 in output array.
The element 2 in the given array, is at position 4, so 4 is places at position 2 in output array.
The output array becomes 1,4,2,3

Develop a program to help Nikitha.

Input format::
First Line of the input contains a single integer that denotes the size of the array n.
Second line consists of n space seperated integer array values. The input array values will always be
a permutation of numbers from 1 to n.

output format::
the output consists of n space separated integers that denote the output array values.
sample input 1:
4
1342
Sample output 1:
1423
Solution:
//Exchange positions in array
/*
sample input 1:
4
1 3 4 2
Sample output 1:
1 4 2 3

*/
package Mock3;
import java.util.*;
public class Main4 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int[] r=new int[n];
for(int i=0;i<n;i++) {
int pos=a[i];
r[pos-1]=i+1;
}
for(int e:r)System.out.print(e+" ");
}

--------------------------------------------------------------------------------------------------------------------------------------
Diagonal Difference
Roshan just found out that matrix can be implemented using 2d arrays. He is good in programming.
So he tries to develop a program to do his math homework. Help Roshan to write a program to do
his math homework.
In his math homework, a square matrix of size N is given. He needs to calculate the absolute
difference between the sums of the diagonals.
Help Roshan by writing a program.
Input Format:
The first line contains a single integer denotes the size of the matrix, 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 a single integer.
sample input 1:
3
11 2 4
456
10 8 -12
Sample Output 1:
15
Explanations:
The primary diagonal elements are:
11 5 -2
Sum across the primary diagonal: 11+5-12=4
Sum across the seconday diagonal: 10+5+4=19
Absolute difference=4-19=15
Solution:
//Diagonal Difference
/*
sample input 1:
3
11 2 4
4 5 6
10 8 -12
Sample Output 1:
15

*/
package Mock3;

import java.util.*;

public class Main5 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
int pd = 0, sd = 0;
for (int i = 0; i < n; i++) {
pd+= a[i][i];
}
int le=n-1;
for(int i=0;i<n;i++) {
sd+=a[i][le--];
}
System.out.println(Math.abs(pd-sd));
}

--------------------------------------------------------------------------------------------------------------------------------------
Power Pairs
Your friend challenges you to play a game, in which you have to find the number of Power pairs in
the series of numbers. A Power pair is formed when a number b is divisible by another number a
that occurs before the number b in the given sequence. you can easily win the challenge by
developing a program to find it.
Input format:
first line of the input contains a single integer that denotes the size of the array n.
second line consists of n space separated integers that denote the array values.
output format:
The output consists a single integer that denotes the number of power pairs in the given sequence.
sample input 1:
3
132
sample output 1:
2
Explanation:
for sequence=[1 3 2]
the sorted pairs are:(1,3),(1,2).
so output should be 2.
Solution:
//Power Pairs
/*
sample input 1:
3
1 3 2
sample output 1:
2

*/
package a;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
int c=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(i==j)continue;
if(a[j]%a[i]==0)++c;
}
}
System.out.println(c);
}

--------------------------------------------------------------------------------------------------------------------------------------
Main1:
COST FOR SPIRAL BINDING
Your final year project papers has to be spiralled (spiral binding). The cost for spiralling depends on
the thickness of the spiral. If thickness is <= 1 cm
the cost is Rs.50, if thickness>1cm and <=2cm, the cost is Rs.100. If thickness>2cm and <=3cm, the
cost is Rs.150. For each 1cm increase in spiral thickness, it costs Rs.50 additionally. A total of 2-
sheets will give a thickness of 1mm. Given the number of sheets, write a program to find the cost of
spiral.
Input format:
first line consists of a single integer that denotes the number of sheets to be spiralled.
output format:
output is an integer that denotes the cost for spiralling.
sample input 1:
225
sample output 1:
100
sample input 2:
488
sample output 2:
150
explanation:
for sample input 1→ 225 sheets has to be spiraled. 225 sheets will make around 1.125cm, since it is
less than 2 cm and greater than 1 cm, it costs Rs. 100.
for sample input 2 → 488 sheets has to be spiraled, 488 sheets will make around 2.44cm, since it is
less than 3cm and greater than 2cm, it costs Rs.150.
solution:
//COST FOR SPIRAL BINDING
/*
sample input 1:
225
sample output 1:
100
sample input 2:
488
sample output 2:
150
*/
package a;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
double th=(double)n/2*0.01;
System.out.println(Math.ceil(th));
int r=(int)Math.ceil(th)*50;
System.out.println(r);
}

--------------------------------------------------------------------------------------------------------------------------------------
Free Fall
A ball drops a ball from the top of the building of height h. Write a program to find the velocity at
which it hits the ground.
Formula:
Equations of motion, v2=u2+2gh.
Note:
Assume Air in the atmosphere is not affecting the speed. Take g=9.8m/s2.
input format:
first input is an integer that denotes the height of the building in meteres.
output format:
Output is a double vlaue that denotes the velocity of the ball at which it hits the ground.(Rounded
off to two decimal places)
sample input 1:
45
sample output 1:
29.70
sample input 2:
15
sample output 2:
17.15
Explanation:
For sample input 1 → Height of the building - 45m. From equations of motion formula we can find
the velocity at which the ball will hit the ground. At the instant when the ball is dropped, its initial
velocity is u=0,, so v2=0+2*9.8*45=√882=29.70
Solution:
//Free Fall
/*
sample input 1:
45
sample output 1:
29.70
sample input 2:
15
sample output 2:
17.15
v2=u2+2gh.
*/
package a;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int h=sc.nextInt();
double v=Math.sqrt(2*h*9.8);
System.out.printf("%.2f",v);
}

--------------------------------------------------------------------------------------------------------------------------------------
Parallel Resistors
In electronic circuits, resistors are used to reduce current flow and adjust signal levels. Resistors can
be connected either in series or in parallel. Given N resistors which are connected in parallel and
their respective resistance values, write a program to find the total resistance.
Formula:
1/R=1/R1+1/R2+1/R3+...+1/Rn
Input format:
first input is an integer that denotes the number of resistors N.
second line consists of series of n integers separated by a space that denotes the resistance values.
output format:
output is a double value that denotes the total resistance.(Rounded off to two decimal places)
sample input 1:
2
12 12
sample output 1:
6.00
sample input 2:
5
12345
sample output 2:
0.44
explanations:
for sample input 1-->2 resistors are connected parallely and their resistance value is 12 and 12. The
formula to find total resistance for the parallel connection is 1/R=1/R1+1/R2+1/R3+...+1/Rn.
the net resistance is, 1/R=1/12+1/12=2/12=⅙. therefore to find R, take reciprocal.
the total resistance is R=6.00
solution:
//Parallel Resistors
/*
sample input 1:
2
12 12
sample output 1:
6.00
sample input 2:
5
1 2 3 4 5
sample output 2:
0.44
*/
package a;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
double d=0;
for(int i=0;i<n;i++) {
d+=a[i];
}
double nu=0;
for(int i=0;i<n;i++) {
nu=nu+(d/a[i]);
}
System.out.printf("%.2f",d/nu);
}

--------------------------------------------------------------------------------------------------------------------------------------
AWESOME CHARACTERS
Raghu has twin sons and they are studying in 8th grade. To test their knowledge, he gave them a fun
task to play with. The task is to find the awesome characters. Two characters x and y are said to be
awesome characters if absolute difference between their ascii values produce a character that is an
uppercase alphabet. Given x and y values as inputs, write a program to find whether the characters
are awesome characters or not and also print the difference in the next line.
Input format:
first line consists of a two space separated characters that denotes x and y.
output format:
output is of two lines.
1) print “Awesome Characters’ if absolute difference between their ascii values produce a
character that is a uppercase alphabet, else print “Normal Characters”.
2) also print the absolute difference between their ascii values.
sample input 1:
AZ
sample output 1:
Normal Characters
25
sample input 1:
v!
sample output 2:
Awesome Characters
85
Explanation:
For sample input 1-->the ascii value for A is 65 and Z is 90. Their absolute difference is 25
and their difference in ascii values did not produce an uppercase alphabet. So A and Z are
normal characters.
For sample input 1-->the ascii value for v is 118 and ! is 33. Their absolute difference is
85and their difference in ascii values produces an uppercase alphabet. So v and ! are
Awesomel characters.
Solution:
//AWESOME CHARACTERS
/*
sample input 1:
A Z
sample output 1:
Normal Characters
25
sample input 1:
v !
sample output 2:
Awesome Characters
85
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
char ch1=sc.next().charAt(0),ch2=sc.next().charAt(0);
int n1=ch1,n2=ch2;
int n=n1+n2;
char ch=(char)n;
if(Character.isUpperCase(ch))
System.out.print("Normal Characters\n"+Math.abs(n1-n2));
else
System.out.print("Awesome Characters\n"+Math.abs(n1-n2));
}

---------------------------------------------------------------------------------------------------------------------------
COST OF TABLE
A rectangular table has 4 semi-circle on all the 4 edges and the semicircle portion is cut from
the table. Given the length and width of the rectangular table in cm, radius of the semicircle,
find the area of the table and if the area is greater than 20000 sqcm, the table will be
painted, else the table will be given a wooden finish.
Given the cost for painting and wooden finish per sq.cm, write a program to find the total
cost of the table.
Note:
Area of semi-circle=(3.14*r*r)/2
input format:
first line consists of s single integer that denotes the length of the table.
second line consists of a single integer that denotes the width of the table.
third line consists of a single integer that denotes the radius of the semi-circle.
next line consists of a single double value that denotes the cost for painting per sq cm.
last line consists of single double value that denotes the cost for wooden finish per sq cm.
output format:
output is a double value that denotes the total cost of the table. (Rounded off to two
decimal places)
sample input 1::
15
17
3
102.3
142.2
sample output 1:
28223.86
sample input 2:
100
70
20
12
11
sample output 2:
53856.00
Solution:
//COST OF TABLE
/*
sample input 1::
15
17
3
102.3
142.2
sample output 1:
28223.86
sample input 2:
100
70
20
12
11
sample output 2:
53856.00
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int l=sc.nextInt(),w=sc.nextInt(),r=sc.nextInt();
double p=sc.nextDouble(),wf=sc.nextDouble();
double ar=0,asc=0,ta,res=0;
ar=l*w;
asc=((3.14*r*r)/2)*4;
ta=ar-asc;
if(ta>2000)res=ta*p;
else res=ta*wf;
System.out.printf("%.2f",res);
}

---------------------------------------------------------------------------------------------------------------------------
7 SEGMENT LED DISPLAY
A seven segment display (SSD_, or seven segment indicator, is a form of electrnic device for
displaying decimal in digital format. kTow single digi numbers a and b are given as inputs,
initially, the number a is displayed in the 7 segment LED display. write a pprogram to
determine how many segments has to be alered (either urn on or turn off) to change from a
to b.
Input ormat:
first input is an integer that denotes the first number a
second input is an integer that denotes the second number b
output format:
output is an integer that denotes the number of segments to be changed.
sample input 1:
6
1
sample output 1:
6
sample input 2:
4
5
sample output 2:
3
explanation:
For sample input 1→ first number 6 is displayed in 7 segment display., it takes 6 segments
(a,f,e,d,c,g) to display. Number 1 takes 2 segments (b,c) to display. So no change from 6 to 1
(a,f,g,e,d) has to be turned off and (b) has to be turned on. Hence a total of 6 segments
needs to be changed.
For sample input 2→ first number 4 is displayed in 7 segment display, it takes 4 segments
(f,g,b,e) to display Number 5 takes 5 segments (a,f,g,c,d) to display. so to change from 4 to
(b) has to be turned of and (a,d) has to he turned on,(f,g and c) are already turned on, so
they need not be considered. Hence a total of 3 segments (b-->off and a,d-->on) needs to be
changed.
Solution:
//7 SEGMENT LED DISPLAY
/*
sample input 1:
6
1
sample output 1:
6
sample input 2:
4
5
sample output 2:
3
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] tt=
{"0000001","1001111","001001","0000110","1001100","0100100","0100000",
"0001111","0000000","0000100","0001000","1100000","0110
001","1000010",
"0110000","0111000"};
int n1=sc.nextInt(),n2=sc.nextInt(),c=0;
for(int i=0;i<7;i++) {
if(tt[n1].charAt(i)!=tt[n2].charAt(i))c++;
}
System.out.println(c);
}

---------------------------------------------------------------------------------------------------------------------------
Main 2
LUCKY CHAIR
Rohan and his friends were playing a game. there are n number of chairs arranged both row
wise and column wise. Each chair is moved with number at the back. Each person has to find
the lucky chair. The chair which is present in the row with maximum sum and column with
the minimum sum is the lucky chair. Given a matrix a, write a program to find the a[x][y],
where x is the row with max sum and y is the column with min sum.
Note:
consider 0 based index for rows and columns. if two rows or columns have same maximum
sum or minimum sum take the first occurring row or column.
input format:
first line consists of single integer that denotes the row size of the matrix.
second line consists of a single integer that denotes the column size of the matrix.
next line consists of series of integers separated by a space that denotes the array values.
output format:
output is an integer that denotes the a[x][y] value.
sample input 1:
3
3
123
456
789
sample output 1:
7
sample input 2:
4
2
10 20
41 25
15 136
145 2
sample output 2:
136
Solution:
//LUCKY CHAIR
/*
sample input 1:
3
3
1 2 3
4 5 6
7 8 9
sample output 1:
7
sample input 2:
4
2
10 20
41 25
15 136
145 2
sample output 2:
136
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt();
int[][] a=new int[n][n];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextInt();
}
}
int[] rs=new int[n];
int[] cs=new int[m];
int maxr=Integer.MIN_VALUE,minc=Integer.MAX_VALUE,i1=-1,j1=-1;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
rs[i]+=a[i][j];
}
}
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
cs[i]+=a[j][i];
}
}
for(int i=0;i<n;i++) {
if(maxr<rs[i]) {
maxr=rs[i];
i1=i;
}
}
for(int i=0;i<m;i++) {
if(minc>cs[i]) {
minc=cs[i];
j1=i;
}
}
System.out.println(a[i1][j1]);
}

--------------------------------------------------------------------------------------------------------------------------
TOTAL RUNS
Given the total runs, number of boudaries and sixes made by players of the cricket team,
write a program to calculate the total percentage of runs made by the players by running
between the wickets.
input format:
first line contains a single integer that denotes the total runs.
second line contains a single integer that denotes the number of boundaries
third line contains a single integer that denotes the number of sixes.
output format:
output contains a single integer denoting the the total percentage of runs made by running
between the wickets.
sample input 1:
100
8
5
sample output 1:
38
sample input 2:
125
5
8
sample output 2:
45
explanation:
for sample input 1→ total runs is 100, he scored 8 boundaries and 5 sizes. so the total runs
made by running between wickets is 100-(8*4+5*6)=38
total percentage of runs he made by running between the wickets is = (38/100)*100=38
solution:
//TOTAL RUNS
/*
sample input 1:
100
8
5
sample output 1:
38
sample input 2:
125
5
8
sample output 2:
45
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int r=sc.nextInt(),b=sc.nextInt(),s=sc.nextInt();
double tr=r-(b*4+s*6);
int rp=(int)(tr*100)/r;
System.out.println(rp);
}
}

---------------------------------------------------------------------------------------------------------------------

DIFFERENCE OF ELEMENTS AT INTERSECTION


Given a matrix of size a[r][c], write a program to print the difference between the elements at
maximum row maximum column intersection and minimum row and minimum column intersection.
input format:
first line contains a single integer denoting the row size
second line contains a single integer denoting the column size
next lines contains the matrix values
output format:
output contains a single integer denoting the difference at intersection elements.
sample input 1:
4
4
1234
5678
9 10 11 12
13 14 15 16
sample output 1:
15
sample input 2:
5
3
125
456
456
253
10 11 15
sample output 2:
14
explanation:
for sample input 1→ the element that is present at the intersection of maximum row and column is
16 and the element that is present at the intersection of minimum row and column is 1.
maximum row sum is=13+14+15+16, at index is 3
maximum column sum is=4+8+12+16, at index 3
so a[3][3]=16
minimum row sum is=1+2+3+4,at index 0.
minimum column sum is=1+5+9+13,at index 0
so a[0][0]=1.
Difference between them is 16-1=15.
Solution:
//DIFFERENCE OF ELEMENTS AT INTERSECTION
/*
sample input 1:
4
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
sample output 1:
15
sample input 2:
5
3
1 2 5
4 5 6
4 5 6
2 5 3
10 11 15
sample output 2:
14
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int r=sc.nextInt(),c=sc.nextInt();
int[][] a=new int[r][c];
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
a[i][j]=sc.nextInt();
}
}
int[] rs=new int[r];
int[] cs=new int[c];
int ri=-1,ci=-1,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
rs[i]+=a[i][j];
}
}
for(int i=0;i<c;i++) {
for(int j=0;j<r;j++) {
cs[i]+=a[j][i];
}
}

for(int i=0;i<r;i++) {
if(max<rs[i]) {
max=rs[i];
ri=i;
}
}
max=Integer.MIN_VALUE;
for(int i=0;i<c;i++) {
if(max<cs[i]) {
max=cs[i];
ci=i;
}
}
int n1=a[ri][ci];
ri=-1;ci=-1;
for(int i=0;i<r;i++) {
if(min>rs[i]) {
min=rs[i];
ri=i;
}
}
min=Integer.MAX_VALUE;
for(int i=0;i<c;i++) {
if(min>cs[i]) {
min=cs[i];
ci=i;
}
}
int n2=a[ri][ci];
System.out.println(n1-n2);
}
}

-------------------------------------------------------------------------------------------------------------------------------------
HIGHEST MULTIPLICATION NUMBER
Given a array of size n, write a program to find the maximum product where multiplication of two
numbers such that the result also exists in the array and find the highest multiplication number
present in the array.
Input format:
First input is an integer that denotes the size of the array, n.
Second line consists of series of integers separated by a space that denotes the array values.
Output format:
Output is an integer that denotes the highest multiplication number in the array.
Sample input 1:
8
2 4 5 35 6 7 12 30
Sample output 1:
35
Sample intput 2:
7
2 4 8 10 25 89 63
Sample output 2:
8
Explanation:
For sample input 1à there are totally 3 numbers where multiplication of two numbers whose result
is found in the array are: 2*6=12, 5*6=30, 5*7=35). Out of these, 35 is the highest multiplication
number present.
Solution:
//HIGHEST MULTIPLICATION NUMBER
/*
Sample input 1:
8
2 4 5 35 6 7 12 30
Sample output 1:
35
Sample intput 2:
7
2 4 8 10 25 89 63
Sample output 2:
8
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int max=Integer.MIN_VALUE;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(i==j)continue;
int p=a[i]*a[j];
boolean found=false;
for(int e:a) {
if(p==e) {
found=true;
break;
}
}
if(found&&max<p)max=p;
}
}
System.out.println(max);
}
}

--------------------------------------------------------------------------------------------------------------------------------------
ARRANGE ARRAY ELEMENTS
Given an array of size n with both positive and negative numbers. Write a program to print all the
negative values first followed by positive values without changing the order if user selects option 1,
else print all the positive values first followed by negative values without changing the order.
Input format:
First input is an integer that denotes the size of the array, n.
Second line consists of series of integers searated by a space that denotes the array values.
Thirs line consists of an integer that denotes the Boolean value x 1 or 0 for the option.
Output format:
Output consists of series of integers separated by a space that represents the arranged array values.
Sample input 1:
10
9 4 5 -8 -5 -4 -9 5 4 8
1
Sample output 1:
-8 -5 -4 -9 9 4 5 5 4 8
Sample input 2:
10
9 4 5 -8 -5 -4 -9 5 4 8
0
Sample output 1:
9 4 5 5 4 8 -8 -5 -4 -9
Explanation:
For smaple input 1àsince the value of x is 1, negative elements are arranged first followed by the
positive elements without changing the order.
Solution:
//ARRANGE ARRAY ELEMENTS
/*
Sample input 1:
10
9 4 5 -8 -5 -4 -9 5 4 8
1
Sample output 1:
-8 -5 -4 -9 9 4 5 5 4 8
Sample input 2:
10
9 4 5 -8 -5 -4 -9 5 4 8
0
Sample output 1:
9 4 5 5 4 8 -8 -5 -4 -9

*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int o=sc.nextInt();
if(o==0) {
for(int e:a) if(e>0)System.out.print(e+" ");
for(int e:a) if(e<0)System.out.print(e+" ");
}else if(o==1) {
for(int e:a) if(e<0)System.out.print(e+" ");
for(int e:a) if(e>0)System.out.print(e+" ");
}else {
System.out.println("Invalid option");
}
}
}

-------------------------------------------------------------------------------------------------------------------------------
DISTANCE TRAVELED
Write a program to calculate the speed at which distance the aeroplane must travel to reach the
destination, given the total speed of the aeroplane and the hours taken to reach the destination
currently and the hours taken to reach destination at a required rate.
Input format:
First line consists of a single integer that denotes the speed of all aeroplane
Second line consists of a single integer that denotes the hours at which the aeroplane currently
travels.
Third line consists of single integer that denotes the hour at which the aeroplane is expected to
travel to reach the destination.
Output format:
Output is a float value that denotes the speed at which the aeroplane must travel to reach the
destination.
Sample input 1:
240
5
6
Sample output 1:
200.00
Sample input 2:
320
2
6
Sample output 2:
106.67
Explanation:
For sample input 1à the distance covered by the aeroplane when travelling at a speed of 240km/h
for 5 hours =240*5=1200km. Hence in 6 hours the speed required by the plane is =1200/6=200.00
Note:
The result should be rounded to two decimal places.
Solution:
//DISTANCE TRAVELED
/*
Sample input 1:
240
5
6
Sample output 1:
200.00
Sample input 2:
320
2
6
Sample output 2:
106.67
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int s=sc.nextInt(),d1=sc.nextInt(),d2=sc.nextInt();
double d3=0;
d3=s*d1;
System.out.printf("%.2f",d3/d2);

}
}

-------------------------------------------------------------------------------------------------------------------------------------
Main 3
MIRROR IMAGE
Write a program to find the mirror image of the given number.
Input format:
First line consists of a single integer that denotes the n value.
Output format:
Output is an integer that denotes the mirror number of n.
Sample input 1:
97
Sample output 1:
67
Sample input 2:
125
Sample output 2:
95
Explanation:
For sample input 1àbinary value of 97 is 1100001.Mirror Image is the reverse of the binary value
which is 1000011 whose decimal value is 67
Solution:
//MIRROR IMAGE
/*
Sample input 1:
97
Sample output 1:
67
Sample input 2:
125
Sample output 2:
95

*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
StringBuffer s=new StringBuffer(Integer.toString(n,2));
s.reverse();
n=new Integer(s.toString());
System.out.println(Integer.valueOf(s.toString(),2));
}
}

---------------------------------------------------------------------------------------------------------------------------------
Allwin’s matrix
Allwin got an array for Christmas, now he plans to construct a matrix with it.
He decides to construct a square matrix.
The resultant square matrix is constructed considering the following points.
1. Side length equal to the size of the array.
2. Each element at index [i][j] is equal to the sum of elements present at indexes between I and
j(inclusive of index I and j)
For a given array, write a program to construct and print the formed matrix.
Note:
If i<j, element at [i][j]=a[i]+a[i+1]+….+a[j].
If i>j, element at [i][j]=a[j]+a[j+1]+….+a[1].
If i=j,element at [i][j]=a[i][j]
Input format:
The first line consists of a single integer that denotes the size of the array
The second line consists of n-space separated integers that denote the elements of the array
Output format:
The output consists of n lines each containing n space separated integers that denote the output
matrix.
Sample input 1:
3
123
Sample output 1:
136
325
653
Sample input 2:
2
45
Sample output 2:
49
95
Solution:

//Allwin’s matrix
/*
Sample input 1:
3
1 2 3
Sample output 1:
1 3 6
3 2 5
6 5 3
Sample input 2:
2
4 5
Sample output 2:
4 9
9 5
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
int[][] r=new int[n][n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(i==j) {
r[i][j]+=a[i];
}else if(i<j) {
for(int k=i;k<=j;k++)r[i][j]+=a[k];
}else {
for(int k=i;k>=j;k--)r[i][j]+=a[k];
}
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
System.out.print(r[i][j]+" ");
}
System.out.println();
}
}
}

--------------------------------------------------------------------------------------------------------------------------------------
Bike race
Ramesh challenges suresh for a bike race where the one who reaches maximum distance in a given
time t wins the race. Their bikes have an acceleration of a1 and a2 respectively.
You being a practical person convince both of them saying bike racing is a dangerous sport and write
a program to predict the outcome of the race.
Input format:
The first line consists of s single integer that denotes the acceleration a1 of ramesh’s bike.
The second line consists of s single integer that denotes the acceleration a2 of suresh’s bike.
The third line consists of a single integer that denotes the given time 1.
Note:
Acceleration unit is m/s2
Initial velocity is zero since the bikes will start from rest.
Laws of motion:
S=ut+1/2at2
Where,
s-distance
t-time
u-initial velocity
a-acceleration
input format:
the first line consists of a single integer that dentes the acceleration a1 of Ramesh’s bike.
The second line consists of fsingle integer that denotes the acceleration a2 of suresh’s bike.
The third line consists of a single integer that dentotes the given time t.
output format:
the first line consists of a single string that denotes the winner of the race(Ramesh/suresh).
The second line consists of a single integer that denotes the absolute difference between distance
covered by the racers in meter.
(or)
A single sring output ‘tie’ if the race ends in a tie.
Sample input 1:
8
10
50
Sample output 1:
Suresh
2500
Sample input 2:
12
12
4
Sample output 1:
Tie
Solution:

//Bike race
/*
Sample input 1:
8
10
50
Sample output 1:
Suresh
2500
Sample input 2:
12
12
4
Sample output 1:
tie
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a1=sc.nextInt(),a2=sc.nextInt(),t=sc.nextInt();
double te=(double)1/2;
double s=te*a1*t*t;
double r=te*a2*t*t;
if(s>r) {
System.out.println("Suresh");
System.out.println((int)Math.abs(s-r));
}else if(s<r) {
System.out.println("Ramesh");
System.out.println((int)Math.abs(s-r));
}else {
System.out.println("Tie");
}
}
}

------------------------------------------------------------------------------------------------------------------------------------
Heavyweight championship
In your company, you plan for a heavyweight championship match between juniors and seniors.
Everyone with experience greater than the given limit ‘t’ are considered as senior.
Given the experience and weights of everyone in your company, write a program to print the
winners.
Note:
The game will never end in a tie.
Input format:
The first line consists of a single integer that denotes the number of employees – n.
The second line consists of n space separated integers that denote the experience of the employees.
The third line consists of n space separated integers that denote the weights of the employees.
The fourth line consists of a single integer that denotes the experience limit ‘t’. any employee with
more that ‘t’ years experience is considered a senior.
Output format:
The first line consists of a single string that denotes the winners of the game(juniors/seniors)
The second line consists of s single integer that denotes the absolute difference between the
weights of the teams.
Sample input 1:
5
42387
62 44 84 48 68
3
Sample output 1:
Seniors
50
Sample input 2:
8
21423214
62 44 84 48 68 62 44 84
2
Sample output 2:
Juniors
24
Solution:

//Heavyweight championship
/*
Sample input 1:
5
4 2 3 8 7
62 44 84 48 68
3
Sample output 1:
Seniors
50
Sample input 2:
8
2 1 4 2 3 2 1 4
62 44 84 48 68 62 44 84
2
Sample output 2:
Juniors
24
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
int[] b=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
for(int i=0;i<n;i++)b[i]=sc.nextInt();
int t=sc.nextInt();
int sw=0,jw=0;
for(int i=0;i<n;i++) {
if(a[i]>t)sw+=b[i];
else jw+=b[i];
}
if(sw>jw)
System.out.println("Seniors");
else
System.out.println("Juniors");
System.out.println(Math.abs(sw-jw));
}
}

-------------------------------------------------------------------------------------------------------------------------------------
Required run rate
There is a lag in displaying the required run rate of the ongoing cricket match. You plan to solve the
problem by calculating the required run rate using a program. Given the target runs required,
current runs, the total number of overs, and the number of completed overs. Write a program to
print the required run rate to win the match.
Input format:
The first line consists of a single integer that denotes the target runs required.
The second line consists of a single integer that denotes the current runs.
The third line consists of a single integer that consists of the total number of overs.
The fourth line consists of a single integer that consists of the number of completed overs.
Output format:
A single double value (rounded to 2 decimal places) that denotes the required run rate.
Sample input 1:
100
40
12
3
Sample output 1:
6.67
Sample input 2:
80
40
10
2
Sample output 2:
5.00

Solution:

//Required run rate


/*
Sample input 1:
100
40
12
3
Sample output 1:
5.67
Sample input 2:
80
40
10
2
Sample output 2:
5.00
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tr =
sc.nextInt(),cr=sc.nextInt(),to=sc.nextInt(),co=sc.nextInt();
double rr=(double)(tr-cr)/(double)(to-co);
System.out.printf("%.2f",rr);
}
}
Fun with house numbers
A boy is walking a long way from school to his home. To make the walk more fun he decides to add
up all the numbers of the houses that he passes by during his walk. He is regularly taking turns to
change streets, so the numbers don’t appear to him in any particular order.
Whenever he comes across a house with a house number divisible by 3, he multiplies the house
number with 3 and adds the product to the total.
At some point during the walk, the boy encounters a house with a negative number written on it,
which surprises him so much that he stops adding numbers to his total after seeing that house.
For the given sequence of houses write a program to print the sum that the boy will get.
Input format:
The first line consists of a single integer that denotes the number of houses n.
The second line consists of n space separated integers that denote the house numbers.
Output format:
A single integer that denotes the sum that the boy will get.
Sample input 1:
6
123456
Sample output 1:
39

Solution:

//Fun with house numbers


/*
Sample input 1:
6
1 2 3 4 5 6
Sample output 1:
39
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int s=0;
for(int i=0;i<n;i++) {
int num=a[i];
if(num%3==0)num=num*3;
s+=num;
}
System.out.println(s);
}
}

Main 4
open doors
there are n doors in a row, all doors are initially closed. A person walks through all doors n times and
toggle (if open then close, if close then opens) them in the following way:
in the first walk, the person toggles every door
in the seond walk, the person toggles eery second door, ie. 2 nd,4th,6th,8th.
In the third walk, the person toggles every third door, ie. 3 rd,tth,9th.
In nth walk, the person toggles nth door.
Write a program to print all the open doors in ascending order.
Note:
The door numbers start from 1 to n.
Input format:
The first line consists of a single integer n that denotes the number of doors and number of walks
taken.
Output format:
The output consists of multiple lines each containing a single integer that denotes the door numbers
in ascending order.
Sample input 1:
50
Sample output 1:
1
4
9
16
25
36
49
Sample input 2:
30
Sample output 2:
1
4
9
16
25
Solution:
//open doors
/*
Sample input 1:
50
Sample output 1:
1
4
9
16
25
36
49
Sample input 2:
30
Sample output 2:
1
4
9
16
25
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=1;i<=n;i++) {
for(int j=0;j<n;j+=i) {
if(a[j]==1)a[j]=0;
else a[j]=1;
}
}
for(int i=1;i<n;i++)if(a[i]==1)System.out.println(i);
}
}
Volume of watermelon skin
Watermelon is in the shape of sphere. It has a thick skin. Given the radius of the entire fruit and the
radius of the fruit without the skin, write a program to find the volume of the skin alone.
Note:
Volume of spere=4/3*3.14*r3
Input format:
First line consists of a single double value that denotes the radius of the netire fruit.
Second line consists of a single double value that denotes the radius of the fruit without the skin.
Output format:
Output is a double value that denotes the volume of the skin alone.(Rounded off to two decimal
places)
Sample input 1:
15.2
12.6
Sample output 1:
63277.86
Sample input 2:
10.7
7.6
Sample output 2:
3291.00

Solution:
//Volume of watermelon skin
/*
Sample input 1:
15.2
12.6
Sample output 1:
63277.86
Sample input 2:
10.7
7.6
Sample output 2:
3291.00
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double r1=sc.nextDouble(),r2=sc.nextDouble();
double t=(double)4/3;
double v1=t*3.14*Math.pow(r1, 3),v2=t*3.14*Math.pow(r2, 3);
System.out.printf("%.2f",v1-v2);
}
}

Number of chocolates
There are n students standing in a circle. Starting from the first student, a ball is passed m times in
circular manner. The lucky student who has the ball after m passes will get m+index number+is roll
number amount of chocolates.
Given the total number of students n, their corresponding roll numbers and m, write a program to
find the number of chocolates the lucky student will get.
Input format:
First line consists of a single integer that denotes the number of students n.
Second line consists of series of integers separated by a space that denotes the roll numbers.
Third line consists of a single integer that denotes the m value.
Output format:
Output is an integer that denotes the number of chocolates that the lucky student will get.
Sample input 1:
5
100 101 102 103 104
3
Sample output 1:
109
Sample input 2:
7
11 12 14 15 18 36 21
15
Sample output 2:
28
Explanation:
For sample input  students are standing in a circle, the ball is passed 3 times. Now the ball is with
3rd person(0 based index) with register number 103. Since he is the lucky student, he would get 109
chocolates(103 register number)+3(m value)+3(index).
For sample input 2: 7 students are standing in a circle, the ball is passed 15 times. Now the ball is
with 1st person(0based index) with register number 12. Since he is the lucky student, he would get 28
chocolates(12 register number+15 m value+1 index).
Solution:
//Number of chocolates
/*
Sample input 1:
5
100 101 102 103 104
3
Sample output 1:
109
Sample input 2:
7
11 12 14 15 18 36 21
15
Sample output 2:
28
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int m=sc.nextInt();
int v1=0,ind1=-1,ind2=0;
while(v1<m) {
ind1=ind2++;
if(ind2>n-1)ind2=0;
v1++;
}
int res=a[ind2]+m+ind2;
System.out.println(res);
}
}

Special Matrix
Ram needs our help to solve a 2d array problem for him. Given a 2d square matrix, write a program
to find whether the given matrix is special or not.
Matrix is special if sum of upper triangular elements=sum of lower triangular elements=sum of
diagonal elements. Also if it is a special matrix print the sum else print the sum of upper triangle
elements, lower triangle elements and diagonal elements in the same line separated by a space.
Note:
Upper triangular elements are the elements that are present above the diagonal elements.
Lower triangular elements are the elements that are present below the diagonal elements.
Consider only left top to right bottom diagonal elements.
Input format:
First line consists of s single integer that denotes the size of the matrix n
The next n lines consist of a series of integers separated by a space that denotes the matrix values.
Output format:
Output is of two lines,
1) Print special matrix if sum of upper triangle elements, sum of lower triangle elements and sum
of diagonal elements are all equal, else print normal matrix
2) If special matrix print the sum else print the sum of upper triangle elements, lower triangle
elements and diagonal elements in the same line separated by a space.
Sample input 1:
4
1434
5678
9 10 15 12
13 1 0 16
sample output 1:
special matrix
38
sample input 2:
3
13 0 1
6 10 4
887
sample output 2:
normal matrix 5 22 30
solution:
//Special Matrix
/*
Sample input 1:
4
1 4 3 4
5 6 7 8
9 10 15 12
13 1 0 16
sample output 1:
special matrix
38
sample input 2:
3
13 0 1
6 10 4
8 8 7
sample output 2:
normal matrix 5 22 30
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[][] a=new int[n][n];
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
a[i][j]=sc.nextInt();
}
}
//sum of upper triangle
int sut=0;
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
sut+=a[i][j];
}
}
//sum of diagonal
int sd=0;
for(int i=0;i<n;i++)sd+=a[i][i];
//sum of lower triangle
int slt=0;
for(int i=1;i<n;i++) {
for(int j=0;j<i;j++) {
slt+=a[i][j];
}
}
if(sut==sd&&sut==slt) {
System.out.println("Special Matrix");
System.out.println(sut);
}else {
System.out.println("Normal Matrix");
System.out.println(sut+" "+slt+" "+sd);
}
}
}

Paper correction
Teachers get paid for correcting the students board exam papers.
1) If a teacher corrects<=100papers, he gets rs.2 per paper.
2) If he corrects>100 and <=200 papers, the total amount he gets for correcting all the papers is
derived by formula -(rs.2 for each paper that he corrects after 100 copies+rs.x)
3) If he corrects>200 and <=300 papers, the total amount he gets for correcting all the papers is
derived by formula -(rs.3 for each paper that he corrects after 200 copies+rs.2 * x)
4) If he corrects>300 and <=400 papers, the total amount he gets for correcting all the papers is
derived by formula -(rs.5 for each paper that he corrects after 300 copies+rs.3 * x)
5) If he corrects>400 and <=500 papers, the total amount he gets for correcting all the papers is
derived by formula -(rs.6 for each paper that he corrects after 400 copies+rs.4 * x)
6) If he corrects>500 papers, the total amount he gets for correcting all the papers is derived by
formula -(rs.10 for each paper that he corrects after 500 copies+rs.5 * x)
Given the number of papers that the teacher had corrected and the value for x, write a program to
calculate the amount he gets.
Input format:
First line consists of a single integer that denotes the number of papers that the teacher has
corrected.
Second line consists of a single integer that denotes the x value.
Output format:
Output is an integer that denotes total amount that the teacher would get.
Sample input 1:
100
20
Sample output 1:
200
Sample input 2:
248
150
Sample output 2:
444
Explanation:
For sample input 1: the teacher corrects 100 papers, he gets rs.2 for each paper he corrects so he
would get rs.200 for correcting all papers.
For sample input 2: the teacher corrects 248 papers, he gets rs.3 for each paper he corrects after 200
copies plus rs.2x, so he would get rs.444(48*3+2*150) for correcting all papers.
Solution:
//Paper correction
/*
Sample input 1:
100
20
Sample output 1:
200
Sample input 2:
248
150
Sample output 2:
444
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int np=sc.nextInt();
int x=sc.nextInt();
int res=0;
if(np<=100)res=np*2;
else if(np<=200)res=(np-100)*2+x;
else if(np<=300)res=(np-200)*3+2*x;
else if(np<=400)res=(np-300)*5+3*x;
else if(np<=500)res=(np-400)*6+4*x;
else res=(np-500)*10+5*x;
System.out.println(res);
}
}
Special odd array
Given an array of n integers, write a program to find whether the given array is special odd array or
not. Also print the number of special odd values.
Input format:
First line consists of a single integer that denotes the array size.
Second line consists of series of integers separated by a space hat denotes the array values.
Output format:
Output is of two line.
1) Print special odd array if there are odd number of odd digits in each of the elements in the array,
else print normal array.
2) Number of special odd values.
Sample input 1:
3
1 23 351
Sample output 1:
Special odd array
3
Sample input 2:
7
1 25 48 46 35 125 191
Sample output 2:
Normal array
3
Explanation:
For sample input 1: there are 3 elements in the array 1 23 351
First element : 1- has 1 odd digit 1
Second element: 23- has 1 odd digit 3
Third element: 351 has 3 odd digit 3, 5, 1
So its is a special odd array and there are 3 values that satisfies the condition.

For sample input 2: there are 7 elements in the array 1 25 48 46 35 125 191
First element: 1- has 1 odd digit 1
Second elemet: 25- has 1 odd digit 5
Last element : 191- has 3 odd digit 1 9 1
All other elements has even number of odd digits 35 125 or no odd digts at all 48 46, so its is
considered as normal array. Also there are only 3 values that satisfies the condition.
Solution:
//Special odd array
/*
Sample input 1:
3
1 23 351
Sample output 1:
Special odd array
3
Sample input 2:
7
1 25 48 46 35 125 191
Sample output 2:
Normal array
3
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
int[] r=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
for(int i=0;i<n;i++) {
int t=a[i],oc=0;
while(t>0) {
int d=t%10;
if(d%2!=0)oc++;
t=t/10;
}
r[i]=oc;
}
int toc=0;
for(int e:r) {
if(e%2!=0)toc++;
}
if(toc==n)System.out.println("Special Odd Array");
else System.out.println("Normal Array");
System.out.println(toc);
}
}
Main 5
Magic number
Sherlock holmes is trying to solve a puzzle that leads him to his next destination. He has a note that
contains two integers that specify the lower bound and the upper bound and says to find the
smallest number divisible by all the numbers in that interval. Help sherlock by developing a program
for him.
Note 1: both the upper bound and the lower bound are inclusive.
Note 2: always upper bound>=lower bound.
Input format:
The first line of the input contains single integer that denotes the lower bound
The second line of the input contains a single integer that denotes the upper bound.
Output format:
Output is an integer that denotes the smallest number divisible by all the numbers in the interval
between upper bound and the lower bound.
Sample input 1:
2
6
Sample output 1:
60
Explanation:
60 is the smallest number that is divisible by all 2 3 4 5 6
Sample input 2:
4
10
Sample output 2:
2520
Explanation:
2520 is the smallest number that is divisible by all 4 5 6 7 8 9 10
Solution:
//Magic number
/*
Sample input 1:
2
6
Sample output 1:
60
Explanation:
60 is the smallest number that is divisible by all 2 3 4 5 6
Sample input 2:
4
10
Sample output 2:
2520
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int lb=sc.nextInt(),ub=sc.nextInt();
int n=2;
while(n++>0) {
boolean flag=true;
for(int i=lb;i<=ub;i++) {
if(n%i!=0) {
flag=false;
break;
}
}
if(flag)break;
}
System.out.println(n);
}
}
The sticks
On summer vacation, jennier and her sister used to have ice-candy everyday. Jennifer’s sister used
to collect those ice sticks.
Now she has n ice sticks of different lengths. Jennifer’s sister wanted to play a game with those
sticks. So Jennifer suggested a game, that is, game of triangle.
In this game, there are n sticks of different lengths and one has to find the number of triangles that
can be constructed using any three of these sticks.
Constraints:
· The sum of the lengths of any two sides of a triangle must be greater that the third side.
· Equal triangles that are constructed from different sticks are considered different.
Input format:
First line contains a single integer that denotes the size of the array n.
Second line contains of n space separated integer values of the array that denote the stick lengths.
Output format:
A single integer output that denotes the number of triangles that can be formed.
Sample input 1:
4
3579
Sample output 1:
3
Explanation:
The triangles can be formed sing
3 5 7 – because 3+5>7 and 5+7>3 and 3.7>5
5 7 9 – because 5+7>9 and 7+9>5 and 9+5>7
3 7 9 – because 3+7>9 and 7+9>3 and 9+3>7
And 3 5 9 cannot be used to construct triangle because 3+5<9
Solution:
//The sticks
/*
Sample input 1:
4
3 5 7 9
Sample output 1:
3

*/
import java.util.*;
public class sticks
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int count=0;
for (int i = 0; i < n-2; ++i)
{
int k = i + 2;
for (int j = i+1; j < n; ++j)
{
while (k < n && a[i] + a[j] > a[k])
++k;
if(k>j)
count += k - j - 1;
}
}
System.out.println(count);
}
}

Matrix assignment
Ravi is a 6h grade student in ABC school. He had a lecture session on matrix subtraction and an
assignment to be solved.
Given two matrices, find the difference between each elements in the matrices and print the matrix.
If the dimensions of the two matrices are not matching, then print invalid.
Input format:
The first line contains a single integer denotes the row size of the first matrix, m.
The next line contains a single integer denotes the column size of the first matrix, n.
The next m lines denote the matrix’s rows, with each line containing n space-separated integers of
the second matrix.
Output format:
If the dimensions are matching, print m lines that denote the result matrix’s rows, with each line
containing n space separated integers of the difference matrix.
Else print invalid
Sample input 1:
3
3
100
400
033
3
3
123
986
342
Sample output 1:
0 -2 -3
-5 -8 -6
-3 -1 1
Solution:
//Matrix assignment
/*
Sample input 1:
3
3
1 0 0
4 0 0
0 3 3
3
3
1 2 3
9 8 6
3 4 2
Sample output 1:
0 -2 -3
-5 -8 -6
-3 -1 1
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r1=sc.nextInt(),c1=sc.nextInt();
int[][] a=new int[r1][c1];
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
a[i][j]=sc.nextInt();
}
}
int r2=sc.nextInt(),c2=sc.nextInt();
int[][] b=new int[r1][c1];
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
b[i][j]=sc.nextInt();
}
}
int[][] r=new int[r1][c1];
if(r1!=r2||c1!=c2) {
System.out.println("Invalid Matrix");
}else {
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
r[i][j]=a[i][j]-b[i][j];
}
}
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
System.out.print(r[i][j]+" ");
}
System.out.println();
}

}
}
}
Middle element
The middle of the array arr is defined as follows:
If arr contains odd number of element, its middle is the element whose index number is the same
when counting from the beginning of the array and from its end;
If arr contains an even number of elements, its middle is the average of the two elements whose
index numbers when counting from the beginning and from the end of the array differ by one.
When array arr, write a program to find its middle and if it consists of two elements, replace those
elements with the value of middle. Print the resulting array as the answer.
Note:
If the middle produces a floating point result, take its floor value.
Input format:
The first line contains a single integer that denotes the size of the array, n.
The second line consists of n space separated integer values of the array.
Output format:
Output consists of space separated integer values of the result array.
Sample input 1:
5
152 23 7 887 243
Sample output 1:
152 23 7 887 243
Explanation:
Here the number of elements is odd. So the middle is 7 and the result array is same as the input
array
Sample input 3:
6
7 2 2 5 10 7
Sample output 3:
7 2 3 10 7
Solution:
//Middle element
/*
Sample input 1:
5
152 23 7 887 243
Sample output 1:
152 23 7 887 243
Sample input 3:
6
7 2 2 5 10 7
Sample output 3:
7 2 3 10 7
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
if(n%2==0) {
for(int i=0;i<n;i++) {
if(i<(n/2)-1||(i>n/2))System.out.print(a[i]+" ");
if(i==(n/2)-1)System.out.print((a[i]+a[i+1])/2+" ");
}
}else {
for(int e:a)System.out.print(e+" ");
}
}
}
Find the century
John is an archaeologist, and he wants to categorize the artefacts that he found by their centuries.
He knows their year of origin, now he needs to find their century.
Given a year, write a program to print the century of its origin.
The first century spans from the year 1 up to and including the year 100, the second – from the year
101 up to and including the year 200, and so on.
Input format:
First line of the input contains a single integer that denotes the year of origin.
Output format:
Output is a single integer that denotes the century of origin.
Sample input 1:
1992
Sample output 1:
20
Sample input 2:
1300
Sample output 2:
13
Solution:
//Find the century
/*
Sample input 1:
1992
Sample output 1:
20
Sample input 2:
1300
Sample output 2:
13
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int r=n%100;
int q=n/100;
if(r>0)System.out.println(q+1);
else System.out.println(q);
}
}
Adult or juvenile
Ram is planning to go on a world tour. Before visiting the countries he wants to verify whether he’s
an adult as per that countries rules. Help him develop a program to check whether he is an adult or
juvenile using his age and the adulthood age in a particular country.
Note that people whose age is the same as the adulthood age are considered adult.
Input format:
The first line contains a single integer that corresponds to the age of ram.
The second line contains a single integer that corresponds to the adulthood age of a particular
country.
Output format:
The first line of the output is a string. Print adult, if ram satisfies the adulthood age, else print
juvenile.
The second line of the output is an integer that denotes the absolute difference between ram’s age
and adulthood age.
Sample input 1:
32
18
Sample output 1:
Adult
14
Sample input 2:
12
16
Sample output 2:
Juvenile
4
Solution:
//Adult or juvenile
/*
Sample input 1:
32
18
Sample output 1:
Adult
14
Sample input 2:
12
16
Sample output 2:
Juvenile
4

*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
if(a>=b)System.out.println("Adult\n"+Math.abs(a-b));
else System.out.println("Juvenile\n"+Math.abs(a-b));
}
}
Main Set6
1)group multiplication:
Given array of integers of size n and k value. Write a pgm to modify the array based on following
condition
1)split the array into groups each of size k and if the size of the last group is less than k, leave the
group as it is.
2)each element of the group is multiplied with the kth element and leave the kth element as it is
NOTE:
It is 1 –indexed based iteration
Input format:
First line contains single integer that denotes the size of the array
The Second line contains space separated integers values of the array
The third line contains a single integer that denotes the value of k.
Output format:
Output is a series of integers separated by space that denotes the array values.
SAMPLE INPUT 1:
10
1 2 3 4 5 6 7 8 9 10
3
SAMPLE OUTPUT 1:
3 6 3 24 30 6 63 72 9 10
EXPLANATION:
1)for first element [1 2 3] kth element is 3. So multiply 3 with each of the numbers in the array
Before the kth element and leave the kth element as it is. The 1 st set of number in the array would
look like [3 6 3]
2)for the second element [4 5 6 ] kth element is 6.so multiply 6 with each of the numbers in the
array before the kth element and leave the kth element as it is .the second set of numbers in the
array would look like [24 30 6].
3) for the third element [7 8 9 ] kth element is 9.so multiply 9 with each of the numbers in the array
before the kth element and leave the kth element as it is .the third set of numbers in the array
would look like [63 72 9].
4)for first iteration you have only one number which is less than size of k.so leave the number as it is
SAMPLE INPUT 2:
4
5 6 14 5
2
SAMPLE OUTPUT 2:
30 6 70 5
Solution:
/*
SAMPLE INPUT 1:
10
1 2 3 4 5 6 7 8 9 10
3
SAMPLE OUTPUT 1:
3 6 3 24 30 6 63 72 9 10

SAMPLE INPUT 2:
4
5 6 14 5
2
SAMPLE OUTPUT 2:
30 6 70 5

*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int k=sc.nextInt();
int gs=0,g=k;
for(int i=0;i<n;i+=k) {
for(int j=i;j<i+k&&j<n;j++) {
int t1=i+k-1;
if(j==t1||t1>n-1)
System.out.print(a[j]+" ");
else {
int t=a[i+k-1];
System.out.print(a[j]*t+" ");
}
}
}
}
}

2)The recruitment
Candidates apply for the vacant position in ABC company. Each candidate having his/her own
proficiency level (pl) which is represented as positive integer. Higher the proficiency level better his
the chance of recruitment
There are only n places in the team and the recruiter hires only those who have their pl. greater than
or equal to m.
All applicants are standing in a queue and if the current once satisfy the required condition , he is
taken into a team right away. Once the team is full all the remaining people are denied. Regardess of
their pl.
write a program to find the total pl of the applicants who get hired?
Input format:
First line contains single integer that denotes the size of the array
The Second line contains space separated integers values of the array
The third line contains a single integer that denotes the value of n.
The fourth line contains a single integer that denotes the value of m.
Output format:
A single integer value that denotes the total pl of applicants who get hired
Sample input1:
5
31213
2
2
Sample output 1:
5
Explanation:
The first applicant of the third applicant get selected with pl 3 and 2, respectively and total is 5.
Sample input 2:
7
4236254
3
4
Sample output 2:
15
Solution:
/*
Sample input1:
5
3 1 2 1 3
2
2
Sample output 1:
5
Sample input 2:
7
4 2 3 6 2 5 4
3
4
Sample output 2:
15

*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
int[] a=new int[s];
for(int i=0;i<s;i++)a[i]=sc.nextInt();
int n=sc.nextInt();
int m=sc.nextInt();
int sum=0,ts=0;
for(int i=0;i<s&&ts<n;i++) {
if(a[i]>=m) {
sum+=a[i];
ts++;
}
}
System.out.println(sum);
}
}

3) reduce the fraction


Ram is a 4 th grade kid studying in ABC international school, his maths teacher has given him
assignment on fraction. Give a fraction ,find the reduced form of that particular fraction.
Write a program to help ram
Input format:
First line contains single integer that denotes the numerator
The Second line contains single integer that denotes the denominator.
Output format:
If answer is whole number that output is a single integer that denotes the whole number.
It answer is mixed fraction after reducing the output is “whole number” space “numerator”
(slash)/”denominator” refer samples for more clarity.
If numerator is less than denominator, the output is “numerator” (slash)/”denominator”
If numerator is zero just print 0
If denominator is zero print invalid
Sample input 1:
11
9
Sample output 1:
12/9
Sample input 2:
4
2
Sample output 2:
2
Sample input 3:
3

9
Sample output 3:
1/3
Sample input 4:
0
9
Sample output 4:
0

Sample input 5:
3
0
Sample output 5:
Invaild.
Solution:

/*
Sample input 1:
11
9
Sample output 1:
12/9
Sample input 2:
4
2
Sample output 2:
2
Sample input 3:
3

9
Sample output 3:
1/3
Sample input 4:
0
9
Sample output 4:
0

Sample input 5:
3
0
Sample output 5:
Invaild.
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int div = sc.nextInt();
if (div != 0) {
int num = n / div;
int den = n % div;
if(den!=0)
System.out.println(num +""+den+"/"+div);
else
System.out.println(num);
}else {
System.out.println("Invalid5m");
}
}
}

4) mary”s belief
Once many year a famous song , and the line from it stuck in her head . that line was “will you still
love me when I am no longer young and beautiful?” . mary beliefs that a person is loved if and only if
he /she is both young and beautiful . but it is quite a depressing thought , so she wants to put her
belief to the test
Knowing whether a person is young, beautiful and loved , write a program to find out if they
contradict mary’s belief
The person contradict mary’s belief if one of the following is true.
They are young and beautiful but not loved
They are loved but not young or not beautiful
Input format:
First line contains single integer that denotes the whether the person is young ( 1-young, 0-not
young)
The Second line contains single integer that denotes the whether the person is beautiful ( 1-
beautiful, 0-not beautiful)
The third line contains a single integer that denotes the whether the person is loved( 1-loved, 0-not
loved)

Output format:
Print the person type in the first line and print whether it contradict or not in the second line.
Sample input1:
1
1
1
Sample output:
Young beautiful loved
No
Sample input 2:
0
0
1
Sample output 2:
Not young Not beautiful loved
Yes
Solution:

/*
Sample input1:
1
1
1
Sample output:
Young beautiful loved
No
Sample input 2:
0
0
1
Sample output 2:
Not young Not beautiful loved
Yes
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int y=sc.nextInt(),b=sc.nextInt(),l=sc.nextInt();
if((y==0||y==1)&&(b==0||b==1)&&(l==0||l==1)) {
System.out.print((y==1)?"Young ":"Not Young ");
System.out.print((b==1)?"Beautiful ":"Not Beautiful ");
System.out.print((l==1)?"Loved ":"Not Loved ");
System.out.println();
if((y==1&&b==1&&l==0)||(l==1&&(y==0||b==0)))
System.out.println("Yes");
else
System.out.println("No");
}
}
}
5)teaching maths
Ankit is teaching maths to the two 9 th class students vinay and sanjit . he is trying to explain the
theory of matrices , which is the collection of element which is arranged in rows and columns . ankit
gives one matrix each to both of them and ask them to add the respective elements of the matrices
then he explains that the resultant matrix is called the sum of the two matrix After adding the
respective element of the matrices .
Write a program to help vinay and sanjit to find the sum of the two matrix
Input format:
First line contains single integer that denotes the row size of the first matrix (m1)
The next line contains single integer that denotes the column size of the first matrix (n1)
The next “m1” line denotes the matrix row , with each line containing space separated integer to the
first matrix.
The next line contains the single integer denote the row size of the second matrix (m2)
The next line contains single integer that denotes the column size of the second matrix (n2)
The next “m2” line denotes the matrix row , with each line containing space separated integer to the
second matrix
Note: if the two matrix cannot be added print invalid.

Output format:
Each line denotes the sum of matrix &amp; row, each line containing space separated integer of the
sum of matrix
Sample Input 1:
3
3
100
100
032
3
3
123
986
340
Sample output 1:
223
11 6 8
372
Sample input 2:
2
3
112
054
3
2
97
01
82
Sample output 2:
Invalid
Solution:

/*
Sample Input 1:
3
3
1 0 0
1 0 0
0 3 2
3
3
1 2 3
9 8 6
3 4 0
Sample output 1:
2 2 3
10 8 6
3 7 2
Sample input 2:
2
3
1 1 2
0 5 4
3
2
9 7
0 1
8 2
Sample output 2:
Invalid

*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r1=sc.nextInt();
int c1=sc.nextInt();
int[][] a=new int[r1][c1];
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
a[i][j]=sc.nextInt();
}
}
int r2=sc.nextInt();
int c2=sc.nextInt();
int[][] b=new int[r2][c2];
for(int i=0;i<r2;i++) {
for(int j=0;j<c2;j++) {
b[i][j]=sc.nextInt();
}
}
int[][] c=new int[r1][c1];
if(r1==r2&&c1==c2) {
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
c[i][j]=a[i][j]+b[i][j];
}
}
}
if(r1==r2&&c1==c2) {
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("Invalid");
}
}

6) pile of boxes
(The pile of boxes can be constructed by placing boxes one on top of the other forming a vertical
column. It is not possible to place more than one box on the top of another one directly) each box
has a strength value – the number of boxes that it can hold without collapsing
Write a pgm to calculate the minimal number of piles that need to be constructed to use up all of
these boxes.
Input format:
1 st line contains a single integer value that denotes the number of boxes
2 nd line contains line containing space separated integer values that denotes the strength of each
box
Output format:

Maximum number of piles are needed


Sample input 1:
5
11212
Sample output 1:
1
Sample input 2:
6
421100
Sample output 2:
2
Explaination :
For [4 2 1 1 0 0 ] the output should be 2.it is possible to construct two piles[4,3,1,0] and [1,0]
Solution:

/*
Sample input 1:
5
3 1 2 1 3
Sample output 1:
2
Sample input 2:
6
4 2 1 1 0 0
Sample output 2:
2
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int zc=0,np=0;
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(a[i]<a[j]) {
int t=a[i];
a[i]=a[j];
a[j]=t;
}
if(a[i]==0)zc++;
}
}
int start=0,end=n-1;
if(zc==0) {
while(true) {
int ini=a[start++];
for(int j=1;j<=ini;j++)end--;
if(start>=n||end<0) {
np++;
break;
}else np++;
}
}else {
start=0;
end=n-1;
while(a[end]==0)end--;
while(true) {
int ini=a[start++];
for(int j=1;j<ini;j++)end--;
if(zc>0)zc--;
if(start>=n||end<0) {
np++;
break;
}else np++;
}
while(zc-->0)np++;
}
System.out.println(np);
}
}

You might also like