Given an array arr[], the task is to make all the array elements equal with the given operation.
Operation: In a single operation, any element of the array can be either multiplied by 2 or by 3.
If it's possible to make all the array elements equal with the given operation then print true else print false.
Example:
Input: arr[]={100,50,75}
Output: true
Approach
C++
#include <bits/stdc++.h>using namespace std;bool isEqualElements(vector<int> &arr){for (int i = 0; i < arr.size(); i++){while (arr[i] % 2 == 0)arr[i] = arr[i] / 2;while (arr[i] % 3 == 0)arr[i] = arr[i] / 3;}for (int i = 0; i < arr.size(); i++){if (arr[i] != arr[0]){return false;}}return true;}int main(){vector<int> arr = {100, 50, 75};if (isEqualElements(arr))cout << "true";elsecout << "false";}
No comments:
Post a Comment