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.

**Examples :**

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.

**Approach :**

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 :

## C++

`// CPP program to find number of spectators ` `// standing at a time ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `result(` `long` `long` `n, ` `long` `long` `k, ` `long` `long` `t) ` `{ ` ` ` `// If the time is less than k ` ` ` `// then we can print directly t time. ` ` ` `if` `(t <= k) ` ` ` `cout << t; ` ` ` ` ` `// If the time is n then k spectators ` ` ` `// are standing. ` ` ` `else` `if` `(t <= n) ` ` ` `cout << k; ` ` ` ` ` `// Otherwise we calculate the spectators ` ` ` `// standing. ` ` ` `else` `{ ` ` ` `long` `long` `temp = t - n; ` ` ` `temp = k - temp; ` ` ` `cout << temp; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Stores the value of n, k and t ` ` ` `// t is time ` ` ` `// n & k is the number of specators ` ` ` `long` `long` `n, k, t; ` ` ` `n = 10; ` ` ` `k = 5; ` ` ` `t = 12; ` ` ` `result(n, k, t); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of spectators ` `// standing at a time ` ` ` `class` `GFG { ` ` ` ` ` `static` `void` `result(` `long` `n, ` `long` `k,` `long` `t) ` ` ` `{ ` ` ` `// If the time is less than k ` ` ` `// then we can print directly t time. ` ` ` `if` `(t <= k) ` ` ` `System.out.println(t); ` ` ` ` ` `// If the time is n then k spectators ` ` ` `// are standing. ` ` ` `else` `if` `(t <= n) ` ` ` `System.out.println(k); ` ` ` ` ` `// Otherwise we calculate the ` ` ` `// spectators standing. ` ` ` `else` `{ ` ` ` `long` `temp = t - n; ` ` ` `temp = k - temp; ` ` ` `System.out.println(temp); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `// Stores the value of n, k and t ` ` ` `// t is time ` ` ` `// n & k is the number of specators ` ` ` `long` `n, k, t; ` ` ` `n = ` `10` `; ` ` ` `k = ` `5` `; ` ` ` `t = ` `12` `; ` ` ` `result(n, k, t); ` ` ` ` ` `} ` `} ` ` ` `/*This code is contributed by Nikita Tiwari.*/` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find number of spectators ` `# standing at a time ` ` ` `def` `result(n, k, t) : ` ` ` ` ` `# If the time is less than k ` ` ` `# then we can print directly t time. ` ` ` `if` `(t <` `=` `k) : ` ` ` `print` `(t ) ` ` ` ` ` `# If the time is n then k spectators ` ` ` `# are standing. ` ` ` `elif` `(t <` `=` `n) : ` ` ` `print` `( k) ` ` ` ` ` `# Otherwise we calculate the ` ` ` `# spectators standing. ` ` ` `else` `: ` ` ` ` ` `temp ` `=` `t ` `-` `n ` ` ` `temp ` `=` `k ` `-` `temp ` ` ` `print` `(temp) ` ` ` `# Driver code ` ` ` `# Stores the value of n, k and t ` `# t is time ` `# n & k is the number of specators ` `n ` `=` `10` `k ` `=` `5` `t ` `=` `12` `result(n, k, t) ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of ` `// spectators standing at a time ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `void` `result(` `long` `n, ` `long` `k,` `long` `t) ` ` ` `{ ` ` ` `// If the time is less than k ` ` ` `// then we can print directly t time. ` ` ` `if` `(t <= k) ` ` ` `Console.WriteLine(t); ` ` ` ` ` `// If the time is n then k spectators ` ` ` `// are standing. ` ` ` `else` `if` `(t <= n) ` ` ` `Console.WriteLine(k); ` ` ` ` ` `// Otherwise we calculate the ` ` ` `// spectators standing. ` ` ` `else` `{ ` ` ` `long` `temp = t - n; ` ` ` `temp = k - temp; ` ` ` `Console.WriteLine(temp); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// Stores the value of n, k and t ` ` ` `// t is time ` ` ` `// n & k is the number of specators ` ` ` `long` `n, k, t; ` ` ` `n = 10; ` ` ` `k = 5; ` ` ` `t = 12; ` ` ` `result(n, k, t); ` ` ` ` ` `} ` `} ` ` ` `//This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number of ` `// spectators standing at a time ` ` ` `function` `result(` `$n` `, ` `$k` `, ` `$t` `) ` `{ ` ` ` `// If the time is less ` ` ` `// than k then we can ` ` ` `// print directly t time. ` ` ` `if` `(` `$t` `<= ` `$k` `) ` ` ` `echo` `t; ` ` ` ` ` `// If the time is n then ` ` ` `// k spectators are standing. ` ` ` `else` `if` `(` `$t` `<= ` `$n` `) ` ` ` `echo` `k; ` ` ` ` ` `// Otherwise we calculate ` ` ` `// the spectators standing. ` ` ` `else` ` ` `{ ` ` ` `$temp` `= ` `$t` `- ` `$n` `; ` ` ` `$temp` `= ` `$k` `- ` `$temp` `; ` ` ` `echo` `$temp` `; ` ` ` `} ` `} ` ` ` `// Driver code ` ` ` `// Stores the value of n, k and t ` `// t is time ` `// n & k is the number of specators ` `$n` `= 10; ` `$k` `= 5; ` `$t` `= 12; ` `result(` `$n` `, ` `$k` `, ` `$t` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3

**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 contribute@geeksforgeeks.org. 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour
- Minimum time to reach a point with +t and -t moves at time t
- Find Index of given fibonacci number in constant time
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Program to find the time remaining for the day to complete
- Number of ways to arrange K different objects taking N objects at a time
- Number of containers that can be filled in the given time
- Find minimum number to be divided to make a number a perfect square
- Find the smallest number whose digits multiply to a given number n
- Find n'th number in a number system with only 3 and 4
- Find the Largest number with given number of digits and sum of digits
- Find count of digits in a number that divide the number
- Find if a number is divisible by every number in a list
- Find maximum number that can be formed using digits of a given number
- Given a number N in decimal base, find number of its digits in any base (base b)
- Find the largest good number in the divisors of given number N
- Find M-th number whose repeated sum of digits of a number is N
- Given number of matches played, find number of teams in tournament
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the maximum number of composite summands of a number