Scaler Topics Fortnightly Contest - 8 Editorial

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

This article is part of the Scaler Topics Fortnightly Contest - 8.

Weird Language

You visit a very special village which has a weird language. In their language the order of lowercase alphabets is decided by the frequency of letters in the special string A, incase of clash of frequencies, put in order according to english alphabets. If a letter does not appear in the special string its frequency is said to be 0.

You are given a string B and you need to return the string obtained after sorting B in decreasing order according to above ordering. Refer to samples for more clarity.

Problem Constraints

a<=A[i],B[i]<=z1<=A,B<=105\begin{aligned} 'a' <= A[i] , B[i] <= 'z' \\ 1 <= |A|, |B| <= 10^5 \\ \end{aligned}

Input Format

  • First agrument A is a string according to which alphabets are sorted.
  • Second agrument B is a string which needs to be sorted.

Output Format

  • Return a single string after sorting B according to above rules.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example

Example Input
Input 1:

Input 2:

Example Output
Output 1:

Output 2:

Example Explanation
Explanation 1: 'c' occurs most frequently (3 times) that's why it comes first then 'a' (2 times) , 'b' then 'x', 'y' & 'z' in their natural order as all have frequency equal to zero.

Explanation 2: Only 'z' is present in special string so it comes first and rest in their natural order.

Hint

  • Can you think of using a hashmap?
  • What can be the comparator function?

Complete Solution

Weird Language Complete Solution

Add Everything

Alice and Bob are playing a game. Alice will give a string A and a string B, both consisting of characters between (1 - 9). Bob can insert '+' anywhere in the string (possibly none or any number of times) and compute the values. Bob will compute all such possiblity and find their sum. If this is equal to the second string B given by Alice, then Alice wins, otherwise Bob wins. Help them to find out who wins.

Problem Constraints

1<=A<=101<=Ai<=9\begin{aligned} 1 <= |A| <= 10 \\ 1 <= Ai <= 9 \\ \end{aligned}

Input Format

  • The first argument is the String A.
  • The second argument is the String B.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Output Format

  • Return a string "Alice win" if Alice wins, "Bob win" otherwise.

Example

Example Input
Input 1:

Input 2:

Example Output
Output 1:

Output 2:

Example Explanation
Explanation 1: if A = 125 and B = 176:
The possiblities to insert '+' are 125,1+25,12+5,1+2+5125, 1 + 25, 12 + 5, 1 + 2 + 5.
Thus, the sum is 125+26+17+8=176125 + 26 + 17 + 8 = 176.
As this is equal to the String B, Alice wins.

Explanation 2:

The different possibilites are:
111,1+11,11+1,1+1+1=111+12+12+3=138.111, 1 + 11, 11 + 1, 1 + 1 + 1 = 111 + 12 + 12 + 3 = 138.
This is not equal to 145. Therefore, Bob wins.

Hint

  • For a number of length N, there are 2N - 1 different possible cases.
  • If we use binary representation, 0000, 0001, 0010.
  • Here, 0 represents absent and 1 represent present,which we can use to say whether '+' is present or absent.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Complete Solution

Add Everything Complete Solution

Dramatics Group

Ms. Malni wants to create groups of students for an Inter-School Dramatics competition. There are a total of N sections with Ai number of students in each section. Each group should have at least B number of students. There can be no more than C students in a group from the same section, i.e., no more than C students from the same section belong to the same group.

Note: It is possible that some students are not included in any group.

Help Ms. Malni find the maximum number of groups she could form. As the answer can be very large, return the remainder after diving the result with 109+7 .

Problem Constraints

1<=N<=1051<=Ai<=1051<=B<=Σ(Ai)1<=C<=105\begin{aligned} 1 <= N <= 10^5 \\ 1 <= Ai <= 10^5 \\ 1 <= B <= Σ(Ai) \\ 1 <= C <= 10^5 \\ \end{aligned}

Input Format

  • The first argument is an integer array A. Representing the number of students in each section.
  • The second argument is an integer B. Representing the minimum size of the group.
  • The third argument is an integer C. Representing that no more than C students can be in a group from the same section.

Output Format

  • Return an integer denoting the maximum number of groups that can be formed.

Example

Example Input
Input 1:

Input 2:

Example Output
Output 1:

Output 2:

Example Explanation
Explanation 1: Possible two groups are (1,1,1,2,2,2,3),(2,3,3,3,4,4,4){(1,1,1,2,2,2,3), (2,3,3,3,4,4,4)}

Explanation 2: Possible two groups are (1,1,1,2),(1,2,2,2){(1,1,1,2), (1,2,2,2)}.

Note:

  • The number represents the student's section in the above-given explanation.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Hint

  • If it is possible to create X number of groups, we can also make less than X groups.Hence we can use Binary Search.

Complete Solution

Dramatics Group Complete Solution

Empty Buckets

Mr. Naaya has N number of buckets, and each bucket has a volume given by Ai and the amount of water it holds given by Bi.

Mr. Naaya wants to get the maximum number of empty buckets. Moreover, he has to do so as soon as possible. Mr. Naaya takes x seconds to pour x units of water from one bucket to another.

A bucket can’t hold water greater than its volume; no water should be wasted.

Help Mr. Naaya find the maximum number of empty buckets he could get in the minimum possible time.

Problem Constraints

1<=A<=100A=BBi<=Ai1<=Ai<=1001<=Bi<=100\begin{aligned} 1 <= |A| <= 100 \\ |A| = |B| \\ Bi <= Ai \\ 1 <= Ai <= 100 \\ 1 <= Bi <= 100 \\ \end{aligned}

Input Format

  • The first argument is an integer array A. Representing the volume of the ith bucket.
  • The second argument is an integer array B. Representing the amount of water in the ith bucket.

Output Format

  • Return an array of integers of size two, where the first element denotes the maximum number of empty buckets that Mr. Naaya can get, and the second element represents the minimum time required to get the maximum number of empty buckets.

Example

Example Input Input 1:

Input 2:

Example Output
Output 1:

Output 2:

Example Explanation
Explanation 1: Transfer 3 units of water from the first bucket to the third bucket which takes 3 seconds,Then transfer 4 units of water from the second bucket to the third bucket, which takes 4 units of time. Hence, a maximum of 2 empty buckets in a minimum time of 7 seconds.

Explanation 2: All buckets are completely filled, hence 0 empty buckets.

Hint

  • Think of finding the minimum number of buckets required to fill the water.
  • Use Dynamic Programming to get the minimum number of buckets and the time required.
  • Let the states of DP be dp[idx][waterLeft]dp[idx][waterLeft].

Complete Solution

Empty Buckets Complete Solution