Check presence of integer between min and max of Array that divide all Array elements
Last Updated :
24 Aug, 2022
Given an array A[] of size N, the task is to check if there exists any integer that divide all the elements of that array and lies in the range of minimum and maximum elements (both included) of that array.
Examples:
Input: A = {27, 6, 9, 3, 21}
Output: 1
Explanation: Here, 3 lies between min(3) and max(27) of this array,
and divides all elements of this array.
Input: A = {4, 7, 12, 13, 20}
Output: 0
Explanation: No number between 4 and 20 (both included) divides
all the elements in the array.
Approach:
The idea is to simply check for all possible numbers between minimum and maximum element of the array, whether any of them divides all the elements of that array or not.
Follow below steps to implement this idea:
- Use two nested loops, one is from minimum element to maximum element and one is for checking if it divides all the numbers or not.
- In the nested loop, initialize count to 0 and increment the count whenever it divides the value.
- If in any iteration the count becomes equal to N, then return true.
- Otherwise, if no such element is found, return false.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool solve( int arr[], int n)
{
int mini = *min_element(arr, arr + n);
int maxi = *max_element(arr, arr + n);
int count = 0;
for ( int i = mini; i <= maxi; i++) {
for ( int j = 0; j < n; j++) {
if (arr[j] % i == 0)
count++;
}
if (count == n)
return true ;
count = 0;
}
return false ;
}
int main()
{
int A[] = { 27, 6, 9, 3, 21 };
int N = sizeof (A) / sizeof (A[0]);
if (solve(A, N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean solve( int [] arr, int n)
{
int mini = Arrays.stream(arr).min().getAsInt();
int maxi = Arrays.stream(arr).max().getAsInt();
int count = 0 ;
for ( int i = mini; i <= maxi; i++) {
for ( int j = 0 ; j < n; j++) {
if (arr[j] % i == 0 )
count++;
}
if (count == n)
return true ;
count = 0 ;
}
return false ;
}
public static void main(String args[])
{
int A[] = { 27 , 6 , 9 , 3 , 21 };
int N = A.length;
if (solve(A, N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def solve(arr, n) :
mini = min (arr)
maxi = max (arr)
count = 0
for i in range (mini, maxi + 1 , 1 ) :
for j in range ( 0 , n, 1 ) :
if (arr[j] % i = = 0 ) :
count + = 1
if (count = = n) :
return True
count = 0
return False
if __name__ = = "__main__" :
A = [ 27 , 6 , 9 , 3 , 21 ]
N = len (A)
if (solve(A, N)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
public static bool solve( int [] arr, int n)
{
int mini = arr.Min();
int maxi = arr.Max();
int count = 0;
for ( int i = mini; i <= maxi; i++) {
for ( int j = 0; j < n; j++) {
if (arr[j] % i == 0)
count++;
}
if (count == n)
return true ;
count = 0;
}
return false ;
}
public static void Main()
{
int [] A = { 27, 6, 9, 3, 21 };
int N = A.Length;
if (solve(A, N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function solve(arr,n)
{
let mini = Math.min.apply( null , arr);
let maxi = Math.max.apply( null , arr);
let count = 0;
for (let i = mini; i <= maxi; i++) {
for (let j = 0; j < n; j++) {
if (arr[j] % i == 0)
count++;
}
if (count == n)
return true ;
count = 0;
}
return false ;
}
let A = [ 27, 6, 9, 3, 21 ];
let N = A.length;
if (solve(A, N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(d * N), where d = max_element – min_element
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...