Minimum and Maximum element of an array which is divisible by a given number k | C ++ implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ) << " STRNEWLINE " ; cout << " Maximum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " << getMax ( arr , n , k ) ; return 0 ; }