Minimum time required to print given string from a circular container based on given conditions | Function to calculate minimum time to print all characters in the string ; Current element where the pointer is pointing ; Find index of that element ; Calculate absolute difference between pointer index and character index as clockwise distance ; Subtract clockwise time from 26 to get anti - clockwise time ; Add minimum of both times to the answer ; Add one unit time to print the character ; Print the final answer ; Given Input ; Function Call
funs minTime ( string word ) { int ans = 0 ; let curr = 0 ; for ( let i = 0 ; i < word . Length ; i ++ ) { int k = word [ i ] . charAt ( 0 ) - ' ' . charAt ( 0 ) ; let a = Math . abs ( curr - k ) ; let b = 26 - Math . abs ( curr - k ) ; ans += Math . min ( a , b ) ; ans ++ ; curr = word [ i ] . charAt ( 0 ) - ' ' . charAt ( 0 ) ; } document . write ( ans ) ; } let str = " " ; minTime ( str ) ;