Scaler Topics Fortnightly Contest - 28 Editorial

Learn via video courses
Topics Covered

DSA Fortnightly - 28 Editorial

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

Lucky Number

In this realm, A holds the status of a lucky number. Given a string B with a size exceeding A, your task is to eliminate precisely A characters and ascertain whether the resulting string (with its characters arranged in any manner) is a palindrome or not.

Problem Constraints

0<=A<B1<=B<=105a<=Bi<=z\begin{aligned} 0 <= A < |B| \\ 1 <= |B| <= 10^5\\ 'a' <= Bi <= 'z'\\ \end{aligned}

Input Format

  • The first argument is integer A.
  • The second argument is string of characters B.

Output Format

  • Return 1 if after removing exactly A characters from B , it becomes a palindrome else return 0.

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: In "abb" we can remove B[0], hence the string becomes "bb" which is a palindrome.

Explanation 2: "ab" is not a palindrome.

Hint

  • Think of a greedy approach to apply.

Complete Solution

Lucky Number Complete Solution

Palindromifier

Given a string A of length N, containing only lowercase letters from the English alphabet. We would like to turn string A into a palindrome by applying two types of operations to the string-

The first operation allows you to choose i (2 ≤ i ≤ n−1) and to append the substring A2A3…Ai (i-1 characters) reversed to the front of A.

The second operation allows you to choose i (2 ≤ i ≤ n−1) and to append the substring Ai+1Ai+2…AN-1 (n-i characters) reversed to the end of A.

Find if the string can be turned into a palindrome in at most 1 move. Return 1 if it can be done, else print 0.

Problem Constraints

KaTeX parse error: Expected & or \\ or \end at position 36: … |A| <= 10^3\\ ‘̲a’ <= A[i] <= ‘…

Input Format

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

Output Format

  • Return an integer, 1 or 0.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: As this string is already a palindrome, we don’t need to perform any operation.

Explanation 2: There is no possible move to convert this string into a palindrome in the next move.

Hint

  • Simple brute force can be enough to solve this problem.

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

Palindromifier Complete Solution

Height of wall

Consider a wall represented by an array A, consisting of N segments horizontally. Each element Ai in the array signifies the height of the wall at the i-th segment. As the new constructor of the wall, your task is to maximize the overall height of the wall while adhering to a specific condition. The height of the wall is defined as the maximum number of vertically stacked blocks within any given N segments arranged horizontally. You can increase the height of a particular segment by adding one more block on top of it only if the height of the immediately consecutive segment is greater than or equal to the current one. Determine the maximum height achievable for the constructed wall.

Assume that you only have B additional blocks, hence you can do the above operations no more than B times.

Problem Constraints

2>=A<=10001<=Ai<=1071<=B<=108\begin{aligned} 2 >= |A| <= 1000\\ 1 <= Ai <= 10^7\\ 1 <= B <= 10^8\\ \end{aligned}

Input Format

  • The first argument is array of integers A.
  • The second argument is integer B.

Output Format

  • Return maximum height of wall possible.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: In the initial test case, one potential optimal sequence of operations could be: [1, 3, 3] → [2, 3, 3] → [2, 4, 3] → [3, 4, 3] → [4, 4, 3].

Explanation 2: [3, 5] → [4, 5] → [5, 5] → [6, 5].

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

  • Use binary search to reach the answer.
  • Look at the constraints carefully and try to analyse code by analysing complexity.

Complete Solution

Height of wall Complete Solution

Subarray Size-OR

Given an array A of integers of size N, find the number of non-empty subarrays with their size equal to OR sum.

Problem Constraints

1<=A<=21050<=A[i]<=A\begin{aligned} 1 <= |A| <= 2⋅10^5\\ 0 <= A[i] <= |A|\\ \end{aligned}

Input Format

  • The first argument is an integer array A.

Output Format

  • Return an integer, the number of subarrays.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1:

There are a total of 6 subarrays - [1], [0], [1], [1, 0], [0, 1], [1, 0, 1]. Only the 2 subarrays - [1] and [1] have the size = OR sum, i.e., 1.

Explanation 2:

As all the subarrays have the OR as 0, no OR can match the size. So, the answer is 0.

Hint

  • How many times can an OR sum change from a particular fixed point? How can you optimize the search?

Complete Solution

Subarray Size-OR Complete Solution