Minimum and Maximum element of an array which is divisible by a given number k
Given an array, the task is to find the minimum and maximum elements in the array which is divisible by a given number k.
Examples:
Input: arr[] = {12, 1235, 45, 67, 1}, k=5
Output: Minimum = 45, Maximum = 1235
Input: arr[] = {10, 1230, 45, 67, 1}, k=10
Output: Minimum = 10, Maximum = 1230
Approach:
- Take a min variable which store the minimum element and initialize with INT_MAX and compare it with every element of the array and update the next minimum element which is divisible by k.
- Take a max variable which store the maximum element and initialize with INT_MIN and compare it with every element of the array and update the next maximum element which is divisible by k.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum element int getMin(int arr[], int n, int k) { int res = INT_MAX; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) res = min(res, arr[i]); } return res; } // Function to find the maximum element int getMax(int arr[], int n, int k) { int res = INT_MIN; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) res = max(res, arr[i]); } return res; } // Driver code int main() { int arr[] = { 10, 1230, 45, 67, 1 }; int k = 10; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum element of array which is divisible by k: " << getMin(arr, n, k) << "\n"; cout << "Maximum element of array which is divisible by k: " << getMax(arr, n, k); return 0; } |
chevron_right
filter_none
Java
//Java implementation of the above approach class GFG { // Function to find the minimum element static int getMin(int arr[], int n, int k) { int res = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) { res = Math.min(res, arr[i]); } } return res; } // Function to find the maximum element static int getMax(int arr[], int n, int k) { int res = Integer.MIN_VALUE; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) { res = Math.max(res, arr[i]); } } return res; } // Driver code public static void main(String[] args) { int arr[] = {10, 1230, 45, 67, 1}; int k = 10; int n = arr.length; System.out.println("Minimum element of array which is divisible by k: " + getMin(arr, n, k)); System.out.println("Maximum element of array which is divisible by k: " + getMax(arr, n, k)); } } //This code contribute by Shikha Singh |
chevron_right
filter_none
Python 3
# Python 3 implementation of the # above approach import sys # Function to find the minimum element def getMin(arr, n, k): res = sys.maxsize for i in range(n): if (arr[i] % k == 0): res = min(res, arr[i]) return res # Function to find the maximum element def getMax(arr, n, k): res = 0 for i in range(1, n): if (arr[i] % k == 0): res = max(res, arr[i]) return res # Driver code if __name__ == "__main__": arr = [ 10, 1230, 45, 67, 1 ] k = 10 n = len(arr) print("Minimum element of array which", "is divisible by k: ", getMin(arr, n, k)) print( "Maximum element of array which", "is divisible by k: ", getMax(arr, n, k)) # This code is contributed # by ChitraNayal |
chevron_right
filter_none
C#
// C# implementation of the above approach using System; class GFG { // Function to find the minimum element static int getMin(int []arr, int n, int k) { int res = int.MaxValue; for (int i = 0; i < n; i++) { if (arr[i] % k == 0) { res = Math.Min(res, arr[i]); } } return res; } // Function to find the maximum element static int getMax(int []arr, int n, int k) { int res = int.MinValue; for (int i = 1; i < n; i++) { if (arr[i] % k == 0) { res = Math.Max(res, arr[i]); } } return res; } // Driver code static public void Main () { int []arr = {10, 1230, 45, 67, 1}; int k = 10; int n = arr.Length; Console.WriteLine("Minimum element of array " + "which is divisible by k: " + getMin(arr, n, k)); Console.WriteLine("Maximum element of array " + "which is divisible by k: " + getMax(arr, n, k)); } } // This code is contributes by ajit |
chevron_right
filter_none
PHP
<?php // PHP implementation of the above approach // Function to find the minimum element function getMin($arr, $n, $k) { $res = PHP_INT_MAX; for ($i = 0; $i < $n; $i++) { if ($arr[$i] % $k == 0) $res = min($res, $arr[$i]); } return $res; } // Function to find the maximum element function getMax($arr, $n, $k) { $res = PHP_INT_MIN; for ($i = 1; $i < $n; $i++) { if ($arr[$i] % $k == 0) $res = max($res, $arr[$i]); } return $res; } // Driver code $arr = array( 10, 1230, 45, 67, 1 ); $k = 10; $n = sizeof($arr); echo "Minimum element of array which is " . "divisible by k: ", getMin($arr, $n, $k) , "\n"; echo "Maximum element of array which is " . "divisible by k: ", getMax($arr, $n, $k); // This code is contributed by akt_mit ?> |
chevron_right
filter_none
Output:
Minimum element of array which is divisible by k: 10 Maximum element of array which is divisible by k: 1230
Recommended Posts:
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Minimum pair sum operations to make array each element divisible by 4
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Sum and Product of minimum and maximum element of an Array
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Maximum and minimum of an array using minimum number of comparisons
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Minimum number of given operations required to reduce the array to 0 element
- Search an element in an unsorted array using minimum number of comparisons
- Remove minimum number of elements such that no common element exist in both array
- Maximum count of elements divisible on the left for any element
- Maximum element in an array such that its previous and next element product is maximum
- Elements of an array that are not divisible by any element of another array
- Find an array element such that all elements are divisible by it
- Maximum possible remainder when an element is divided by other element in the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


