Given an array arr[] of non-negative integers, the task is to check whether all array elements can be reduced to the minimum element in the array by choosing any positive integer X and update an array element arr[i] to arr[i]%X. If it is possible to do so, then print Yes. Otherwise, print No.
Examples:
Input: arr[] = {1, 1, 3, 4}
Output: Yes
Explanation:
Following operations can be performed to reduce all array elements to 1:
- Choose X = 2 for array element arr[3], and replacing it with arr[3] % 2, modifies the array arr[] to {1, 1, 1, 4}.
- Choose X = 3 for array element arr[4], and replacing it with arr[4] % 3, modifies the array arr[] to {1, 1, 1, 1}.
After the above operations all array elements have been reduced to the minimum element of arr[]. Therefore, print Yes.
Input: arr[] = {1, 2, 3}
Output: No
Approach: The given problem can be solved by using an observation that any non-negative integer, say num can be changed to the following integers [0, ceil(num/2) – 1] by choosing the value of X as num/2. It is very evident rest of X will have shorter range of changed numbers than X equal to num/2. Follow the steps below to solve the problem:
- Initialize a variable, say mini that stores the minimum element of the array arr[].
- Traverse the given array arr[] and if the value of mini doesn’t lie in the range[0, ceil(arr[i]/2) – 1] for any array element arr[i] then print No. Otherwise, print Yes.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if every integer // in the array can be reduced to the // minimum array element string isPossible( int arr[], int n)
{ // Stores the minimum array element
int mini = INT_MAX;
// Find the minimum element
for ( int i = 0; i < n; i++)
mini = min(mini, arr[i]);
// Traverse the array arr[]
for ( int i = 0; i < n; i++) {
if (arr[i] == mini)
continue ;
// Stores the maximum value
// in the range
int Max = (arr[i] + 1) / 2 - 1;
// Check whether mini lies in
// the range or not
if (mini < 0 || mini > Max)
return "No" ;
}
// Otherwise, return Yes
return "Yes" ;
} // Driver Code int main()
{ int arr[] = { 1, 1, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << isPossible(arr, N);
return 0;
} |
// Java program for the above approach import java.io.*;
import java.util.*;
class GFG{
// Function to check if every integer // in the array can be reduced to the // minimum array element static String isPossible( int arr[], int n)
{ // Stores the minimum array element
int mini = Integer.MAX_VALUE;
// Find the minimum element
for ( int i = 0 ; i < n; i++)
mini = Math.min(mini, arr[i]);
// Traverse the array arr[]
for ( int i = 0 ; i < n; i++) {
if (arr[i] == mini)
continue ;
// Stores the maximum value
// in the range
int Max = (arr[i] + 1 ) / 2 - 1 ;
// Check whether mini lies in
// the range or not
if (mini < 0 || mini > Max)
return "No" ;
}
// Otherwise, return Yes
return "Yes" ;
} // Driver code public static void main(String args[])
{ int arr[] = { 1 , 1 , 3 , 4 };
int N = arr.length;
System.out.print(isPossible(arr, N));
} } // This code is contributed by code_hunt. |
# Python 3 program for the above approach import sys
# Function to check if every integer # in the array can be reduced to the # minimum array element def isPossible(arr, n):
# Stores the minimum array element
mini = sys.maxsize
# Find the minimum element
for i in range (n):
mini = min (mini, arr[i])
# Traverse the array arr[]
for i in range (n):
if (arr[i] = = mini):
continue
# Stores the maximum value
# in the range
Max = (arr[i] + 1 ) / / 2 - 1
# Check whether mini lies in
# the range or not
if (mini < 0 or mini > Max ):
return "No"
# Otherwise, return Yes
return "Yes"
# Driver Code if __name__ = = '__main__' :
arr = [ 1 , 1 , 3 , 4 ]
N = len (arr)
print (isPossible(arr, N))
# This code is contributed by ipg2016107.
|
// C# program for the above approach using System;
public class GFG
{ // Function to check if every integer // in the array can be reduced to the // minimum array element static String isPossible( int []arr, int n)
{ // Stores the minimum array element
int mini = int .MaxValue;
// Find the minimum element
for ( int i = 0; i < n; i++)
mini = Math.Min(mini, arr[i]);
// Traverse the array []arr
for ( int i = 0; i < n; i++) {
if (arr[i] == mini)
continue ;
// Stores the maximum value
// in the range
int Max = (arr[i] + 1) / 2 - 1;
// Check whether mini lies in
// the range or not
if (mini < 0 || mini > Max)
return "No" ;
}
// Otherwise, return Yes
return "Yes" ;
} // Driver code public static void Main(String []args)
{ int []arr = { 1, 1, 3, 4 };
int N = arr.Length;
Console.Write(isPossible(arr, N));
} } // This code is contributed by Princi Singh |
<script> // JavaScript program for the above approach // Function to check if every integer // in the array can be reduced to the // minimum array element function isPossible(arr, n)
{ // Stores the minimum array element
var mini = Number.MAX_VALUE;
// Find the minimum element
for ( var i = 0; i < n; i++)
mini = Math.min(mini, arr[i]);
// Traverse the array arr[]
for ( var i = 0; i < n; i++) {
if (arr[i] == mini)
continue ;
// Stores the maximum value
// in the range
var Max = (arr[i] + 1) / 2 - 1;
// Check whether mini lies in
// the range or not
if (mini < 0 || mini > Max)
return "No" ;
}
// Otherwise, return Yes
return "Yes" ;
} // Driver code var arr = [ 1, 1, 3, 4 ];
var N = arr.length;
document.write(isPossible(arr, N));
// This code is contributed by shivanisinghss2110 </script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)