Christmas Candy-Python

Friends in a Line:
There are N friends numbered from 1 to N, seated in a straight line.
The ith seat is taken by friend numbered A[i].

Candy Distribution:
The friends decide to distribute candies among each other.

Candy-Gifting Criteria:
For each case, the friend at the ith seat (with number A[i]) will gift one candy to the friend at the jth seat if:
j>i (Friend at seat j comes after Friend at seat i)
A[j] < A[i] (Friend at seat j has a lower number than Friend at seat i)

Goal:
Find the number of people who receive at least one candy.

The solution involves iterating through the seats, checking the conditions, and counting the friends who receive at least one candy.

Code:


t = int(input())
for _ in range(t):
    n = int(input())
    b = list(map(int, input().split()))

    t = b[0]
    r = set()

    for i in range(1, n):
        if b[i] < t:
            r.add(i)
        t = max(b[i], t)

    print(len(r))