codeparrot/xlcost-text-to-code huggingface dataset:
Tower of Hanoi | Set 2 | Function to print order of movement of N disks across three rods to place all disks on the third rod from the first - rod using binary representation ; Iterate over the range [ 0 , 2 ^ N - 1 ] ; Print the movement of the current rod ; Driver Code
function TowerOfHanoi ( N ) { for ( let x = 1 ; x <= Math . pow ( 2 , N ) - 1 ; x ++ ) { document . write ( " " + ( ( x & x - 1 ) % 3 + 1 ) + " " + ( ( ( x x - 1 ) + 1 ) % 3 + 1 ) + " " ) ; } } let N = 3 ; TowerOfHanoi ( N ) ;
Check if a path exists from a given cell to any boundary element of the Matrix with sum of elements not exceeding K | Function to check if it is valid to move to the given index or not ; Otherwise return false ; Function to check if there exists a path from the cell ( X , Y ) of the matrix to any boundary cell with sum of elements at most K or not ; Base Case ; If K >= board [ X ] [ Y ] ; Stores the current element ; Mark the current cell as visited ; Visit all the adjacent cells ; Mark the cell unvisited ; Return false ; Driver code
function isValid ( board , i , j , K ) { if ( board [ i ] [ j ] <= K ) { return true ; } return false ; } function findPath ( board , X , Y , M , N , K ) { if ( X < 0 X == M Y < 0 Y == N ) { return true ; } if ( isValid ( board , X , Y , K ) ) { let board_XY = board [ X ] [ Y ] ; board [ X ] [ Y ] = Number . MAX_VALUE ; if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) || findPath ( board , X - 1 , Y , M , N , K - board_XY ) || findPath ( board , X , Y + 1 , M , N , K - board_XY ) || findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) { return true ; } board [ X ] [ Y ] = board_XY ; } return false ; } let grid = [ [ 25 , 5 , 25 , 25 , 25 , 25 ] , [ 25 , 1 , 1 , 5 , 12 , 25 ] , [ 25 , 1 , 12 , 0 , 15 , 25 ] , [ 22 , 1 , 11 , 2 , 19 , 15 ] , [ 25 , 2 , 2 , 1 , 12 , 15 ] , [ 25 , 9 , 10 , 1 , 11 , 25 ] , [ 25 , 25 , 25 , 25 , 25 , 25 ] ] ; let K = 17 ; let M = grid . length ; let N = grid [ 0 ] . length ; let X = 2 , Y = 3 ; if ( findPath ( grid , X , Y , M , N , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of all subsets whose sum is a Perfect Number from a given array | Function to check is a given number is a perfect number or not ; Stores sum of divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find the sum of all the subsets from an array whose sum is a perfect number ; Stores the total number of subsets , i . e . 2 ^ n ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider array elements from positions of set bits in the binary representation of n ; If sum of chosen elements is a perfect number ; Driver Code
function isPerfect ( x ) { var sum_div = 1 ; for ( var i = 2 ; i <= parseInt ( x / 2 ) ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } function subsetSum ( arr , n ) { var total = 1 << n ; for ( i = 0 ; i < total ; i ++ ) { var sum = 0 ; for ( j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) sum += arr [ j ] ; if ( isPerfect ( sum ) != 0 ) { document . write ( sum + " " ) ; } } } var arr = [ 5 , 4 , 6 ] ; var N = arr . length ; subsetSum ( arr , N ) ;
Pen Distribution Problem | Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code
function solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) { if ( N == 0 || ( QuitP1 && QuitP2 ) ) { document . write ( " " + " " + N + " " ) ; document . write ( " " + " " + P1 + " " ) ; document . write ( " " + " " + P2 + " " ) ; return ; } if ( Move == 0 && QuitP1 == false ) { let req_P1 = ( Math . pow ( 2 , X ) ) ; if ( req_P1 <= N ) { P1 += req_P1 ; N -= req_P1 ; } else { QuitP1 = true ; } } else if ( Move == 1 && QuitP2 == false ) { let req_P2 = ( Math . pow ( 3 , X ) ) ; if ( req_P2 <= N ) { P2 += req_P2 ; N -= req_P2 ; } else { QuitP2 = true ; } } X ++ ; Move = ( ( Move == 1 ) ? 0 : 1 ) ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } function PenGame ( N ) { let P1 = 0 ; let P2 = 0 ; let X = 0 ; let Move = 0 ; let QuitP1 = false ; let QuitP2 = false ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } let N = 22 ; PenGame ( N ) ;
Generate all N digit numbers having absolute difference as K between adjacent digits | Function that recursively finds the possible numbers and append into ans ; Base Case ; Check the sum of last digit and k less than or equal to 9 or not ; If k == 0 , then subtraction and addition does not make any difference Hence , subtraction is skipped ; Function to call checkUntil function for every integer from 1 to 9 ; check_util function recursively store all numbers starting from i ; Function to print the all numbers which satisfy the conditions ; Given N and K ; To store the result ; Function Call ; Print Resultant Numbers
function checkUntil ( num , K , N , ans ) { if ( N == 1 ) { ans . push ( num ) ; return ; } if ( ( num % 10 + K ) <= 9 ) checkUntil ( 10 * num + ( num % 10 + K ) , K , N - 1 , ans ) ; if ( K > 0 ) { if ( ( num % 10 - K ) >= 0 ) checkUntil ( 10 * num + num % 10 - K , K , N - 1 , ans ) ; } } function check ( K , N , ans ) { for ( let i = 1 ; i <= 9 ; i ++ ) { checkUntil ( i , K , N , ans ) ; } } function print ( ans ) { for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let N = 4 , K = 8 ; let ans = [ ] check ( K , N , ans ) ; print ( ans ) ;
Minimum Cost Path in a directed graph via given set of intermediate nodes | Javascript program to implement the above approach ; Stores minimum - cost of path from source ; Function to Perform BFS on graph g starting from vertex v ; If destination is reached ; Set flag to true ; Visit all the intermediate nodes ; If any intermediate node is not visited ; If all intermediate nodes are visited ; Update the minSum ; Mark the current node visited ; Traverse adjacent nodes ; Mark the neighbour visited ; Find minimum cost path considering the neighbour as the source ; Mark the neighbour unvisited ; Mark the source unvisited ; Stores the graph ; Number of nodes ; Source ; Destination ; Keeps a check on visited and unvisited nodes ; Stores intemediate nodes ; If no path is found
class pair { constructor ( f , s ) { this . first = f ; this . second = s ; } } var minSum = 100000000 ; function getMinPathSum ( graph , visited , necessary , src , dest , currSum ) { if ( src == dest ) { var flag = true ; for ( var i of necessary ) { if ( ! visited [ i ] ) { flag = false ; break ; } } if ( flag ) minSum = Math . min ( minSum , currSum ) ; return ; } else { visited [ src ] = true ; for ( var node of graph [ src ] ) { if ( ! visited [ node . first ] ) { visited [ node . first ] = true ; getMinPathSum ( graph , visited , necessary , node . first , dest , currSum + node . second ) ; visited [ node . first ] = false ; } } visited [ src ] = false ; } } var graph = Array . from ( Array ( 7 ) , ( ) => Array ( ) ) ; graph [ 0 ] . push ( new pair ( 1 , 2 ) ) ; graph [ 0 ] . push ( new pair ( 2 , 3 ) ) ; graph [ 0 ] . push ( new pair ( 3 , 2 ) ) ; graph [ 1 ] . push ( new pair ( 4 , 4 ) ) ; graph [ 1 ] . push ( new pair ( 0 , 1 ) ) ; graph [ 2 ] . push ( new pair ( 4 , 5 ) ) ; graph [ 2 ] . push ( new pair ( 5 , 6 ) ) ; graph [ 3 ] . push ( new pair ( 5 , 7 ) ) ; graph [ 3 ] . push ( new pair ( 0 , 1 ) ) ; graph [ 4 ] . push ( new pair ( 6 , 4 ) ) ; graph [ 5 ] . push ( new pair ( 4 , 2 ) ) ; graph [ 6 ] . push ( new pair ( 7 , 11 ) ) ; var n = 7 ; var source = 0 ; var dest = 6 ; var visited = Array ( n ) . fill ( false ) ; var necessary = [ ] ; necessary . push ( 2 ) ; necessary . push ( 4 ) ; getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ; if ( minSum == 100000000 ) document . write ( - 1 ) ; else document . write ( minSum ) ;
Unique subsequences of length K with given sum | Function to find all the subsequences of a given length and having sum S ; Termination condition ; Add value to sum ; Check if the resultant sum equals to target sum ; If true ; Prlet resultant array ; End this recursion stack ; Check all the combinations using backtracking ; Check all the combinations using backtracking ; Given array ; To store the subsequence ; Function Call
function comb ( arr , len , r , ipos , op , opos , sum ) { if ( opos == r ) { let sum2 = 0 ; for ( let i = 0 ; i < opos ; i ++ ) { sum2 = sum2 + op [ i ] ; } if ( sum == sum2 ) { for ( let i = 0 ; i < opos ; i ++ ) document . write ( op [ i ] + " " ) ; document . write ( ) ; } return ; } if ( ipos < len ) { comb ( arr , len , r , ipos + 1 , op , opos , sum ) ; op [ opos ] = arr [ ipos ] ; comb ( arr , len , r , ipos + 1 , op , opos + 1 , sum ) ; } } let arr = [ 4 , 6 , 8 , 2 , 12 ] ; let K = 3 ; let S = 20 ; let N = arr . length ; let op = Array . from ( { length : N } , ( _ , i ) => 0 ) ; comb ( arr , N , K , 0 , op , 0 , S ) ;
Traverse the matrix in Diagonally Bottum | Static variable for changing row and column ; Flag variable for handling bottum up diagonal traversing ; Recursive function to traverse the matrix diagonally bottom - up ; Base Condition ; Condition when to traverse Bottom Diagonal of the matrix ; Print matrix cell value ; Recursive function to traverse The matrix diagonally ; Recursive function to change diagonal ; Initialize the 5 x 5 matrix ; Function call for traversing matrix
let k1 = 0 , k2 = 0 ; let flag = true ; function traverseMatrixDiagonally ( m , i , j , row , col ) { if ( i >= row j >= col ) { if ( flag ) { let a = k1 ; k1 = k2 ; k2 = a ; flag = ! flag ; k1 ++ ; } else { let a = k1 ; k1 = k2 ; k2 = a ; flag = ! flag ; } document . write ( " " ) ; return false ; } document . write ( m [ i ] [ j ] + " " ) ; if ( traverseMatrixDiagonally ( m , i + 1 , j + 1 , row , col ) ) { return true ; } if ( traverseMatrixDiagonally ( m , k1 , k2 , row , col ) ) { return true ; } return true ; } let mtrx = [ [ 10 , 11 , 12 , 13 , 14 ] , [ 15 , 16 , 17 , 18 , 19 ] , [ 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 ] , [ 30 , 31 , 32 , 33 , 34 ] ] ; traverseMatrixDiagonally ( mtrx , 0 , 0 , 5 , 5 ) ;
Minimum colors required such that edges forming cycle do not have same color | C ++ implementation to find the minimum colors required to such that edges forming cycle don 't have same color ; Variable to store the graph ; To store that the vertex is visited or not ; Boolean Value to store that graph contains cycle or not ; Variable to store the color of the edges of the graph ; Function to traverse graph using DFS Traversal ; Loop to iterate for all edges from the source vertex ; If the vertex is not visited ; Condition to check cross and forward edges of the graph ; Presence of Back Edge ; Driver Code ; Loop to run DFS Traversal on vertex which is not visited ; Loop to print the colors of the edges
const n = 5 , m = 7 ; let g = new Array ( ) ; for ( let i = 0 ; i < m ; i ++ ) { g . push ( [ ] ) } let col = new Array ( n ) . fill ( 0 ) ; let cyc ; let res = new Array ( m ) ; function dfs ( v ) { col [ v ] = 1 ; for ( let p of g [ v ] ) { let to = p [ 0 ] let id = p [ 1 ] ; if ( col [ to ] == 0 ) { dfs ( to ) ; res [ id ] = 1 ; } else if ( col [ to ] == 2 ) { res [ id ] = 1 ; } else { res [ id ] = 2 ; cyc = true ; } } col [ v ] = 2 ; } g [ 0 ] . push ( [ 1 , 0 ] ) ; g [ 0 ] . push ( [ 2 , 1 ] ) ; g [ 1 ] . push ( [ 2 , 2 ] ) ; g [ 1 ] . push ( [ 3 , 3 ] ) ; g [ 2 ] . push ( [ 3 , 4 ] ) ; g [ 3 ] . push ( [ 4 , 5 ] ) ; g [ 4 ] . push ( [ 2 , 6 ] ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( col [ i ] == 0 ) { dfs ( i ) ; } } document . write ( ( cyc ? 2 : 1 ) + " " ) ; for ( let i = 0 ; i < m ; ++ i ) { document . write ( res [ i ] + ' ' ) ; }
Perfect Sum Problem | Function to print the subsets whose sum is equal to the given target K ; Create the new array with length equal to array set [ ] to create binary array as per n ( decimal number ) ; Convert the array into binary array ; Calculate the sum of this subset ; Check whether sum is equal to target if it is equal , then print the subset ; Function to find the subsets with sum K ; Calculate the total no . of subsets ; Run loop till total no . of subsets and call the function for each subset ; Driver code
function sumSubsets ( set , n , target ) { let x = new Array ( set . length ) ; let j = set . length - 1 ; while ( n > 0 ) { x [ j ] = n % 2 ; n = Math . floor ( n / 2 ) ; j -- ; } let sum = 0 ; for ( let i = 0 ; i < set . length ; i ++ ) if ( x [ i ] == 1 ) sum = sum + set [ i ] ; if ( sum == target ) { document . write ( " " ) ; for ( let i = 0 ; i < set . length ; i ++ ) if ( x [ i ] == 1 ) document . write ( set [ i ] + " " ) ; document . write ( " " ) ; } } function findSubsets ( arr , K ) { let x = Math . pow ( 2 , arr . length ) ; for ( let i = 1 ; i < x ; i ++ ) sumSubsets ( arr , i , K ) ; } let arr = [ 5 , 10 , 12 , 13 , 15 , 18 ] ; let K = 30 ; findSubsets ( arr , K ) ;
Sum of subsets of all the subsets of an array | O ( N ) | To store factorial values ; Function to return ncr ; Function to return the required sum ; Initialising factorial ; Multiplier ; Finding the value of multipler according to the formula ; To store the final answer ; Calculate the final answer ; Driver code
let fact = new Array ( 10 ) ; function ncr ( n , r ) { return ( fact [ n ] / fact [ r ] ) / fact [ n - r ] ; } function findSum ( arr , n ) { fact [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; let mul = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) mul += Math . pow ( 2 , i ) * ncr ( n - 1 , i ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += mul * arr [ i ] ; return ans ; } let arr = [ 1 , 1 ] ; let n = arr . length ; document . write ( findSum ( arr , n ) ) ;
Count number of ways to reach destination in a maze | Javascript implementation of the approach ; Function to return the count of possible paths in a maze [ R ] [ C ] from ( 0 , 0 ) to ( R - 1 , C - 1 ) that do not pass through any of the marked cells ; If the initial cell is blocked , there is no way of moving anywhere ; Initializing the leftmost column ; If we encounter a blocked cell in leftmost row , there is no way of visiting any cell directly below it . ; Similarly initialize the topmost row ; If we encounter a blocked cell in bottommost row , there is no way of visiting any cell directly below it . ; The only difference is that if a cell is - 1 , simply ignore it else recursively compute count value maze [ i ] [ j ] ; If blockage is found , ignore this cell ; If we can reach maze [ i ] [ j ] from maze [ i - 1 ] [ j ] then increment count . ; If we can reach maze [ i ] [ j ] from maze [ i ] [ j - 1 ] then increment count . ; If the final cell is blocked , output 0 , otherwise the answer ; Function to return the count of all possible paths from ( 0 , 0 ) to ( n - 1 , m - 1 ) ; We have to calculate m + n - 2 C n - 1 here which will be ( m + n - 2 ) ! / ( n - 1 ) ! ( m - 1 ) ! ; Function to return the total count of paths from ( 0 , 0 ) to ( n - 1 , m - 1 ) that pass through at least one of the marked cells ; Total count of paths - Total paths that do not pass through any of the marked cell ; return answer ; Driver code
var R = 4 var C = 4 function countPaths ( maze ) { if ( maze [ 0 ] [ 0 ] == - 1 ) return 0 ; for ( var i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( var i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( var i = 1 ; i < R ; i ++ ) { for ( var j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == - 1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } function numberOfPaths ( m , n ) { var path = 1 ; for ( var i = n ; i < ( m + n - 1 ) ; i ++ ) { path *= i ; path /= ( i - n + 1 ) ; } return path ; } function solve ( maze ) { var ans = numberOfPaths ( R , C ) - countPaths ( maze ) ; return ans ; } var maze = [ [ 0 , 0 , 0 , 0 ] , [ 0 , - 1 , 0 , 0 ] , [ - 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] ; document . write ( solve ( maze ) ) ;
Rat in a Maze with multiple steps or jump allowed | Maze size ; A utility function to print solution matrix sol [ N ] [ N ] ; A utility function to check if x , y is valid index for N * N maze ; if ( x , y outside maze ) return false ; This function solves the Maze problem using Backtracking . It mainly uses solveMazeUtil ( ) to solve the problem . It returns false if no path is possible , otherwise return true and print the path in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; A recursive utility function to solve Maze problem ; if ( x , y is goal ) return true ; Check if maze [ x ] [ y ] is valid ; mark x , y as part of solution path ; Move forward in x direction ; Move forward in x direction ; If moving in x direction doesn 't give  solution then Move down in y direction  ; If none of the above movements work then BACKTRACK : unmark x , y as part of solution path ; Driver code
let N = 4 ; function printSolution ( sol ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { document . write ( sol [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function isSafe ( maze , x , y ) { if ( x >= 0 && x < N && y >= 0 && y < N && maze [ x ] [ y ] != 0 ) { return true ; } return false ; } function solveMaze ( maze ) { let sol = [ [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] ; if ( solveMazeUtil ( maze , 0 , 0 , sol ) == false ) { document . write ( " " ) ; return false ; } printSolution ( sol ) ; return true ; } function solveMazeUtil ( maze , x , y , sol ) { if ( x == N - 1 && y == N - 1 ) { sol [ x ] [ y ] = 1 ; return true ; } if ( isSafe ( maze , x , y ) == true ) { sol [ x ] [ y ] = 1 ; for ( let i = 1 ; i <= maze [ x ] [ y ] && i < N ; i ++ ) { if ( solveMazeUtil ( maze , x + i , y , sol ) == true ) { return true ; } if ( solveMazeUtil ( maze , x , y + i , sol ) == true ) { return true ; } } sol [ x ] [ y ] = 0 ; return false ; } return false ; } let maze = [ [ 2 , 1 , 0 , 0 ] , [ 3 , 0 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 ] ] ; solveMaze ( maze ) ;
Prime numbers after prime P with sum S | vector to store prime and N primes whose sum equals given S ; function to check prime number ; square root of x ; since 1 is not prime number ; if any factor is found return false ; no factor found ; function to display N primes whose sum equals S ; function to evaluate all possible N primes whose sum equals S ; if total equals S And total is reached using N primes ; display the N primes ; if total is greater than S or if index has reached last element ; add prime [ index ] to set vector ; include the ( index ) th prime to total ; remove element from set vector ; exclude ( index ) th prime ; function to generate all primes ; all primes less than S itself ; if i is prime add it to prime vector ; if primes are less than N ; Driver Code
var set = [ ] ; var prime = [ ] ; function isPrime ( x ) { var sqroot = Math . sqrt ( x ) ; var flag = true ; if ( x == 1 ) return false ; for ( var i = 2 ; i <= sqroot ; i ++ ) if ( x % i == 0 ) return false ; return true ; } function display ( ) { var length = set . length ; for ( var i = 0 ; i < length ; i ++ ) document . write ( set [ i ] + " " ) ; document . write ( " " ) ; } function primeSum ( total , N , S , index ) { if ( total == S && set . length == N ) { display ( ) ; return ; } if ( total > S index == prime . length ) return ; set . push ( prime [ index ] ) ; primeSum ( total + prime [ index ] , N , S , index + 1 ) ; set . pop ( ) ; primeSum ( total , N , S , index + 1 ) ; } function allPrime ( N , S , P ) { for ( var i = P + 1 ; i <= S ; i ++ ) { if ( isPrime ( i ) ) prime . push ( i ) ; } if ( prime . length < N ) return ; primeSum ( 0 , N , S , 0 ) ; } var S = 54 , N = 2 , P = 3 ; allPrime ( N , S , P ) ;
A backtracking approach to generate n bit Gray Codes | We have 2 choices for each of the n bits either we can include i . e invert the bit or we can exclude the bit i . e we can leave the number as it is . ; Base case when we run out bits to process we simply include it in gray code sequence . ; Ignore the bit . ; Invert the bit . ; Returns the vector containing the gray code sequence of n bits . ; num is passed by reference to keep track of current code . ; Driver code
function grayCodeUtil ( res , n , num ) { if ( n == 0 ) { res . push ( num [ 0 ] ) ; return ; } grayCodeUtil ( res , n - 1 , num ) ; num [ 0 ] = num [ 0 ] ^ ( 1 << ( n - 1 ) ) ; grayCodeUtil ( res , n - 1 , num ) ; } function grayCodes ( n ) { let res = [ ] ; let num = [ 0 ] ; grayCodeUtil ( res , n , num ) ; return res ; } let n = 3 ; let code = grayCodes ( n ) ; for ( let i = 0 ; i < code . length ; i ++ ) document . write ( code [ i ] + " " ) ;
Remove Invalid Parentheses | method checks if character is parenthesis ( openor closed ) ; method returns true if string contains valid parenthesis ; method to remove invalid parenthesis ; visit set to ignore already visited string ; queue to maintain BFS ; pushing given string as starting node into queue ; If answer is found , make level true so that valid string of only that level are processed . ; Removing parenthesis from str and pushing into queue , if not visited already ; Driver Code
function isParenthesis ( c ) { return ( ( c == ' ' ) || ( c == ' ' ) ) ; } function isValidString ( str ) { let cnt = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) cnt ++ ; else if ( str [ i ] == ' ' ) cnt -- ; if ( cnt < 0 ) return false ; } return ( cnt == 0 ) ; } function removeInvalidParenthesis ( str ) { if ( str . length == 0 ) return ; let visit = new Set ( ) ; let q = [ ] ; let temp ; let level = false ; q . push ( str ) ; visit . add ( str ) ; while ( q . length != 0 ) { str = q . shift ( ) ; if ( isValidString ( str ) ) { document . write ( str + " " ) ; level = true ; } if ( level ) continue ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ! isParenthesis ( str [ i ] ) ) continue ; temp = str . substring ( 0 , i ) + str . substring ( i + 1 ) ; if ( ! visit . has ( temp ) ) { q . push ( temp ) ; visit . add ( temp ) ; } } } } let expression = " " ; removeInvalidParenthesis ( expression ) ; expression = " " ; removeInvalidParenthesis ( expression ) ;
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Function to print pairs ; Initializing the rem values with 0 's. ; Perform the remainder operation only if the element is x , as numbers greater than x can 't  be used to get a sum x.  Updating the count of remainders. ; Traversing the remainder list from start to middle to find pairs ; The elements with remainders i and x - i will result to a sum of x . Once we get two elements which add up to x , we print x and break . ; Once we reach middle of remainder array , we have to do operations based on x . ; If x is even and we have more than 1 elements with remainder x / 2 , then we will have two distinct elements which add up to x . if we dont have more than 1 element , print " No " . ; When x is odd we continue the same process which we did in previous loop . ; Driver code ; Function calling
function printPairs ( a , n , x ) { let i ; let rem = new Array ( x ) ; for ( i = 0 ; i < x ; i ++ ) { rem [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] < x ) { rem [ a [ i ] % x ] ++ ; } } for ( i = 1 ; i < x / 2 ; i ++ ) { if ( rem [ i ] > 0 && rem [ x - i ] > 0 ) { document . write ( " " + " " ) ; break ; } } if ( i >= x / 2 ) { if ( x % 2 == 0 ) { if ( rem [ x / 2 ] > 1 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } else { if ( rem [ x / 2 ] > 0 && rem [ x - x / 2 ] > 0 ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; } } } } let A = [ 1 , 4 , 45 , 6 , 10 , 8 ] ; let n = 16 ; let arr_size = A . length ; printPairs ( A , arr_size , n ) ;