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

Sheldon's Tie Breaker

Rajesh and Howard were arguing about what to watch on TV. They decided to settle it with a game of Rock, Paper, Scissors. When Sheldon jumped on saying "People familiar with each other tend to tie each other 70-80 percent of times."

He instead proposed to play a game of Rock, Paper, Scissor, Lizard, Spock. Where each player simultaneously choose one of the five using the respective sign.

He dictates rules as "Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes scissors."

You are given two string A, B representing the choice of Rajesh and Howard respectively. Please tell whether winner is "Howard", "Rajesh" or "Tie".

Problem Constraints

A,B"Rock","Paper","Scissor","Lizard","Spock"\begin{aligned} A, B ∈ { "Rock", "Paper", "Scissor", "Lizard", "Spock"}\\ \end{aligned}

Input Format

  • First argument is string A representing choice of Rajesh.
  • Second argument is string B representing choice of Howard.

Output Format

  • Return a String representing winner or "Tie"

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: As 'Spock vaporizes rock', winner is Howard.

Explanation 2: Both are same; Tie.

Hint

  • This is a simple Ad-Hoc problem. Just implement what is asked.

Complete Solution

Sheldon's Tie breaker Complete Solution

Strange in Range

You are given 3 integers A, B and C. Find the count of integers in the range A to B (inclusive) which are strange with respect to C. An integer N is strange with respect to C if (N ⊕ C) < C.

Problem Constraints

1<=A<=B<=10181<=C<=1018\begin{aligned} 1 <= A<= B <=10^18 \\ 1 <= C <= 10^18 \\ \end{aligned}

Input Format

  • The first argument is an integer A, the second argument is an integer B and the third and last argument is an integer C.
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 count of integers in the range A to B (inclusive) which are strange with respect to C.

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: The integers from 1 to 5 that are strange with respect to 1 are 1, 2 and 3.

Explanation 2: There are no integers from 2 to 4 that are strange with respect to 1.

Hint

  • Let MSB of N be at ith position
  • N XOR C < C means that ith bit of C is 1.

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

Strange in Range Complete Solution

Minimum Expense to Reach Home

Given a undirected graph with an array A where A[i] represents value of node i. There is a 2-D vector B representing edges where each vector element has 2 values u,v representing there is edge from node u to node v. Node 1 is your current location and you have to reach Node E.

Now there is condition, you may pass through a edge uv only if abs(A[u]-A[v]) < C otherwise it will cost C coins to pass through that path once. Initially, you have D coins with yourself.

You have to tell the maximum coins from D which you may save while travelling from Node 1 to Node E.

Return -1 in case it is impossible to reach Node E with D coins.

Problem Constraints

1<=A.size()<=1051<=A[i]<=1051<=B.size()<=1051<=B[i][0]<=A.size()1<=B[i][1]<=A.size()1<=C<=1051<=D<=1091<=E<=N\begin{aligned} 1 <= A.size() <= 10^5 \\ 1 <= A[i] <= 10^5 \\ 1 <= B.size() <= 10^5 \\ 1 <= B[i][0] <= A.size() \\ 1 <= B[i][1] <= A.size() \\ 1 <= C <= 10^5 \\ 1 <= D <= 10^9 \\ 1 <= E <= N \\ \end{aligned}

Input Format

  • First argument Integer array A representing value of nodes in the Graph.
  • Second argument 2-D vector B representing edges in Graph.
  • Third argument Integer C as per the question.
  • Fourth argument Integer D representing total coins you have.
  • Fifth argument Integer E representing your destination you have to reach finally.

Output Format

  • You have to return the maximum possible coins you will have when you reach destination Node E.
  • Return -1 in case cost to reach destination is more than D.

Example

Example Input Input 1:

Input 2:

Input 3:

Example Output Output 1:

Output 2:

Output 2:

Example Explanation Explanation 1: Path 1->2->3

Explanation 2: No path possible under 15 coins.

Explanation 3: Path 1->4->3

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

  • See if we can use bfs approach to solve the question. In case of abs(A[i]-A[j]) >= C we have to push the in front instead of back of the queue.

Complete Solution

Minimum expense to reach home Complete Solution

Chocolate Boxes II

There are N chocolates and M boxes. The sweetness of each chocolate is given by an array A of size N, price of each chocolate is given by an array B of size N. Each chocolate is within a box and the box number for each chocolate is given by an array C of size N.

There is a price associated with each box as well which is given by an array D of size M. If you purchase a box, all chocolates inside it are also purchased and you don't have to pay for each chocolate inside it separately. You can purchase any chocolate separately or as part of a box.

The total sweetness is the sum of the sweetness of all chocolates purchased separately or as part of a box. You have E units of money. What is the maximum total sweetness you can get if you purchase chocolates optimally?

Problem Constraints

1<=N<=1031<=M<=1031<=A[i]<=1061<=B[i]<=E1<=C[i]<=M1<=D[i]<=E1<=E<=103\begin{aligned} 1 <= N <= 10^3\\ 1 <= M <= 10^3\\ 1 <= A[i] <= 10^6\\ 1 <= B[i] <= E\\ 1 <= C[i] <= M\\ 1 <= D[i] <= E\\ 1 <= E <= 10^3\\ \end{aligned}

Input Format

  • The first argument is an integer array A, the second argument is an integer array B, the third argument is an integer array C, the fourth argument is an integer array D and the fifth and last argument is an integer E.

Output Format

  • Return an integer denoting the maximum total sweetness you can get if you purchase chocolates optimally?

Example

Example Input Input 1:

Input 2:

Example Output Output 1:

Output 2:

Example Explanation Explanation 1: You can purchase all the chocolates. First 2 as part of the box 1 costing 10 and the 3rd separately costing 1. Total sweetness = 2 + 1 + 2 = 5

Explanation 2: You can't purchase all chocolates. The optimal strategy is to purchase chocolate 1 giving sweetness = 10. You can purchase it both separately or as part of the box.

Hint

  • Can you solve the solve the problem if you are allowed to purchase chocolates separately only?
  • It is classical knapsack-dp problem.
  • Can you extend the same idea to original problem?

Complete Solution

Chocolate Boxes II Complete Solution