codeparrot/xlcost-text-to-code huggingface dataset:
Print n x n spiral matrix using O ( 1 ) extra space | Prints spiral matrix of size n x n containing numbers from 1 to n x n ; x stores the layer in which ( i , j ) th element lies ; Finds minimum of four inputs ; For upper right half ; For lower left half ; Driver code ; Print a n x n spiral matrix in O ( 1 ) space
function printSpiral ( n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { let x ; x = Math . min ( Math . min ( i , j ) , Math . min ( n - 1 - i , n - 1 - j ) ) ; if ( i <= j ) document . write ( ` ${ ( n - 2 * x ) * ( n - 2 * x ) - ( i - x ) - ( j - x ) } ` ) ; else document . write ( ` ${ ( n - 2 * x - 2 ) * ( n - 2 * x - 2 ) + ( i - x ) + ( j - x ) } ` ) ; } document . write ( " " ) ; } } let n = 5 ; printSpiral ( n ) ;
Convert a given Binary Tree to Doubly Linked List | Set 1 | A binary tree node has data , and left and right pointers ; * This is the core function to convert Tree to list . This function follows * steps 1 and 2 of the above algorithm ; Base case ; Convert the left subtree and link to root ; Convert the left subtree ; Find inorder predecessor . After this loop , left will povar to the inorder predecessor ; Make root as next of the predecessor ; Make predecssor as previous of root ; Convert the right subtree and link to root ; Convert the right subtree ; Find inorder successor . After this loop , right will povar to the inorder successor ; Make root as previous of successor ; Make successor as next of root ; The main function that first calls bintree2listUtil ( ) , then follows step 3 of the above algorithm ; Base case ; Convert to DLL using bintree2listUtil ( ) ; bintree2listUtil ( ) returns root node of the converted DLL . We need pointer to the leftmost node which is head of the constructed DLL , so move to the leftmost node ; Function to print nodes in a given doubly linked list ; Let us create the tree shown in above diagram ; Convert to DLL ; Prvar the converted list
class Node { constructor ( val ) { this . data = val ; this . left = null ; this . right = null ; } } var root ; function bintree2listUtil ( node ) { if ( node == null ) return node ; if ( node . left != null ) { var left = bintree2listUtil ( node . left ) ; for ( ; left . right != null ; left = left . right ) left . right = node ; node . left = left ; } if ( node . right != null ) { var right = bintree2listUtil ( node . right ) ; for ( ; right . left != null ; right = right . left ) right . left = node ; node . right = right ; } return node ; } function bintree2list ( node ) { if ( node == null ) return node ; node = bintree2listUtil ( node ) ; while ( node . left != null ) node = node . left ; return node ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . right ; } } root = new Node ( 10 ) ; root . left = new Node ( 12 ) ; root . right = new Node ( 15 ) ; root . left . left = new Node ( 25 ) ; root . left . right = new Node ( 30 ) ; root . right . left = new Node ( 36 ) ; var head = bintree2list ( root ) ; printList ( head ) ;
A Boolean Matrix Question | Javascript Code For A Boolean Matrix Question ; Initialize all values of row [ ] as 0 ; Initialize all values of col [ ] as 0 ; Store the rows and columns to be marked as 1 in row [ ] and col [ ] arrays respectively ; Modify the input matrix mat [ ] using the above constructed row [ ] and col [ ] arrays ; A utility function to print a 2D matrix ; Driver program to test above functions
function modifyMatrix ( mat , R , C ) { let row = new Array ( R ) ; let col = new Array ( C ) ; for ( i = 0 ; i < R ; i ++ ) { row [ i ] = 0 ; } for ( i = 0 ; i < C ; i ++ ) { col [ i ] = 0 ; } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] = 1 ; col [ j ] = 1 ; } } } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( row [ i ] == 1 col [ j ] == 1 ) { mat [ i ] [ j ] = 1 ; } } } } function printMatrix ( mat , R , C ) { let i , j ; for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 1 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 ] ] ; document . write ( " " ) printMatrix ( mat , 3 , 4 ) ; modifyMatrix ( mat , 3 , 4 ) ; document . write ( " " ) ; printMatrix ( mat , 3 , 4 ) ;
Print unique rows in a given boolean matrix | Given a binary matrix of M X N of integers , you need to return only unique rows of binary array ; Function that prints all unique rows in a given matrix . ; Traverse through the matrix ; Check if there is similar column is already printed , i . e if i and jth column match . ; If no row is similar ; Print the row ; Driver Code
let ROW = 4 ; let COL = 5 ; function findUniqueRows ( M ) { for ( let i = 0 ; i < ROW ; i ++ ) { let flag = 0 ; for ( let j = 0 ; j < i ; j ++ ) { flag = 1 ; for ( let k = 0 ; k < COL ; k ++ ) if ( M [ i ] [ k ] != M [ j ] [ k ] ) flag = 0 ; if ( flag == 1 ) break ; } if ( flag == 0 ) { for ( let j = 0 ; j < COL ; j ++ ) document . write ( M [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } } let M = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 0 , 0 ] ] findUniqueRows ( M )
Program for Rank of Matrix | javascript program to find rank of a matrix ; function for exchanging two rows of a matrix ; function for finding rank of matrix ; Before we visit current row ' row ' , we make sure that mat [ row ] [ 0 ] , ... . mat [ row ] [ row - 1 ] are 0. Diagonal element is not zero ; This makes all entries of current column as 0 except entry ' mat [ row ] [ row ] ' ; Diagonal element is already zero . Two cases arise : 1 ) If there is a row below it with non - zero entry , then swap this row with that row and process that row 2 ) If all elements in current column below mat [ r ] [ row ] are 0 , then remvoe this column by swapping it with last column and reducing number of columns by 1. ; Find the non - zero element in current column ; Swap the row with non - zero element with this row . ; If we did not find any row with non - zero element in current columnm , then all values in this column are 0. ; Reduce number of columns ; Copy the last column here ; Process this row again ; Uncomment these lines to see intermediate results display ( mat , R , C ) ; printf ( < br > ) ; ; Function to display a matrix ; Driver code
var R = 3 ; var C = 3 ; function swap ( mat , row1 , row2 , col ) { for ( i = 0 ; i < col ; i ++ ) { var temp = mat [ row1 ] [ i ] ; mat [ row1 ] [ i ] = mat [ row2 ] [ i ] ; mat [ row2 ] [ i ] = temp ; } } function rankOfMatrix ( mat ) { var rank = C ; for ( row = 0 ; row < rank ; row ++ ) { if ( mat [ row ] [ row ] != 0 ) { for ( col = 0 ; col < R ; col ++ ) { if ( col != row ) { var mult = mat [ col ] [ row ] / mat [ row ] [ row ] ; for ( i = 0 ; i < rank ; i ++ ) mat [ col ] [ i ] -= mult * mat [ row ] [ i ] ; } } } else { reduce = true ; for ( var i = row + 1 ; i < R ; i ++ ) { if ( mat [ i ] [ row ] != 0 ) { swap ( mat , row , i , rank ) ; reduce = false ; break ; } } if ( reduce ) { rank -- ; for ( i = 0 ; i < R ; i ++ ) mat [ i ] [ row ] = mat [ i ] [ rank ] ; } row -- ; } } return rank ; } function display ( mat , row , col ) { for ( i = 0 ; i < row ; i ++ ) { for ( j = 0 ; j < col ; j ++ ) document . write ( " " + mat [ i ] [ j ] ) ; document . write ( ' ' ) ; } } var mat = [ [ 10 , 20 , 10 ] , [ - 20 , - 30 , 10 ] , [ 30 , 50 , 0 ] ] ; document . write ( " " + rankOfMatrix ( mat ) ) ;
Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | javascript implementation of the approach ; A structure to represent a cell index ; Constructor ; A simple solution to find sums for a given array of cell indexes ; Iterate through all cell indexes ; Compute sum for current cell index ; Driver code
var R = 3 ; var C = 3 ; class Cell { constructor ( r , c ) { this . r = r ; this . c = c ; } } function printSums ( mat , arr , n ) { for ( i = 0 ; i < n ; i ++ ) { var sum = 0 , r = arr [ i ] . r , c = arr [ i ] . c ; for ( j = 0 ; j < R ; j ++ ) { for ( k = 0 ; k < C ; k ++ ) { if ( j != r && k != c ) { sum += mat [ j ] [ k ] ; } } } document . write ( sum + " " ) ; } } var mat = [ [ 1 , 1 , 2 ] , [ 3 , 4 , 6 ] , [ 5 , 3 , 2 ] ] ; var arr = [ new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) ] ; var n = arr . length ; printSums ( mat , arr , n ) ;
Count number of islands where every island is row | This function takes a matrix of ' X ' and ' O ' and returns the number of rectangular islands of ' X ' where no two islands are row - wise or column - wise adjacent , the islands may be diagonaly adjacent ; Initialize result ; Traverse the input matrix ; If current cell is ' X ' , then check whether this is top - leftmost of a rectangle . If yes , then increment count ; Driver Code
function countIslands ( mat , m , n ) { let count = 0 ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == ' ' ) { if ( ( i == 0 mat [ i - 1 ] [ j ] == ' ' ) && ( j == 0 mat [ i ] [ j - 1 ] == ' ' ) ) count ++ ; } } } return count ; } let m = 6 ; let n = 3 ; let mat = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; document . write ( " " + countIslands ( mat , m , n ) ) ;
Find a common element in all rows of a given row | Specify number of rows and columns ; Returns common element in all rows of mat [ M ] [ N ] . If there is no common element , then - 1 is returned ; An array to store indexes of current last column ; To store index of row whose current last element is minimum ; Initialize current last element of all rows ; Initialize min_row as first row ; Keep finding min_row in current last column , till either all elements of last column become same or we hit first column . ; Find minimum in current last column ; eq_count is count of elements equal to minimum in current last column . ; Traverse current last column elements again to update it ; Decrease last column index of a row whose value is more than minimum . ; Reduce last column index by 1 ; If equal count becomes M , return the value ; Driver Code
let M = 4 ; let N = 5 ; function findCommon ( mat ) { let column = new Array ( M ) ; let min_row ; let i ; for ( i = 0 ; i < M ; i ++ ) column [ i ] = N - 1 ; min_row = 0 ; while ( column [ min_row ] >= 0 ) { for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i ] [ column [ i ] ] < mat [ min_row ] [ column [ min_row ] ] ) min_row = i ; } let eq_count = 0 ; for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i ] [ column [ i ] ] > mat [ min_row ] [ column [ min_row ] ] ) { if ( column [ i ] == 0 ) return - 1 ; column [ i ] -= 1 ; } else eq_count ++ ; } if ( eq_count == M ) return mat [ min_row ] [ column [ min_row ] ] ; } return - 1 ; } let mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 2 , 4 , 5 , 8 , 10 ] , [ 3 , 5 , 7 , 9 , 11 ] , [ 1 , 3 , 5 , 7 , 9 ] ] ; let result = findCommon ( mat ) if ( result == - 1 ) { document . write ( " " ) ; } else { document . write ( " " , result ) ; }
Find a common element in all rows of a given row | Specify number of rows and columns ; Returns common element in all rows of mat [ M ] [ N ] . If there is no common element , then - 1 is returned ; A hash map to store count of elements ; Increment the count of first element of the row ; Starting from the second element of the current row ; If current element is different from the previous element i . e . it is appearing for the first time in the current row ; Find element having count equal to number of rows ; No such element found ; Driver Code
let M = 4 ; let N = 5 ; function findCommon ( mat ) { let cnt = new Map ( ) ; let i , j ; for ( i = 0 ; i < M ; i ++ ) { if ( cnt . has ( mat [ i ] [ 0 ] ) ) { cnt . set ( mat [ i ] [ 0 ] , cnt . get ( mat [ i ] [ 0 ] ) + 1 ) ; } else { cnt . set ( mat [ i ] [ 0 ] , 1 ) ; } for ( j = 1 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] != mat [ i ] [ j - 1 ] ) { if ( cnt . has ( mat [ i ] [ j ] ) ) { cnt . set ( mat [ i ] [ j ] , cnt . get ( mat [ i ] [ j ] ) + 1 ) ; } else { cnt . set ( mat [ i ] [ j ] , 1 ) ; } } } } for ( let [ key , value ] of cnt . entries ( ) ) { if ( value == M ) return key ; } return - 1 ; } let mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 2 , 4 , 5 , 8 , 10 ] , [ 3 , 5 , 7 , 9 , 11 ] , [ 1 , 3 , 5 , 7 , 9 ] , ] let result = findCommon ( mat ) ; if ( result == - 1 ) document . write ( " " ) ; else document . write ( " " + result ) ;
Given a matrix of ‘ O ’ and ‘ X ’ , replace ' O ' with ' X ' if surrounded by ' X ' | Size of given matrix is M X N ; A recursive function to replace previous value ' prevV ' at ' ( x , ▁ y ) ' and all surrounding values of ( x , y ) with new value ' newV ' . ; Base cases ; Replace the color at ( x , y ) ; Recur for north , east , south and west ; Returns size of maximum size subsquare matrix surrounded by ' X ' ; Step 1 : Replace all ' O ' with ' - ' ; Call floodFill for all ' - ' lying on edges Left side ; Right side ; Top side ; Bottom side ; Step 3 : Replace all ' - ' with ' X ' ; Driver Code
let M = 6 ; let N = 6 ; function floodFillUtil ( mat , x , y , prevV , newV ) { if ( x < 0 x >= M y < 0 y >= N ) return ; if ( mat [ x ] [ y ] != prevV ) return ; mat [ x ] [ y ] = newV ; floodFillUtil ( mat , x + 1 , y , prevV , newV ) ; floodFillUtil ( mat , x - 1 , y , prevV , newV ) ; floodFillUtil ( mat , x , y + 1 , prevV , newV ) ; floodFillUtil ( mat , x , y - 1 , prevV , newV ) ; } function replaceSurrounded ( mat ) { for ( let i = 0 ; i < M ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] == ' ' ) mat [ i ] [ j ] = ' ' ; for ( let i = 0 ; i < M ; i ++ ) if ( mat [ i ] [ 0 ] == ' ' ) floodFillUtil ( mat , i , 0 , ' ' , ' ' ) ; for ( let i = 0 ; i < M ; i ++ ) if ( mat [ i ] [ N - 1 ] == ' ' ) floodFillUtil ( mat , i , N - 1 , ' ' , ' ' ) ; for ( let i = 0 ; i < N ; i ++ ) if ( mat [ 0 ] [ i ] == ' ' ) floodFillUtil ( mat , 0 , i , ' ' , ' ' ) ; for ( let i = 0 ; i < N ; i ++ ) if ( mat [ M - 1 ] [ i ] == ' ' ) floodFillUtil ( mat , M - 1 , i , ' ' , ' ' ) ; for ( let i = 0 ; i < M ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] == ' ' ) mat [ i ] [ j ] = ' ' ; } let mat = [ [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ] ; replaceSurrounded ( mat ) ; for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) document . write ( mat [ i ] [ j ] + " " ) ; document . write ( " " ) ; }
Convert a given Binary Tree to Doubly Linked List | Set 2 | A tree node ; Standard Inorder traversal of tree ; Changes left pointers to work as previous pointers in converted DLL The function simply does inorder traversal of Binary Tree and updates left pointer using previously visited node ; Changes right pointers to work as next pointers in converted DLL ; Find the right most node in BT or last node in DLL ; Start from the rightmost node , traverse back using left pointers . While traversing , change right pointer of nodes ; The leftmost node is head of linked list , return it ; The main function that converts BST to DLL and returns head of DLL ; Set the previous pointer ; Set the next pointer and return head of DLL ; Traverses the DLL from left tor right ; Let us create the tree shown in above diagram
class node { constructor ( val ) { this . data = val ; this . left = null ; this . right = null ; } } var prev ; function inorder ( root ) { if ( root == null ) return ; inorder ( root . left ) ; document . write ( root . data + " " ) ; inorder ( root . right ) ; } function fixPrevptr ( root ) { if ( root == null ) return ; fixPrevptr ( root . left ) ; root . left = prev ; prev = root ; fixPrevptr ( root . right ) ; } function fixNextptr ( root ) { while ( root . right != null ) root = root . right ; while ( root != null && root . left != null ) { var left = root . left ; left . right = root ; root = root . left ; } return root ; } function BTTtoDLL ( root ) { prev = null ; fixPrevptr ( root ) ; return fixNextptr ( root ) ; } function printlist ( root ) { while ( root != null ) { document . write ( root . data + " " ) ; root = root . right ; } } var root = new node ( 10 ) ; root . left = new node ( 12 ) ; root . right = new node ( 15 ) ; root . left . left = new node ( 25 ) ; root . left . right = new node ( 30 ) ; root . right . left = new node ( 36 ) ; document . write ( " " ) ; inorder ( root ) ; var head = BTTtoDLL ( root ) ; document . write ( " " ) ; printlist ( head ) ;
Convert a given Binary Tree to Doubly Linked List | Set 3 | A binary tree node has data , left pointers and right pointers ; head -- > Pointer to head node of created doubly linked list ; A simple recursive function to convert a given Binary tree to Doubly Linked List root -- > Root of Binary Tree ; Base case ; Initialize previously visited node as NULL . This is so that the same value is accessible in all recursive calls ; Recursively convert left subtree ; Now convert this node ; Finally convert right subtree ; Function to print nodes in a given doubly linked list ; Let us create the tree as shown in above diagram ; convert to DLL ; Print the converted List
class Node { constructor ( val ) { this . data = val ; this . left = null ; this . right = null ; } } var root ; var head ; function BinaryTree2DoubleLinkedList ( root ) { if ( root == null ) return ; var prev = null ; BinaryTree2DoubleLinkedList ( root . left ) ; if ( prev == null ) head = root ; else { root . left = prev ; prev . right = root ; } prev = root ; BinaryTree2DoubleLinkedList ( root . right ) ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . right ; } } root = new Node ( 10 ) ; root . left = new Node ( 12 ) ; root . right = new Node ( 15 ) ; root . left . left = new Node ( 25 ) ; root . left . right = new Node ( 30 ) ; root . right . left = new Node ( 36 ) ; BinaryTree2DoubleLinkedList ( root ) ; printList ( head ) ;
Collect maximum points in a grid using two traversals | A Memoization based program to find maximum collection using two traversals of a grid ; checks whether a given input is valid or not ; Driver function to collect Math . max value ; if P1 or P2 is at an invalid cell ; if both traversals reach their destinations ; If both traversals are at last row but not at their destination ; If subproblem is already solved ; Initialize answer for this subproblem ; this variable is used to store gain of current cell ( s ) ; Recur for all possible cases , then store and return the one with max value ; This is mainly a wrapper over recursive function getMaxUtil ( ) . This function creates a table for memoization and calls getMaxUtil ( ) ; Create a memoization table and initialize all entries as - 1 ; Calculation maximum value using memoization based function getMaxUtil ( ) ; Driver code
var R = 5 ; var C = 4 ; function isValid ( x , y1 , y2 ) { return ( x >= 0 && x < R && y1 >= 0 && y1 < C && y2 >= 0 && y2 < C ) ; } function getMaxUtil ( arr , mem , x , y1 , y2 ) { if ( ! isValid ( x , y1 , y2 ) ) return Number . MIN_VALUE ; if ( x == R - 1 && y1 == 0 && y2 == C - 1 ) return ( y1 == y2 ) ? arr [ x ] [ y1 ] : arr [ x ] [ y1 ] + arr [ x ] [ y2 ] ; if ( x == R - 1 ) return Number . MIN_VALUE ; if ( mem [ x ] [ y1 ] [ y2 ] != - 1 ) return mem [ x ] [ y1 ] [ y2 ] ; var ans = Number . MIN_VALUE ; var temp = ( y1 == y2 ) ? arr [ x ] [ y1 ] : arr [ x ] [ y1 ] + arr [ x ] [ y2 ] ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 - 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 + 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 - 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 + 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 - 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 + 1 ) ) ; return ( mem [ x ] [ y1 ] [ y2 ] = ans ) ; } function geMaxCollection ( arr ) { var mem = Array ( R ) . fill ( ) . map ( ( ) => Array ( C ) . fill ( ) . map ( ( ) => Array ( C ) . fill ( 0 ) ) ) ; for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { for ( l = 0 ; l < C ; l ++ ) mem [ i ] [ j ] [ l ] = - 1 ; } } return getMaxUtil ( arr , mem , 0 , 0 , C - 1 ) ; } var arr = [ [ 3 , 6 , 8 , 2 ] , [ 5 , 2 , 4 , 3 ] , [ 1 , 1 , 20 , 10 ] , [ 1 , 1 , 20 , 10 ] , [ 1 , 1 , 20 , 10 ] , ] ; document . write ( " " + geMaxCollection ( arr ) ) ;
Find length of the longest consecutive path from a given starting character | tool matrices to recur for adjacent cells . ; dp [ i ] [ j ] Stores length of longest consecutive path starting at arr [ i ] [ j ] . ; check whether mat [ i ] [ j ] is a valid cell or not . ; Check whether current character is adjacent to previous character ( character processed in parent call ) or not . ; i , j are the indices of the current cell and prev is the character processed in the parent call . . also mat [ i ] [ j ] is our current character . ; If this cell is not valid or current character is not adjacent to previous one ( e . g . d is not adjacent to b ) or if this cell is already included in the path than return 0. ; If this subproblem is already solved , return the answer ; Initialize answer ; recur for paths with different adjacent cells and store the length of longest path . ; save the answer and return ; Returns length of the longest path with all characters consecutive to each other . This function first initializes dp array that is used to store results of subproblems , then it calls recursive DFS based function getLenUtil ( ) to find max length path ; check for each possible starting point ; recur for all eight adjacent cells ; Driver program
let x = [ 0 , 1 , 1 , - 1 , 1 , 0 , - 1 , - 1 ] ; let y = [ 1 , 0 , 1 , 1 , - 1 , - 1 , 0 , - 1 ] ; let R = 3 ; let C = 3 ; let dp = new Array ( R ) ; for ( let i = 0 ; i < R ; i ++ ) { dp [ i ] = new Array ( C ) ; for ( let j = 0 ; j < C ; j ++ ) { dp [ i ] [ j ] = 0 ; } } function isvalid ( i , j ) { if ( i < 0 j < 0 i >= R j >= C ) return false ; return true ; } function isadjacent ( prev , curr ) { return ( ( curr . charCodeAt ( ) - prev . charCodeAt ( ) ) == 1 ) ; } function getLenUtil ( mat , i , j , prev ) { if ( ! isvalid ( i , j ) || ! isadjacent ( prev , mat [ i ] [ j ] ) ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; let ans = 0 ; for ( let k = 0 ; k < 8 ; k ++ ) ans = Math . max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , mat [ i ] [ j ] ) ) ; return dp [ i ] [ j ] = ans ; } function getLen ( mat , s ) { for ( let i = 0 ; i < R ; ++ i ) for ( let j = 0 ; j < C ; ++ j ) dp [ i ] [ j ] = - 1 ; let ans = 0 ; for ( let i = 0 ; i < R ; i ++ ) { for ( let j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == s ) { for ( let k = 0 ; k < 8 ; k ++ ) ans = Math . max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , s ) ) ; } } } return ans ; } let mat = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; document . write ( getLen ( mat , ' ' ) + " " ) ; document . write ( getLen ( mat , ' ' ) + " " ) ; document . write ( getLen ( mat , ' ' ) + " " ) ; document . write ( getLen ( mat , ' ' ) ) ;