There are n spectators in the stadium, labeled from 1 to n.
At time 1, the first spectator stands.
At time 2, the second spectator stands.
At time k, the k-th spectator stands.
At time k + 1, the (k + 1)-th spectator stands and the first spectator sits.
At time k + 2, the (k + 2)-th spectator stands and the second spectator sits.
At time n, the n-th spectator stands and the (n – k)-th spectator sits.
At time n + 1, the (n + 1 – k)-th spectator sits.
At time n + k, the n-th spectator sits.
Find the number of spectators standing in the stadium at a time t.
Input : 10 5 3 Output : 3 Explanation : n = 10, k = 5, t = 3 At time 1, 1st spectator stands. At time 2, 2nd spectator stands. At time 3, 3rd spectator stands. Thus, the result is 3 as there are total of 3 spectators standing. Input :10 5 7 Output : 5 Explanation : n = 10, k = 5, t = 7 At time 1, 1st spectator stands. At time 2, 2nd spectator stands. At time 3, 3rd spectator stands. At time 4, 4th spectator stands. At time 5, 5th spectator stands. At time 6, 6th spectator stands and 1st spectator sits [(n-k) = 6 - 5 = 1 as mentioned above]. At time 7, 7th spectator stands and 2nd spectator sits. So, now there are total of 5 spectators standing. Thus, result is 5.
Now we can observe certain conditions in this problem :
NOTE : There can only be maximum of k spectators standing in the stadium.
1) If the time is less than k value then that means spectators are still standing. So the result would be t.
2) In time between n and k(inclusive), the spectators standing are k. [Observation]
3) After n time, the spectators starts to sit one by one. So we calculate the spectators sitting by ‘t – n’. Then subtract k by this value. This produces the result.
Below is the implementation of the above approach :
Time Complexity : O(1)
Space Complexity : O(1)
This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Write a program to reverse digits of a number
- Program to find whether a no is power of two
- Write an Efficient Method to Check if a Number is Multiple of 3
- Program to find parity
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Find Union and Intersection of two unsorted arrays
- Check if a number is multiple of 5 without using / and % operators
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
Improved By : Sam007