Given an array, the task is to find the minimum and maximum elements in the array which are 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 that stores the minimum element and initialize it 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 that stores the maximum element and initialize it 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++
#include <bits/stdc++.h>
using namespace std;
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;
}
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;
}
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;
}
|
C
#include <stdio.h>
#include <limits.h>
int max( int a, int b)
{
int max = a;
if (max < b)
max = b;
return max;
}
int min( int a, int b)
{
int min = a;
if (min > b)
min = b;
return min;
}
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;
}
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;
}
int main()
{
int arr[] = { 10, 1230, 45, 67, 1 };
int k = 10;
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Minimum element of array which is divisible by k: %d\n" ,getMin(arr, n, k));
printf ( "Maximum element of array which is divisible by k: %d\n" ,getMax(arr, n, k));
return 0;
}
|
Java
class GFG {
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;
}
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;
}
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));
}
}
|
Python 3
import sys
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
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
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))
|
C#
using System;
class GFG
{
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;
}
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;
}
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));
}
}
|
PHP
<?php
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 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 ;
}
$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 );
?>
|
Javascript
<script>
function getMin(arr, n, k)
{
let res = Number.MAX_VALUE;
for (let i = 0; i < n; i++) {
if (arr[i] % k == 0)
res = Math.min(res, arr[i]);
}
return res;
}
function getMax(arr, n, k)
{
let res = Number.MIN_VALUE;
for (let i = 1; i < n; i++) {
if (arr[i] % k == 0)
res = Math.max(res, arr[i]);
}
return res;
}
let arr = [ 10, 1230, 45, 67, 1 ];
let k = 10;
let n = arr.length;
document.write( "Minimum element of array which is divisible by k: "
+ getMin(arr, n, k) + "<br>" );
document.write( "Maximum element of array which is divisible by k: "
+ getMax(arr, n, k));
</script>
|
Output
Minimum element of array which is divisible by k: 10
Maximum element of array which is divisible by k: 1230
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!