codeparrot/xlcost-text-to-code huggingface dataset:
Find the repeating and the missing | Added 3 new methods | Program to Find the repeating and missing elements ; Driver code
function printTwoElements ( arr , size ) { var i ; document . write ( " " ) ; for ( i = 0 ; i < size ; i ++ ) { var abs_value = Math . abs ( arr [ i ] ) ; if ( arr [ abs_value - 1 ] > 0 ) arr [ abs_value - 1 ] = - arr [ abs_value - 1 ] ; else document . write ( abs_value ) ; } document . write ( " " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > 0 ) document . write ( i + 1 ) ; } } arr = new Array ( 7 , 3 , 4 , 5 , 5 , 6 , 2 ) ; n = arr . length ; printTwoElements ( arr , n ) ;
Find four elements that sum to a given value | Set 1 ( n ^ 3 solution ) | A naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; Fix the first element and find other three ; Fix the second element and find other two ; Fix the third element and find the fourth ; find the fourth ; Driver Code
function findFourElements ( A , n , X ) { for ( let i = 0 ; i < n - 3 ; i ++ ) { for ( let j = i + 1 ; j < n - 2 ; j ++ ) { for ( let k = j + 1 ; k < n - 1 ; k ++ ) { for ( let l = k + 1 ; l < n ; l ++ ) if ( A [ i ] + A [ j ] + A [ k ] + A [ l ] == X ) document . write ( A [ i ] + " " + A [ j ] + " " + A [ k ] + " " + A [ l ] ) ; } } } } let A = [ 10 , 20 , 30 , 40 , 1 , 2 ] ; let n = A . length ; let X = 91 ; findFourElements ( A , n , X ) ;
Find four elements that sum to a given value | Set 2 | The function finds four elements with given sum X ; Store sums of all pairs in a hash table ; Traverse through all pairs and search for X - ( current pair sum ) . ; If X - sum is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver Code ; Function call
function findFourElements ( arr , n , X ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) mp . set ( arr [ i ] + arr [ j ] , [ i , j ] ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let sum = arr [ i ] + arr [ j ] ; if ( mp . has ( X - sum ) ) { let p = mp . get ( X - sum ) ; if ( p [ 0 ] != i && p [ 0 ] != j && p [ 1 ] != i && p [ 1 ] != j ) { document . write ( arr [ i ] + " " + arr [ j ] + " " + arr [ p [ 0 ] ] + " " + arr [ p [ 1 ] ] ) ; return ; } } } } } let arr = [ 10 , 20 , 30 , 40 , 1 , 2 ] ; let n = arr . length ; let X = 91 ; findFourElements ( arr , n , X ) ;
Find four elements that sum to a given value | Set 2 | Javascript program to find four elements with the given sum ; Function to find 4 elements that add up to given sum ; Iterate from 0 to temp . length ; Iterate from 0 to arr . length ; Iterate from i + 1 to arr . length ; Store curr_sum = arr [ i ] + arr [ j ] ; Check if X - curr_sum if present in map ; Store pair having map value X - curr_sum ; Print the output ; Program for two Sum ; Driver Code ; Function call
class pair { constructor ( first , sec ) { this . first = first ; this . sec = sec ; } } function fourSum ( X , arr , map ) { let temp = new Array ( arr . length ) ; for ( let i = 0 ; i < temp . length ; i ++ ) temp [ i ] = 0 ; for ( let i = 0 ; i < arr . length - 1 ; i ++ ) { for ( let j = i + 1 ; j < arr . length ; j ++ ) { let curr_sum = arr [ i ] + arr [ j ] ; if ( map . has ( X - curr_sum ) ) { let p = map . get ( X - curr_sum ) ; if ( p . first != i && p . sec != i && p . first != j && p . sec != j && temp [ p . first ] == 0 && temp [ p . sec ] == 0 && temp [ i ] == 0 && temp [ j ] == 0 ) { document . write ( arr [ i ] + " " + arr [ j ] + " " + arr [ p . first ] + " " + arr [ p . sec ] ) ; temp [ p . sec ] = 1 ; temp [ i ] = 1 ; temp [ j ] = 1 ; break ; } } } } } function twoSum ( nums ) { let map = new Map ( ) ; for ( let i = 0 ; i < nums . length - 1 ; i ++ ) { for ( let j = i + 1 ; j < nums . length ; j ++ ) { map . set ( nums [ i ] + nums [ j ] , new pair ( i , j ) ) ; } } return map ; } let arr = [ 10 , 20 , 30 , 40 , 1 , 2 ] ; let n = arr . length ; let X = 91 ; let map = twoSum ( arr ) ; fourSum ( X , arr , map ) ;
Search an element in an array where difference between adjacent elements is 1 | x is the element to be searched in arr [ 0. . n - 1 ] ; Traverse the given array starting from leftmost element ; If x is found at index i ; Jump the difference between current array element and x ; Driver program
function search ( arr , n , x ) { let i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + Math . abs ( arr [ i ] - x ) ; } document . write ( " " ) ; return - 1 ; } let arr = [ 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 ] ; let n = arr . length ; let x = 3 ; document . write ( " " + x + " " + search ( arr , n , 3 ) ) ;
Third largest element in an array of distinct elements | JavaScript program to find third Largest element in an array of distinct elements ; There should be atleast three elements ; Find first largest element ; Find second largest element ; Find third largest element ; Driver Code
function thirdLargest ( arr , arr_size ) { if ( arr_size < 3 ) { document . write ( " " ) ; return ; } let first = arr [ 0 ] ; for ( let i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; let second = Number . MIN_VALUE ; for ( let i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > second && arr [ i ] < first ) second = arr [ i ] ; let third = Number . MIN_VALUE ; for ( let i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > third && arr [ i ] < second ) third = arr [ i ] ; document . write ( " " + " " , third ) ; } let arr = [ 12 , 13 , 1 , 10 , 34 , 16 ] ; let n = arr . length ; thirdLargest ( arr , n ) ;
Third largest element in an array of distinct elements | JavaScript program to find third Largest element in an array ; There should be atleast three elements ; Initialize first , second and third Largest element ; Traverse array elements to find the third Largest ; If current element is greater than first , then update first , second and third ; If arr [ i ] is in between first and second ; If arr [ i ] is in between second and third ; Driver program to test above function
function thirdLargest ( arr , arr_size ) { if ( arr_size < 3 ) { document . write ( " " ) ; return ; } var first = arr [ 0 ] , second = - 1000000000 , third = - 1000000000 ; for ( var i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { third = second ; second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { third = second ; second = arr [ i ] ; } else if ( arr [ i ] > third ) third = arr [ i ] ; } document . write ( " " + third ) ; } var arr = [ 12 , 13 , 1 , 10 , 34 , 16 ] ; var n = arr . length ; thirdLargest ( arr , n ) ;
Check if there exist two elements in an array whose sum is equal to the sum of rest of the array | Function to check whether two elements exist whose sum is equal to sum of rest of the elements . ; Find sum of whole array ; If sum of array is not even than we can not divide it into two part ; For each element arr [ i ] , see if there is another element with vaalue sum - arr [ i ] ; If element exist than return the pair ; Driver program .
function checkPair ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = Math . floor ( sum / 2 ) ; let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let val = sum - arr [ i ] ; if ( ! s . has ( arr [ i ] ) ) { s . add ( arr [ i ] ) } if ( s . has ( val ) ) { document . write ( " " + arr [ i ] + " " + val + " " ) ; return true ; } s . add ( arr [ i ] ) ; } return false ; } let arr = [ 2 , 11 , 5 , 1 , 4 , 7 ] ; let n = arr . length ; if ( checkPair ( arr , n ) == false ) { document . write ( " " ) ; }
Search an element in an unsorted array using minimum number of comparisons | Function to search an element in minimum number of comparisons ; 1 st comparison ; no termination condition and thus no comparison ; this would be executed at - most n times and therefore at - most n comparisons ; replace arr [ n - 1 ] with its actual element as in original ' arr [ ] ' ; if ' x ' is found before the ' ( n - 1 ) th ' index , then it is present in the array final comparison ; else not present in the array ; driver program
function search ( arr , n , x ) { if ( arr [ n - 1 ] == x ) return " " ; let backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( let i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " " ; return " " ; } } } let arr = [ 4 , 6 , 1 , 5 , 8 ] ; let n = arr . length ; let x = 1 ; document . write ( search ( arr , n , x ) ) ;
Count of only repeated element in a sorted array of consecutive elements | Assumptions : vector a is sorted , max - difference of two adjacent elements is 1 ; if a [ m ] = m + a [ 0 ] , there is no repeating character in [ s . . m ] ; if a [ m ] < m + a [ 0 ] , there is a repeating character in [ s . . m ] ; Driver method
function sequence ( a ) { if ( a . length == 0 ) return [ 0 , 0 ] let s = 0 let e = a . length - 1 while ( s < e ) { let m = Math . floor ( ( s + e ) / 2 ) ; if ( a [ m ] >= m + a [ 0 ] ) s = m + 1 else e = m } return [ a [ s ] , a . length - ( a [ a . length - 1 ] - a [ 0 ] ) ] } let p = sequence ( [ 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 ] ) document . write ( " " + p [ 0 ] + " " + p [ 1 ] + " " )
Find element in a sorted array whose frequency is greater than or equal to n / 2. | Javascript code to find majority element in a sorted array ; driver code
function findMajority ( arr , n ) { return arr [ Math . floor ( n / 2 ) ] ; } let arr = [ 1 , 2 , 2 , 3 ] ; let n = arr . length ; document . write ( findMajority ( arr , n ) ) ;
Minimum absolute difference of adjacent elements in a circular array | Javascript program to find maximum difference between adjacent elements in a circular array . ; Checking normal adjacent elements ; Checking circular link ; driver code
function minAdjDifference ( arr , n ) { if ( n < 2 ) return ; let res = Math . abs ( arr [ 1 ] - arr [ 0 ] ) ; for ( let i = 2 ; i < n ; i ++ ) res = Math . min ( res , Math . abs ( arr [ i ] - arr [ i - 1 ] ) ) ; res = Math . min ( res , Math . abs ( arr [ n - 1 ] - arr [ 0 ] ) ) ; document . write ( " " + res ) ; } let a = [ 10 , 12 , 13 , 15 , 10 ] ; let n = a . length ; minAdjDifference ( a , n ) ;
Find the first , second and third minimum elements in an array | Javascript program to find the first , second and third minimum element in an array ; Check if current element is less than firstmin , then update first , second and third ; Check if current element is less than secmin then update second and third ; Check if current element is less than then update third ; Driver program
let MAX = 100000 function Print3Smallest ( array , n ) { let firstmin = MAX , secmin = MAX , thirdmin = MAX ; for ( let i = 0 ; i < n ; i ++ ) { if ( array [ i ] < firstmin ) { thirdmin = secmin ; secmin = firstmin ; firstmin = array [ i ] ; } else if ( array [ i ] < secmin ) { thirdmin = secmin ; secmin = array [ i ] ; } else if ( array [ i ] < thirdmin ) thirdmin = array [ i ] ; } document . write ( " " + firstmin + " " ) ; document . write ( " " + secmin + " " ) ; document . write ( " " + thirdmin + " " ) ; } let array = [ 4 , 9 , 1 , 32 , 12 ] ; let n = array . length ; Print3Smallest ( array , n ) ;
Program to find the minimum ( or maximum ) element of an array | javascript program to find minimum ( or maximum ) element in an array . ; If there is single element , return it . Else return minimum of first element and minimum of remaining array . ; If there is single element , return it . Else return maximum of first element and maximum of remaining array . ; Driver code
function getMin ( arr , i , n ) { return ( n == 1 ) ? arr [ i ] : Math . min ( arr [ i ] , getMin ( arr , i + 1 , n - 1 ) ) ; } function getMax ( arr , i , n ) { return ( n == 1 ) ? arr [ i ] : Math . max ( arr [ i ] , getMax ( arr , i + 1 , n - 1 ) ) ; } var arr = [ 12 , 1234 , 45 , 67 , 1 ] ; var n = arr . length ; document . write ( " " + getMin ( arr , 0 , n ) + " " ) ; document . write ( " " + getMax ( arr , 0 , n ) ) ;
Program to find the minimum ( or maximum ) element of an array | Javascript program to find minimum ( or maximum ) element in an array . ; Driver code
function getMin ( arr , n ) { return Math . min . apply ( Math , arr ) ; } function getMax ( arr , n ) { return Math . max . apply ( Math , arr ) ; } var arr = [ 12 , 1234 , 45 , 67 , 1 ] ; var n = arr . length ; document . write ( " " + getMin ( arr , n ) + " " ) ; document . write ( " " + getMax ( arr , n ) ) ;
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Hashmap ; Traverse all array elements ; Update the frequency of array [ i ] ; Increase the index ; Driver code
function findCounts ( arr , n ) { let hash = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { hash [ i ] = 0 ; } let i = 0 ; while ( i < n ) { hash [ arr [ i ] - 1 ] ++ ; i ++ ; } document . write ( " " + " " ) ; for ( i = 0 ; i < n ; i ++ ) { document . write ( ( i + 1 ) + " " + hash [ i ] + " " ) ; } } let arr = [ 2 , 3 , 3 , 2 , 5 ] ; findCounts ( arr , arr . length ) ; let arr1 = [ 1 ] ; findCounts ( arr1 , arr1 . length ) ; let arr3 = [ 4 , 4 , 4 , 4 ] ; findCounts ( arr3 , arr3 . length ) ; let arr2 = [ 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 ] ; findCounts ( arr2 , arr2 . length ) ; let arr4 = [ 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 ] ; findCounts ( arr4 , arr4 . length ) ; let arr5 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ; findCounts ( arr5 , arr5 . length ) ; let arr6 = [ 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ] ; findCounts ( arr6 , arr6 . length ) ;
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Traverse all array elements ; If this element is already processed , then nothing to do ; Find index corresponding to this element For example , index for 5 is 4 ; If the elementIndex has an element that is not processed yet , then first store that element to arr [ i ] so that we don 't  lose anything. ; After storing arr [ elementIndex ] , change it to store initial count of ' arr [ i ] ' ; If this is NOT first occurrence of arr [ i ] , then decrement its count . ; And initialize arr [ i ] as 0 means the element ' i + 1' is not seen so far ; Driver code
function findCounts ( arr , n ) { let i = 0 ; while ( i < n ) { if ( arr [ i ] <= 0 ) { i ++ ; continue ; } let elementIndex = arr [ i ] - 1 ; if ( arr [ elementIndex ] > 0 ) { arr [ i ] = arr [ elementIndex ] ; arr [ elementIndex ] = - 1 ; } else { arr [ elementIndex ] -- ; arr [ i ] = 0 ; i ++ ; } } document . write ( " " ) ; for ( let j = 0 ; j < n ; j ++ ) document . write ( j + 1 + " " + Math . abs ( arr [ j ] ) + " " ) ; } let arr = [ 2 , 3 , 3 , 2 , 5 ] ; findCounts ( arr , arr . length ) ; let arr1 = [ 1 ] ; findCounts ( arr1 , arr1 . length ) ; let arr3 = [ 4 , 4 , 4 , 4 ] ; findCounts ( arr3 , arr3 . length ) ; let arr2 = [ 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 ] ; findCounts ( arr2 , arr2 . length ) ; let arr4 = [ 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 ] ; findCounts ( arr4 , arr4 . length ) ; let arr5 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ; findCounts ( arr5 , arr5 . length ) ; let arr6 = [ 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ] ; findCounts ( arr6 , arr6 . length ) ;
Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Subtract 1 from every element so that the elements become in range from 0 to n - 1 ; Use every element arr [ i ] as index and add ' n ' to element present at arr [ i ] % n to keep track of count of occurrences of arr [ i ] ; To print counts , simply print the number of times n was added at index corresponding to every element ; Driver Code
function printfrequency ( arr , n ) { for ( let j = 0 ; j < n ; j ++ ) arr [ j ] = arr [ j ] - 1 ; for ( let i = 0 ; i < n ; i ++ ) arr [ arr [ i ] % n ] = arr [ arr [ i ] % n ] + n ; for ( let i = 0 ; i < n ; i ++ ) document . write ( ( i + 1 ) + " " + parseInt ( arr [ i ] / n , 10 ) + " " ) ; } let arr = [ 2 , 3 , 3 , 2 , 5 ] ; let n = arr . length ; printfrequency ( arr , n ) ;
Delete an element from array ( Using two traversals and one traversal ) | This function removes an element x from arr [ ] and returns new size after removal ( size is reduced only when x is present in arr [ ] ; Search x in array ; If x found in array ; reduce size of array and move all elements on space ahead ; driver code ; Delete x from arr [ ]
function deleteElement ( arr , n , x ) { let i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) break ; if ( i < n ) { n = n - 1 ; for ( let j = i ; j < n ; j ++ ) arr [ j ] = arr [ j + 1 ] ; } return n ; } let arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] ; let n = arr . length ; let x = 6 ; n = deleteElement ( arr , n , x ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Delete an element from array ( Using two traversals and one traversal ) | This function removes an element x from arr [ ] and returns new size after removal . Returned size is n - 1 when element is present . Otherwise 0 is returned to indicate failure . ; If x is last element , nothing to do ; Start from rightmost element and keep moving elements one position ahead . ; If element was not found ; Else move the next element in place of x ; Driver program to test above function ; Delete x from arr [ ]
function deleteElement ( arr , n , x ) { if ( arr [ n - 1 ] == x ) return ( n - 1 ) ; let prev = arr [ n - 1 ] , i ; for ( i = n - 2 ; i >= 0 && arr [ i ] != x ; i -- ) { let curr = arr [ i ] ; arr [ i ] = prev ; prev = curr ; } if ( i < 0 ) return 0 ; arr [ i ] = prev ; return ( n - 1 ) ; } let arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] ; let n = arr . length ; let x = 6 ; n = deleteElement ( arr , n , x ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Count Inversions of size three in a given array | returns count of inversion of size 3 ; initialize result ; count all smaller elements on right of arr [ i ] ; count all greater elements on left of arr [ i ] ; update inversion count by adding all inversions that have arr [ i ] as middle of three elements ; driver program to test above function
function getInvCount ( arr , n ) { let invcount = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let small = 0 ; for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; let great = 0 ; for ( let j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ] ) great ++ ; invcount += great * small ; } return invcount ; } let arr = [ 8 , 4 , 2 , 1 ] ; let n = arr . length ; document . write ( " " + getInvCount ( arr , n ) ) ;
Trapping Rain Water | Function to return the maximum water that can be stored ; To store the maximum water that can be stored ; For every element of the array except first and last element ; Find maximum element on its left ; Find maximum element on its right ; Update maximum water value ; Driver code
function maxWater ( arr , n ) { let res = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { let left = arr [ i ] ; for ( let j = 0 ; j < i ; j ++ ) { left = Math . max ( left , arr [ j ] ) ; } let right = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { right = Math . max ( right , arr [ j ] ) ; } res += Math . min ( left , right ) - arr [ i ] ; } return res ; } let arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let n = arr . length ; document . write ( maxWater ( arr , n ) ) ;
Trapping Rain Water | Javascript program to find maximum amount of water that can be trapped within given set of bars . ; Method for maximum amount of water ; left [ i ] contains height of tallest bar to the left of i 'th bar including itself ; Right [ i ] contains height of tallest bar to the right of ith bar including itself ; Initialize result ; Fill left array ; Fill right array ; Calculate the accumulated water element by element consider the amount of water on i 'th bar, the  amount of water accumulated on this particular  bar will be equal to min(left[i], right[i]) - arr[i] . ; Driver method to test the above function
let arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; function findWater ( n ) { let left = new Array ( n ) ; let right = new Array ( n ) ; let water = 0 ; left [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) left [ i ] = Math . max ( left [ i - 1 ] , arr [ i ] ) ; right [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) right [ i ] = Math . max ( right [ i + 1 ] , arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) water += Math . min ( left [ i ] , right [ i ] ) - arr [ i ] ; return water ; } document . write ( " " + findWater ( arr . length ) ) ;
Trapping Rain Water | JAVASCRIPT Code For Trapping Rain Water ; initialize output ; maximum element on left and right ; indices to traverse the array ; update max in left ; water on curr element = max - curr ; update right maximum ; Driver program to test above function
function findWater ( arr , n ) { let result = 0 ; let left_max = 0 , right_max = 0 ; let lo = 0 , hi = n - 1 ; while ( lo <= hi ) { if ( arr [ lo ] < arr [ hi ] ) { if ( arr [ lo ] > left_max ) left_max = arr [ lo ] ; else result += left_max - arr [ lo ] ; lo ++ ; } else { if ( arr [ hi ] > right_max ) right_max = arr [ hi ] ; else result += right_max - arr [ hi ] ; hi -- ; } } return result ; } let arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let n = arr . length ; document . write ( " " + " " + findWater ( arr , n ) ) ;
Trapping Rain Water | Function to return the maximum water that can be stored ; Let the first element be stored as previous , we shall loop from index 1 ; To store previous wall 's index ; To store the water until a larger wall is found , if there are no larger walls then delete temp value from water ; If the current wall is taller than the previous wall then make current wall as the previous wall and its index as previous wall 's index  for the subsequent loops ; Because larger or same height wall is found ; Since current wall is shorter than the previous , we subtract previous wall ' s ▁ height ▁ from ▁ the ▁ current ▁ wall ' s height and add it to the water ; Store the same value in temp as well If we dont find any larger wall then we will subtract temp from water ; If the last wall was larger than or equal to the previous wall then prev_index would be equal to size of the array ( last element ) If we didn 't find a wall greater than or equal  to the previous wall from the left then  prev_index must be less than the index  of the last element ; Temp would ' ve ▁ stored ▁ the ▁ water ▁ collected ▁ ▁ from ▁ previous ▁ largest ▁ wall ▁ till ▁ the ▁ end ▁ ▁ of ▁ array ▁ if ▁ no ▁ larger ▁ wall ▁ was ▁ found ▁ then ▁ ▁ it ▁ has ▁ excess ▁ water ▁ and ▁ remove ▁ that ▁ ▁ from ▁ ' water ' var ; We start from the end of the array , so previous should be assigned to the last element ; Loop from the end of array up to the ' previous ▁ index ' which would contain the " largest ▁ wall ▁ from ▁ the ▁ left " ; Right end wall will be definitely smaller than the ' previous ▁ index ' wall ; Return the maximum water ; Driver code
function maxWater ( arr , n ) { let size = n - 1 ; let prev = arr [ 0 ] ; let prev_index = 0 ; let water = 0 ; let temp = 0 ; for ( let i = 1 ; i <= size ; i ++ ) { if ( arr [ i ] >= prev ) { prev = arr [ i ] ; prev_index = i ; temp = 0 ; } else { water += prev - arr [ i ] ; temp += prev - arr [ i ] ; } } if ( prev_index < size ) { water -= temp ; prev = arr [ size ] ; for ( let i = size ; i >= prev_index ; i -- ) { if ( arr [ i ] >= prev ) { prev = arr [ i ] ; } else { water += prev - arr [ i ] ; } } } return water ; } let arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let n = arr . length ; document . write ( maxWater ( arr , n ) ) ;