Find middle point segment from given segment lengths | Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
function findSegment ( n , m , segment_length ) { let meet_point = ( 1.0 * n ) / 2.0 ; let sum = 0 ; let segment_number = 0 , i ; for ( i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( sum == meet_point ) { segment_number = - 1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } let n = 13 ; let m = 3 ; let segment_length = [ 3 , 2 , 8 ] ; let ans = findSegment ( n , m , segment_length ) ; document . write ( ans ) ;