Skip to content
Related Articles

Related Articles

Interactive Problems in Competitive Programming | Set 2

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 09 Sep, 2019

Introduction to interactive problems in competitive programming is already discussed in the previous article. In this article, another problem based on the same concept is discussed.

Assume that the judge has a hidden (it means the user can’t access it) sorted array.

The user (i.e. you) need to tell whether a given element VAL lies in that sorted array space given by starting and ending indexes as START and END respectively.
In order for the user to tell whether VAL exists or not in the given array space, the user needs to interact with the judge so that the user can get to know the value at a particular index with the help of the judge.

Let’s take an example:
The user needs to find VAL = 2 between START = 0 and END = 5.
The judge stores the sorted array arr[] = {1, 2, 3, 4, 5, 6} (0-based indexing).
Now the user is allowed to give input as indexes to the judge and the judge has to respond the user with the value at that index.
Basically, for some input index by the user, the judge responds with the value at that input index and this process continues till the user finds finally that whether the value VAL is in the array or not.

The user tries to attempt a binary search as the array is sorted. Here’s a part of the program which the user uses while implementing binary search as:

    if VAL is equal to Value_Given_By_Judge
        Prints "Value exist" and return 
    else if VAL is less than A[mid]
        end = mid-1
    else if VAL is greater than A[mid]
        start = mid+1

Two variables used by the user are start = 0 and end = 5

Following interaction happens:

User calculates mid = (start + end) / 2 = 2;
Judge outputs 3 // Value at index 2
User updates mid = (start + end) / 2 = 4
Judge OUTPUT : 5 // Value at index 4
User again performs TASK and prints "Value exists" 
and returns as it finds VAL  

So, in order to interact with the online judge, it is required that the online judge takes input from the user and then give some corresponding output which can be taken in as input. It is similar to printing input/output on the output screen but in some languages like C++, Python, Java etc printing to output screen is a time consuming/costly process so what happens is that the whole output of program is stored in a buffer area and then after the program terminates, all the values from buffer are printed on to the output screen altogether. But this can create a problem with interactive problems because the online judge will take input from only the output screen (and not the buffer), so it is required to transfer the content of the buffer to the actual output screen. This transfer is actually called pushing as we are forcing the buffer to load its content onto output screen.

Commands Used for Pushing are as follows:

  1. C++: fflush(stdout)
  2. Java: System.out.flush()
  3. Python:
    import sys

Following Questions can be practiced to develop some interactive problem skills and to get familiar with how it works.
Problem 1
Problem 2

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!