Minimum operations required to convert all characters of a String to a given Character | Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the String less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the String is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
function countOperations ( n , k ) { var div = 2 * k + 1 ; if ( n / 2 <= k ) { document . write ( 1 + " " ) ; if ( n > k ) document . write ( k + 1 ) ; else document . write ( n ) ; } else { if ( n % div == 0 ) { var oprn = parseInt ( n / div ) ; document . write ( oprn + " " ) ; var pos = k + 1 ; document . write ( pos + " " ) ; for ( i = 1 ; i <= oprn ; i ++ ) { document . write ( pos + " " ) ; pos += div ; } } else { var oprn = parseInt ( n / div + 1 ) ; document . write ( oprn + " " ) ; var pos = n % div ; if ( n % div > k ) pos -= k ; for ( i = 1 ; i <= oprn ; i ++ ) { document . write ( pos + " " ) ; pos += div ; } } } } var str = " " ; var ch = ' ' ; var n = str . length ; var k = 4 ; countOperations ( n , k ) ;