Rachel is submitting assignment from every student. She has given them a few minutes to arrange their assignments before submission. Just then Rahul remembers that his assignment is with Rohan. Rohan has to pass the assignment to Rahul. All the students are sitting in a straight line. He cannot pass the assignment in front of the teacher because then she will assume they have copied the assignment and will punish both of them. We have to help Rohan to find a way to pass the assignment to Rahul in such a way that he is not caught.
Assignment can be passed under some restrictions as given below:
- A student i can pass the assignment only to his immediate neighbor i.e i-1 and i+1
- At any given time a student may pass the assignment to other student, accept the assignment from some other student or may keep the assignment to himself.
- Teacher is keeping watch over students from l_i to r_i including both l_i and r_i
- If a student is being watched he cannot pass the note or accept the note, otherwise he is caught.
- If he keeps the assignment and he is being watched, he is not caught.
Given four inputs n, m, s, f where n is the total number of students, m is the number of steps during which teacher keeps vigil over students from l_i to r_i , s is the position of Rohan and f is the position of Rahul.
Each m query contains three inputs: The time at which she is watching, the leftmost child she is watching and the rightmost child she is watching.
We have to output a sequence of three words: "Left" "Right" and "Keep" denoting the passing direction of current student.
Examples:
Input: n = 4, m = 4, s = 3, f = 4
1 2 4
2 1 2
3 3 4
4 2 3
Output: KeepRight
During time = 1, teacher is watching all
the student from 2 to 4. Student 3 who
has the assignment therefore cannot pass
it to his neighbor, thus he keeps the
assignment. During time = 2, teacher is
watching student 1 and 2, therefore
student 3 can pass the assignment to
his right to student 4. Since student
4 is Rahul therefore our answer is
"KeepRight"
Input: n = 10, m = 1, s = 1, f = 10
1 5 6
Output: RightRightRightRightRightRight
RightRightRightRight
During time = 1, teacher is watching
student 5 and 6 therefore student 1
can easily pass the assignment to his
right to student 2. After this teacher
stops watching any student therefore
they can keep on passing the assignment
to their right until the assignment
reaches Rahul. Therefore the answer is
"RightRightRightRightRightRight
RightRightRightRight"
Approach:
At a given moment if the teacher is watching either the student currently holding the assignment or the student to whom the assignment is to be passed, then the student will keep it to himself else he will pass the assignment to his neighbor sitting towards Rahul.
Below is the implementation:
C++
// CPP program to find the way
#include <bits/stdc++.h>
using namespace std;
void solve(int n, int m, int s, int f,
long t[], int l[], int r[])
{
int dir;
string val;
// If Rahul sits to right of Rohan
// then student will either pass
// it right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan
// then student will either pass
// it left or keep it to himself
else {
dir = -1;
val = "Left";
}
string ans = "";
// current is keeping track of
// the position of assignment
// at a given time
int i = 0, current = s;
// tim variable keeping track of
// current time
long tim = 1;
while (1) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the
// student having the assignment
// or the student to whom the
// assignment is to be passed
// then the student keeps it to
// itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
cout << ans << endl;
}
// Driver function for the program
int main()
{
int n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
long t[m + 2] = { 1, 2, 3, 4 };
// matrix saving leftmost child being
// watched for each query
int l[m + 2] = { 2, 1, 3, 2 };
// matrix saving rightmost child
// being watched for each query
int r[m + 2] = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
return 0;
}
Java
// Java program to find the way
import java.io.*;
class Hiding {
static void solve(int n, int m, int s, int f,
long t[], int l[], int r[])
{
int dir;
String val;
// If Rahul sits to right of Rohan
// then student will either pass it
// right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan then
// student will either pass it left
// or keep it to himself
else {
dir = -1;
val = "Left";
}
String ans = "";
int i = 0, current = s;
// Variable keeping track of current time
long tim = 1;
while (1 > 0) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the student
// having the assignment or the
// student to whom the assignment is
// to be passed then the student
// keeps it to itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
System.out.println(ans);
}
// Driver Program
public static void main(String args[])
{
int n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
long t[] = { 1, 2, 3, 4 };
// matrix saving leftmost child
// being watched
int l[] = { 2, 1, 3, 2 };
// matrix saving rightmost child
// being watched
int r[] = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
}
}
Python3
# Python program to find the way
def solve(n, m, s, f, t, l, r):
val = "";
# If Rahul sits to right of Rohan
# then student will either pass it
# right or keep it to himself
if (s < f):
dir = 1;
val = "Right";
# If Rahul sits to left of Rohan then
# student will either pass it left
# or keep it to himself
else:
dir = -1;
val = "Left";
ans = "";
i = 0;
current = s;
# Variable keeping track of current time
tim = 1;
while (1 > 0):
# If time duration of query
# matches with the current time
if (i < m and tim == t[i]):
# If teacher is watching the student
# having the assignment or the
# student to whom the assignment is
# to be passed then the student
# keeps it to itself
if ((current >= l[i] and current <= r[i]) or \
(current + dir >= l[i] and current + dir <= r[i])):
ans += "Keep";
tim += 1;
i += 1;
continue;
i += 1;
# If the students are safe then student
# passes the assignment to his neighbor
current += dir;
ans += val;
tim += 1;
# If the assignment has reached Rahul
# then we break the loop
if (current == f):
break;
print(ans);
# Driver Program
if __name__ == '__main__':
n, m, s, f = 4, 4, 3, 4;
# matrix saving time for each query
t = [ 1, 2, 3, 4 ];
# matrix saving leftmost child
# being watched
l = [ 2, 1, 3, 2 ];
# matrix saving rightmost child
# being watched
r = [ 4, 2, 4, 3 ];
solve(n, m, s, f, t, l, r);
# This code is contributed by 29AjayKumar
C#
// C# program to find the way
using System;
class Hiding
{
static void solve(int n, int m, int s, int f,
long []t, int []l, int []r)
{
int dir;
String val;
// If Rahul sits to right of Rohan
// then student will either pass it
// right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan then
// student will either pass it left
// or keep it to himself
else {
dir = -1;
val = "Left";
}
String ans = "";
int i = 0, current = s;
// Variable keeping track of current time
long tim = 1;
while (1 > 0) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the student
// having the assignment or the
// student to whom the assignment is
// to be passed then the student
// keeps it to itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current +
dir <= r[i]))
{
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
Console.Write(ans);
}
// Driver Program
public static void Main()
{
int n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
long []t = { 1, 2, 3, 4 };
// matrix saving leftmost
// child being watched
int []l = { 2, 1, 3, 2 };
// matrix saving rightmost
// child being watched
int []r = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
}
}
// This code is contributed by nitin mittal
JavaScript
<script>
// JavaScript program to find the way
function solve(n,m,s,f,t,l,r)
{
let dir;
let val;
// If Rahul sits to right of Rohan
// then student will either pass it
// right or keep it to himself
if (s < f) {
dir = 1;
val = "Right";
}
// If Rahul sits to left of Rohan then
// student will either pass it left
// or keep it to himself
else {
dir = -1;
val = "Left";
}
let ans = "";
let i = 0, current = s;
// Variable keeping track of current time
let tim = 1;
while (1 > 0) {
// If time duration of query
// matches with the current time
if (i < m && tim == t[i]) {
// If teacher is watching the student
// having the assignment or the
// student to whom the assignment is
// to be passed then the student
// keeps it to itself
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep";
tim++;
i++;
continue;
}
i++;
}
// If the students are safe then student
// passes the assignment to his neighbor
current += dir;
ans += val;
tim++;
// If the assignment has reached Rahul
// then we break the loop
if (current == f)
break;
}
document.write(ans+"<br>");
}
// Driver Program
let n = 4, m = 4, s = 3, f = 4;
// matrix saving time for each query
let t = [ 1, 2, 3, 4 ];
// matrix saving leftmost child
// being watched
let l = [ 2, 1, 3, 2 ];
// matrix saving rightmost child
// being watched
let r = [ 4, 2, 4, 3 ];
solve(n, m, s, f, t, l, r);
// This code is contributed by rag2127
</script>
Output:
KeepRight
Similar Reads
Payu Iinterview (On-Campus)
Written round: 5 coding and 15 MCQ on ds, algos, dbms. Round 1: Tell me about yourself Given two strings str1 and str2 find if str2 is substring of str1, if yes return the starting index else return -1. Given time as a string in format of HH:MM, draw an analog clock. ( no logic , but made me write t
2 min read
MakeMyTrip Interview Experience | Set 7 (On-Campus)
Recently makemytrip visited our Campus and I got selected in recruitment drive.The placement drive consisted of 4 rounds. Round1: MCQ and coding round It was an online test of 60 minutes consisting of 20 aptitude question and 3 coding question. Platform used for the test was of makemytrip and slight
5 min read
MakeMyTrip Interview Experience | Set 8 (On-Campus)
MakeMyTrip recently visited our campus. There were 4 rounds. Online Round ( 1 Hour ) This round consisted of 20 aptitude questions and 3 coding Questions. Coding Questions: 1. Find âxâ in the equation. The input is in the form of a string. Equation consisted of addition operator only and 2 integers
3 min read
C++ Function Call By Pointer
Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference. Example: C++ // C++ Program to demonstrate // Pass by value and // Pass by reference #include <iostream> using namespace std; // Pas
3 min read
Walmart Labs Interview Experience | Set 2 (On-Campus)
1st round (Written Test) It was an online test of 90 minutes and was conducted on Hackerearth. It consisted of 10 MCQâs and 3 coding questions. MCQâs consisted of general aptitude questions, questions related to networking, programming etc and very easy. The coding questions were as follows: 1. http
6 min read
Paytm Interview Experience | Set 9
The test was held at 7 in the morning and then the shortlist was announced within an hour. The test included three questions: Given an array find a subsequence with maximum sum but all the elements must be non-adjacent Given an array consisting of whole numbers, push all the zeroes at the end but th
3 min read
Directi Interview | Set 8 (Off-Campus)
I applied for Directi off-campus on its career website and I got a call. There were total 5 rounds. Online coding round: 1.5 hours There were a total of 3 questions. All of them were coding questions covering ad-hoc, dp ,hashing ,graphs etc and you could do it only if you do regular online competiti
9 min read
LinkedIn Interview Experience | Set 3 (On-Campus)
LinkedIn Interview Experience (On Campus - Day 1) Online Round : 3 coding Questions in 1 hour : 1. We have to implement int getIntComplement(int N) function , that will give complement (bitwise complement. ) of b a given integer . Start unsetting from the left most set bit of the number. 0 There are
3 min read
Traveloka Interview Experience | On -Campus 2019
Traveloka Interview Experience for 6-month SLI + FTE Eligibility: Resume Shortlisting or 7+ CGPA Round 1- MCQs: 50 MCQS in 55-minute MCQs were C++ output based. Eg. calling main() in the main function, static variable, constant variable, recursion, pointer, string manipulation, most of the MCQs were
3 min read
C++ Functions - Pass By Reference
In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so
3 min read