Minimum number of swaps required to make a number divisible by 60 | Function that prlet minimum number of swap operations required ; Condition if more than one zero exist ; Condition if zero_exist ; Computing total sum of all digits ; Condition if zero does not exist or the sum is not divisible by 3 ; Condition to find a digit that is multiple of 2 other than one zero ; Condition if multiple of 2 do not exist ; Condition for zero swaps means the number is already is divisible by 60 ; Condition for only one swap ; Otherwise 2 swaps required ; Driver Code
function MinimumSwapOperations ( s ) { let zero_exist = false ; let multiple_of_2 = false ; let sum = 0 ; let index_of_zero = 0 ; let more_zero = false ; for ( let i = 0 ; i < s . length ; i ++ ) { let val = s [ i ] - ' ' ; if ( zero_exist == true ) more_zero = true ; if ( val == 0 ) { zero_exist = true ; index_of_zero = i ; } sum += val ; } if ( zero_exist == false sum % 3 != 0 ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < s . length ; i ++ ) { let val = s [ i ] - ' ' ; if ( val % 2 == 0 && i != index_of_zero ) multiple_of_2 = true ; } if ( multiple_of_2 == false ) { document . write ( " " ) ; return ; } let last_val = s . charAt [ s . length - 1 ] - ' ' ; let second_last_val = s [ ( s . length - 2 ) ] - ' ' ; if ( last_val == 0 && second_last_val % 2 == 0 ) document . write ( 0 ) ; else if ( ( last_val == 0 && second_last_val % 2 != 0 ) || ( last_val % 2 == 0 && second_last_val == 0 ) ) document . write ( 1 ) ; else if ( more_zero == true && ( last_val == 0 && second_last_val % 2 != 0 ) ) document . write ( 1 ) ; else document . write ( 2 ) ; } let N = " " ; MinimumSwapOperations ( N ) ;