codeparrot/xlcost-text-to-code huggingface dataset:
Calculate the number of set bits for every number from 0 to N | Function to find the count of set bits in all the integers from 0 to n ; dp [ i ] will store the count of set bits in i ; Count of set bits in 0 is 0 ; For every number starting from 1 ; If current number is even ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) ; If current element is odd ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) + 1 ; Print the count of set bits in i ; Driver code
function findSetBits ( n ) { let dp = new Array ( n + 1 ) ; dp . fill ( 0 ) ; document . write ( dp [ 0 ] + " " ) ; for ( let i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { dp [ i ] = dp [ parseInt ( i / 2 , 10 ) ] ; } else { dp [ i ] = dp [ parseInt ( i / 2 , 10 ) ] + 1 ; } document . write ( dp [ i ] + " " ) ; } } let n = 5 ; findSetBits ( n ) ;
Sort an array of 0 s , 1 s and 2 s | Utility function to print the contents of an array ; Function to sort the array of 0 s , 1 s and 2 s ; Count the number of 0 s , 1 s and 2 s in the array ; Update the array ; Store all the 0 s in the beginning ; Then all the 1 s ; Finally all the 2 s ; Print the sorted array ; Driver program to test above function
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function sortArr ( arr , n ) { let i , cnt0 = 0 , cnt1 = 0 , cnt2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { switch ( arr [ i ] ) { case 0 : cnt0 ++ ; break ; case 1 : cnt1 ++ ; break ; case 2 : cnt2 ++ ; break ; } } i = 0 ; while ( cnt0 > 0 ) { arr [ i ++ ] = 0 ; cnt0 -- ; } while ( cnt1 > 0 ) { arr [ i ++ ] = 1 ; cnt1 -- ; } while ( cnt2 > 0 ) { arr [ i ++ ] = 2 ; cnt2 -- ; } printArr ( arr , n ) ; } let arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] ; let n = arr . length ; sortArr ( arr , n ) ;
Find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted | Javascript program to find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted ; step 1 ( a ) of above algo ; step 1 ( b ) of above algo ; step 2 ( a ) of above algo ; step 2 ( b ) of above algo ; step 2 ( c ) of above algo ; step 3 of above algo
function printUnsorted ( arr , n ) { let s = 0 , e = n - 1 , i , max , min ; for ( s = 0 ; s < n - 1 ; s ++ ) { if ( arr [ s ] > arr [ s + 1 ] ) break ; } if ( s == n - 1 ) { document . write ( " " ) ; return ; } for ( e = n - 1 ; e > 0 ; e -- ) { if ( arr [ e ] < arr [ e - 1 ] ) break ; } max = arr [ s ] ; min = arr [ s ] ; for ( i = s + 1 ; i <= e ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; if ( arr [ i ] < min ) min = arr [ i ] ; } for ( i = 0 ; i < s ; i ++ ) { if ( arr [ i ] > min ) { s = i ; break ; } } for ( i = n - 1 ; i >= e + 1 ; i -- ) { if ( arr [ i ] < max ) { e = i ; break ; } } document . write ( " " + " " + " " + s + " " + e ) ; return ; } let arr = [ 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 ] ; let arr_size = arr . length ; printUnsorted ( arr , arr_size ) ;
Count the number of possible triangles | Function to count all possible triangles with arr [ ] elements ; Count of triangles ; The three loops select three different values from array ; The innermost loop checks for the triangle property ; Sum of two sides is greater than the third ; Driver Code
function findNumberOfTriangles ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] > arr [ k ] && arr [ i ] + arr [ k ] > arr [ j ] && arr [ k ] + arr [ j ] > arr [ i ] ) count ++ ; } } return count ; } let arr = [ 10 , 21 , 22 , 100 , 101 , 200 , 300 ] ; let size = arr . length ; document . write ( " " + findNumberOfTriangles ( arr , size ) ) ;
Count the number of possible triangles | Function to count all possible triangles with arr [ ] elements ; Sort the array elements in non - decreasing order ; Initialize count of triangles ; Fix the first element . We need to run till n - 3 as the other two elements are selected from arr [ i + 1. . . n - 1 ] ; Initialize index of the rightmost third element ; Fix the second element ; Find the rightmost element which is smaller than the sum of two fixed elements The important thing to note here is , we use the previous value of k . If value of arr [ i ] + arr [ j - 1 ] was greater than arr [ k ] , then arr [ i ] + arr [ j ] must be greater than k , because the array is sorted . ; Total number of possible triangles that can be formed with the two fixed elements is k - j - 1. The two fixed elements are arr [ i ] and arr [ j ] . All elements between arr [ j + 1 ] / to arr [ k - 1 ] can form a triangle with arr [ i ] and arr [ j ] . One is subtracted from k because k is incremented one extra in above while loop . k will always be greater than j . If j becomes equal to k , then above loop will increment k , because arr [ k ] + arr [ i ] is always greater than arr [ k ] ; Driver code
function findNumberOfTriangles ( arr ) { let n = arr . length ; arr . sort ( ( a , b ) => a - b ) ; let count = 0 ; for ( let i = 0 ; i < n - 2 ; ++ i ) { let k = i + 2 ; for ( let j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ( k > j ) count += k - j - 1 ; } } return count ; } let arr = [ 10 , 21 , 22 , 100 , 101 , 200 , 300 ] ; let size = arr . length ; document . write ( " " + findNumberOfTriangles ( arr , size ) ) ;
Count the number of possible triangles | CountTriangles function ; If it is possible with a [ l ] , a [ r ] and a [ i ] then it is also possible with a [ l + 1 ] . . a [ r - 1 ] , a [ r ] and a [ i ] ; checking for more possible solutions ; if not possible check for higher values of arr [ l ] ; Driver Code
function CountTriangles ( A ) { var n = A . length ; A . sort ( ) ; var count = 0 ; for ( i = n - 1 ; i >= 1 ; i -- ) { var l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } else { l ++ ; } } } document . write ( " " + count ) ; } var A = [ 4 , 3 , 5 , 7 , 6 ] ; CountTriangles ( A ) ;
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x |
function countPairsBruteForce ( X , Y , m , n ) { let ans = 0 ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( Math . pow ( X [ i ] , Y [ j ] ) > Math . pow ( Y [ j ] , X [ i ] ) ) ) { ans += 1 ; } } } return ans ; }
Maximum items that can be filled in K Knapsacks of given Capacity | 2 - d array to store if a state has been solved ; 2 - d array to store states of DP ; Vector to store power of variable ' C ' . ; function to compute the states ; Base case ; Checking if a state has been solved ; Setting a state as solved ; Recurrence relation ; Returning the solved state ; Function to initialize global variables and find the initial value of ' R ' ; Resizing the variables ; Variable to store the initial value of R ; Input array ; number of knapsacks and capacity ; Performing required pre - computation ; finding the required answer
let v = [ ] ; let dp = [ ] ; let exp_c = [ ] ; function FindMax ( i , r , w , n , c , k ) { if ( i >= n ) { return 0 ; } if ( v [ i ] [ r ] ) { return dp [ i ] [ r ] ; } v [ i ] [ r ] = true ; dp [ i ] [ r ] = FindMax ( i + 1 , r , w , n , c , k ) ; for ( let j = 0 ; j < k ; j ++ ) { let x = ( r / exp_c [ j ] ) % ( c + 1 ) ; if ( x - w [ i ] >= 0 ) { dp [ i ] [ r ] = Math . max ( dp [ i ] [ r ] , w [ i ] + FindMax ( i + 1 , r - w [ i ] * exp_c [ j ] , w , n , c , k ) ) ; } } return dp [ i ] [ r ] ; } function PreCompute ( n , c , k ) { for ( let i = 0 ; i < k ; i ++ ) { exp_c . push ( 0 ) ; } exp_c [ 0 ] = 1 ; for ( let i = 1 ; i < k ; i ++ ) { exp_c [ i ] = ( exp_c [ i - 1 ] * ( c + 1 ) ) ; } for ( let i = 0 ; i < n ; i ++ ) { dp . push ( [ ] ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < ( exp_c [ k - 1 ] * ( c + 1 ) ) ; j ++ ) { dp [ i ] [ 0 ] ; } } for ( let i = 0 ; i < n ; i ++ ) { v . push ( [ ( exp_c [ k - 1 ] * ( c + 1 ) ) ] ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < v [ i ] . length ; j ++ ) { v [ i ] [ j ] = false ; } } let R = 0 ; for ( let i = 0 ; i < k ; i ++ ) { R += exp_c [ i ] * c ; } return R ; } let w = [ 3 , 8 , 9 ] ; let k = 1 , c = 11 ; let n = w . length ; let r = PreCompute ( n , c , k ) ; document . write ( FindMax ( 0 , r , w , n , c , k ) ) ;
Count all distinct pairs with difference equal to k | A simple program to count pairs with difference k ; Pick all elements one by one ; See if there is a pair of this picked element ; Driver program to test above function
function countPairsWithDiffK ( arr , n , k ) { count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; } arr = new Array ( 1 , 5 , 3 , 4 , 2 ) ; n = arr . length ; k = 3 ; document . write ( " " + countPairsWithDiffK ( arr , n , k ) ) ;
Count all distinct pairs with difference equal to k | Standard binary search function ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; Pick a first element point ; Driver program
function binarySearch ( arr , low , high , x ) { if ( high >= low ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( x == arr [ mid ] ) return mid ; if ( x > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } function countPairsWithDiffK ( arr , n , k ) { let count = 0 , i ; arr . sort ( ( a , b ) => a - b ) ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) count ++ ; return count ; } let arr = [ 1 , 5 , 3 , 4 , 2 ] ; let n = arr . length ; let k = 3 ; document . write ( " " + countPairsWithDiffK ( arr , n , k ) ) ;
Count all distinct pairs with difference equal to k | Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; arr [ r ] - arr [ l ] < sum ; Driver program
function countPairsWithDiffK ( arr , n , k ) { let count = 0 ; arr . sort ( ) ; let l = 0 ; let r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } return count ; } let arr = [ 1 , 5 , 3 , 4 , 2 ] ; let n = arr . length ; let k = 3 ; document . write ( " " + countPairsWithDiffK ( arr , n , k ) ) ;
Sum of Bitwise | Javascript program to find sum of Bitwise - OR of all submatrices ; Function to find prefix - count for each row from right to left ; Function to create a booleanean matrix set_bit which stores '1' at an index ( R , C ) if ith bit of arr [ R ] [ C ] is set . ; array to store prefix count of zeros from right to left for booleanean array ; variable to store the count of submatrices with OR value 0 ; For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; stack to store elements and the count of the numbers they popped First part of pair will be the value of inserted element . Second part will be the count of the number of elements pushed before with a greater value ; variable to store the number of submatrices with all 0 s ; Function to find sum of Bitwise - OR of all submatrices ; matrix to store the status of ith bit of each element of matrix arr ; Driver Code
let n = 3 ; function findPrefixCount ( p_arr , set_bit ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = n - 1 ; j >= 0 ; j -- ) { if ( set_bit [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( ! set_bit [ i ] [ j ] ) ? 1 : 0 ; } } } class pair { constructor ( a , b ) { this . first = a ; this . second = b ; } } function matrixOrValueOne ( set_bit ) { let p_arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { p_arr [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) p_arr [ i ] [ j ] = 0 ; } findPrefixCount ( p_arr , set_bit ) ; let count_zero_submatrices = 0 ; for ( let j = 0 ; j < n ; j ++ ) { let i = n - 1 ; let q = [ ] ; let to_sum = 0 ; while ( i >= 0 ) { let c = 0 ; while ( q . length != 0 && q [ q . length - 1 ] . first > p_arr [ i ] [ j ] ) { to_sum -= ( q [ q . length - 1 ] . second + 1 ) * ( q [ q . length - 1 ] . first - p_arr [ i ] [ j ] ) ; c += q [ q . length - 1 ] . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . push ( new pair ( p_arr [ i ] [ j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } function sumOrMatrix ( arr ) { let sum = 0 ; let mul = 1 ; for ( let i = 0 ; i < 30 ; i ++ ) { let set_bit = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) set_bit [ i ] = new Array ( n ) ; for ( let R = 0 ; R < n ; R ++ ) for ( let C = 0 ; C < n ; C ++ ) set_bit [ R ] [ C ] = ( ( arr [ R ] [ C ] & ( 1 << i ) ) != 0 ) ; sum += ( mul * matrixOrValueOne ( set_bit ) ) ; mul *= 2 ; } return sum ; } let arr = [ [ 9 , 7 , 4 ] , [ 8 , 9 , 2 ] , [ 11 , 11 , 5 ] ] ; document . write ( sumOrMatrix ( arr ) ) ;
Count of sub | JavaScript implementation of the approach ; Function to return the total number of required sub - sets ; Variable to store total elements which on dividing by 3 give remainder 0 , 1 and 2 respectively ; Create a dp table ; Process for n states and store the sum ( mod 3 ) for 0 , 1 and 2 ; Use of MOD for large numbers ; Final answer store at dp [ n - 1 ] [ 0 ] ; Driver Program
let MOD = 1000000007 ; function totalSubSets ( n , l , r ) { let zero = Math . floor ( r / 3 ) - Math . ceil ( l / 3 ) + 1 ; let one = Math . floor ( ( r - 1 ) / 3 ) - Math . ceil ( ( l - 1 ) / 3 ) + 1 ; let two = Math . floor ( ( r - 2 ) / 3 ) - Math . ceil ( ( l - 2 ) / 3 ) + 1 ; let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( 3 ) ; for ( let j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ 0 ] = zero ; dp [ 0 ] [ 1 ] = one ; dp [ 0 ] [ 2 ] = two ; for ( let i = 1 ; i < n ; ++ i ) { dp [ i ] [ 0 ] = ( ( dp [ i - 1 ] [ 0 ] * zero ) + ( dp [ i - 1 ] [ 1 ] * two ) + ( dp [ i - 1 ] [ 2 ] * one ) ) % MOD ; dp [ i ] [ 1 ] = ( ( dp [ i - 1 ] [ 0 ] * one ) + ( dp [ i - 1 ] [ 1 ] * zero ) + ( dp [ i - 1 ] [ 2 ] * two ) ) % MOD ; dp [ i ] [ 2 ] = ( ( dp [ i - 1 ] [ 0 ] * two ) + ( dp [ i - 1 ] [ 1 ] * one ) + ( dp [ i - 1 ] [ 2 ] * zero ) ) % MOD ; } return dp [ n - 1 ] [ 0 ] ; } let n = 5 ; let l = 10 ; let r = 100 ; document . write ( totalSubSets ( n , l , r ) ) ;
Check if a word exists in a grid or not | JavaScript program to check if the word exists in the grid or not ; Function to check if a word exists in a grid starting from the first match in the grid level : index till which pattern is matched x , y : current position in 2D array ; Pattern matched ; Out of Boundary ; If grid matches with a letter while recursion ; Marking this cell as visited ; finding subpattern in 4 directions ; marking this cell as unvisited again ; else Not matching then false ; Function to check if the word exists in the grid or not ; if total characters in matrix is less then pattern lenghth ; Traverse in the grid ; If first letter matches , then recur and check ; Driver Code ; Function to check if word exists or not
let r = 4 ; let c = 4 ; function findmatch ( mat , pat , x , y , nrow , ncol , level ) { let l = pat . length ; if ( level == l ) return true ; if ( x < 0 y < 0 x >= nrow y >= ncol ) return false ; if ( mat [ x ] [ y ] == pat [ level ] ) { let temp = mat [ x ] [ y ] ; mat [ x ] [ y ] = ' ' ; let res = findmatch ( mat , pat , x - 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x + 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y - 1 , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y + 1 , nrow , ncol , level + 1 ) ; mat [ x ] [ y ] = temp ; return res ; } return false ; } function checkMatch ( mat , pat , nrow , ncol ) { let l = pat . length ; if ( l > nrow * ncol ) return false ; for ( let i = 0 ; i < nrow ; i ++ ) { for ( let j = 0 ; j < ncol ; j ++ ) { if ( mat [ i ] [ j ] == pat [ 0 ] ) if ( findmatch ( mat , pat , i , j , nrow , ncol , 0 ) ) return true ; } } return false ; } let grid = [ " " . split ( ' ' ) , " " . split ( ' ' ) , " " . split ( ' ' ) , " " . split ( ' ' ) ] ; if ( checkMatch ( grid , " " , r , c ) ) document . write ( " " ) ; else document . write ( " " ) ;
Construct an array from its pair | Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver program to test above function
function constructArr ( arr , pair , n ) { arr [ 0 ] = Math . floor ( ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ) ; for ( let i = 1 ; i < n ; i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ; } let pair = [ 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 ] ; let n = 5 ; let arr = new Array ( n ) ; constructArr ( arr , pair , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Size of the smallest subset with maximum Bitwise OR | Compute bitwise or of all elements in array of size sz ; Recursively calculating the size of minimum subset with maximum or ; If i is arr . length ; If curOr is equal to maxOr ; Return arr . length ; Try the current element in the subset ; Skip the current element ; Return minimum of take and notTake ; Driver Code ; Function Call
function OR ( arr ) { let mOR = 0 ; for ( let i = 0 ; i < arr . length ; ++ i ) { mOR |= arr [ i ] ; } return mOR ; } function maxSubset ( arr , i , curOr , curSize , maxOr ) { if ( i == arr . length ) { if ( curOr == maxOr ) { return curSize ; } else { return arr . length ; } } let take = maxSubset ( arr , i + 1 , curOr arr [ i ] , curSize + 1 , maxOr ) ; let notTake = maxSubset ( arr , i + 1 , curOr , curSize , maxOr ) ; return Math . min ( take , notTake ) ; } let data = [ 5 , 1 , 3 , 4 , 2 ] ; let maxOr = OR ( data ) ; let maxSubsetSize = maxSubset ( data , 0 , 0 , 0 , maxOr ) ; document . write ( maxSubsetSize ) ;
Merge two sorted arrays with O ( 1 ) extra space | Merge ar1 [ ] and ar2 [ ] with O ( 1 ) extra space ; Iterate through all elements of ar2 [ ] starting from the last element ; Find the smallest element greater than ar2 [ i ] . Move all elements one position ahead till the smallest greater element is not found ; If there was a greater element ; Driver method to test the above function
let arr1 = [ 1 , 5 , 9 , 10 , 15 , 20 ] ; let arr2 = [ 2 , 3 , 8 , 13 ] ; function merge ( m , n ) { for ( let i = n - 1 ; i >= 0 ; i -- ) { let j , last = arr1 [ m - 1 ] ; for ( j = m - 2 ; j >= 0 && arr1 [ j ] > arr2 [ i ] ; j -- ) arr1 [ j + 1 ] = arr1 [ j ] ; if ( j != m - 2 last > arr2 [ i ] ) { arr1 [ j + 1 ] = arr2 [ i ] ; arr2 [ i ] = last ; } } } merge ( arr1 . length , arr2 . length ) ; document . write ( " " ) ; for ( let i = 0 ; i < arr1 . length ; i ++ ) { document . write ( arr1 [ i ] + " " ) ; } document . write ( " " ) ; for ( let i = 0 ; i < arr2 . length ; i ++ ) { document . write ( arr2 [ i ] + " " ) ; }
Gould 's Sequence | Function to generate gould 's Sequence ; loop to generate each row of pascal 's Triangle up to nth row ; Loop to generate each element of ith row ; if c is odd increment count ; print count of odd elements ; Get n ; Function call
function gouldSequence ( n ) { for ( var row_num = 1 ; row_num <= n ; row_num ++ ) { var count = 1 ; var c = 1 ; for ( var i = 1 ; i <= row_num ; i ++ ) { c = c * ( row_num - i ) / i ; if ( c % 2 == 1 ) count ++ ; } document . write ( count + " " ) ; } } var n = 16 ; gouldSequence ( n ) ;
Product of maximum in first array and minimum in second | Function to calculate the product ; Sort the arrays to find the maximum and minimum elements in given arrays ; Return product of maximum and minimum . ; Driven code
function minMaxProduct ( arr1 , arr2 , n1 , n2 ) { arr1 . sort ( ( a , b ) => a - b ) ; arr2 . sort ( ( a , b ) => a - b ) ; return ( arr1 [ n1 - 1 ] * arr2 [ 0 ] ) ; } let arr1 = [ 10 , 2 , 3 , 6 , 4 , 1 ] ; let arr2 = [ 5 , 1 , 4 , 2 , 6 , 9 ] ; let n1 = arr1 . length ; let n2 = arr2 . length ; document . write ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) ;
Minimum odd cost path in a matrix | Javascript program to find minimum odd cost path in a matrix ; Function to find the minimum cost ; leftmost element ; rightmost element ; Any element except leftmost and rightmost element of a row is reachable from direct upper or left upper or right upper row 's block ; Counting the minimum cost ; Find the minimum cost ; Driver code
let M = 100 ; let N = 100 ; function find_min_odd_cost ( given , m , n ) { let floor = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { floor [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { floor [ i ] [ j ] = 0 ; } } let min_odd_cost = 0 ; let i , j , temp ; for ( j = 0 ; j < n ; j ++ ) floor [ 0 ] [ j ] = given [ 0 ] [ j ] ; for ( i = 1 ; i < m ; i ++ ) for ( j = 0 ; j < n ; j ++ ) { if ( j == 0 ) { floor [ i ] [ j ] = given [ i ] [ j ] ; floor [ i ] [ j ] += Math . min ( floor [ i - 1 ] [ j ] , floor [ i - 1 ] [ j + 1 ] ) ; } else if ( j == n - 1 ) { floor [ i ] [ j ] = given [ i ] [ j ] ; floor [ i ] [ j ] += Math . min ( floor [ i - 1 ] [ j ] , floor [ i - 1 ] [ j - 1 ] ) ; } else { temp = Math . min ( floor [ i - 1 ] [ j ] , floor [ i - 1 ] [ j - 1 ] ) ; temp = Math . min ( temp , floor [ i - 1 ] [ j + 1 ] ) ; floor [ i ] [ j ] = given [ i ] [ j ] + temp ; } } min_odd_cost = Number . MAX_VALUE ; for ( j = 0 ; j < n ; j ++ ) { if ( floor [ n - 1 ] [ j ] % 2 == 1 ) { if ( min_odd_cost > floor [ n - 1 ] [ j ] ) min_odd_cost = floor [ n - 1 ] [ j ] ; } } if ( min_odd_cost == Number . MIN_VALUE ) return - 1 ; return min_odd_cost ; } let m = 5 , n = 5 ; let given = [ [ 1 , 2 , 3 , 4 , 6 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 100 , 2 , 3 , 4 , 5 ] ] ; document . write ( " " + find_min_odd_cost ( given , m , n ) ) ;
Product of maximum in first array and minimum in second | Function to calculate the product ; Initialize max of first array ; Initialize min of second array ; To find the maximum element in first array ; To find the minimum element in second array ; Process remaining elements ; Driver Code
function minMaxProduct ( arr1 , arr2 , n1 , n2 ) { let max = arr1 [ 0 ] ; let min = arr2 [ 0 ] ; let i ; for ( i = 1 ; i < n1 && i < n2 ; ++ i ) { if ( arr1 [ i ] > max ) max = arr1 [ i ] ; if ( arr2 [ i ] < min ) min = arr2 [ i ] ; } while ( i < n1 ) { if ( arr1 [ i ] > max ) max = arr1 [ i ] ; i ++ ; } while ( i < n2 ) { if ( arr2 [ i ] < min ) min = arr2 [ i ] ; i ++ ; } return max * min ; } let arr1 = [ 10 , 2 , 3 , 6 , 4 , 1 ] ; let arr2 = [ 5 , 1 , 4 , 2 , 6 , 9 ] ; let n1 = 6 ; let n2 = 6 ; document . write ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) ;
Burst Balloon to maximize coins | Javascript program burst balloon problem ; Add Bordering Balloons ; Declare DP Array ; For a sub - array from indices left , right This innermost loop finds the last balloon burst ; Driver code ; Size of the array ; Calling function
function getMax ( A , N ) { var B = new Array ( N + 2 ) ; B [ 0 ] = 1 ; B [ N + 1 ] = 1 ; for ( var i = 1 ; i <= N ; i ++ ) B [ i ] = A [ i - 1 ] ; var dp = new Array ( N + 2 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( N + 2 ) . fill ( 0 ) ; } for ( var length = 1 ; length < N + 1 ; length ++ ) { for ( var left = 1 ; left < N - length + 2 ; left ++ ) { var right = left + length - 1 ; for ( var last = left ; last < right + 1 ; last ++ ) { dp [ left ] [ right ] = Math . max ( dp [ left ] [ right ] , dp [ left ] [ last - 1 ] + B [ left - 1 ] * B [ last ] * B [ right + 1 ] + dp [ last + 1 ] [ right ] ) ; } } } return dp [ 1 ] [ N ] ; } var A = [ 1 , 2 , 3 , 4 , 5 ] ; var N = A . length ; document . write ( getMax ( A , N ) ) ;
Search , insert and delete in an unsorted array | Function to implement search operation ; Driver program ; Using a last element as search element
function findElement ( arr , n , key ) { let i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } let arr = [ 12 , 34 , 10 , 6 , 40 ] ; let n = arr . length ; let key = 40 ; let position = findElement ( arr , n , key ) ; if ( position == - 1 ) document . write ( " " ) ; else document . write ( " " + ( position + 1 ) ) ;