Kth character after replacing each character of String by its frequency exactly X times | javascript program for the above approach Function to find the Kth character after X days ; Variable to store the KthChar ; Traverse the string ; Convert char into int ; Calculate characters ; If K is less than sum than ans = str [ i ] ; Return answer ; Given Input ; Function Call
function FindKthChar ( str , K , X ) { var ans = " " ; var sum = 0 ; for ( i = 0 ; i < str . length ; i ++ ) { var digit = parseInt ( str [ i ] ) ; var range = parseInt ( Math . pow ( digit , X ) ) ; sum += range ; if ( K <= sum ) { ans = str [ i ] ; break ; } } return ans ; } var str = " " ; var K = 9 ; var X = 3 ; var ans = FindKthChar ( str , K , X ) ; document . write ( ans ) ;