New CTS Sample Questions With Solutions
New CTS Sample Questions With Solutions
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.*;
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]+" ");
}
}
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();
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
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:
*/
import java.util.*;
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:
//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.*;
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.*;
*/
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.*;
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
*/
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.*;
--------------------------------------------------------------------------------------------------------------------------------------
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
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.*;
--------------------------------------------------------------------------------------------------------------------------------------
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);
}
}
---------------------------------------------------------------------------------------------------------------------
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.*;
--------------------------------------------------------------------------------------------------------------------------------------
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.*;
------------------------------------------------------------------------------------------------------------------------------------
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:
Solution:
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.*;
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.*;
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.*;
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.*;
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.*;
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.*;
*/
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.*;
}
}
}
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.*;
*/
import java.util.*;
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);
}
}
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.*;
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.*;
Output format:
Each line denotes the sum of matrix & 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.*;
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:
/*
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.*;