Find the Sub | Function to find the subarray ; Pick a starting point ; Consider current starting point as a subarray and update minimum sum and subarray indexes ; Try all subarrays starting with i ; update minimum sum and subarray indexes ; Return starting and ending indexes ; Drivers code
function findSubArray ( arr , n ) { let start , end , min_sum = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { let curr_sum = arr [ i ] ; if ( min_sum > Math . abs ( curr_sum ) ) { min_sum = Math . abs ( curr_sum ) ; start = i ; end = i ; } for ( let j = i + 1 ; j < n ; j ++ ) { curr_sum = curr_sum + arr [ j ] ; if ( min_sum > Math . abs ( curr_sum ) ) { min_sum = Math . abs ( curr_sum ) ; start = i ; end = j ; } } } let p = [ start , end ] ; return p ; } let arr = [ 2 , - 5 , 4 , - 6 , - 3 ] ; let n = arr . length ; let point = findSubArray ( arr , n ) ; document . write ( " " ) ; document . write ( point [ 0 ] + " " + point [ 1 ] ) ;