Codechef•Jul 03, 2025
Degree of Polynomial
Hazrat Ali
Codechef
Chef has a polynomial in one variable x with N terms. The polynomial looks like A0⋅x0+A1⋅x1+…+AN−2⋅xN−2+AN−1⋅xN−1where Ai−1Ai−1 denotes the coefficient of the ithith term xi−1xi−1 for all (1≤i≤N).
Find the degree of the polynomial.
Note: It is guaranteed that there exists at least one term with non-zero coefficient.
Input Format
- First line will contain T, number of test cases. Then the test cases follow.
- First line of each test case contains of a single integer N - the number of terms in the polynomial.
- Second line of each test case contains of N space-separated integers - the ithith integer Ai−1Ai−1 corresponds to the coefficient of xi−1xi−1.
Output Format
For each test case, output in a single line, the degree of the polynomial.
Constraints
- 1≤T≤100
- 1≤N≤1000
- −1000≤Ai≤1000
- Ai≠0 for at least one (0≤i<N).
Sample 1:
Input
4 1 5 2 -3 3 3 0 0 5 4 1 2 4 0
Output
0 1 2 2
Solution
T = int(input())
for _ in range(T):
N = int(input())
A = list(map(int, input().split()))
degree = 0
for i in range(N):
if A[i] != 0:
degree = i
print(degree)