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

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

Tile Square Problem

Jack, a skilled craftsman, faces the task of adorning a rectangular wall measuring N units in length and M units in width. This wall is segmented into a grid of N * M individual blocks, each measuring 1 unit by 1 unit. His goal is to embellish the wall by affixing colorful tiles onto it, arranging them to create a single square pattern. The challenge lies in determining the minimum number of additional tiles required to form this square, considering that some tiles have already been placed on the wall and cannot be removed.

Provided with an array of strings A of size N, where each A[i] represents the i-th row of the wall and A[i][j] represents the j-th block in the i-th row, the letter 'E' denotes an empty block (indicating no tile placed), while 'T' signifies a tile already present on the wall. Calculate the minimum number of new tiles needed to construct a square on the wall. If it's impossible to form a square pattern with the existing tiles, return -1.

Problem Constraints

1<=N,M<=1000A[i][j]=EorT\begin{aligned} 1 <= N, M <= 1000\\ A[i][j] = 'E' or 'T'\\ \end{aligned}

Input Format

  • The first and only argument is an array of strings A.

Output Format

  • Return an integer denoting the required answer.

Build an AI-First Career, Master the Complete Skillset

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
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

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

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: To create a square pattern, 5 additional tiles are needed:

These tiles are required at positions (2, 3), (3, 2), (3, 3), (4, 2), and (4, 3) (1-based indexing). This results in a square pattern with sides equal to 3, resulting in the final arrangement as: [ "EEEE", "TTTE", "TTTE", "TTTE", "EEEE" ]

Explanation 2: As there are no tiles present initially, it's sufficient to place a single tile on any block to form a square with sides equal to 1.

Hint

  • Start by handling the scenario where no tiles exist initially, allowing a single tile to be placed on any block to form a base square.
  • For cases where tiles are present, focus on identifying the bounding rows and columns with existing tiles to determine the required side length of the square, considering the limits of the wall's dimensions.

Complete Solution

Tile Square Problem Complete Solution

Mesmerizing Necklace Arrangement

Jack has gifted Ava a beautiful necklace adorned with red ('R') and blue ('B') beads. However, Ava seeks to enhance its allure without altering the bead sequence.

To imbue the necklace with a mesmerizing quality, it must be divisible into three distinct sections while maintaining the bead sequence. The first and third sections should exclusively feature red beads, and the second section should consist solely of blue beads. Sections might be empty, containing no beads.

Your task is to assist Ava in determining the minimum number of beads to remove without disrupting their order, achieving the mesmerizing look she desires. You're given a string A of size N, where A[i] represents the i-th bead of the necklace.

Problem Constraints

1<=N<=2500A[i]=RorB\begin{aligned} 1 <= N <= 2500 \\ A[i] = 'R' or 'B' \end{aligned}

Input Format

  • The first and only argument is a string A.
Sharpen Your Fundamentals with Free Learning

Output Format

  • Return an integer denoting the required answer.

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: The necklace "RRRBBR" is already mesmerizing as it satisfies the conditions without requiring any bead removal. Therefore, the output is 0.

Explanation 2: In the necklace "BRRBR", by removing the first bead ('B'), the sequence becomes "RRBR", satisfying the conditions for a mesmerizing necklace. Thus, only one bead needs to be removed to achieve the mesmerizing configuration.

Hint

  • Generate prefix arrays to count 'R' and 'B' beads in the necklace prefixes.
  • Utilize a two-pointer approach (nested loops) to find the optimal i and j positions.

How Scaler Transformed Careers in Different Fields

₹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

Mesmerizing Necklace Arrangement Complete Solution

Replacement Score

Given a binary string A consisting of only ‘0’ and ‘1’, the score of the string is defined as the maximum number of replacements you can perform on a string if you can replace “00” with “0” in one removal. You also need to process some queries given in the form of 2 arrays B and C where the ith query denotes that B[i] index of A needs to be replaced by the character C[i] where B[i] is an index and a C[i] is a character(‘0’ or ‘1’). You need to calculate the score after each query.

Problem Constraints

1<=A<=1050<=A[i]<=11<=B=C<=1051<=B[i]<=A0<=C[i]<=1\begin{aligned} 1 <= |A| <= 10^5\\ '0' <= A[i] <= '1'\\ 1 <= |B| = |C| <= 10^5\\ 1 <= B[i] <= |A|\\ '0' <= C[i] <= '1'\\ \end{aligned}

Input Format

  • The first argument is a string A.
  • The second argument is a vector of integers denoting the index for each query.
  • The third argument is a vector of characters denoting the character to be replaced within each query.

Output Format

  • Return an integer array containing the answer to each query.

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: After the first query, the string is “0110”. The score is 0 as we can’t replace anything. After the second query, the string is “0010”. The score is 1 as we can replace one “00”. After the third query, the string is “0011”. The score is 1 as we can replace one “00”. After the fourth query, the string is “0001”. The score is 2 as we can replace 2 times.

Explanation 2: After the first query, the string is “0” and the score is 0.

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

  • The number of consecutive 0s gives the answer.
  • Only the left and the right index matter during a query.

Complete Solution

Replacement Score Complete Solution

Maxed Arrays

An array of integers X of length N is called Maxed with respect to a particular element K inside it if:

  1. 3 <= N
  2. If the index of the element K is i, then 1 < i < N
  3. max(X[1], … , X[i]) ≠ max(X[2], … , X[i])
  4. max(X[i], … , X[N]) ≠ max(X[i], … , X[N-1])

Given an array A of integers, for each element in A from index 1 to |A|, you need to find the sum of the length of all the subarrays of A that are Maxed with respect to each of these elements. Output is an array B where B[i] denotes the sum of the length of all the Maxed subarrays with respect to A[i].

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 argument is an integer array A.

Output Format

  • Return an integer array, the answer for each element in A.

Example

Example Input Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1:

There is no subarray for the 0th element because it would always be the first element in each subarray. For the 1st element, there is only the subarray [0, 2, 1] which satisfies the first 2 conditions. But it doesn’t satisfy the 2nd condition as max(0, 2) = max(2). There is no subarray for 2nd element because it would always be the last element in each subarray.

Explanation 2:

There is no subarray for the 0th element because it would always be the first element in each subarray. The 2 subarrays that satisfy the conditions for the 1st element are [5, 3, 2, 5] and [5, 3, 2, 5, 7]. The sum of lengths is 4 + 5 = 9. A total of 4 subarrays satisfy the conditions for the 2nd element - [5, 3, 2, 5], [5, 3, 2, 5, 7], [3, 2, 5, 7] and [3, 2, 5]. Sum = 3 + 4 + 4 + 5 = 16. No subarray satisfies the conditions for the 3rd and 4th elements.

Hint

  • Stacks can be used to have a list of increasing maximums on one side for each element.

Complete Solution

Maxed Arrays Complete Solution