Minimum and Maximum element of an array which is divisible by a given number k | Python 3 implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code
import sys NEW_LINE def getMin ( arr , n , k ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT res = min ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def getMax ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT res = max ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 1230 , 45 , 67 , 1 ] NEW_LINE k = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ element ▁ of ▁ array ▁ which " , " is ▁ divisible ▁ by ▁ k : ▁ " , getMin ( arr , n , k ) ) NEW_LINE print ( " Maximum ▁ element ▁ of ▁ array ▁ which " , " is ▁ divisible ▁ by ▁ k : ▁ " , getMax ( arr , n , k ) ) NEW_LINE DEDENT