Java ST1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Coding questions

22 April 2023 23:35

Find Triplets
Given an array of distinct integers. The task is to count all the triplets such that the sum of
two elements equals the third element.
Sample Input 1:
4
2648
Sample Output 1:
2
Sample Input 2:
4
1357
Sample Output 2:
0
Input Explanation:
Line1: size of array
Line2: elements of array
Output Explanation:
Number of triplets that can be formed

Sample Input
4
1235

Sample Output
2

Sample Input
6
2 5 9 11 15 30

Sample Output
1

From <https://mymapit.in/#/test/310923918>
Number of Sticks
Mananjay has N sticks of K length. K being different or the same for every stick. He just
wants to know how many sticks of particular length L can he make using only two or only a
single stick(s) he has in stock. Two sticks or Single stick used to make the stick of particular
length L can not be used twice.

Sample Input 1:
5 13
4 9 1 32 13
Sample Output 1:
2
Sample Input 2:
3 11
21 1 9
Sample Output 2:
0
Input Explanation:
Line1:space separated number of sticks in stock N and Length L of the required stick
Line2: space separated length of sticks in stock.

Output Explanation:
Return number of sticks Mananjay can form using the sticks in stock..

Sample Input
44
0437

Sample Output
1

Sample Input
33
123

Sample Output
2

From <https://mymapit.in/#/test/310923918>
Smith Number
Smith Number
A Smith Number is a composite number, whose sum of digits is equal to the sum of digits of its prime
factors. It may be noted here that, 1 is not considered a prime number, hence it is not included in the sum
of digits of distinct prime factors.
Given a number n, determine whether it is a Smith Number or not.

Sample Input 1:
2
4
8
Sample Output 1:
Smith Number
Not Smith Number
Input Explanation:
Line 1: Number of Test Cases (N) followed by N subsequent lines of positive numbers
Output Explanation:
N lines returning if the given number is Smith Number or Not.

Sample Input
2
6
12

Sample Output
Not Smith Number
Not Smith Number

Sample Input
2
666
13

Sample Output
Smith Number
Smith Number

From <https://mymapit.in/#/test/310923918>
Sorted array with unsorted indices
Given an array of distinct integers. The task is to print the index of the largest number first
and then the index of the 2nd largest number and so on till the last one.
Note:
If two or more numbers are the same then print the index of the number which comes first in
the array.
Sample Input 1:
6
264826
Sample Output 1:
315204
Sample Input 2:
4
1375
Sample Output 2:
2310
Input Explanation:
Line1: size of array
Line2: elements of array
Output Explanation:
Index of largest element first and continue till the end of array.

Sample Input
4
1235

Sample Output
3210

Sample Input
6
2 11 9 11 15 2

Sample Output
413205

From <https://mymapit.in/#/test/310923918>
Highest Peak
Vishal went to Shimla for his summer gholidays. He saw 3-4 layers of mountains beautifully
spread infront of him. They looked all almost same from a distance. Vishal decided to find the
highest peak. He got the height of each mountain and fed them in an array. If the mountain was
not smaller than its neighbours he took it as the highest peak. For example:
Input:
7
10 20 15 2 010 90 67
Ouput:
20 or 90

Sample Input
4
2 6 18 14

Sample Output
18

Sample Input
8
0 1 2 4 9 7 5 10

Sample Output
10

From <https://mymapit.in/#/test/310923918>

Largest distance between magnets


Arun has K(>1) very strong magnets with him. He had a container and divided it into smaller
spaces using cardboard so that no two magnets get attracted towards each other and combine
to get destroyed from the edges. He placed some wood chips in some of the divisions he
made in the container. Now he has only N places to store the magnets which is sufficiently
more or equal to K magnets. The position of places which are empty are stored in an array.
Now, you need to place the magnets, such that the minimum distance between the magnets is
as large as possible. Return the largest minimum distance.
Sample Input 1:
64
0 3 4 7 10 9
Sample Output 1:
3
Sample Input 2:
52
42136
Sample Output 2:
5

Input Explanation:
Line1:space separated number of places empty and number of magnets
Line2: space separated position of empty places.

Output Explanation:
Largest minimum distance between the magnets.

Sample Input
63
0 4 3 7 10 9

Sample Output
4

Sample Input
32
123

Sample Output
2

From <https://mymapit.in/#/test/310923918>

Find Maximum
Given an array of integers which is initially increasing and then decreasing, find the
maximum value in the array.
Sample Input 1:
11
18 20 22 81 92 101 240 300 30 20 11
Sample Output 1:
300
Input Explanation:
Line 1: size of array
Line2: Elements of array
Output Explanation:
Maximum Number present in the array.

Sample Input
5
12321

Sample Output
3

Sample Input
6
134562

Sample Output
6

From <https://mymapit.in/#/test/310923918>

Kth prime factor


Given two numbers N and K, print the Kth prime factor among all prime factors of n. If there is no Kth
prime factor output should be -1.
Sample Input 1:
15 2
Sample Output 1:
5
Sample Input 2:
15 3
Sample Output 2:
-1
Input Explanation:
Space Separated N and K
Output Explanation:
Kth prime factor of N
Sample Input
225 2

Sample Output
3

Sample Input
81 5

Sample Output
-1

From <https://mymapit.in/#/test/310923918>

Aggressive Cows

Given an array of length ‘N’, where each element denotes the position of a 
stall. Now you have ‘N’ stalls and an integer ‘K’ which denotes the 
number of cows that are aggressive. To prevent the cows from hurting each 
other, you need to assign the cows to the stalls, such that the minimum 
distance between any two of them is as large as possible. Return the largest 
minimum distance.
Sample Input 1:
64
0 3 4 7 10 9
Sample Output 1:
3
Sample Input 2:
52
42136
Sample Output 2:
5

Input Explanation:
Line1:space separated number of places empty and number of cows
Line2: space separated position of empty places.

Output Explanation:
Largest minimum distance between the cows.
Sample Input
63
0 4 3 7 10 9

Sample Output
4

Sample Input
32
123

Sample Output
2

From <https://mymapit.in/#/test/310923918>

MotoGP
Varun enjoys watching MotoGP. He invited his friend over the weekend at his farm house
and asked his friend to pick a racer and see who wins. They decide to choose the racer such
that the race is close . This can happen only if the racers are comparable in their skill, i.e. the
difference in their skills is less.
There are N racers participating in the race. The skill of the racer k is represented by an
integer T[k]. Varun and his friend need to pick 2 racers for the race such that the difference
in their skills is minimum. This way, it will be a very interesting race. Your task is to help
him do this and report the minimum difference that is possible between 2 racers in the race.

Sample Input 1:
5
4 9 1 32 13
Sample Output 1:
3
Sample Input 2:
3
21 1 9
Sample Output 2:
8

Input Explanation:
Line1:Number of racers
Line2: space separated skills of each racer.

Output Explanation:
Minimum difference that is possible between the skills of racers.

Sample Input
3
0437

Sample Output
1

Sample Input
3
123

Sample Output
1

From <https://mymapit.in/#/test/310923918>

You might also like