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

Water Transportation

There are N warehouses in a water housing facility. Each warehouse contains Ai water containers, and each container has Bi litres of water. Your Truck can transport only C containers of water from the facility. Determine the maximum amount of water that you can transport from the facility.

Problem Constraints

1<=N<=21051<=A[i]<=1031<=B[i]<=1031<=C<=105\begin{aligned} 1 <= N <= 2*10^5 \\ 1 <= A[i] <= 10^3 \\ 1 <= B[i] <= 10^3 \\ 1 <= C <= 10^5 \\ \end{aligned}

Input Format

  • First argument is the array A
  • Second argument is the array B
  • Third argument is an integer C

Output Format

  • Return an integer, denoting the maximum amount of water that you can transport from the facility.

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: We will take 1 container each from 1st, 3rd and 4th warehouse and 2 containers from 2nd warehouse. Hence 10 + 6 + 3 + 10 = 29

Explanation 2: Take 2 containers from 3rd warehouse and 3 containers from 5th warehouse. Hence 14 + 12 = 26 litres.

Hint

  • Try to Think greedy.
  • Try to think how sorting the array will help.

Complete Solution

Water Transportation Complete Solution

Perfect AND

You are given an integer array A. Return 1 if there is a subarray in A such that Bitwise And of all elements in the subarray is equal to B else, return 0.

Problem Constraints

1<=A<=1051<=Ai<2301<=B<230\begin{aligned} 1 <= |A| <= 10^5 \\ 1 <= Ai < 2^30 \\ 1 <= B < 2^30 \\ \end{aligned}

Input Format

  • The first argument is an integer array A.
  • The second argument is an integer B.
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: We can select the subarray [3, 5]. (3 & 5) = 1

Explanation 2: There's no way to get 3 by selecting any subarray.

Hint

  • How the values of AND function will behave if we increase the length of our subarray by adding elements to subarray for either side.

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

Perfect AND Complete Solution

How Many Strings

A string is called good if there doesn't exist a subarray of length greater than B in which all characters are the same. Return the number of good strings of length A, the answer can be very large so return modulo 109+7.

NOTE: The string should only contain lowercase Latin alphabets.

Problem Constraints

1<=A<=10001<=B<=10\begin{aligned} 1 <= A <= 1000\\ 1 <= B <= 10\\ \end{aligned}

Input Format

  • The first argument is an integer A.
  • The second argument is an integer B.

Output Format

  • Return an integer.

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: We can place any character at first position in the string but can only place different character at next position. So choice for first position 26, then choice for second position 25. Therefore, 2625=65026 * 25 = 650.

Explanation 2: We can place any character at any position. So, 262626=1757626 * 26 * 26 = 17576.

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

  • Let's build the string character by character.
  • While adding characters to end of the string we only care about the last character and it's previous continuous occurrence.

Complete Solution

How Many Strings Complete Solution

Get Out

You are in the country of Marley. The country have A cities and the cities are connected with A-1 bidrectional roads such that it is possible to reach any city from other city in the country. You want to get out of the country as you've heard titans will be invading the country soon. To get out you need to reach one of the coastal cities. Coastal cities are those cities which from which only one road is connected. Find the minimum time for all the cities to reach a coastal city. Roads connecting two cities are represented by the matrix B, where for ith road, B[i][0] and B[i][1] represent the cities connected by a bidirectional road and B[i][2] represents the time required to travel through this road.

Problem Constraints

2<=A<=1051<=B[i][0],B[i][1]<=A1<=B[i][2]<=104\begin{aligned} 2 <= A <= 10^5 \\ 1 <= B[i][0], B[i][1] <= A \\ 1 <= B[i][2] <= 10^4 \\ \end{aligned}

Input Format

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

Output Format

  • Return an integer array.

Example

Example Input

Input 1:

Input 2:

Example Output

Output 1:

Output 2:

Example Explanation

Explanation 1: The coastal cities are 1 and 3 as they are connected by only one road. From city 2 city 1 one is closest with distance 5.

Explanation 2: The coastal cities are 3, 4 and 5. From 1 city 3 is closest. From 2 city 3 is closest.

Hint

  • The problem converts to shortest path question from some special nodes.

Complete Solution

Get Out Complete Solution