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

Subsequence Game

Alice and Bob are playing yet another game. They are given a string A of size N which consists of only 'A's and 'B's. Alice's favorite character is 'A' and Bob's favorite character is 'B'. The game is played in turns and Alice goes first. Initially, the score of both players is 0. The rules of the game are as follows:

  1. The current player has to make a valid move. If the current player can't make a valid move the game ends.
  2. The current player has to choose a non-empty subsequence of string A which only consists of their favorite characters and remove them from the string. The score of the current player is incremented by the number of favorite characters of the opponent.
  3. The rest of the string is concatenated and the other player goes for their move.

The winner of the game is the person with the maximum score. Return 1 if Alice can win otherwise return 0, in a game where both players plays optimally, where Alice tries to win and Bob tries to end the game with a win or draw.

Problem Constraints

1<=N<=105length(A)=NAconsistofcharactersAandB.\begin{aligned} 1 <= N <= 10^5\\ length(A) = N\\ A consist of characters 'A' and 'B'.\\ \end{aligned}

Input Format

  • First argument A, is a string.

Output Format

  • Return a single integer.

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: The moves are as follows: 1 -> Alice picks subsequence [1] string becomes = "B" Alice score = 1 2 -> Bob picks subsequence [1] string becomes = "" Bob score = 0 3 -> Alice can't choose any subsequence, hence game ends.

Alice score > Bob score, so Alice wins.

Explanation 2: The moves are as follows: 1 -> Alice can't choose any subsequence, hence game ends. Alice score = Bob score = 0

Hint

  • Do we really need to consider all possible moves? Can count of 'A's and 'B's help?

Complete Solution

Subsequence Game Complete Solution

MEX Subarray

Given an array of integers, A. Return the number of subarrays with non-zero MEX. MEX is defined as the smallest non-negative integer not present in an array.

Problem Constraints

1<=A<=1050<=A[i]<=109\begin{aligned} 1 <= |A| <= 10^5\\ 0 <= A[i] <= 10^9\\ \end{aligned}

Input Format

  • The first and only argument is an integer array A.
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 an integer.

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: Subarray [0] = 1, [0, 1] = 2, [0, 1, 2] = 3 have non-zero MEX.

Explanation 2: All the subarray's have positive MEX.

Hint

  • Which subarrays will have zero MEX?
  • How can we find them?

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

MEX Subarray Complete Solution

Similar Subarray

Let's define two arrays to be similar if they are of the same size and on sorting both the array have the same element in the corresponding index. Given two integer arrays, A of size N and B of size M. Find the count of tuple (i,j,k,l) (1<=i<=j<=Nand1<=k<=l<=M)(1<=i<=j<=N and 1<=k<=l<=M) such that subarray A[i...j] and subarray B[k...l] are similar.

Since this number can be large, return the value mod 109+710^9+7.

Problem Constraints

1<=N,M<=20001<=A[i],B[i]<=100000\begin{aligned} 1 <= N , M <= 2000\\ 1 <= A[i], B[i] <= 100000\\ \end{aligned}

Input Format

  • First argument contains an integer array A.
  • Second argument contains an integer array B.

Output Format

  • Return an integer denoting the answer.

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: first tuple is (2,2,2,2): [3] and [3] are similar second tuple is (3,3,1,1): [2] and [2] are similar third tuple is (2,3,1,2): [3,2] and [2,3] are similar

Explanation 2: There are no valid tuples.

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

  • Note that Brute force of O(N^4) can be reduced to O(N^3) by considering the fact that only tuple with property j-i = l-k will be counted.
  • Can you optimise it further?

Complete Solution

Similar Subarray Complete Solution

Edge Path

You are playing a special game on an undirected weighted connected graph without multiple edges. The graph has A nodes and its edges are represented by the matrix B, such that there is an edge between node B[i][1] and B[i][2] of weight B[i][3]. You are initially at node 1, and you have to reach node A, by traveling through edges. You can make two types of moves:

  1. Go from node u to node v, such that there is an edge between u and v, and add the corresponding weight of the edge to your cost.
  2. Make a special move from node u to node v and add C[u]+C[v]C[u] + C[v] to your cost. A special move from u to v exists if there are k nodes a1, a2 ..... ak (each node may or may not be unique), such that D[1]<=k+1<=D[2]D[1] <= k + 1 <= D[2], and there exist an edge between pairs (u, a1), (a1, a2) ........ (ak - 1, ak) and (ak, v).

Return the minimum cost incurred to reach node A.

Problem Constraints

1<=A<=1041<=B<=minimum(105,A(A1)/2)1<=B[i][1],B[i][2]<=A0<=B[i][3]<=109size(C)=A0<=C[i]<=109size(D)=21<=D[1]<=D[2]<=50\begin{aligned} 1 <= A <= 10^4\\ 1 <= B <= minimum(10^5, A * (A - 1)/2)\\ 1 <= B[i][1], B[i][2] <= A\\ 0 <= B[i][3] <= 10^9\\ size(C) = A\\ 0 <= C[i] <= 10^9\\ size(D) = 2\\ 1 <= D[1] <= D[2] <= 50\\ \end{aligned}

Input Format

  • First argument A, is an integer.
  • Second argument B is a matrix of integers.
  • Third argumend C, is an array of integers.
  • Fourth argument D, is an array of integers.

Output Format

  • Return a single value, denoting the minimum cost to reach node A from node 1.

Example

Example Input

Input 1:

Example Input

Input 2:

Example Output

Output 1:

Example Output

Output 2:

Example Explanation

Explanation 1: Optimal path:

  1. Special move: 1 -> 2 -> 3 Total cost = C[1] + C[3] = 0 There may exist other optimal paths as well.

Example Explanation

Explanation 2: Optimal path:

  1. Special move: 1 -> 4 -> 2 -> 3 -> 2 cost = C[1] + C[2] = 0
  2. Normal move: 2 -> 4 cost = 1 Total cost = 0 + 1 = 1 There may exist other optimal paths as well.

Hint

  • We are required to find shortest cost path. Can you think of modifying any shortest path algorithm? Dijkstra?

Complete Solution

Edge Path Complete Solution