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

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

Count of Distinct Pairwise Sum - I

Given an array of integers A, your task is to find the number of distinct integers that can be obtained as the sum of any two integers chosen from array A.

For Example: Consider the array A = [1, 2, 1].

The possible unique sums of pairwise combinations from A are 1 + 1 = 2 and 1 + 2 = 3 and 2 + 1 = 3. Hence, the number of distinct integers possible as the sum of any two chosen integers from the array A is 2 (which are 2 and 3 in this case).

Problem Constraints

2<=A<=1030<=A[i]<=103\begin{aligned} 2 <= |A| <= 103\\ 0 <= A[i] <= 103 \end{aligned}

Input Format

  • First and only argument is an array of integers A.

Output Format

  • Return an integer denoting the required answer.

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: Explanation given in the problem statement.

Explanation 2: Only sum possible is 2.

Hint

  • Use Nested loop along with a set to hold the distinct sums possible..

Complete Solution

Count of Distinct Pairwise Sum - I Complete Solution

Count of Distinct Pairwise Sum - II

Given an array of integers A, your task is to find the number of distinct integers that can be obtained as the sum of any two integers chosen from array A.

For Example: Consider the array A = [1, 2, 1].

The possible unique sums of pairwise combinations from A are 1 + 1 = 2 and 1 + 2 = 3 and 2 + 1 = 3. Hence, the number of distinct integers possible as the sum of any two chosen integers from the array A is 2 (which are 2 and 3 in this case).

Problem Constraints

2<=A<=1050<=A[i]<=103\begin{aligned} 2 <= |A| <= 10^5 \\ 0 <= A[i] <= 10^3 \\ \end{aligned}

Input Format

  • First and only argument is an array of integers 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 denoting the required answer.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: The explanation is given in the problem statement.

Explanation 2: The only sum possible is 2.

Hint

  • Create an array out of A which contains only distinct numbers, the size of this array can go up to 103.
  • Now, you can do brute force & find the answer.

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

Count of Distinct Pairwise Sum - II Complete Solution

Minimum Total Travel Time

There is a land represented in the form of a matrix A having dimensions N x M where each of its cells denotes a unique city. Moreover, A[i][j] = 0 represents a city under construction and no one can travel through this city or visit this city, although apart from these kinds of cities every other city is constructed and denoted by A[i][j] = 1; one can travel through or visit these cities.

You and your two other friends initially are in cities B, C, and D.

Obviously, A[B[0]][B[1]]!=0,A[B[0]][B[1]] != 0, A[C[0]][C[1]]!=0A[C[0]][C[1]] != 0, and A[D[0]][D[1]]!=0A[D[0]][D[1]] != 0 (0 - based indexing) which means any of you guys are not in an under-construction city.

You all three want to meet, so looking for a city such that the sum of travel time for all of you will be minimized. If they can’t meet then return -1.

Note:

  • One can travel from a city to any adjacent city, meaning left, right, or up, down.
  • Traveling from one city to another incurs 1 unit of time.

Determine the value of X at which the randomness of the array reaches 0. Return the rounded value of 104 * X.

Problem Constraints

1<=N,M<=1053<=NM<=105A[i][j] can either be 0 or 1.B,C,D=20<=B[0],C[0],D[0]<N0<=B[1],C[1],D[1]<M\begin{aligned} 1 <= N, M <= 10^5 \\ 3 <= N * M <= 105 \\ A[i][j]\ can\ either\ be \ 0 \ or\ 1. \\ |B|, |C|, |D| = 2 \\ 0 <= B[0], C[0], D[0] < N \\ 0 <= B[1], C[1], D[1] < M \\ \end{aligned}

Input Format

  • First argument is a 2D array of integers A
  • Second argument is an array of integers B
  • Third argument is an array of integers C
  • Fourth argument is an array of integers D

Output Format

  • Return an integer denoting the minimum sum of the total time required by all of you to travel to the meeting city.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: The optimal meeting point is (1, 1). The total travel time for all of you will be 2.

Explanation 2: The only possible meeting point is A[0, 1] such that the minimum total travel time to this city is 2.

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

  • Think about using a traversal technique like BFS or DFS.

Complete Solution

Minimum Total Travel Time Complete Solution

Krishna’s Divine Partition

In the legendary saga of Mahabharata, guided by the divine wisdom of Lord Krishna, we encounter a mystical challenge. Imagine that Krishna revealed a sacred string A to the Pandavas, consisting of N digits. Each digit within the string ranges from 1 to 9. The significance lies in partitioning the string into substrings, ensuring that each substring, when interpreted as a decimal number, is strictly greater than the previous substring, symbolizing a divine strictly increasing order.

For instance, consider a sacred string "763569" with six digits. By partitioning it into three substrings: "7", "63", and "569", each representing strictly increasing decimal values, you embody the essence of Krishna's teachings.

Your task is to unravel the secret concealed within the sacred string. Determine the maximum number of partitions while preserving the divine order prescribed by Krishna.

Problem Constraints

1<=N<=20001<=A[i]<=9\begin{aligned} 1 <= N <= 2000 \\ '1' <= A[i] <= '9' \\ \end{aligned}

A and B only contain lowercase English characters.

Input Format

  • The first and only argument is a string A, of length N.

Output Format

  • Return an Integer representing the maximum number of partitions as described.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: Substrings formed: "7", "63", "569"

Explanation 2: Substrings formed: "2", "8", "34", "98", "3867"

Hint

  • Carefully look at constraints of the problem, dynamic programming might help. But what will be the states?

Complete Solution

Krishna’s Divine Partition Complete Solution