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

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

Months Cycle

You are given an integer array A, which denotes the days of different months.Months are consecutive. If a year is a leap year then the February month has 29 days except 28. And all the days in a month are the same as the real calendar. If the array represents the months of consecutive years then return 1 else return 0.

Problem Constraints

1<=A<=3628<=A[i]<=31\begin{aligned} 1 <= |A| <= 36\\ 28 <= A[i] <= 31\\ \end{aligned}

Input Format

  • The first argument is an integer array A.

Output Format

  • Return an 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: Months are December, January and February. The february month was not of leap year.

Explanation 2: Months are January ,February, March and April. This is a leap year. But april has 30 days. So, it should be [31, 29, 31, 30]

Hint

  • In a calender there are 31 days in January,
  • 28 or 29 days in February (depending on whether the year is leap or not),
  • 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July,
  • 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.

Complete Solution

Months Cycle selection Complete Solution

Nearest similar number

You are given an integer A. Your task is to find an integer X, such that:

  • Number of prime factors in the prime factorization of A and X are the same.
  • Prime factorization of A and X differs by exactly one prime factor. For ex: if A = 12 (223), then possible value of X can be 18 (323), 30(253), 20(225) etc.

Among all possible values of X, return the one which has minimum possible value of abs(A-X). If there are multiple values of X possible, return the largest one.

Problem Constraints

1<=A<=105\begin{aligned} 1 <= A <= 10^5\\ \end{aligned}

Input Format

  • The first and only argument given is the integer 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 X

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: Prime Factorisation of 12 = 223. If we change one 2->3, we get 233 = 18, and abs(18-12) = 6. But if we change one 3->2, we get 222 = 8, and abs(12-8) = 4, which is the minimum we can acheive.

Explanation 2: Prime factorisation of 36 = 2233. We get minimum value of abs(N-X) by changing 3->2, so X = 2223 = 24.

Hint

  • Try to think of brute force approach.
  • Since the value of N is only upto 1e5, we can generate all the prime factors of it.

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

Nearest similar number Complete Solution

Chocolate Boxes

There are N types of chocolates. Given an array A where A[i] is the number of chocolates of ith type. You have B boxes. One box can carry an infinite number of chocolates of one type. You have to find a way to put all the chocolates in the boxes such that the chocolates in the box with the maximum number of chocolates is minimum. Return the number of chocolates in the box with the maximum number of chocolates.

Problem Constraints

1<=len(A)<=1051<=A[i]<=109len(A)<=B<=2105\begin{aligned} 1 <= len(A) <= 10^5\\ 1 <= A[i] <= 10^9\\ len(A) <= B <= 2 * 10^5\\ \end{aligned}

Input Format

  • The first argument is an integer array A.
  • The second argument is a single integer B.

Output Format

  • Return the number of chocolates in the box with the maximum number of chocolates.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: Box 1 has 3 chocolates of type 1 Box 2 has 12 chocolates of type 2 Box 3 has 12 chocolates of type 2 Box 4 has 8 chocolates of type 3

Explanation 2: Box 1 has 29 chocolates of type 1 Box 2 has 19 chocolates of type 2 Box 3 has 22 chocolates of type 3 Box 4 has 13 chocolates of type 4 Box 5 has 17 chocolates of type 5 Box 6 has 15 chocolates of type 6 Box 7 has 15 chocolates of type 6 Box 8 has 15 chocolates of type 7 Box 9 has 15 chocolates of type 8 Box 10 has 14 chocolates of type 8

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

  • If there are x boxes allocated for a specific chocolate type it is optimal to distribute the chocolates evenly in these boxes. So the max chocolate in a box will be ceil(A[i] / x).
  • Try to think greedily. First assign 1 box to each chocolate type.
  • Now try to think which type of chocolate should get one new box and come up with an algorithm.

Complete Solution

Chocolate Boxes Complete Solution

Pseudo Primes

A number is a Pseudo Prime if the sum of its distinct prime factors is also a prime number. Return the number of Psuedo primes in the range A to B.

Problem Constraints

1<=A<=B<=106\begin{aligned} 1 <= A <= B <= 10^6\\ \end{aligned}

Input Format

  • The first argument is a single integer A.
  • The second argument is a single integer B.

Output Format

  • Return the number of Pseudo primes in the range A to B.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: The Pseudo Primes are 2, 3, 4, 5, 6, 7, 8, 9, 10. Sum of their distinct prime factors are 2 = 2 3 = 3 4 = 2 5 = 5 6 = 5 7 = 7 8 = 2 9 = 3 10 = 7

Explanation 2: There are 24 Pseudo primes in the given range.

Hint

  • The naive approach will be to iterate over all the numbers.
  • Find their prime factors sum them and check if the sum is prime.
  • But this is too slow as iterating over the range is O(N) and then prime factorisation is O(sqrt(N)) and then checking
  • if the sum is prime is another O(sqrt(N)).
  • Overall time complexity is O(N sqrt(N)) which is slow for the given constraints.
  • Try to use Sieve of Eratosthenis.

Complete Solution

Pseudo Primes Complete Solution