Passing the Assignment
Last Updated :
09 Jul, 2021
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 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 to including both and
- 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 to , 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: [Tex]”Right” [/Tex]and 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++
#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 (s < f) {
dir = 1;
val = "Right" ;
}
else {
dir = -1;
val = "Left" ;
}
string ans = "" ;
int i = 0, current = s;
long tim = 1;
while (1) {
if (i < m && tim == t[i]) {
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep" ;
tim++;
i++;
continue ;
}
i++;
}
current += dir;
ans += val;
tim++;
if (current == f)
break ;
}
cout << ans << endl;
}
int main()
{
int n = 4, m = 4, s = 3, f = 4;
long t[m + 2] = { 1, 2, 3, 4 };
int l[m + 2] = { 2, 1, 3, 2 };
int r[m + 2] = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
return 0;
}
|
Java
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 (s < f) {
dir = 1 ;
val = "Right" ;
}
else {
dir = - 1 ;
val = "Left" ;
}
String ans = "" ;
int i = 0 , current = s;
long tim = 1 ;
while ( 1 > 0 ) {
if (i < m && tim == t[i]) {
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep" ;
tim++;
i++;
continue ;
}
i++;
}
current += dir;
ans += val;
tim++;
if (current == f)
break ;
}
System.out.println(ans);
}
public static void main(String args[])
{
int n = 4 , m = 4 , s = 3 , f = 4 ;
long t[] = { 1 , 2 , 3 , 4 };
int l[] = { 2 , 1 , 3 , 2 };
int r[] = { 4 , 2 , 4 , 3 };
solve(n, m, s, f, t, l, r);
}
}
|
Python3
def solve(n, m, s, f, t, l, r):
val = "";
if (s < f):
dir = 1 ;
val = "Right" ;
else :
dir = - 1 ;
val = "Left" ;
ans = "";
i = 0 ;
current = s;
tim = 1 ;
while ( 1 > 0 ):
if (i < m and tim = = t[i]):
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 ;
current + = dir ;
ans + = val;
tim + = 1 ;
if (current = = f):
break ;
print (ans);
if __name__ = = '__main__' :
n, m, s, f = 4 , 4 , 3 , 4 ;
t = [ 1 , 2 , 3 , 4 ];
l = [ 2 , 1 , 3 , 2 ];
r = [ 4 , 2 , 4 , 3 ];
solve(n, m, s, f, t, l, r);
|
C#
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 (s < f) {
dir = 1;
val = "Right" ;
}
else {
dir = -1;
val = "Left" ;
}
String ans = "" ;
int i = 0, current = s;
long tim = 1;
while (1 > 0) {
if (i < m && tim == t[i]) {
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current +
dir <= r[i]))
{
ans += "Keep" ;
tim++;
i++;
continue ;
}
i++;
}
current += dir;
ans += val;
tim++;
if (current == f)
break ;
}
Console.Write(ans);
}
public static void Main()
{
int n = 4, m = 4, s = 3, f = 4;
long []t = { 1, 2, 3, 4 };
int []l = { 2, 1, 3, 2 };
int []r = { 4, 2, 4, 3 };
solve(n, m, s, f, t, l, r);
}
}
|
Javascript
<script>
function solve(n,m,s,f,t,l,r)
{
let dir;
let val;
if (s < f) {
dir = 1;
val = "Right" ;
}
else {
dir = -1;
val = "Left" ;
}
let ans = "" ;
let i = 0, current = s;
let tim = 1;
while (1 > 0) {
if (i < m && tim == t[i]) {
if ((current >= l[i] && current <= r[i]) ||
(current + dir >= l[i] && current + dir
<= r[i])) {
ans += "Keep" ;
tim++;
i++;
continue ;
}
i++;
}
current += dir;
ans += val;
tim++;
if (current == f)
break ;
}
document.write(ans+ "<br>" );
}
let n = 4, m = 4, s = 3, f = 4;
let t = [ 1, 2, 3, 4 ];
let l = [ 2, 1, 3, 2 ];
let r = [ 4, 2, 4, 3 ];
solve(n, m, s, f, t, l, r);
</script>
|
Output:
KeepRight
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...