Scaler Topics Fortnightly Contest - 17 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

DSA Fortnightly - 17 Editorial

This article is part of the Scaler Topics Fortnightly Contest - 17

Optimal Subarray selection

Given an array A of N integers and an array B of Q queries. Each query consists of two integers B[i][0], B[i][1] and you have to determine the sum of the maximum sum subarray among all the subarrays [x, y] such that x is lesser or equal to B[i][0] and y is greater or equal to B[i][1].

Note: [x, y] denotes the subarray starting at index x and ending at index y.

Problem Constraints

1<=N<=105105<=A[i]<=1051<=Q<=1051<=B[i][0]<=B[i][1]<=N\begin{aligned} 1 <= N <= 10^5\\ -10^5 <= A[i] <= 10^5\\ 1 <= Q <= 10^5\\ 1 <= B[i][0] <= B[i][1] <= N\\ \end{aligned}

Input Format

  • The first argument is the array A and the second argument B is the array of queries.

Output Format

  • Return a list of integers.

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: For the first query the maximum sum subarryay is [1, 4]. For the second query the maximum sum subarray is [2, 4].

Explanation 2: For the first query the maximum sum subarryay is [1, 4]. For the second query the maximum sum subarray is [1, 5].

Hint

  • We can solve this problem with the pefix sum and it's maxima and minima computation.

Complete Solution

Optimal Subarray selection Complete Solution

Swap Sort

You are given an array A of N integers. In one operation, you can swap any two adjacent integers of A such that the integers have different counts of digits. You will have to find if it's possible to sort the array by performing any number of operations

Problem Constraints

1<=N<=1051<=A[i]<=109\begin{aligned} 1 <= N <= 10^5\\ 1 <= A[i] <= 10^9\\ \end{aligned}

Input Format

  • Only argument A is an array of integers
Free Courses by top Scaler instructors

Output Format

  • Return 1 if its possible to sort the array and 0 if its not possible.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: In the first operation we can swap the second and third element of the array, A becomes [10, 5, 18]. In the next operation, we can swap the first two elements. Thus A becomes [5, 10, 18]. Hence, we are able to sort this array.

Explanation 2: We would never be able to sort the given array. So, we return 0.

Hint

  • We would never be able to change the relative ordering of the elements with the same number of digits.

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

Swap Sort Complete Solution

Powerball Tournament

Scaler is going to organize a powerball tournament. In the tournament there are A types of powerballs. All the candidates will get exactly N powerballs. Array B of size N, denotes the type of the powerball and array C of size N, denotes the strength of the powerball(which can be negative as well).

Rules of tournament are:

  • All the candidates have to choose some subset out of A types of powerballs.
  • Number of powerballs of each type in the chosen subset should be same.
  • Candidates with maximum non negative total strength of the powerball will win the tournament.

You being one of the candidate of the tournament have to the maximize the total strength of the powerball. Return the maximum possible total strength of the powerball.

Problem Constraints

1<=N<=1051<=A<=1051<=Bi<=A104<=Ci<=104\begin{aligned} 1 <= N <= 10^5\\ 1 <= A <= 10^5\\ 1 <= Bi <= A\\ -10^4 <= Ci <= 10^4\\ \end{aligned}

Input Format

  • The first argument given is an integer, A.
  • The second argument given is an array, B.
  • The third argument given is an array, C.

Output Format

  • Return an integer denoting the maximum possible total strength of the powerball.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: It is optimal to choose 1st, 2nd, 3rd, and 4th powerball. So there are 2 powerballs of 2nd-type and 2 powerballs of 3rd-type. Total strength - 6+6+5+5 = 22.

Explanation 2: It is impossible to obtain a non-negative sum using any set of powerballs.

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

  • It is always optimal to take powerball with max strength for a fixed type.
  • If we fix the number of powerballs for each type, then it is always optimal to take all powerballs with positive sum.

Complete Solution

Powerball Tournament Complete Solution

Aesthetic food

Ramya is hosting a dinner night for her guests. She has invited A guests to her place for the dinner. She has N boxes of spices using which she will prepare the food(N>=A). Spices in her kitchen are present in an array of boxes, B and she will use a continuous segment of spices to prepare the food of a single guest. For every guest she will use atleast one box of spice to prepare the food, and each box of spices can be used to prepare the food for exactly one guest.

Aesthetic value of the food is defined by the variety of spices used to prepare the food, Formally, no of distinct spices used to prepare the food is its aesthetic value. Help her determine the maximum possible total Aesthetic value of her prepared dishes.

Problem Constraints

1<=N<=20001<=A<=min(N,50)1<=Bi<=N\begin{aligned} 1 <= N <= 2000\\ 1 <= A <= min(N, 50)\\ 1 <= Bi <= N\\ \end{aligned}

Input Format

  • The first argument given is an integer, A.
  • The second argument given is an array, B.

Output Format

  • Return an integer denoting the maximum possible total Aesthetic value of her prepared dishes.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: Since there is only one guest coming to her house, it will be optimal to use all 4 spices to prepare food. Since there are 2 distinct spices so answer is 2.

Explanation 2: It will be optimal to use first 2 spices for the first guest and rest all spices for second guest. Aesthetic value of food for guest 1 = 2 Aesthetic value of food for guest 2 = 3 Total Aesthetic value = 2+3 = 5.

Hint

Try to think of a dynamic programming approach:

  • DP[i][j] - maximum aesthetic value that we can acheive by using first i spices for preparing j dishes.
  • For j = 1: the answer is equal to the number of distinct values on a prefix(i).
  • For j > 1: the answer can be deduced as follows (here c(x,i) denotes the number of distinct elements in range (x,i)): DP(i, j) = max 1<=x<i(dp(x-1, j-1)+c(x,i)).

Complete Solution

Aesthetic food Complete Solution