Passing the Assignment in C++


In this tutorial, we have to write an algorithm to find a way to pass an assignment without being caught by the invigilator. Each student has to submit their assignment to the invigilator. Student A's assignment is with Student B, so Student B has to return/pass the assignment to Student A without the invigilator noticing them.

All the students are sitting in a queue. We need to find a way to pass the assignment back to Student A without being caught. Various requirements under which they can pass assignments are as follow −

  • Student A (At index i) can pass assignments to its neighbor who is at index (i-1) and (i+1)

  • Students can give, receive or retain the assignment with them.

  • The invigilator is watching all the students from the index [il, rl].

  • When the student is in the range of the invigilator's watch, they cannot send or receive the assignment.

  • If the student retains the assignment with them while in that range, the invigilator won't catch them.

We are given four inputs p, q, r, s where p is the number of total students, q is the total number of steps during which the invigilator is keeping watch from il to rl, c is the position of Student A, and d is the position of Student B.

Each step (q) has three inputs −

  • The total time the invigilator will be keeping a watch over the given range.

  • In the leftmost inclusive range, the invigilator is watching.

  • The rightmost inclusive range, the invigilator, is watching.

An output sequence of 3 words is required: "Left," "Right," and "Keep," denoting the activity of the student if they are passing the assignment(left/right) or keeping it. For Example,

Steps

Input

8 3 2 7
1 4 6
2 1 8
3 5 6

Output

Right
Keep
Right
Right
Right
Right

Explanation

By following these instructions, assignment would reach from student at index 2 to the student at index 7 without being caught.

Input

5 1 1 3
1 2 5

Output

Keep
Right
Right

Explanation

By following these instructions, assignment would reach from student at index 1 to the student at index 3 without being caught.

Approach to Find the Solution

At a given instance, if the invigilator is keeping a watch in that range, either the student currently having the assignment or the student to whom the assignment is to be sent, then that student will retain that assignment with him. Else he passes it to his adjacent student in the direction of the final target.

Example

#include <bits/stdc++.h>
using namespace std;
void solve(int p, int q, int r, int s,
long t[], int l[], int ar[]){
   int dir;
   string val;
   if (r < s) {
      dir = 1;
      val = "Right";
   } else {
      dir = -1;
      val = "Left";
   }
   string answer = "";
   int i = 0, current = r;
   long tim = 1;
   while (1) {
      if (i < q && tim == t[i]) {
         if ((current >= l[i] && current <= ar[i]) ||
         (current + dir >= l[i] && current + dir <= ar[i])) {
            answer += "Keep\n";
            tim++;
            i++;
            continue;
         }
         i++;
      }
      current += dir;
      answer += val+"\n";
      tim++;
      if (current == s)
         break;
   }
   cout << answer << endl;
}
int main(){
   int p = 8, q = 3, r = 2, s = 7;
   long t[q + 2] = { 1,2,3 };
   int l[q + 2] = { 4,1,5 };
   int ar[q + 2] = { 6,8,6 };
   solve(p, q, r, s, t, l, ar);
   return 0;
}

Output

Right
Keep
Right
Right
Right
Right

Conclusion

In this tutorial, we learned to write an algorithm to find a way to pass an assignment without being caught by the invigilator along with the c++ code. We can also write this code in java, python, and other languages. The above algorithm is an important algorithm for competitive coding competitions. This question includes a real-life problem, which we solved through C++ code. We hope you find this tutorial helpful.

Updated on: 07-Mar-2022

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements