Equalize all Array elements by dividing with another Array element Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N positive integers (> 0). The task is to check if it is possible to make all array elements equal by performing the given operation. Print "Yes" if possible, else print "No". The operation that can be performed is Choose two indices i, j, 1 < i, j < N, i and j are distinct.Replace a[i] with value a[i] divided by a[j] (ceil value).Examples: Input: N = 3, arr[] = {5, 1, 4}Output: NoExplanation: There exists no sequence of operation which can make all elements equal to 1. Input: N = 4, arr[] = {3, 3, 4, 4}Output: YesExplanation: We can perform the following operations, i = 3, j = 0, arr[3] = ceil(4/3) = 2, Array becomes { 3, 3, 4, 2}i = 2, j = 3, arr[2] = ceil(4/2) = 2, Array becomes { 3, 3, 2, 2}i = 1, j = 3, arr[1] = ceil(3/2) = 2, Array becomes { 3, 2, 2, 2}i = 0, j = 3, arr[0] = ceil(3/2) = 2, Array becomes { 2, 2, 2, 2}Approach: This can be solved with the following idea: Observation 1. If all numbers are equal initially, we have to do nothing & the answer is "Yes".Observation 2. If some ai is 1, then the answer is "No" because this ai can't become bigger during operations and all other elements can't become 1 because after the last operation some aj > 1.Observation 3. If all ai ≥ 2, then the answer is "Yes". We can simulate an algorithm: let's take i, such that ai is the maximum possible, and j, such that aj is the smallest possible. Make operation with (i, j). It is true, because after each operation ai decreases at least by times (and rounded up) and all elements are bounded ax ≥ 2 after each operation (ai >= aj and aj >= 2).Below are the steps involved in the implementation of the code: Initialize allSame = true and isOne = falseFor i = 0 to N - 1, Set allSame = false if arr[i] != arr[0]Set isOne = True if arr[i] equals 1[end for]If allSame equals true, then print "Yes"else if, isOne equals true, then print "No"else print "Yes"Below is the implementation for the above approach: C++ // C++ code of the above approach: #include <bits/stdc++.h> using namespace std; // Function to check whether all elements // can become equal or not void equalizeDividing(int arr[], int n) { // Initialize the variables bool allSame = true, isOne = false; // Perform the algorithm for (int i = 0; i < n; i++) { if (arr[i] != arr[0]) { allSame = false; } if (arr[i] == 1) { isOne = true; } } if (allSame == true) { cout << "Yes" << endl; } else if (isOne == true) { cout << "No" << endl; } else { cout << "Yes" << endl; } } // Driver code int main() { int n1 = 3; int arr1[] = { 5, 1, 4 }; // Function call equalizeDividing(arr1, n1); int n2 = 4; int arr2[] = { 3, 3, 4, 4 }; // Function call equalizeDividing(arr2, n2); int n3 = 4; int arr3[] = { 1, 1, 1, 1 }; // Function call equalizeDividing(arr3, n3); return 0; } Java // java code of the above approach: import java.util.Arrays; class GFG { // Function to check whether all elements // can become equal or not static void equalizeDividing(int[] arr, int n) { // Initialize the variables boolean allSame = true, isOne = false; // Perform the algorithm for (int i = 0; i < n; i++) { if (arr[i] != arr[0]) { allSame = false; } if (arr[i] == 1) { isOne = true; } } if (allSame) { System.out.println("Yes"); } else if (isOne) { System.out.println("No"); } else { System.out.println("Yes"); } } // Driver code public static void main(String[] args) { int n1 = 3; int[] arr1 = { 5, 1, 4 }; // Function call equalizeDividing(arr1, n1); int n2 = 4; int[] arr2 = { 3, 3, 4, 4 }; // Function call equalizeDividing(arr2, n2); int n3 = 4; int[] arr3 = { 1, 1, 1, 1 }; // Function call equalizeDividing(arr3, n3); } } // This code is contributed by shivamgupta0987654321 Python3 # Python3 code of the above approach: # Function to check whether all elements # can become equal or not def equalizeDividing(arr, n): # Initialize the variables allSame = True isOne = False # Perform the algorithm for i in range(n): if arr[i] != arr[0]: allSame = False if arr[i] == 1: isOne = True if allSame: print("Yes") elif isOne: print("No") else: print("Yes") # Driver code if __name__ == "__main__": n1 = 3 arr1 = [5, 1, 4] # Function call equalizeDividing(arr1, n1) n2 = 4 arr2 = [3, 3, 4, 4] # Function call equalizeDividing(arr2, n2) n3 = 4 arr3 = [1, 1, 1, 1] # Function call equalizeDividing(arr3, n3) # This code is contributed by rambabuguphka C# // c# code of the above approach: using System; class GFG { // Function to check whether all elements // can become equal or not static void equalizeDividing(int[] arr, int n) { // Initialize the variables bool allSame = true, isOne = false; // Perform the algorithm for (int i = 0; i < n; i++) { if (arr[i] != arr[0]) { allSame = false; } if (arr[i] == 1) { isOne = true; } } if (allSame) { Console.WriteLine("Yes"); } else if (isOne) { Console.WriteLine("No"); } else { Console.WriteLine("Yes"); } } // Driver code public static void Main(string[] args) { int n1 = 3; int[] arr1 = { 5, 1, 4 }; // Function call equalizeDividing(arr1, n1); int n2 = 4; int[] arr2 = { 3, 3, 4, 4 }; // Function call equalizeDividing(arr2, n2); int n3 = 4; int[] arr3 = { 1, 1, 1, 1 }; // Function call equalizeDividing(arr3, n3); } } // This code is contributed by Tapesh(tapeshdua420) JavaScript // JavaScript code of the above approach: // Function to check whether all elements // can become equal or not function equalizeDividing(arr, n) { // Initialize the variables let allSame = true; let isOne = false; // Perform the algorithm for (let i = 0; i < n; i++) { if (arr[i] !== arr[0]) { allSame = false; } if (arr[i] === 1) { isOne = true; } } if (allSame) { console.log("Yes"); } else if (isOne) { console.log("No"); } else { console.log("Yes"); } } // Driver code let n1 = 3; let arr1 = [5, 1, 4]; // Function call equalizeDividing(arr1, n1); let n2 = 4; let arr2 = [3, 3, 4, 4]; // Function call equalizeDividing(arr2, n2); let n3 = 4; let arr3 = [1, 1, 1, 1]; // Function call equalizeDividing(arr3, n3); // This code is contributed by Tapesh(tapeshdua420) OutputNo Yes YesTime Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Equalize all Array elements by dividing with another Array element I infinity4321cg Follow Improve Article Tags : Greedy Mathematical DSA Greedy Algorithms Constructive Algorithms +1 More Practice Tags : GreedyMathematical Similar Reads Find element in array that divides all array elements Given an array of n non-negative integers. Find such element in the array, that all array elements are divisible by it. Examples : Input : arr[] = {2, 2, 4}Output : 2 Input : arr[] = {2, 1, 3, 1, 6}Output : 1 Input: arr[] = {2, 3, 5}Output : -1 Brute Force Approach: The brute force approach to solve 7 min read Find an array element such that all elements are divisible by it Given an array of numbers, find the number among them such that all numbers are divisible by it. If not possible print -1. Examples: Input : arr = {25, 20, 5, 10, 100} Output : 5 Explanation : 5 is an array element which divides all numbers. Input : arr = {9, 3, 6, 2, 15} Output : -1 Explanation : N 8 min read Elements of an array that are not divisible by any element of another array Given two arrays A[] and B[], write an efficient code to determine if every element of B[] is divisible by at least 1 element of A[]. Display those elements of B[], which are not divisible by any of the elements in A[]. Examples : Input : A[] = {100, 200, 400, 100, 600} B[] = {45, 90, 48, 1000, 3000 10 min read Divide every element of one array by other array elements Given two arrays and the operation to be performed is that the every element of a[] should be divided by all the element of b[] and their floor value has to be calculated. Examples : Input : a[] = {5, 100, 8}, b[] = {2, 3} Output : 0 16 1 Explanation : Size of a[] is 3. Size of b[] is 2. Now 5 has t 6 min read Make Array elements equal by increasing or decreasing the elements by 1 Given an integer N. Consider an array arr having N elements where arr[i] = 2*i + 1. (The array is 0-indexed). Your task is to find the minimum number of such operations required to make all the elements of the array equal where you are allowed to select two indices i and j and increase arr[i] by 1 a 3 min read Equalize an array using array elements only Given an array of integers, the task is to count minimum number of operations to equalize the array (make all array elements same). And return -1 if it is not possible to equalize. To equalize an array, we need to move values from higher numbers to smaller numbers. Number of operations is equal to n 6 min read Count array elements that divide the sum of all other elements Given an array arr[], the task is to count the number of elements from the array which divide the sum of all other elements. Examples: Input: arr[] = {3, 10, 4, 6, 7} Output: 3 3 divides (10 + 4 + 6 + 7) i.e. 27 10 divides (3 + 4 + 6 + 7) i.e. 20 6 divides (3 + 10 + 4 + 7) i.e. 24 Input: arr[] = {1, 9 min read 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 8 min read Count of Array elements to be divided by 2 to make at least K elements equal Given an integer array arr[] of size N, the task is to find the minimum number of array elements required to be divided by 2, to make at least K elements in the array equal.Example : Input: arr[] = {1, 2, 2, 4, 5}, N = 5, K = 3 Output: 1 Explanation: Dividing 4 by 2 modifies the array to {1, 2, 2, 2 9 min read Check if all array elements can be made divisible by K by replacing array elements with sum of pairs Given an array arr[] of size N and a positive integer K, the task is to make every element of the array divisible by K by repeatedly selecting a pair of indices (i, j) and perform arr[i] = arr[i] + arr[j]. Examples: Input: arr[] = {3, 6, 3}, K = 6Output: TrueExplanation: Update arr[0] = arr[0] + arr 7 min read Like