Category: HCF

Sort an array in increasing order of GCD of their digits

Sort an array in increasing order of GCD of their digitsGiven an array arr[] consisting of N positive integers, the task is to sort the array arr[] according to the increasing order of GCD of digits of each element. If GCD of two or more elements are the same then, sort according to their values.Examples:Input: […]

Continue Reading

Modify array by replacing elements with their farthest co-prime number from a given range

import java.io.*;import java.lang.*;import java.util.*;class GFG{static int gcd(int a, int b){             if (a == 0)        return b;        return gcd(b % a, a);}static void update(int arr[], int n){             for(int i = 0; i < n; i++)    {                                 int d = 0;                        int coPrime = -1;                for(int j = 2; j

Continue Reading

Generate an N-length sequence from two given arrays whose GCD is K

         def GCD(a, b):     if not b:         return a     return GCD(b, a % b)    def GCDArr(a):     ans = a[0]     for i in a:         ans = GCD(ans, i)     return ans    def findSubseqUtil(a, b, ans, k, i):                  if len(ans) == len(a):                              if GCDArr(ans) == k:             print(ans)             return […]

Continue Reading