Degree of Polynomial: DPOLY

                     

Degree of Polynomial CodeChef Solution

CU Submission link

Public Submission link

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 xx with NN terms. The polynomial looks like A0⋅x0+A1⋅x1+…+AN−2⋅xN−2+AN−1⋅xN−1A0⋅x0+A1⋅x1+…+AN−2⋅xN−2+AN−1⋅xN−1 where Ai−1Ai−1 denotes the coefficient of the ithith term xi−1xi−1 for all (1≤i≤N)(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 TT, number of test cases. Then the test cases follow.
  • First line of each test case contains of a single integer NN – the number of terms in the polynomial.
  • Second line of each test case contains of NN 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≤1001≤T≤100
  • 1≤N≤10001≤N≤1000
  • −1000≤Ai≤1000−1000≤Ai≤1000
  • Ai≠0Ai≠0 for at least one (0≤i<N)(0≤i<N).

Sample Input 1 

4
1
5
2
-3 3
3
0 0 5
4
1 2 4 0

Sample Output 1 

0
1
2
2

Explanation

Test case 11: There is only one term x0x0 with coefficient 55. Thus, we are given a constant polynomial and the degree is 00.

Test case 22: The polynomial is −3⋅x0+3⋅x1=−3+3⋅x−3⋅x0+3⋅x1=−3+3⋅x. Thus, the highest power of xx with non-zero coefficient is 11.

Test case 33: The polynomial is 0⋅x0+0⋅x1+5⋅x2=0+0+5⋅x20⋅x0+0⋅x1+5⋅x2=0+0+5⋅x2. Thus, the highest power of xx with non-zero coefficient is 22.

Test case 44: The polynomial is 1⋅x0+2⋅x1+4⋅x2+0⋅x3=1+2⋅x+4⋅x21⋅x0+2⋅x1+4⋅x2+0⋅x3=1+2⋅x+4⋅x2. Thus, the highest power of xx with non-zero coefficient is 22.


Degree of Polynomial CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
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=0;
		    for(int i=0;i< n;i++)
		    {
		        if(a[i]!=0)
		        {
		            max=i;
		        }
		    }
		    System.out.println(max);
		    t--;
		}
	}
}

Degree of Polynomial CodeChef Solution in C++ 17



int main() {
	int t;
	cin>>t;
	while(t--){
	    int n,ans;
	    cin>>n;
	    int a[n];
	    for(int i=0;i< n;i++){
	        cin>>a[i];
	        if(a[i]!=0) ans=i;
	    }
	    cout<< ans<< endl;
	}
	return 0;
} 

* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


This is Degree of Polynomial CodeChef Solution

Click here for more CodeChef problems and solutions

Recently added progies