Warning: Undefined array key "pathway" in /home/clients/160b20c93964292618c158d21ce27bf5/sites/tech.org-services.ch/wp-content/themes/Newspaper/functions.php on line 543
Monday, December 5, 2022
HomeSoftware DevelopmentMinimal increments required to make pair of X with Array components non...

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


  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

    

    public static void predominant(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

Most Popular

Recent Comments