Content of a Polynomial - GeeksforGeeks

22 Apr, 2021
Given an array arr[] which denotes the integer coefficients of the polynomial, the task is to find the content of the polynomial. 
Content of polynomials with integer coefficients is defined as the greatest common divisor of its integer coefficients.That is for:
F(x) = amxm + am-1xm-1 + ........+a1x + a0Then, Content of Polynomial = gcd(am, am-1, am-2...., a1, a0)
Examples: 
Input: arr[]  = {9, 30, 12} Output: 3Explanation:Given Polynomial can be: 9x2 + 30x + 12Therefore, Content = gcd(9, 30, 12) = 3
Input: arr[] = {2, 4, 6}Output: 2
Approach: The idea is to find the Greatest common divisor of all the elements of the array which can be computed by finding the GCD repeatedly by choosing two elements at a time. That is:
gcd(a, b, c)
 = gcd(gcd(a, b), c)
 = gcd(a, gcd(b, c))
 = gcd(gcd(a, c), b)
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to find the// content of the polynomial #include <bits/stdc++.h>using namespace std; #define newl "\n"#define ll long long#define pb push_back // Function to find the content// of the polynomialint findContent(int arr[], int n){    int content = arr[0];     // Loop to iterate over the    // elements of the array    for (int i = 1; i < n; i++) {         //__gcd(a, b) is a inbuilt        // function for Greatest        // Common Divisor        content = __gcd(content, arr[i]);    }     return content;} // Driver Codeint main(){    int n = 3;    int arr[] = { 9, 6, 12 };     // Function call    cout << findContent(arr, n);    return 0;}
// Java implementation to find the// content of the polynomialclass GFG{ // Function to find the content// of the polynomialstatic int findContent(int arr[], int n){    int content = arr[0];     // Loop to iterate over the    // elements of the array    for(int i = 1; i < n; i++)    {                 //__gcd(a, b) is a inbuilt        // function for Greatest        // Common Divisor        content = __gcd(content, arr[i]);    }    return content;} static int __gcd(int a, int b){    return b == 0 ? a : __gcd(b, a % b);    } // Driver Codepublic static void main(String[] args){    int n = 3;    int arr[] = { 9, 6, 12 };     // Function call    System.out.print(findContent(arr, n));}} // This code is contributed by sapnasingh4991
# Python3 implementation to find the# content of the polynomialfrom math import gcd # Function to find the content# of the polynomialdef findContent(arr, n):         content = arr[0]     # Loop to iterate over the    # elements of the array    for i in range(1, n):         # __gcd(a, b) is a inbuilt        # function for Greatest        # Common Divisor        content = gcd(content, arr[i])     return content # Driver Codeif __name__ == '__main__':         n = 3    arr = [ 9, 6, 12 ]     # Function call    print(findContent(arr, n)) # This code is contributed by mohit kumar 29
// C# implementation to find the// content of the polynomialusing System; class GFG{ // Function to find the content// of the polynomialstatic int findContent(int []arr, int n){    int content = arr[0];     // Loop to iterate over the    // elements of the array    for(int i = 1; i < n; i++)    {                 //__gcd(a, b) is a inbuilt        // function for Greatest        // Common Divisor        content = __gcd(content, arr[i]);    }    return content;} static int __gcd(int a, int b){    return b == 0 ? a : __gcd(b, a % b);    } // Driver Codepublic static void Main(String[] args){    int n = 3;    int []arr = { 9, 6, 12 };     // Function call    Console.Write(findContent(arr, n));}} // This code is contributed by PrinciRaj1992
<script> // Javascript implementation to find the// content of the polynomial // Function to find the content// of the polynomialfunction findContent(arr, n){    var content = arr[0];     // Loop to iterate over the    // elements of the array    for(var i = 1; i < n; i++)    {                 //__gcd(a, b) is a inbuilt        // function for Greatest        // Common Divisor        content = __gcd(content, arr[i]);    }    return content;} function __gcd(a, b){    return b == 0 ? a : __gcd(b, a % b);    } // Driver Codevar n = 3;var arr = [ 9, 6, 12 ]; // Function calldocument.write(findContent(arr, n)); // This code is contributed by kirti </script>
  
3
 
 
mohit kumar 29
sapnasingh4991
princiraj1992
Kirti_Mangal
GCD-LCM
maths-polynomial
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Fizz Buzz Implementation
Program to multiply two matrices
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair