Minimal operations to make a number magical | Function to calculate the minimal changes ; Maximum digits that can be changed ; Nested loops to generate all 6 digit numbers ; Counter to count the number of change required ; If first digit is equal ; If 2 nd digit is equal ; If 3 rd digit is equal ; If 4 th digit is equal ; If 5 th digit is equal ; If 6 th digit is equal ; Checks if less then the previous calculate changes ; Returns the answer ; Number stored in string ; Prints the minimum operations
function calculate ( s ) { let ans = 6 ; for ( let i = 0 ; i < 10 ; ++ i ) { for ( let j = 0 ; j < 10 ; ++ j ) { for ( let k = 0 ; k < 10 ; ++ k ) { for ( let l = 0 ; l < 10 ; ++ l ) { for ( let m = 0 ; m < 10 ; ++ m ) { for ( let n = 0 ; n < 10 ; ++ n ) { if ( i + j + k == l + m + n ) { let c = 0 ; if ( i != s [ 0 ] - ' ' ) c ++ ; if ( j != s [ 1 ] - ' ' ) c ++ ; if ( k != s [ 2 ] - ' ' ) c ++ ; if ( l != s [ 3 ] - ' ' ) c ++ ; if ( m != s [ 4 ] - ' ' ) c ++ ; if ( n != s [ 5 ] - ' ' ) c ++ ; if ( c < ans ) ans = c ; } } } } } } } return ans ; } let s = " " ; document . write ( calculate ( s ) ) ;