0% found this document useful (0 votes)
2 views8 pages

Cts Java Questions

Uploaded by

jjegatha393
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Cts Java Questions

Uploaded by

jjegatha393
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROBLEM STATEMENT

PROBLEM 1:
You are managing a warehouse where cartons of goods have a
current weight of 0 kilograms. In each operation, you can perform
exactly one of the following changes to the weight of a carton:
 Either add 3 kilograms to the carton.
 Or multiply the carton's weight by 2 kilograms.
Your task is to find and return an integer value representing the
number of unique weights that the carton can have after
performing exactly Y operations on it.

Note:
 You cannot perform more than Y operations.
 The weight can go negative.

Input Specification:
 input1: An integer value Y representing the number of
operations that can be performed.

Output Specification:
Return an integer value representing the number of unique
weights that the carton can have after exactly Y operations.
Sample Input:
Y=2

Sample Output:
4

Explanation:
Starting with weight 0:
 Add 3: weight = 3
 Add 3 again: weight = 6
 Multiply by 2: weight = 0
 Add 3, then multiply by 2: weight = 6
Unique weights: {0, 3, 6} = 3 unique weights
PROBLEM 2:
Kate is given a string S of length N representing the arrangement
of two colors: R (Red) and B (Blue), representing color red and
blue respectively. She has to find the minimum number of non-
overlapping substrings that should be divided into such a way that
when the substrings are rearranged, S becomes sorted.
A sorted string is one where all the occurrences of B come before
all the occurrences of R.
Your task is to find and return an integer value representing the
minimum number of non-overlapping substrings needed to
achieve to make S sorted. If S is already sorted, return 1.

Input Specification:
 input1: An integer value N representing the length of string S
 input2: A string S consisting of characters R (Red) and B
(Blue).

Output Specification:
Return an integer value representing the minimum number of
non-overlapping substrings needed to achieve to make S sorted.
If S is already sorted, return 1.

Sample Input:
N=6
S = "BRRBR"
Sample Output:
3

Explanation:
String "BRRBR" needs to be split into 3 substrings to make it
sorted:
 Substring 1: "B"
 Substring 2: "RR"
 Substring 3: "BR" (rearranges to "RB") After rearrangement:
"BBBRRR" (all B's before all R's)
PROBLEM 3:

Alice has a robot that arranges cables represented by a binary string S


of length N. According to the new cable maintenance rules:

 Every time the robot arranges the cables with a '01' pattern, it
costs X rupees.

 If the robot arranges the cables with a '10' pattern, it costs Y


rupees.

Alice can rearrange the cables in any order to minimize the total cost.
Your task is to find and return an integer value representing the
minimum total cost that Alice has to pay.

Input Specification:

 input1: An integer N, representing the length of the string.

 input2: An integer X, representing the cost of '01' pattern.

 input3: An integer Y, representing the cost of '10' pattern.

 input4: A string value S, representing the given string.

Output Specification:

Return an integer value representing the minimum total cost that Alice
has to pay.
Sample Input:

N=4

X=5

Y=3

S = "0011"

Sample Output:
9

Explanation:
String "0011" has:
 Count of '0' = 2
 Count of '1' = 2
Possible arrangements:
 "0011": "01" appears twice = 2 * 5 = 10
 "0101": "01" appears twice = 2 * 5 = 10
 "1100": "10" appears once = 1 * 3 = 3, "00" appears once (no
cost) = 3
 "1010": "10" appears twice = 2 * 3 = 6, "01" appears once = 1
* 5 = 5, Total = 11
Minimum cost = 9 (arrangement: "0011" with optimal pattern
count)
PROBLEM 4:
You're at a school fundraiser where students have brought in jars
of coins to donate. Each jar contains a di erent amount, but
some students accidentally brought in the same amount. You
want to tally the total donation by only counting each unique coin
amount once.
You are given the number of coins students have brought in jars,
in the form of an array. Find and return the sum of all the distinct
coin values to see how much the school raised!

Input Specification:
 input1: An integer array representing the number of coins in
jars
 input2: An integer representing the number of students

Output Specification:
Return the sum of all the distinct coin values to see how much the
school raised.

Sample Input:
input1: {1, 2, 3, 2}
input2: 4

Sample Output:
6
Explanation:
Distinct coin amounts: {1, 2, 3} Sum of distinct values = 1 + 2 + 3 =
6

You might also like