Please Fill out the Feedback Form for Code Radius

Degree of Polynomial : DPOLY | CodeChef Solution

2 min read

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 x with N terms. The polynomial looks like A_0\cdot x^0 + A_1\cdot x^1 + \ldots + A_{N-2}\cdot x^{N-2} + A_{N-1}\cdot x^{N-1}A0​⋅x0+A1​⋅x1+…+AN−2​⋅xN−2+AN−1​⋅xN−1 where A_{i-1}Ai−1​ denotes the coefficient of the i^{th}ith term x^{i-1}xi−1 for all (1\le i\le 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 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 ith integer Ai-1 corresponds to the coefficient of xi-1.

Output Format

For each test case, output in a single line, the degree of the polynomial.

Constraints

Sample

Input
4
1
5
2
-3 3
3
0 0 5
4
1 2 4 0
Output
0
1
2
2

Explanation

Test case 1: There is only one term x^0x0 with coefficient 5. Thus, we are given a constant polynomial and the degree is 0.
Test case 2: The polynomial is -3\cdot x^0 + 3\cdot x^1 = -3 + 3\cdot x−3⋅x0+3⋅x1=−3+3⋅x. Thus, the highest power of x with non-zero coefficient is 1.
Test case 3: The polynomial is 0\cdot x^0 + 0\cdot x^1 + 5\cdot x^2= 0+0 + 5\cdot x^20⋅x0+0⋅x1+5⋅x2=0+0+5⋅x2. Thus, the highest power of xwith non-zero coefficient is 2.
Test case 4: The polynomial is 1\cdot x^0 + 2\cdot x^1+ 4\cdot x^2 + 0\cdot x^3= 1 + 2\cdot x + 4\cdot x^21⋅x0+2⋅x1+4⋅x2+0⋅x3=1+2⋅x+4⋅x2. Thus, the highest power of x with non-zero coefficient is 2.

Solution

cpp
#include <bits/stdc++.h> using namespace std; // Solution from : Code Radius [ https://radiuscode.blogspot.com/ ] int main() { int a,t; cin>>t; for(a=0;a<t;a++) { int n,a[1000],i; cin>>n; for(i=0;i<n;i++) { cin>>a[i]; } for(i=n-1;i>=0;i--) { if(a[i]!=0) { break; } } cout<<i<<endl; } return 0; }
Please First Try to Solve Problem by Yourself.

You may like these posts

  • ProblemYou and your friend are playing a game with hoops. There are N hoops (where N is odd) in a row. You jump into hoop 1, and your friend jumps into hoop N. Then you jump into h…
  • ProblemIf Give an integer N . Write a program to obtain the sum of the first and last digits of this number. Please Share this article... Input Format The first line contains a…
  •  ProblemWrite a program to obtain length (L) and breadth (B) of a rectangle and check whether its area is greater or perimeter is greater or both are equal. Please Share thi…
  • ProblemAlice and Bob are meeting after a long time. As usual they love to play some math games. This times Alice takes the call and decides the game. The game is very simple, Alice…
  •  ProblemChef is a big fan of Coldplay. Every Sunday, he will drive to a park taking M minutes to reach there, and during the ride he will play a single song on a loop. Today, …
  •  ProblemChefland has 2 different types of coconut, type A and type B. Type A contains only x_a milliliters of coconut water and type B contains only x_b grams of coconut pulp.…

Post a Comment