Check if all the elements can be made equal on dividing with X and Y
Given an array arr[] and two integers X and Y. The task is to check whether it is possible to make all the elements equal by dividing them with X and Y any number of times including 0.
Examples:
Input: arr[] = {2, 4, 6, 8}, X = 2, Y = 3
Output: Yes
2 -> 2
4 -> (4 / X) = (4 / 2) = 2
6 -> (6 / Y) = (6 / 3) = 2
8 -> (8 / X) = (8 / 2) = 4 and 4 -> (4 / X) = (4 / 2) = 2Input: arr[] = {2, 4, 10}, X = 11, Y = 12
Output: No
Approach: Find the gcd of all the elements from the given array because this gcd is the value which can we get by dividing all the elements with some arbitary constants say gcd = arr[0] / k1 or arr[1] / k2 or … or arr[n-1] / kn. Now the task is to find whether these constants k1, k2, k3, …, kn are of the form X * X * X * … * Y Y Y * ….. If yes then it is possible to make all the elements equal with the given operation else it isn’t.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if num // is of the form x*x*x*...*y*y*... bool isDivisible(int num, int x, int y) { // While num divisible is divible // by either x or y, keep dividing while (num % x == 0 || num % y == 0) { if (num % x == 0) num /= x; if (num % y == 0) num /= y; } // If num > 1, it means it cannot be // further divided by either x or y if (num > 1) return false; return true; } // Function that returns true if all // the array elements can be made // equal with the given operation bool isPossible(int arr[], int n, int x, int y) { // To store the gcd of the array elements int gcd = arr[0]; for (int i = 1; i < n; i++) gcd = __gcd(gcd, arr[i]); // For every element of the array for (int i = 0; i < n; i++) { // Check if k is of the form x*x*..*y*y*... // where (gcd * k = arr[i]) if (!isDivisible(arr[i] / gcd, x, y)) return false; } return true; } // Driver code int main() { int arr[] = { 2, 4, 6, 8 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 2, y = 3; if (isPossible(arr, n, x, y)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach class GFG { // Function that returns true if num // is of the form x*x*x*...*y*y*... public static boolean isDivisible(int num, int x, int y) { // While num divisible is divible // by either x or y, keep dividing while (num % x == 0 || num % y == 0) { if (num % x == 0) num /= x; if (num % y == 0) num /= y; } // If num > 1, it means it cannot be // further divided by either x or y if (num > 1) return false; return true; } // Funcion to calculate gcd of two numbers // using Euclid's algorithm public static int _gcd(int a, int b) { while (a != b) { if (a > b) a = a - b; else b = b - a; } return a; } // Function that returns true if all // the array elements can be made // equal with the given operation public static boolean isPossible(int[] arr, int n, int x, int y) { // To store the gcd of the array elements int gcd = arr[0]; for (int i = 1; i < n; i++) gcd = _gcd(gcd, arr[i]); // For every element of the array for (int i = 0; i < n; i++) { // Check if k is of the form x*x*..*y*y*... // where (gcd * k = arr[i]) if (!isDivisible(arr[i] / gcd, x, y)) return false; } return true; } // Driver code public static void main(String[] args) { int[] arr = { 2, 4, 6, 8 }; int n = arr.length; int x = 2, y = 3; if (isPossible(arr, n, x, y)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by // sanjeev2552 |
Python3
# Python3 implementation of the approach from math import gcd as __gcd # Function that returns True if num # is of the form x*x*x*...*y*y*... def isDivisible(num, x, y): # While num divisible is divible # by either x or y, keep dividing while (num % x == 0 or num % y == 0): if (num % x == 0): num //= x if (num % y == 0): num //= y # If num > 1, it means it cannot be # further divided by either x or y if (num > 1): return False return True # Function that returns True if all # the array elements can be made # equal with the given operation def isPossible(arr, n, x, y): # To store the gcd of the array elements gcd = arr[0] for i in range(1,n): gcd = __gcd(gcd, arr[i]) # For every element of the array for i in range(n): # Check if k is of the form x*x*..*y*y*... # where (gcd * k = arr[i]) if (isDivisible(arr[i] // gcd, x, y) == False): return False return True # Driver code arr = [2, 4, 6, 8] n = len(arr) x = 2y = 3 if (isPossible(arr, n, x, y) == True): print("Yes") else: print("No") # This code is contributed by mohit kumar 29 |
Yes
Recommended Posts:
- Check if elements of array can be made equal by multiplying given prime numbers
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Check if every index i has an index j such that sum of elements in both directions are equal
- Check if array sum can be made K by three operations on it
- Check whether an array can be made strictly decreasing by modifying at most one element
- Check whether an array can be made strictly increasing by modifying atmost one element
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Count elements such that there are exactly X elements with values greater than or equal to X
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count sub-arrays which have elements less than or equal to X
- Element equal to the sum of all the remaining elements
- XOR of all elements of array with set bits equal to K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



