Equalize all Array elements by dividing with another Array element
Last Updated :
17 Aug, 2023
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: No
Explanation: There exists no sequence of operation which can make all elements equal to 1.
Input: N = 4, arr[] = {3, 3, 4, 4}
Output: Yes
Explanation: 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 = false
- For 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++
#include <bits/stdc++.h>
using namespace std;
void equalizeDividing( int arr[], int n)
{
bool allSame = true , isOne = false ;
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;
}
}
int main()
{
int n1 = 3;
int arr1[] = { 5, 1, 4 };
equalizeDividing(arr1, n1);
int n2 = 4;
int arr2[] = { 3, 3, 4, 4 };
equalizeDividing(arr2, n2);
int n3 = 4;
int arr3[] = { 1, 1, 1, 1 };
equalizeDividing(arr3, n3);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static void equalizeDividing( int [] arr, int n)
{
boolean allSame = true , isOne = false ;
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" );
}
}
public static void main(String[] args)
{
int n1 = 3 ;
int [] arr1 = { 5 , 1 , 4 };
equalizeDividing(arr1, n1);
int n2 = 4 ;
int [] arr2 = { 3 , 3 , 4 , 4 };
equalizeDividing(arr2, n2);
int n3 = 4 ;
int [] arr3 = { 1 , 1 , 1 , 1 };
equalizeDividing(arr3, n3);
}
}
|
Python3
def equalizeDividing(arr, n):
allSame = True
isOne = False
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" )
if __name__ = = "__main__" :
n1 = 3
arr1 = [ 5 , 1 , 4 ]
equalizeDividing(arr1, n1)
n2 = 4
arr2 = [ 3 , 3 , 4 , 4 ]
equalizeDividing(arr2, n2)
n3 = 4
arr3 = [ 1 , 1 , 1 , 1 ]
equalizeDividing(arr3, n3)
|
C#
using System;
class GFG {
static void equalizeDividing( int [] arr, int n)
{
bool allSame = true , isOne = false ;
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" );
}
}
public static void Main( string [] args)
{
int n1 = 3;
int [] arr1 = { 5, 1, 4 };
equalizeDividing(arr1, n1);
int n2 = 4;
int [] arr2 = { 3, 3, 4, 4 };
equalizeDividing(arr2, n2);
int n3 = 4;
int [] arr3 = { 1, 1, 1, 1 };
equalizeDividing(arr3, n3);
}
}
|
Javascript
function equalizeDividing(arr, n) {
let allSame = true ;
let isOne = false ;
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" );
}
}
let n1 = 3;
let arr1 = [5, 1, 4];
equalizeDividing(arr1, n1);
let n2 = 4;
let arr2 = [3, 3, 4, 4];
equalizeDividing(arr2, n2);
let n3 = 4;
let arr3 = [1, 1, 1, 1];
equalizeDividing(arr3, n3);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...