Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIGCD of array is greater than one

GCD of array is greater than one

Given an array of n integers. The array is considered best if GCD of all its elements is greater than 1. If the array is not best, we can choose an index i (1 <= i < n) and replace numbers ai and ai + 1 by ai – ai + 1 and ai + ai + 1 respectively. Find the minimum number of operations to be done on the array to make it best.

Examples: 

Input : n = 2
        a[] = [1, 1]
Output : 1
Explanation:
Here, gcd(1,1) = 1. So, to make 
it best we have to replace array 
by [(1-1), (1+1)] = [0, 2]. Now, 
gcd(0, 2) > 1. Hence, in one
operation array become best.

Input : n = 3
        a[] = [6, 2, 4]
Output :0
Explanation:
Here, gcd(6,2,4) > 1.
Hence, no operation is required. 

We first calculate the gcd(array) and check whether it is greater than 1. If yes then the array is already best else we greedily check for no. of moves required to make all ones by using the property that when there are two odd numbers then you can make them even in one move else if there is one odd and one even then you require two moves.

Below is the implementation of the above approach: 

C++




// CPP program to find bestArray
#include<bits/stdc++.h>
using namespace std;
 
// Calculating gcd of two numbers
int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b%a, a);
}
 
void bestArray(int arr[], int n){
        bool even[n] = {false};
        int ans = 0;
 
        // calculating gcd and
        // counting the even numbers
        for(int i = 0; i < n; i++){
            ans = gcd(ans, arr[i]);
            if(arr[i] % 2 == 0)
                even[i] = true;
        }
 
        // check array is already best
        if(ans > 1)
            cout << 0 << endl;
        else{
 
            // counting the number
            // of operations required.
            ans = 0;
            for(int i = 0; i < n-1; i++){
                if(!even[i]){
                    even[i] = true;
                    even[i+1] = true;
                    if(arr[i+1]%2 != 0){
                        ans+=1;
                    }
                    else
                        ans+=2;
                }
            }
            if(!even[n-1]){
                ans+=2;
            }
            cout << ans << endl;
        }
}
 
// driver code
int main(){
     
        int arr[] = {57, 30, 28, 81, 88, 32, 3, 42, 25};
        int n = 9;
        bestArray(arr, n);
 
        int arr1[] = {1, 1};
        n = 2;
        bestArray(arr1, n);
 
        int arr2[] = {6, 2, 4};
        n = 3;
        bestArray(arr2, n);
}
 
/*This code is contributed by Sagar Shukla.*/


Java




// Java code to find best array
import java.util.*;
import java.lang.*;
 
public class neveropen{
     
    // function to calculate gcd of two numbers.
    public static int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b%a, a);
    }
 
    public static void bestArray(int arr[], int n){
        boolean even[] = new boolean[n];
        int ans = 0;
        for(int i=0; i<n; i++)
            even[i] = false;
 
        // calculating gcd and
        // counting the even numbers
        for(int i=0; i<n; i++){
            ans = gcd(ans, arr[i]);
            if(arr[i]%2 == 0)
                even[i] = true;
        }
         
        // check array is already best
        if(ans > 1)
            System.out.println(0);
        else{
             
            // counting the number of operations required.
            ans = 0;
            for(int i=0; i<n-1; i++){
                if(!even[i]){
                    even[i] = true;
                    even[i+1] = true;
                    if(arr[i+1]%2 != 0){
                        ans+=1;
                    }
                    else
                        ans+=2;
                }
            }
            if(!even[n-1]){
                ans+=2;
            }
            System.out.println(ans);
        }
    }
     
    // driver code
    public static void main(String argc[]){
         
        int arr[] = {57, 30, 28, 81, 88, 32, 3, 42, 25};
        int n = 9;
        bestArray(arr, n);
         
        int arr1[] = {1, 1};
        n = 2;
        bestArray(arr1, n);
         
        int arr2[] = {6, 2, 4};
        n = 3;
        bestArray(arr2, n);
    }
}
 
/*This code is contributed by Sagar Shukla.*/


Python




