Minimum and Maximum element of an array which is divisible by a given number k | Java implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code
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 ) ) ; } }