Lost in the Fest!!
Source: codechef
Difficulty: 872

Lost in the Fest!!
Bhoomi was excited for the annual fest at
IIIT Pune
. She found a spot in the queue, eagerly waiting for the performance. But there was a problem: tall students ahead blocked her view!
Frustrated, she wondered: "How much do I have to move to be able to see the performance?"
There are$N$students who want to watch the performance at the fest. They're standing in a straight queue, and the$i$-th student in the queue has a height of$H_i$.
A student will be able to watch the performance if and only if they are
strictly taller
than every student ahead of them.
That is, student$i$will be able to watch the performance if and only if$H_i \gt H_j$for every$1 \leq j \lt i$.
Bhoomi is the last student in the queue, with a height of$H_N$.
Every second, she can swap places with the person just in front of her - that is, if she's currently at position$i$,
she can move to position$i-1$(and the person previously at position$i-1$will move to position$i$instead).
How many seconds will it take till Bhoomi is able to see the performance?
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of two lines of input.
The first line of each test case contains a single integer$N$— the number of students in the queue.
The second line contains$N$space-separated integers$H_1, H_2, \ldots, H_N$, denoting the heights of the students in the queue.
Output Format
For each test case, output on a new line the answer: the number of seconds till Bhoomi is able to see the performance.
Constraints$1 \leq T \leq 100$$1 \leq N \leq 100$$1 \leq H_i \leq 100$Sample 1:
Explanation:
Test case$1$:
The process is as follows.
Initially, Bhoomi is at position$N = 6$with a height of$3$, and cannot see the performance - for example,$H_5 = 6$is greater than her height.
Second$1$: Bhoomi moves to position$5$, so the heights are now$[2, 4, 1, 7, 3, 5]$. Bhoomi still cannot see the performance.
Second$2$: Bhoomi moves to position$4$, so the heights are now$[2, 4, 1, 3, 7, 5]$. Bhoomi still cannot see the performance.
Second$3$: Bhoomi moves to position$3$, so the heights are now$[2, 4, 3, 1, 7, 5]$. Bhoomi still cannot see the performance.
Second$4$: Bhoomi moves to position$2$, so the heights are now$[2, 3, 4, 1, 7, 5]$.
Bhoomi can now see the performance, so it took four seconds.
Test case$2$:
Bhoomi can see the performance from the start.
Test case$3$:
Bhoomi needs to move to the start of the queue to see the performance, which will take her three seconds.

Sample Input:
3
6
2 4 1 7 5 3
4
1 2 3 4
4
3 1 2 3

Sample Output:
4
0
3

C++ solution:
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<int> a(n);
        for(int i = 0; i < n; i++) cin >> a[i];
        int h = a[n - 1];
        int swaps = 0;
        int pos = n - 1;
        while(pos > 0 && a[pos - 1] >= h){
            swap(a[pos], a[pos - 1]);
            pos--;
            swaps++;
        }
        while(pos > 0){
            bool f = true;
            for(int i = 0; i < pos; i++){
                if(a[i] >= h){
                    f = false;
                    break;
                }
            }
            if(f) break;
            pos--;
            swaps++;
        }
        cout << swaps << "\n";
    }
    return 0;
}

Go solution:
package main
import (
    "fmt"
    "bufio"
    "os"
    "strconv"
    "strings"

)
func main(){
    // your code goes here
    reader := bufio.NewReader(os.Stdin)
    line, _ := reader.ReadString('\n')
    T, _ :=strconv.Atoi(strings.TrimSpace(line))
    
    for t :=0;t<T;t++{
        line, _ := reader.ReadString('\n')
        N, _ := strconv.Atoi(strings.TrimSpace(line))
        line, _ = reader.ReadString('\n')
        parts :=strings.Fields(line)
        heights :=make([]int,N)
        for i:= 0;i<N;i++{
            heights[i], _= strconv.Atoi(parts[i])
        }
        bhm :=heights[N-1]
        posi:=N
        for i:=0;i<N-1;i++{
            if heights[i]>=bhm{
                posi = i+1
                break
            }
        }
        seconds:=N-posi
        fmt.Println(seconds)
    }
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
                Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0) {
            int N = sc.nextInt();
            int[] A = new int[N];
            for (int i = 0; i < N; i++) {
                A[i] = sc.nextInt();
            }
            int max = A[N - 1], temp = N - 1;
            for (int i = N - 2; i >= 0; i--) {
                if (A[i] >= max) {
                    temp = i;
                }
            }
            int sum = N - temp - 1;
            System.out.println(sum);
        }
    }
}

JavaScript solution:
// your code goes here
const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let inputLines = [];
rl.on("line", (line) => {
    inputLines.push(line);
}).on("close", () => {
    let karthikeya = parseInt(inputLines[0]); // Number of test cases
    let index = 1;

    while (karthikeya--) {
        let nithya = parseInt(inputLines[index]); // Number of students
        let love = inputLines[index + 1].split(" ").map(Number); // Heights array
        index += 2;

        let x = love[nithya - 1]; // Bhoomi's height
        let count = 0;

        
        for (let i = 0; i < nithya - 1; i++) {
            if (love[i] < x) count++;
            else break;
        }

        let swaps = nithya - (count + 1);
        console.log(swaps);
    }
});

Python solution:
def time_to_see_performance(test_cases):
    results = []
    for N, heights in test_cases:
        bhoomi_height = heights[-1]
        seconds = 0
        position = N - 1  

        while position > 0:
            
            can_see = True
            for i in range(position):
                if heights[i] >= bhoomi_height:
                    can_see = False
                    break
            if can_see:
                break
            
            heights[position], heights[position - 1] = heights[position - 1], heights[position]
            position -= 1
            seconds += 1

        results.append(seconds)
    return results


# Input reading
T = int(input())
test_cases = []
for _ in range(T):
    N = int(input())
    heights = list(map(int, input().split()))
    test_cases.append((N, heights))

# Process and print results
results = time_to_see_performance(test_cases)
for res in results:
    print(res)

Ruby solution:
def solve
  test_cases = gets.to_i
  test_cases.times do
    n = gets.to_i
    heights = gets.split.map(&:to_i)
    k = heights.last
    answer = 0
    
    (0...n-1).each do |i|
      if heights[i] >= k
        answer = n - i - 1
        break
      end
    end
    
    puts answer
  end
end

solve