Friday, October 7, 2022
HomeWordPress DevelopmentMinimal increments required to make pair of X with Array parts non...

Minimal increments required to make pair of X with Array parts non co-prime


  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

    

    public static void principal(String args[])

    {

  

        

        int n = 5;

  

        

        int arr[] = { 4, 3, 1, 4, 6 };

  

        

        int X = 3;

  

        

        

        int min_operation = 0;

  

        

        

        

        ArrayList<Integer> X_Values = new ArrayList<>();

  

        

        for (int i = 0; i < n; i++) {

  

            

            

            

            int gcd = GCD(X, arr[i]);

  

            

            

            if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1) {

  

                

                

                min_operation += 0;

  

                

                

                X_Values.add(X);

            }

  

            

            

            else if (arr[i] % 2 == 0 || X % 2 == 0) {

  

                

                if (arr[i] % 2 == 0) {

                    

                    

                    X_Values.add(X + 1);

  

                    

                    

                    X += 1;

                }

                else {

  

                    

                    

                    arr[i] += 1;

  

                    

                    

                    X_Values.add(X);

                }

  

                

                

                min_operation += 1;

            }

            else {

  

                

                

                if (GCD(arr[i] + 1, X) > 1

                    || GCD(arr[i], X + 1) > 1) {

  

                    

                    

                    if (GCD(arr[i] + 1, X) > 1) {

  

                        

                        arr[i] += 1;

  

                        

                        

                        X_Values.add(X);

                    }

  

                    

                    

                    else {

  

                        

                        

                        X_Values.add(X + 1);

  

                        

                        X += 1;

                    }

  

                    

                    min_operation += 1;

                }

  

                

                

                else {

  

                    

                    arr[i] += 1;

                    X_Values.add(X + 1);

  

                    

                    X += 1;

  

                    

                    

                    

                    min_operation += 2;

                }

            }

        }

  

        

        

        System.out.println("Minimal operations required : "

                           + min_operation);

  

        

        System.out.println("arr[] : "

                           + Arrays.toString(arr));

  

        

        System.out.println("Values of X :  " + X_Values);

    }

  

    

    

    static int GCD(int a, int b)

    {

        return b == 0 ? a : GCD(b, a % b);

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments