Scaler Topics Fortnightly Contest - 16 Editorial

DSA Fortnightly - 16 Editorial
This article is part of the Scaler Topics Fortnightly Contest - 16
Water Supply
There are N taps in the city, which are connected such that if one tap is opened then all other taps will also automatically get open.
Now, you are given two integers A and B and an integer array C.
- A - Total amount of water poured out from all taps.
- B - The amount of water that needs to be poured out from the last tap.
- C - The outgoing hole size of each tap.
Now, you can block the holes of some taps so, that no water comes out from them. Now, the amount of water that comes out from each tap is directly proportional to the size of the hole of that tap. Like if the sum of the size of all tap holes which are open is X. Then the amount of water that comes out from each open tap will be (C[i]*A)/X.
Now, your task is to find the minimum number of tapes whose holes you have to close so that amount of water that came out from the last tap is at least B.
Problem Constraints
Input Format
- The first argument is an integer A.
- The second argument is an integer B.
- The third argument is an integer array C.
Output Format
- Return an integer.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Example
Example Input
Input 1:
Input 2:
Example Output
Output 1:
Output 2:
Example Explanation
Explanation 1:
Total amount of water poured from all taps is 10 and that has to come all out from last tap.
So, we have to close all other tap.
Explanation 2: Total amount of water poured from all taps is 80 and out of that atleast 20 has to come out from last tap. So, according to the formula given in problem if no tap hole is closed then. Water that came out from first tap will be (480)/10 = 32 Water that came out from second tap will be (180)/10 = 8 Water that came out from third tap will be (280)/10 = 16 Water that came out from fourth tap will be (380)/10 = 24 So, no need to close outgoing hole of any tap.
Hint
- It is clear that we have to block taps with greater outgoing hole first.
- So, we can sort the array C, excluding the last element because we have to keep that open.
Complete Solution
Maximum Sweetness
There are A number of students in a class and one of them is the representative of the class. A class representative has a bag of A chocolates where the sweetness of the ith chocolate is i (starting from 1).
He can choose atmost B chocolates from a bag of A chocolates to maximize the sweetness and here sweetness is not defined as the sum of the sweetness of chosen chocolates, it will be the XNOR of the sweetness of all the chocolates chosen. So, you have to output the maximum sweetness class representative can get.
Note - While calculating XNOR of two numbers X and Y, remember that you have to take XNOR only till the last set bit encountered in the largest integer of X and Y.
Example - if two integers are 5(101) and 2(10). Then their XNOR will be 0(000). The larger of 5 and 2 is 5 and the largest set bit in 5 is 4
Problem Constraints
Input Format
- First argument is an integer A, which represents number of chocolates in bag.
- Second argument is an integer B, which represents maximum number of chocolates class representative can choose.
Output Format
- Return an integer C, which represents maximum sweetness representative can get.
Example
Example Input
Input 1:
Input 2:
Example Output
Output 1:
Output 2:
Example Explanation
Explanation 1:
Class representative can take chocolates from type 1 to 10 and atmost 2 chocolates type he can choose.
But the maximum XNOR he will get by taking out chocolate of type 8 and 9 as their binary representation is 1000 and 1001
resepectively which will give XNOR as 14(1110). And in all other cases XNOR value will be either less than or equal to 14.
Explanation 2:
Class representative can take a maximum of 3 types, He will take only chocolate of type 7 as this will
give maximum XNOR value. Selecting more than one will not be the optimal choice here as that will never give more than 7 value
because in binary representation of 7 - (111) all bits are set and it is the largest type here.
Hint
- What would be the maximum sweetness if B = 1?
- What will be maximum sweetness if A=1 or A=2?
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Complete Solution
Good Number
You are given an integer A. You have to find the minimum good number not less than A. A good number is a positive number which has the same difference between the adjacent digits.
Problem Constraints
Input Format
- The first argument contains the integer A.
Output Format
- Return a single integer denoting the maximum number not less than A.
Example
Example Input
Input 1:
Input 2:
Example Output
Output 1:
Output 2:
Example Explanation
Explanation 1:
The nearest good number to 157 is 159 as 5-1=9-5.
Explanation 2: The nearest good number to 8 is 8.
Turn Learning into Career Growth
Hint
- Try brute forcing the solution.
- There are not many good numbers present, so explore all of them.
Complete Solution
XOR Subsequence
You are given an integer array A and an integer B. Return 1 if there exists a subsequence of this array such that XOR of all elements in the subsequence equals B else, return 0.
Problem Constraints
Input Format
- The first argument is an integer array A.
- The second argument is an integer B.
Output Format
- Return an integer.
Example
Example Input
Input 1:
Input 2:
Example Output
Output 1:
Output 2:
Example Explanation
Explanation 1:
We can take the subsequence [1, 9] as (1^9) = 8.
Explanation 2: There is no subsequence whose XOR equals 4.
Hint
- Think of dividing the arrays in two parts.
- Then use some property of xor.




