Check whether each Array element can be reduced to minimum element by replacing it with remainder with some X
Last Updated :
27 Sep, 2021
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isPossible( int arr[], int n)
{
int mini = INT_MAX;
for ( int i = 0; i < n; i++)
mini = min(mini, arr[i]);
for ( int i = 0; i < n; i++) {
if (arr[i] == mini)
continue ;
int Max = (arr[i] + 1) / 2 - 1;
if (mini < 0 || mini > Max)
return "No" ;
}
return "Yes" ;
}
int main()
{
int arr[] = { 1, 1, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << isPossible(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static String isPossible( int arr[], int n)
{
int mini = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
mini = Math.min(mini, arr[i]);
for ( int i = 0 ; i < n; i++) {
if (arr[i] == mini)
continue ;
int Max = (arr[i] + 1 ) / 2 - 1 ;
if (mini < 0 || mini > Max)
return "No" ;
}
return "Yes" ;
}
public static void main(String args[])
{
int arr[] = { 1 , 1 , 3 , 4 };
int N = arr.length;
System.out.print(isPossible(arr, N));
}
}
|
Python3
import sys
def isPossible(arr, n):
mini = sys.maxsize
for i in range (n):
mini = min (mini, arr[i])
for i in range (n):
if (arr[i] = = mini):
continue
Max = (arr[i] + 1 ) / / 2 - 1
if (mini < 0 or mini > Max ):
return "No"
return "Yes"
if __name__ = = '__main__' :
arr = [ 1 , 1 , 3 , 4 ]
N = len (arr)
print (isPossible(arr, N))
|
C#
using System;
public class GFG
{
static String isPossible( int []arr, int n)
{
int mini = int .MaxValue;
for ( int i = 0; i < n; i++)
mini = Math.Min(mini, arr[i]);
for ( int i = 0; i < n; i++) {
if (arr[i] == mini)
continue ;
int Max = (arr[i] + 1) / 2 - 1;
if (mini < 0 || mini > Max)
return "No" ;
}
return "Yes" ;
}
public static void Main(String []args)
{
int []arr = { 1, 1, 3, 4 };
int N = arr.Length;
Console.Write(isPossible(arr, N));
}
}
|
Javascript
<script>
function isPossible(arr, n)
{
var mini = Number.MAX_VALUE;
for ( var i = 0; i < n; i++)
mini = Math.min(mini, arr[i]);
for ( var i = 0; i < n; i++) {
if (arr[i] == mini)
continue ;
var Max = (arr[i] + 1) / 2 - 1;
if (mini < 0 || mini > Max)
return "No" ;
}
return "Yes" ;
}
var arr = [ 1, 1, 3, 4 ];
var N = arr.length;
document.write(isPossible(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...