Skip to content
Related Articles

Related Articles

Check presence of integer between min and max of Array that divide all Array elements

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Aug, 2022
View Discussion
Improve Article
Save Article

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++




// C++ code to implement above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether a number exist
// or not that lies between min and max element
// and divides all the elements in the array
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;
}
 
// Driver code
int main()
{
    int A[] = { 27, 6, 9, 3, 21 };
 
    // Function call
    int N = sizeof(A) / sizeof(A[0]);
    if (solve(A, N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




// JAVA code to implement above approach
 
import java.util.*;
class GFG {
 
    // Function to check whether a number exist
    // or not that lies between min and max element
    // and divides all the elements in the array
    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;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int A[] = { 27, 6, 9, 3, 21 };
 
        // Function call
        int N = A.length;
        if (solve(A, N))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Taranpreet


Python3




# Python3 code for the above approach
 
# Function to check whether a number exist
# or not that lies between min and max element
# and divides all the elements in the array
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
 
# Driver code
if __name__ == "__main__":
     
    A = [ 27, 6, 9, 3, 21 ]
  
    # Function call
    N = len(A)
    if (solve(A, N)) :
        print("Yes")
    else :
        print("No")
         
        # This code is contributed by code_hunt.


C#




// C# implementation of above approach
using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG {
 
    // Function to check whether a number exist
    // or not that lies between min and max element
    // and divides all the elements in the array
    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;
    }
  
  // Driver Code
  public static void Main()
  {
      int[] A = { 27, 6, 9, 3, 21 };
  
        // Function call
        int N = A.Length;
        if (solve(A, N))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
  }
}
 
// This code is contributed by sanjoy_62.


Javascript




// Javascript code to implement above approach
 
<script>
// Function to check whether a number exist
// or not that lies between min and max element
// and divides all the elements in the array
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;
}
 
// Driver code
 
     let A = [ 27, 6, 9, 3, 21 ];
 
    // Function call
    let N = A.length;
    if (solve(A, N))
        document.write("Yes");
    else
        document.write("No");
         
        // This code is contributed by satwik4409.
    </script>


Output

Yes

Time Complexity: O(d * N), where d = max_element – min_element

Space Complexity: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!