C++ code to number of standing spectators at time t

Suppose we have three numbers n, k and t. Amal is analyzing Mexican waves. There are n spectators numbered from 1 to n. They start from time 0. At time 1, first spectator stands, at time 2, second spectator stands. At time k, kth spectator stands then at time (k+1) the (k+1) th spectator stands and first spectator sits, at (k+2), the (k+2)th spectator stands but 2nd one sits, now at nth time, nth spectator stands and (n-k)th spectator sits. At time (n+1), the (n+1-k)th spectator sits and so on. We have to find the number of spectators stands at time t.

So, if the input is like n = 10; k = 5; t = 3, then the output will be 3, because before 5, no one will sit so all spectators from 1 to 3 are standing.


To solve this, we will follow these steps −

return minimum of t, k and (n + k - t)


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int n, int k, int t){
   return min({ t, k, n + k - t });
int main(){
   int n = 10;
   int k = 5;
   int t = 3;
   cout << solve(n, k, t) << endl;


10, 5, 3