codeparrot/xlcost-text-to-code huggingface dataset:
Binary Search Tree insert with Parent Pointer | Node structure ; A utility function to create a new BST Node ; A utility function to do inorder traversal of BST ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; Set parent of root of left subtree ; Set parent of root of right subtree ; return the ( unchanged ) Node pointer ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 ; prvar iNoder traversal of the BST
class Node { constructor ( ) { this . key = 0 ; this . left = null ; this . right = null ; this . parent = null ; } } function newNode ( item ) { var temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; temp . parent = null ; return temp ; } function inorder ( root ) { if ( root != null ) { inorder ( root . left ) ; document . write ( " " + root . key + " " ) ; if ( root . parent == null ) document . write ( " " ) ; else document . write ( " " + root . parent . key + " " ) ; inorder ( root . right ) ; } } function insert ( node , key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) { var lchild = insert ( node . left , key ) ; node . left = lchild ; lchild . parent = node ; } else if ( key > node . key ) { var rchild = insert ( node . right , key ) ; node . right = rchild ; rchild . parent = node ; } return node ; } var root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; inorder ( root ) ;
Minimum Possible value of | ai + aj | function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is Math . abs value is smaller than smallest update smallest and reset count to 1 ; if Math . abs value is equal to smallest increment count value ; print result ; driver program
function pairs ( arr , n , k ) { var smallest = 1000000000 ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) { if ( Math . abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = Math . abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( Math . abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } document . write ( " " + smallest + " " ) ; document . write ( " " + count + " " ) ; } var arr = [ 3 , 5 , 7 , 5 , 1 , 9 , 9 ] ; var k = 12 ; var n = arr . length ; pairs ( arr , n , k ) ;
Rank of an element in a stream | Driver code
var a = [ 5 , 1 , 14 , 4 , 15 , 9 , 7 , 20 , 11 ] ; var key = 20 ; var arraySize = a . length ; var count = 0 ; for ( i = 0 ; i < arraySize ; i ++ ) { if ( a [ i ] <= key ) { count += 1 ; } } document . write ( " " + key + " " + ( count - 1 ) ) ;
Special two digit numbers in a Binary Search Tree | A binary tree node ; Function to create a new node ; If the tree is empty , return a new , single node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Function to find if number is special or not ; Check if number is two digit or not ; Function to count number of special two digit number ; Driver code ; Function call
class Node { constructor ( d ) { this . info = d ; this . left = this . right = null ; } } let head ; let count = 0 ; function insert ( node , info ) { if ( node == null ) { return ( new Node ( info ) ) ; } else { if ( info <= node . info ) { node . left = insert ( node . left , info ) ; } else { node . right = insert ( node . right , info ) ; } return node ; } } function check ( num ) { let sum = 0 , i = num , sum_of_digits , prod_of_digits ; if ( num < 10 num > 99 ) return 0 ; else { sum_of_digits = ( i % 10 ) + Math . floor ( i / 10 ) ; prod_of_digits = ( i % 10 ) * Math . floor ( i / 10 ) ; sum = sum_of_digits + prod_of_digits ; } if ( sum == num ) return 1 ; else return 0 ; } function countSpecialDigit ( rt ) { let x ; if ( rt == null ) return ; else { x = check ( rt . info ) ; if ( x == 1 ) count = count + 1 ; countSpecialDigit ( rt . left ) ; countSpecialDigit ( rt . right ) ; } } let root = null ; root = insert ( root , 50 ) ; root = insert ( root , 29 ) ; root = insert ( root , 59 ) ; root = insert ( root , 19 ) ; root = insert ( root , 53 ) ; root = insert ( root , 556 ) ; root = insert ( root , 56 ) ; root = insert ( root , 94 ) ; root = insert ( root , 13 ) ; countSpecialDigit ( root ) ; document . write ( count ) ;
Sum of middle row and column in Matrix | Javascript program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver function
var MAX = 100 ; function middlesum ( mat , n ) { var row_sum = 0 , col_sum = 0 ; for ( i = 0 ; i < n ; i ++ ) row_sum += mat [ parseInt ( n / 2 ) ] [ i ] ; document . write ( " " + row_sum + " " ) ; for ( i = 0 ; i < n ; i ++ ) col_sum += mat [ i ] [ parseInt ( n / 2 ) ] ; document . write ( " " + col_sum ) ; } var mat = [ [ 2 , 5 , 7 ] , [ 3 , 7 , 2 ] , [ 5 , 6 , 9 ] ] ; middlesum ( mat , 3 ) ;
Rotate the matrix right by K times | size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver code ; rotate matrix by k ; display rotated matrix
var M = 3 ; var N = 3 ; function rotateMatrix ( matrix , k ) { var temp = Array ( M ) . fill ( 0 ) ; k = k % M ; for ( i = 0 ; i < N ; i ++ ) { for ( t = 0 ; t < M - k ; t ++ ) temp [ t ] = matrix [ i ] [ t ] ; for ( j = M - k ; j < M ; j ++ ) matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] ; for ( j = k ; j < M ; j ++ ) matrix [ i ] [ j ] = temp [ j - k ] ; } } function displayMatrix ( matrix ) { for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < M ; j ++ ) document . write ( matrix [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var matrix = [ [ 12 , 23 , 34 ] , [ 45 , 56 , 67 ] , [ 78 , 89 , 91 ] ] ; var k = 2 ; rotateMatrix ( matrix , k ) ; displayMatrix ( matrix ) ;
Interchange elements of first and last rows in matrix | Javascript code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; input in the array ; printing the interchanged matrix
function interchangeFirstLast ( m ) { let rows = m . length ; for ( let i = 0 ; i < m [ 0 ] . length ; i ++ ) { let t = m [ 0 ] [ i ] ; m [ 0 ] [ i ] = m [ rows - 1 ] [ i ] ; m [ rows - 1 ] [ i ] = t ; } } let m = [ [ 8 , 9 , 7 , 6 ] , [ 4 , 7 , 6 , 5 ] , [ 3 , 2 , 1 , 8 ] , [ 9 , 9 , 7 , 7 ] ] interchangeFirstLast ( m ) ; for ( let i = 0 ; i < m . length ; i ++ ) { for ( let j = 0 ; j < m [ 0 ] . length ; j ++ ) document . write ( m [ i ] [ j ] + " " ) ; document . write ( " " ) ; }
Row wise sorting in 2D array | JavaScript Program to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver code
function sortRowWise ( m ) { for ( let i = 0 ; i < m . length ; i ++ ) { for ( let j = 0 ; j < m [ i ] . length ; j ++ ) { for ( let k = 0 ; k < m [ i ] . length - j - 1 ; k ++ ) { if ( m [ i ] [ k ] > m [ i ] [ k + 1 ] ) { let t = m [ i ] [ k ] ; m [ i ] [ k ] = m [ i ] [ k + 1 ] ; m [ i ] [ k + 1 ] = t ; } } } } for ( let i = 0 ; i < m . length ; i ++ ) { for ( let j = 0 ; j < m [ i ] . length ; j ++ ) document . write ( m [ i ] [ j ] + " " ) ; document . write ( " " ) ; } return 0 ; } let m = [ [ 9 , 8 , 7 , 1 ] , [ 7 , 3 , 0 , 2 ] , [ 9 , 5 , 3 , 2 ] , [ 6 , 3 , 1 , 2 ] ] ; sortRowWise ( m ) ;
Program for Markov matrix | Javascript code to check Markov Matrix ; outer loop to access rows and inner to access columns ; Find sum of current row ; Matrix to check ; calls the function check ( )
let n = 3 function checkMarkov ( m ) { for ( let i = 0 ; i < n ; i ++ ) { let sum = 0 ; for ( let j = 0 ; j < n ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; } let m = [ [ 0 , 0 , 1 ] , [ 0.5 , 0 , 0.5 ] , [ 1 , 0 , 0 ] ] ; if ( checkMarkov ( m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to check diagonal matrix and scalar matrix | JavaScript program to check matrix is diagonal matrix or not . ; Function to check matrix is diagonal matrix or not . ; condition to check other elements except main diagonal are zero or not . ; Driver code
let N = 4 ; function isDiagonalMatrix ( mat ) { for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; return true ; } let mat = [ [ 4 , 0 , 0 , 0 ] , [ 0 , 7 , 0 , 0 ] , [ 0 , 0 , 5 , 0 ] , [ 0 , 0 , 0 , 1 ] ] ; if ( isDiagonalMatrix ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to check diagonal matrix and scalar matrix | Javascript Program to check matrix is scalar matrix or not . ; Function to check matrix is scalar matrix or not . ; Check all elements except main diagonal are zero or not . ; Check all diagonal elements are same or not . ; Driver Code ; Function call
let N = 4 ; function isScalarMatrix ( mat ) { for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( let i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; } let mat = [ [ 2 , 0 , 0 , 0 ] , [ 0 , 2 , 0 , 0 ] , [ 0 , 0 , 2 , 0 ] , [ 0 , 0 , 0 , 2 ] ] ; if ( isScalarMatrix ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Construct a Binary Tree from Postorder and Inorder | A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to construct binary of size n from Inorder traversal in [ ] and Postrder traversal post [ ] . Initial values of inStrt and inEnd should be 0 and n - 1. The function doesn 't do any error  checking for cases where inorder and postorder  do not form a tree  ; Base case ; Pick current node from Postrder traversal using postIndex and decrement postIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of value in arr [ start ... end ] The function assumes that value is postsent in in [ ] ; This funtcion is here just to test ; Driver Code
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function buildUtil ( In , post , inStrt , inEnd , postStrt , postEnd ) { if ( inStrt > inEnd ) return null ; let node = new Node ( post [ postEnd ] ) ; if ( inStrt == inEnd ) return node ; let iIndex = search ( In , inStrt , inEnd , node . data ) ; node . left = buildUtil ( In , post , inStrt , iIndex - 1 , postStrt , postStrt - inStrt + iIndex - 1 ) ; node . right = buildUtil ( In , post , iIndex + 1 , inEnd , postEnd - inEnd + iIndex , postEnd - 1 ) ; return node ; } function search ( arr , strt , end , value ) { let i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) break ; } return i ; } function preOrder ( node ) { if ( node == null ) return ; document . write ( node . data + " " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } let In = [ 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 ] ; let post = [ 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ] ; let n = In . length ; let root = buildUtil ( In , post , 0 , n - 1 , 0 , n - 1 ) ; document . write ( " " ) ; preOrder ( root ) ;
Check given matrix is magic square or not | Javascript program to check whether a given matrix is magic matrix or not ; Returns true if mat [ ] [ ] is magic square , else returns false . ; calculate the sum of the prime diagonal ; the secondary diagonal ; For sums of Rows ; check if every row sum is equal to prime diagonal sum ; For sums of Columns ; check if every column sum is equal to prime diagonal sum ; driver program to test above function
var N = 3 function isMagicSquare ( mat ) { var sum = 0 , sum2 = 0 ; for ( var i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( var i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( var i = 0 ; i < N ; i ++ ) { var rowSum = 0 ; for ( var j = 0 ; j < N ; j ++ ) rowSum += mat [ i ] [ j ] ; if ( rowSum != sum ) return false ; } for ( var i = 0 ; i < N ; i ++ ) { var colSum = 0 ; for ( var j = 0 ; j < N ; j ++ ) colSum += mat [ j ] [ i ] ; if ( sum != colSum ) return false ; } return true ; } var mat = [ [ 2 , 7 , 6 ] , [ 9 , 5 , 1 ] , [ 4 , 3 , 8 ] ] ; if ( isMagicSquare ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count sub | Javascript implementation to count sub - matrices having sum divisible by the value ' k ' ; function to count all sub - arrays divisible by k ; create auxiliary hash array to count frequency of remainders ; Traverse original array and compute cumulative sum take remainder of this current cumulative sum and increase count by 1 for this remainder in mod [ ] array ; as the sum can be negative , taking modulo twice ; Initialize result ; Traverse mod [ ] ; If there are more than one prefix subarrays with a particular mod value . ; add the subarrays starting from the arr [ i ] which are divisible by k itself ; function to count all sub - matrices having sum divisible by the value ' k ' ; Variable to store the final output ; Set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' ; Count number of subarrays in temp [ ] having sum divisible by ' k ' and then add it to ' tot _ count ' ; required count of sub - matrices having sum divisible by ' k ' ; Driver program to test above
var SIZE = 10 ; function subCount ( arr , n , k ) { var mod = Array ( k ) . fill ( 0 ) ; var cumSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { cumSum += arr [ i ] ; mod [ ( ( cumSum % k ) + k ) % k ] ++ ; } var result = 0 ; for ( var i = 0 ; i < k ; i ++ ) if ( mod [ i ] > 1 ) result += ( mod [ i ] * ( mod [ i ] - 1 ) ) / 2 ; result += mod [ 0 ] ; return result ; } function countSubmatrix ( mat , n , k ) { var tot_count = 0 ; var left , right , i ; var temp = Array ( n ) ; for ( left = 0 ; left < n ; left ++ ) { temp = Array ( n ) . fill ( 0 ) ; for ( right = left ; right < n ; right ++ ) { for ( i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i ] [ right ] ; tot_count += subCount ( temp , n , k ) ; } } return tot_count ; } var mat = [ [ 5 , - 1 , 6 ] , [ - 2 , 3 , 8 ] , [ 7 , 4 , - 9 ] ] ; var n = 3 , k = 4 ; document . write ( " " + countSubmatrix ( mat , n , k ) ) ;
Minimum operations required to make each row and column of matrix equals | Function to find minimum operation required to make sum of each row and column equals ; Initialize the sumRow [ ] and sumCol [ ] array to 0 ; Calculate sumRow [ ] and sumCol [ ] array ; Find maximum sum value in either row or in column ; Find minimum increment required in either row or column ; Add difference in corresponding cell , sumRow [ ] and sumCol [ ] array ; Update the count variable ; If ith row satisfied , increment ith value for next iteration ; If jth column satisfied , increment jth value for next iteration ; Utility function to print matrix ; Driver program
function findMinOpeartion ( matrix , n ) { let sumRow = new Array ( n ) ; let sumCol = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { sumRow [ i ] = 0 ; sumCol [ i ] = 0 ; } for ( let i = 0 ; i < n ; ++ i ) for ( let j = 0 ; j < n ; ++ j ) { sumRow [ i ] += matrix [ i ] [ j ] ; sumCol [ j ] += matrix [ i ] [ j ] ; } let maxSum = 0 ; for ( let i = 0 ; i < n ; ++ i ) { maxSum = Math . max ( maxSum , sumRow [ i ] ) ; maxSum = Math . max ( maxSum , sumCol [ i ] ) ; } let count = 0 ; for ( let i = 0 , j = 0 ; i < n && j < n ; ) { let diff = Math . min ( maxSum - sumRow [ i ] , maxSum - sumCol [ j ] ) ; matrix [ i ] [ j ] += diff ; sumRow [ i ] += diff ; sumCol [ j ] += diff ; count += diff ; if ( sumRow [ i ] == maxSum ) ++ i ; if ( sumCol [ j ] == maxSum ) ++ j ; } return count ; } function printMatrix ( matrix , n ) { for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < n ; ++ j ) document . write ( matrix [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let matrix = [ [ 1 , 2 ] , [ 3 , 4 ] ] ; document . write ( findMinOpeartion ( matrix , 2 ) + " " ) ; printMatrix ( matrix , 2 ) ;
Count frequency of k in a matrix of size n where matrix ( i , j ) = i + j | Javascript program to find the frequency of k in matrix where m ( i , j ) = i + j ; Driver Code
function find ( n , k ) { if ( n + 1 >= k ) return ( k - 1 ) ; else return ( 2 * n + 1 - k ) ; } var n = 4 , k = 7 ; var freq = find ( n , k ) ; if ( freq < 0 ) document . write ( " " ) ; else document . write ( " " + k + " " + freq + " " ) ;
Given 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . . . k ' s print them in zig zag way . | function that prints given number of 1 ' s , ▁ ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; two - dimensional array to store numbers . ; for even row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index ; for odd row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index . ; printing the stored elements . ; Driver Code
function ZigZag ( rows , columns , numbers ) { let k = 0 ; let arr = new Array ( rows ) ; for ( let i = 0 ; i < rows ; i ++ ) { arr [ i ] = new Array ( rows ) ; for ( let j = 0 ; j < columns ; j ++ ) { arr [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < rows ; i ++ ) { if ( i % 2 == 0 ) { for ( let j = 0 ; j < columns && numbers [ k ] > 0 ; j ++ ) { arr [ i ] [ j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } else { for ( let j = columns - 1 ; j >= 0 && numbers [ k ] > 0 ; j -- ) { arr [ i ] [ j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } } for ( let i = 0 ; i < rows ; i ++ ) { for ( let j = 0 ; j < columns ; j ++ ) document . write ( arr [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let rows = 4 ; let columns = 5 ; let Numbers = [ 3 , 4 , 2 , 2 , 3 , 1 , 5 ] ; ZigZag ( rows , columns , Numbers ) ;
Number of cells a queen can move with obstacles on the chessborad | Return the number of position a Queen can move . ; d11 , d12 , d21 , d22 are for diagnoal distances . r1 , r2 are for vertical distance . c1 , c2 are for horizontal distance . ; Initialise the distance to end of the board . ; For each obstacle find the minimum distance . If obstacle is present in any direction , distance will be updated . ; Chessboard size ; number of obstacles ; Queen x position ; Queen y position ; x position of obstacles ; y position of obstacles
function numberofPosition ( n , k , x , y , obstPosx , obstPosy ) { let d11 , d12 , d21 , d22 , r1 , r2 , c1 , c2 ; d11 = Math . min ( x - 1 , y - 1 ) ; d12 = Math . min ( n - x , n - y ) ; d21 = Math . min ( n - x , y - 1 ) ; d22 = Math . min ( x - 1 , n - y ) ; r1 = y - 1 ; r2 = n - y ; c1 = x - 1 ; c2 = n - x ; for ( let i = 0 ; i < k ; i ++ ) { if ( x > obstPosx [ i ] && y > obstPosy [ i ] && x - obstPosx [ i ] == y - obstPosy [ i ] ) d11 = Math . min ( d11 , x - obstPosx [ i ] - 1 ) ; if ( obstPosx [ i ] > x && obstPosy [ i ] > y && obstPosx [ i ] - x == obstPosy [ i ] - y ) d12 = Math . min ( d12 , obstPosx [ i ] - x - 1 ) ; if ( obstPosx [ i ] > x && y > obstPosy [ i ] && obstPosx [ i ] - x == y - obstPosy [ i ] ) d21 = Math . min ( d21 , obstPosx [ i ] - x - 1 ) ; if ( x > obstPosx [ i ] && obstPosy [ i ] > y && x - obstPosx [ i ] == obstPosy [ i ] - y ) d22 = Math . min ( d22 , x - obstPosx [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] < y ) r1 = min ( r1 , y - obstPosy [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] > y ) r2 = Math . min ( r2 , obstPosy [ i ] - y - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] < x ) c1 = Math . min ( c1 , x - obstPosx [ i ] - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] > x ) c2 = Math . min ( c2 , obstPosx [ i ] - x - 1 ) ; } return d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2 ; } let n = 8 ; let k = 1 ; let Qposx = 4 ; let Qposy = 4 ; let obstPosx = [ 3 ] ; let obstPosy = [ 5 ] ; document . write ( numberofPosition ( n , k , Qposx , Qposy , obstPosx , obstPosy ) ) ;
Maximum product of 4 adjacent elements in matrix | Javascript program to find out the maximum product in the matrix which four elements are adjacent to each other in one direction ; function to find max product ; iterate the rows . ; iterate the columns . ; check the maximum product in horizontal row . ; check the maximum product in vertical row . ; check the maximum product in diagonal ( going through down - right ) ; check the maximum product in diagonal ( going through up - right ) ; Driver code
let n = 5 ; function FindMaxProduct ( arr , n ) { let max = 0 , result ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i ] [ j - 1 ] * arr [ i ] [ j - 2 ] * arr [ i ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j ] * arr [ i - 2 ] [ j ] * arr [ i - 3 ] [ j ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j - 1 ] * arr [ i - 2 ] [ j - 2 ] * arr [ i - 3 ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 1 ) <= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j + 1 ] * arr [ i - 2 ] [ j + 2 ] * arr [ i - 3 ] [ j + 3 ] ; if ( max < result ) max = result ; } } } return max ; } let arr = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 , 9 , 1 ] , [ 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 1 , 0 ] , [ 9 , 6 , 4 , 2 , 3 ] ] ; document . write ( FindMaxProduct ( arr , n ) ) ;
Maximum product of 4 adjacent elements in matrix | Javascript implementation of the above approach ; Window Product for each row . ; Maximum window product for each row ; Global maximum window product ; Driver Code ; Function call
function maxPro ( a , n , m , k ) { let maxi = 1 , mp = 1 ; for ( let i = 0 ; i < n ; ++ i ) { let wp = 1 ; for ( let l = 0 ; l < k ; ++ l ) { wp *= a [ i ] [ l ] ; } mp = wp ; for ( let j = k ; j < m ; ++ j ) { wp = wp * a [ i ] [ j ] / a [ i ] [ j - k ] ; maxi = Math . max ( maxi , Math . max ( mp , wp ) ) ; } } return maxi ; } let n = 6 , m = 5 , k = 4 ; let a = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 , 9 , 1 ] , [ 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 1 , 0 ] , [ 9 , 6 , 4 , 2 , 3 ] , [ 1 , 1 , 2 , 1 , 1 ] ] let maxpro = maxPro ( a , n , m , k ) ; document . write ( maxpro ) ;
Minimum flip required to make Binary Matrix symmetric |  ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; finding the transpose of the matrix ; Finding the number of position where element are not same . ; Driver code
/ *JavaScript Program to find minimum flip required to make Binary Matrix symmetric along main diagonal function minimumflip ( mat , n ) { let transpose = new Array ( n ) ; for ( var i = 0 ; i < transpose . length ; i ++ ) { transpose [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) transpose [ i ] [ j ] = mat [ j ] [ i ] ; let flip = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( transpose [ i ] [ j ] != mat [ i ] [ j ] ) flip ++ ; return flip / 2 ; } let n = 3 ; let mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] ] ; document . write ( minimumflip ( mat , n ) ) ;
Minimum flip required to make Binary Matrix symmetric | Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; Comparing elements across diagonal ; Driver Code
function minimumflip ( mat , n ) { let flip = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; } let n = 3 ; let mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] ] ; document . write ( minimumflip ( mat , n ) ) ;