# code to find the best array
from fractions import gcd
 
def bestArray(a,n):
 
    even = [False]*n
    ans = 0
 
    # calculating the gcd and
    # counting the even numbers
    for i in xrange(n):
        ans = gcd(ans, a[i])
        if a[i]%2 == 0:
            even[i] = True
 
    # check if array is already best.
    if ans > 1:
        print (0)
    else:      
 
        # calculating the no of
        # operations required.
        ans = 0
        for i in xrange(n-1):
            if not even[i]:
                even[i] = True
                even[i+1] = True
                if a[i+1]%2 != 0:
                    ans += 1
                else:
                    ans += 2
        if not even[n-1]:
            ans += 2
        print (ans)
 
# driver code
n = 9
a = [57, 30, 28, 81, 88, 32, 3, 42, 25]
bestArray(a,n)
n = 2
a = [1, 1]
bestArray(a,n)
n = 3
a = [6, 2, 4]
bestArray(a,n)


C#




// C# code to find best array
using System;
 
public class GFG {
     
    // function to calculate gcd
    // of two numbers.
    public static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
             
        return gcd(b % a, a);
    }
 
    public static void bestArray(int []arr, int n)
    {
        bool []even = new bool[n];
        int ans = 0;
         
        for(int i = 0; i < n; i++)
            even[i] = false;
 
        // calculating gcd and
        // counting the even numbers
        for(int i = 0; i < n; i++)
        {
            ans = gcd(ans, arr[i]);
             
            if(arr[i] % 2 == 0)
                even[i] = true;
        }
         
        // check array is already best
        if(ans > 1)
            Console.WriteLine(0);
        else
        {
             
            // counting the number of
            // operations required.
            ans = 0;
            for(int i = 0; i < n-1; i++)
            {
                if(!even[i])
                {
                    even[i] = true;
                    even[i+1] = true;
                    if(arr[i+1] % 2 != 0)
                        ans += 1;
                    else
                        ans += 2;
                }
            }
            if(!even[n-1])
                ans += 2;
                 
            Console.WriteLine(ans);
        }
    }
     
    // driver code
    public static void Main()
    {
         
        int []arr = {57, 30, 28, 81, 88,
                            32, 3, 42, 25};
        int n = 9;
        bestArray(arr, n);
         
        int []arr1 = {1, 1};
        n = 2;
        bestArray(arr1, n);
         
        int []arr2 = {6, 2, 4};
        n = 3;
        bestArray(arr2, n);
    }
}
 
// This code is contributed by vt_m.


Javascript




<script>
 
// JavaScript code to find best array
 
       
    // function to calculate gcd
    // of two numbers.
    function gcd( a,  b)
    {
        if (a == 0)
            return b;
               
        return gcd(b % a, a);
    }
   
    function bestArray(arr,  n)
    {
        var even = new Array(n);
        var ans = 0;
           
        for(var i = 0; i < n; i++)
            even[i] = false;
   
        // calculating gcd and
        // counting the even numbers
        for(var i = 0; i < n; i++)
        {
            ans = gcd(ans, arr[i]);
               
            if(arr[i] % 2 == 0)
                even[i] = true;
        }
           
        // check array is already best
        if(ans > 1)
            document.write(0);
        else
        {
               
            // counting the number of
            // operations required.
            ans = 0;
            for(var i = 0; i < n-1; i++)
            {
                if(!even[i])
                {
                    even[i] = true;
                    even[i+1] = true;
                    if(arr[i+1] % 2 != 0)
                        ans += 1;
                    else
                        ans += 2;
                }
            }
            if(!even[n-1])
                ans += 2;
                   
            document.write(ans + "<br>");
        }
    }
       
    // driver code
 
           
         var arr = [57, 30, 28, 81, 88,
                            32, 3, 42, 25];
        var n = 9;
        bestArray(arr, n);
           
        var arr1 = [1, 1];
        n = 2;
        bestArray(arr1, n);
           
        var arr2 = [6, 2, 4];
        n = 3;
        bestArray(arr2, n);
         
</script>


Output:  

8
1
0

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments