VOOZH about

URL: https://www.geeksforgeeks.org/dsa/interactive-problems-in-competitive-programming/

⇱ Interactive Problems in Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Interactive Problems in Competitive Programming

Last Updated : 28 Dec, 2023

Interactive Problems are those problems in which our solution or code interacts with the judge in real time. When we develop a solution for an Interactive Problem then the input data given to our solution may not be predetermined but is built for that problem specifically. The solution performs a series of exchange of data with the judge and at the end of the conversation the judge decides whether our solution was correct or not.

Guessing the Number (An Interactive Problem)


In this problem the user has to guess the number during a communication with the judge. The user is provided with the upper and lower bound and he/she can ask the judge whether a number is the number to be guessed. The judge replies with -1 if the number is smaller than the number to be guessed or 1 if number is greater than the number to be guessed or 0 if it is equal to the number to be guessed.


Approach 1 : Linear Guessing 


The user can query the judge for all the numbers between lower limit and upper limit to find the solution. 

👁 Approach 1



Output
2
Number guessed is :2

Time Complexity: O(n)


Approach 2 : Applying Binary Search 


We can also apply binary search interactively to find the solution. This solution is efficient as compared to the previous approach.

👁 Image



Output
6
Number guessed is :6

Time Complexity: O(logn) 
Algorithm Paradigm: Divide and Conquer

Comment
Article Tags:
Article Tags: