A number guessing game generates a secret number and gives the player a limited number of attempts to guess it.
You are given a secret number secret, a maximum number of attempts maxAttempts, and an array of guesses made by the player. Your task is to process each guess, provide the appropriate hint, and determine whether the player wins or loses.
After each incorrect guess, provide a hint:
Lower if the guess is greater than the secret number.Higher if the guess is smaller than the secret number.Win if the guess matches the secret number.Stop the game when the player guesses correctly or runs out of attempts. Report the final result along with the number of attempts used.
Input: secret = 42, maxAttempts = 5, guesses = [50, 25, 42]
Output: Lower, Higher, Win in 3 attempts
Explanation: The first guess, 50, is greater than 42, so the hint is Lower. The second guess, 25, is smaller than 42, so the hint is Higher. The third guess matches the secret number, resulting in a win after 3 attempts.
Input: secret = 10, maxAttempts = 3, guesses = [10]
Output: Win in 1 attempt
Explanation: The first guess matches the secret number, so the player wins immediately using just 1 attempt.
Input: secret = 75, maxAttempts = 2, guesses = [50, 60]
Output: Lose - attempts exhausted
Explanation: Both guesses are smaller than the secret number, so the hints are Higher and Higher. The player uses all 2 attempts without guessing the secret number and loses.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
42 5 3 50 25 42
Lower, Higher, Win in 3 attempts
50 5 4 30 60 40 50
Higher, Lower, Higher, Win in 4 attempts