Uploaded by SHANU SHARMA

BPIT Screening Test

advertisement
1 - Problem Title
Isuru Udana and prime
Problem Description
Isuru Udana, of the best competitive programmer of University of Colombo School of Computing .
He is currently doing a research Internship in california university.Isuru Udana is busy with his
research on prime numbers these days.Once, while roaming in the campus he saw numbers written
in a spiral form and he observed that many prime numbers occur on both the diagonals .
He wants to know what percentage of prime numbers is there on both the diagonals to that of all
the numbers on the diagonal. Your task is to write a program to help Isuru Udana.
__Note__: Size of matrix is always odd and it is a square matrix.
Input Format
First line of input contains an integer T, which is the number of test cases.<br>
Next T lines contains an integer which denotes size of the matrix (Size of above matrix is 7).
Output Format
Print the percentage of number of prime numbers on the diagonal to the total numbers on
diagonal, correct to six decimal places.
Sample TestCase Input
2
7
3
Sample TestCase Output
61.538462
60.000000
java Code
import java.util.Scanner;
public class Main {
public static boolean isPrime(long x) {
if (x % 2 == 0) {
return false;
}
for (long i = 3; i * i <= x; i += 2) {
if (x % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) throws Exception {
int j = 1;
int prime = 0;
int total = 1;// start at 1 ,total no of element in both diagonal = 1,
and 1 common element h both diagonal me
double[] dp = new double[10005];
dp[0] = 0;// dp[1] store answer for 3*3 matrix , dp [2] = store answer
for 5*5 matrix so on.......
int r = 2; // initially steps 2 then 4 6 soon ..........
for (int i = 1; i < 10005; ++i) {
for (int k = 0; k < 4; ++k) {
j += r;
if (isPrime(j)) {
prime++;
}
total++; // increment the total number of element of both
diagonal
}
r += 2;// steps increment by 2 for next dimension (3*3 to 5*5)
dp[i] = ((double) prime) / ((double) total) * 100.0; // check for 4
diagonal element in 3*3 matrix and store ans at dp[1]
}
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int s = sc.nextInt();
System.out.println(String.format("%.6f", dp[s/ 2]));
}
}
}
2 - Problem Title
Nikita and her boyfriend
Problem Description
Nikita is making a colour full Graph as a birthday gift for her boyfriend, a fellow programmer! She
has n integers. She wants to draw an undirected connected graph with N edge Each node is shaded
in either red or black color. We define nr to be the number of red nodes and nb to be the number of
black nodes. No 2 adjacent nodes have the same color.
<br><br>
Last Night before the birthday party, Nikita's mischievous little brother removed the last n-2
number. She is really disappointed because she lost the last n-2 number. She calls you because you
are her best friend. You suggest giving an array on the birthday. Nikita has only 2 numbers among
these n numbers. These numbers are the first 2 numbers in n number. If she gifts simply these 2
numbers as an array, then her boyfriend will be disappointed. Nikita never wants her boyfriend to be
disappointed at a birthday party. You suggest making it a magical array. property of magical:
The shortest array such that bitwise xor of its elements is the first number and the sum of its
elements is the second number . Can you help her to build this array Because you're her best friend
Input Format
The only line contains 2 numbers (which denote First and second number ).
Output Format
If there's no array that satisfies the condition, print the difference of 2 numbers(absolute difference
) Otherwise:print an integer n representing the length of the desired array. The next line should
contain n __positive__ integers, the array itself. Array must be Sorted
Sample TestCase Input
4 10
Sample TestCase Output
2
37
java Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long u = sc.nextLong();
long v = sc.nextLong();
if (u % 2 != v % 2 || u > v) {
System.out.println(Math.abs(u-v));
return;
}
if (u == v) {
if (u == 0) {
System.out.println(0);
} else {
System.out.println(1);
System.out.println(u);
}
return;
}
long x = (v - u) / 2;
if ((u & x)!=0) {
System.out.println(3);
long [] arr= new long [3];
arr[0]=x;
arr[1]=u;
arr[2]=x;
Arrays.sort(arr);
System.out.println(arr[0]+ " " + arr[1] + " " + arr[2]);
}
else {
System.out.println(2);
long [] arr= new long [2];
arr[0]=x;
arr[1]=(u ^ x) ;
Arrays.sort(arr);
System.out.println(arr[0]+ " " +arr[1]);
}
}
}
3 - Problem Title
Coronavirus - Help Italy
Problem Description
This Time is the coronavirus global pandemic. The number of coronavirus patients increases
day to day in Italy. Story of ATK Hospital, ATK's single deluxe room is designed to give
enough space for mobility to the Covid 19 patient and accommodation for one attendant
with attached bathing facilities. The ambience of the room is enhanced by the french
windows that allow abundant natural light. To make you feel more at ease, our single deluxe
rooms are additionally equipped with refrigerators, additional seating and a round table.
For example, if a doctor has expertise of 2(i.e, K=2), he will help recover the 1st patient in 2
seconds, 2nd patient in 4 seconds(2 * K) and 3rd patient in 6 seconds(3 * K) that means to
help recover 3 patients he needs a total of 12 seconds.
For the 4th patient he would need another(4 * K) seconds i.e 8 seconds, thus to help 4
patients recover the minimum time he would need would be 20 seconds and maximum time
needed would be 29 seconds.
Input Format
First line contains N, total Number of Covid 19 patients, In the next line the first integer
denotes the number of doctors M, and M Integer follow in the Next line each denoting
expertise value <b>( k)</b> of doctor
Output Format
Print minimum time needed by M doctors to make N Covid 19 patients healthy in ATK
hospital.
Sample TestCase Input
10
4
1234
Sample TestCase Output
12
java code:
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int nov = sc.nextInt();
int noc = sc.nextInt();
int cost[] = new int[noc];
for (int i = 0; i < cost.length; i++) {
cost[i] = sc.nextInt();
}
int ans = 0;
int lo = 0;
int hi = ((cost[cost.length - 1] * nov) + cost[cost.length - 1]) * (nov /
2);
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (isitsafe(nov, cost, mid)) {
hi = mid - 1;
ans = mid;
} else {
lo = mid + 1;
}
}
System.out.println(ans);
}
public static boolean isitsafe(int nov, int[] cost, int mid) {
int time = 0;
int mult = 1;
int i = 0;
int Get_Batches_vaccinated = 0;
while (time <= mid && i < cost.length) {
if (time + cost[i] * mult <= mid) {
time += cost[i] * mult;
Get_Batches_vaccinated++;
if (time < mid) {
mult++;
}
if (time > mid) {
time = 0;
}
} else {
i++;
mult = 1;
time = 0;
}
}
if (Get_Batches_vaccinated >= nov) {
return true;
} else {
return false;
}
}
}
4 - Problem Title
Friend and Game
Problem Description
Two friends were playing a mind game, the game was as follows :<br>
Both players will get 4-4 chances each, player 1 will always go first, both players takes turn
alternatively . They will roll the dice 8 times and note it, if even number comes that means
__Increasing__ else __Decreasing__, after 8 dice roll will start. so,the game is both players have to
build the string with non repeating digit(1-9), whose value is minimum possible and the follows the
relation . your task is to print final number.
For example, suppose after 2-2 turns they got relation as __Increasing__, __Decreasing__,
__Decreasing__, __Increasing__. In this case final String will contains value from 1 to 5
And will be holding this relation between each consecutive digit, we have to print such minimum
possible String. Here the answer will be __"14325"__.(1,4):__Increasing__, (4,3) :__Decreasing__,
(3,2):__Decreasing__,(2,5):__Increasing__
Input Format
First line contains T, denoting the number of test case <br>
Second line Contains String of length 8, representing value appears on dice after it is rolled.
Output Format
Print smallest possible number following the relation
Sample TestCase Input
1
25426251
Sample TestCase Output
132456987
JAVA Code
import java.util.*;
public class Main {
// Returns minimum number made from given sequence without repeating digits
static String getMinNumberForPattern(String seq)
{
int n = seq.length();
if (n >= 9)
return "-1";
char result[] = new char[n + 1];
int count = 1;
// The loop runs for each input character as well as
// one additional time for assigning rank to each remaining characters
for (int i = 0; i <= n; i++)
{
if (i == n || seq.charAt(i) == 'I')
{
for (int j = i - 1; j >= -1; j--)
{
result[j + 1] = (char) ((int) '0' + count++);
if (j >= 0 && seq.charAt(j) == 'I')
break;
}
}
}
return new String(result);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- >0){
String str = sc.next();
String s = "";
for (int i = 0; i < str.length(); i++) {
if (Character.getNumericValue(str.charAt(i)) % 2 == 0) {
s += "I";
} else {
s += "D";
}
}
str = s;
System.out.println(getMinNumberForPattern(str));
}
}
}
Download