Problem
In mathematics, the degree of polynomials in one variable is the highest power of the variable in the algebraic expression with non-zero coefficient.
Chef has a polynomial in one variable with terms. The polynomial looks like where denotes the coefficient of the term for all .
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 , number of test cases. Then the test cases follow.
- First line of each test case contains of a single integer - the number of terms in the polynomial.
- Second line of each test case contains of space-separated integers - the integer corresponds to the coefficient of .
Output Format
For each test case, output in a single line, the degree of the polynomial.
Constraints
- for at least one .
Sample 1:
4 1 5 2 -3 3 3 0 0 5 4 1 2 4 0
0 1 2 2
Explanation:
Test case : There is only one term with coefficient . Thus, we are given a constant polynomial and the degree is .
Test case : The polynomial is . Thus, the highest power of with non-zero coefficient is .
Test case : The polynomial is . Thus, the highest power of with non-zero coefficient is .
Test case : The polynomial is . Thus, the highest power of with non-zero coefficient is .
Program :
import java.util.*;
import java.lang.Math.*;
class Codechef
{
public static void main(String args[])
{
Scanner es = new Scanner(System.in);
int t,n,i,j,p,k;
int a[]= new int[1000];
t=es.nextInt();
for(i=0;i<t;i++)
{
n=es.nextInt();
p=n-1;
for(j=0;j<n;j++)
a[j]=es.nextInt();
for(k=0;k<n;k++)
{
if(a[k]!=0)
p=k;
}
System.out.println(p);
}
es.close();
}
}
0 Comments