codeparrot/xlcost-text-to-code huggingface dataset:
Make largest palindrome by changing at most K | Returns maximum possible palindrome using k changes ; Initialize l and r by leftmost and rightmost ends ; First try to make String palindrome ; Replace left and right character by maximum of both ; If k is negative then we can 't make  String palindrome ; At mid character , if K > 0 then change it to 9 ; If character at lth ( same as rth ) is less than 9 ; If none of them is changed in the previous loop then subtract 2 from K and convert both to 9 ; If one of them is changed in the previous loop then subtract 1 from K ( 1 more is subtracted already ) and make them 9 ; Driver code to test above methods
function maximumPalinUsingKChanges ( str , k ) { let palin = str . split ( " " ) ; let ans = " " ; let l = 0 ; let r = str . length - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { palin [ l ] = palin [ r ] = String . fromCharCode ( Math . max ( str . charAt ( l ) , str . charAt ( r ) ) ) ; k -- ; } l ++ ; r -- ; } if ( k < 0 ) { return " " ; } l = 0 ; r = str . length - 1 ; while ( l <= r ) { if ( l == r ) { if ( k > 0 ) { palin [ l ] = ' ' ; } } if ( palin [ l ] < ' ' ) { if ( k >= 2 && palin [ l ] == str [ l ] && palin [ r ] == str [ r ] ) { k -= 2 ; palin [ l ] = palin [ r ] = ' ' ; } else if ( k >= 1 && ( palin [ l ] != str [ l ] palin [ r ] != str [ r ] ) ) { k -- ; palin [ l ] = palin [ r ] = ' ' ; } } l ++ ; r -- ; } for ( let i = 0 ; i < palin . length ; i ++ ) ans += palin [ i ] ; return ans ; } let str = " " ; let k = 3 ; document . write ( maximumPalinUsingKChanges ( str , k ) ) ;
Minimize the count of adjacent pairs with different parity | Javascript implementation of above approach ; Recursive function to calculate minimum adjacent pairs with different parity ; If all the numbers are placed ; If replacement is not required ; If replacement is required ; backtracking ; backtracking ; Function to display the minimum number of adjacent elements with different parity ; Store no of even numbers not present in the array ; Store no of odd numbers not present in the array ; Erase exisiting numbers ; Store non - exisiting even and odd numbers ; Driver code
var min = 10000 ; function parity ( even , odd , v , i ) { if ( i == v . length even . length == 0 && odd . length == 0 ) { var count = 0 ; for ( var j = 0 ; j < v . length - 1 ; j ++ ) { if ( v [ j ] % 2 != v [ j + 1 ] % 2 ) count ++ ; } if ( count < min ) min = count ; return min ; } if ( v [ i ] != - 1 ) min = parity ( even , odd , v , i + 1 ) ; else { if ( even . length != 0 ) { var x = even . back ( ) ; even . pop ( ) ; v [ i ] = x ; min = parity ( even , odd , v , i + 1 ) ; even . push ( x ) ; } if ( odd . length != 0 ) { var x = odd [ odd . length - 1 ] ; odd . pop ( ) ; v [ i ] = x ; min = parity ( even , odd , v , i + 1 ) ; odd . push ( x ) ; } } return min ; } function minDiffParity ( v , n ) { var even = [ ] ; var odd = [ ] ; var m = new Map ( ) ; for ( var i = 1 ; i <= n ; i ++ ) m . set ( i , 1 ) ; for ( var i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] != - 1 ) m . delete ( v [ i ] ) ; } m . forEach ( ( value , key ) => { if ( i . first % 2 == 0 ) even . push ( key ) ; else odd . push ( key ) ; } ) ; min = parity ( even , odd , v , 0 ) ; document . write ( min ) ; } var n = 8 ; var v = [ 2 , 1 , 4 , - 1 , - 1 , 6 , - 1 , 8 ] ; minDiffParity ( v , n ) ;
Find triplet such that number of nodes connecting these triplets is maximum | JavaScript implementation of the approach ; To store the required nodes ; Parent array to retrace the nodes ; Visited array to prevent DFS in direction on Diameter path ; DFS function to find the startnode ; DFS function to find the endnode of diameter and maintain the parent array ; DFS function to find the end node of the Longest Branch to Diameter ; Function to find the required nodes ; To find start node of diameter ; To find end node of diameter ; x is the end node of diameter ; Mark all the nodes on diameter using back tracking ; Find the end node of longest branch to diameter ; Driver code
let MAX = 100005 ; let adjacent = [ ] ; let visited = new Array ( MAX ) ; let startnode , endnode , thirdnode ; let maxi = - 1 , N ; let parent = new Array ( MAX ) ; let vis = new Array ( MAX ) ; function dfs ( u , count ) { visited [ u ] = true ; let temp = 0 ; for ( let i = 0 ; i < adjacent [ u ] . length ; i ++ ) { if ( ! visited [ adjacent [ u ] [ i ] ] ) { temp ++ ; dfs ( adjacent [ u ] [ i ] , count + 1 ) ; } } if ( temp == 0 ) { if ( maxi < count ) { maxi = count ; startnode = u ; } } } function dfs1 ( u , count ) { visited [ u ] = true ; let temp = 0 ; for ( let i = 0 ; i < adjacent [ u ] . length ; i ++ ) { if ( ! visited [ adjacent [ u ] [ i ] ] ) { temp ++ ; parent [ adjacent [ u ] [ i ] ] = u ; dfs1 ( adjacent [ u ] [ i ] , count + 1 ) ; } } if ( temp == 0 ) { if ( maxi < count ) { maxi = count ; endnode = u ; } } } function dfs2 ( u , count ) { visited [ u ] = true ; let temp = 0 ; for ( let i = 0 ; i < adjacent [ u ] . length ; i ++ ) { if ( ! visited [ adjacent [ u ] [ i ] ] && ! vis [ adjacent [ u ] [ i ] ] ) { temp ++ ; dfs2 ( adjacent [ u ] [ i ] , count + 1 ) ; } } if ( temp == 0 ) { if ( maxi < count ) { maxi = count ; thirdnode = u ; } } } function findNodes ( ) { dfs ( 1 , 0 ) ; for ( let i = 0 ; i <= N ; i ++ ) visited [ i ] = false ; maxi = - 1 ; dfs1 ( startnode , 0 ) ; for ( let i = 0 ; i <= N ; i ++ ) visited [ i ] = false ; let x = endnode ; vis [ startnode ] = true ; while ( x != startnode ) { vis [ x ] = true ; x = parent [ x ] ; } maxi = - 1 ; for ( let i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] ) dfs2 ( i , 0 ) ; } } for ( let i = 0 ; i < MAX ; i ++ ) adjacent . push ( [ ] ) ; N = 4 ; adjacent [ 1 ] . push ( 2 ) ; adjacent [ 2 ] . push ( 1 ) ; adjacent [ 1 ] . push ( 3 ) ; adjacent [ 3 ] . push ( 1 ) ; adjacent [ 1 ] . push ( 4 ) ; adjacent [ 4 ] . push ( 1 ) ; findNodes ( ) ; document . write ( " " + startnode + " " + endnode + " " + thirdnode + " " ) ;
Percentage increase in volume of the sphere if radius is increased by a given percentage | javascript program to find percentage increase in the volume of the sphere if radius is increased by a given percentage ; Driver code
function newvol ( x ) { document . write ( " " + " " + ( Math . pow ( x , 3 ) / 10000 + 3 * x + ( 3 * Math . pow ( x , 2 ) ) / 100 ) + " " ) ; } var x = 10 ; newvol ( x ) ;
Length of the chord of the circle whose radius and the angle subtended at the center by the chord is given | Function to find the length of the chord ; Driver code
function length_of_chord ( r , x ) { document . write ( " " + " " + 2 * r * Math . sin ( x * ( 3.14 / 180 ) ) + " " ) ; } let r = 4 , x = 63 ; length_of_chord ( r , x ) ;
Area of a square inscribed in a circle which is inscribed in an equilateral triangle | Function to find the area of the square ; a cannot be negative ; area of the square ; Driver code
function area ( a ) { if ( a < 0 ) return - 1 ; var area = Math . sqrt ( a ) / 6 ; return area ; } var a = 10 ; document . write ( area ( a ) . toFixed ( 6 ) ) ;
Length of longest rod that can fit into a cuboid | Function to find the length ; temporary variable to hold the intermediate result ; length of longest rod is calculated using square root function ; Driver code ; calling longestRodInCuboid ( ) function to get the length of longest rod
function longestRodInCuboid ( length , breadth , height ) { let result ; let temp ; temp = length * length + breadth * breadth + height * height ; result = Math . sqrt ( temp ) ; return result ; } let length = 12 , breadth = 9 , height = 8 ; document . write ( longestRodInCuboid ( length , breadth , height ) ) ;
Check whether a given point lies on or inside the rectangle | Set 3 | function to Check whether a given point lies inside or on the rectangle or not ; Driver code
function LiesInsieRectangle ( a , b , x , y ) { if ( x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0 ) return true ; return false ; } let a = 7 , b = 2 , x = 4 , y = 5 ; if ( LiesInsieRectangle ( a , b , x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize volume of cuboid with given sum of sides | Return the maximum volume . ; for length ; for breadth ; for height ; calculating maximum volume . ; Driver code
function maxvolume ( s ) { let maxvalue = 0 ; for ( let i = 1 ; i <= s - 2 ; i ++ ) { for ( let j = 1 ; j <= s - 1 ; j ++ ) { let k = s - i - j ; maxvalue = Math . max ( maxvalue , i * j * k ) ; } } return maxvalue ; } let s = 8 ; document . write ( maxvolume ( s ) ) ;
Maximize volume of cuboid with given sum of sides | Return the maximum volume . ; finding length ; finding breadth ; finding height ; Driven Program
function maxvolume ( s ) { let length = parseInt ( s / 3 ) ; s -= length ; let breadth = parseInt ( s / 2 ) ; let height = s - breadth ; return length * breadth * height ; } let s = 8 ; document . write ( maxvolume ( s ) ) ;
Area of a Hexagon | function for calculating area of the hexagon . ; Length of a side
function hexagonArea ( s ) { return ( ( 3 * Math . sqrt ( 3 ) * ( s * s ) ) / 2 ) ; } let s = 4 ; document . write ( " " + hexagonArea ( s ) ) ;
Maximum number of squares that can fit in a right angle isosceles triangle | function for finding max squares ; return in O ( 1 ) with derived formula ; Driver program
function maxSquare ( b , m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; a } let b = 10 , m = 2 ; document . write ( maxSquare ( b , m ) ) ;
Check if right triangle possible from given area and hypotenuse | Prints three sides of a right triangle from given area and hypotenuse if triangle is possible , else prints - 1. ; Descriminant of the equation ; applying the linear equation formula to find both the roots ; Driver code
function findRightAngle ( A , H ) { let D = Math . pow ( H , 4 ) - 16 * A * A ; if ( D >= 0 ) { let root1 = ( H * H + Math . sqrt ( D ) ) / 2 ; let root2 = ( H * H - Math . sqrt ( D ) ) / 2 ; let a = Math . sqrt ( root1 ) ; let b = Math . sqrt ( root2 ) ; if ( b >= a ) document . write ( a + " " + b + " " + H + " " ) ; else document . write ( b + " " + a + " " + H + " " ) ; } else document . write ( " " ) ; } findRightAngle ( 6 , 5 ) ;
Maximum number of 2 x2 squares that can be fit inside a right isosceles triangle | Program to count number of 2 x 2 squares in a right isosceles triangle ; Removing the extra part we would always need ; Since each square has base of length of 2 ; Driver code
function numberOfSquares ( base ) { base = ( base - 2 ) ; base = Math . floor ( base / 2 ) ; return base * ( base + 1 ) / 2 ; } let base = 8 ; document . write ( numberOfSquares ( base ) ) ;
Bitwise OR of bitwise AND of all possible non | Function to find the Bitwise OR of Bitwise AND of all possible subarrays after performing the every query ; Traversing each pair of the query ; Stores the Bitwise OR ; Updating the array ; Find the Bitwise OR of new updated array ; Print the ans ; Driver Code
function performQuery ( arr , Q ) { for ( let i = 0 ; i < Q . length ; i ++ ) { let or = 0 ; let x = Q [ i ] [ 0 ] ; arr [ x - 1 ] = Q [ i ] [ 1 ] ; for ( let j = 0 ; j < arr . length ; j ++ ) { or = or | arr [ j ] ; } document . write ( or + " " ) ; } } let arr = [ 1 , 2 , 3 ] ; let Q = [ [ 1 , 4 ] , [ 3 , 0 ] ] ; performQuery ( arr , Q ) ;
Smallest length of number divisible by K formed by using D only | Function to form the smallest number possible ; Array to mark the remainders counted already ; Iterate over the range ; If that remainder is already found , return - 1 ; Driver Code
function smallest ( k , d ) { let cnt = 1 ; let m = d % k ; let v = new Array ( k ) . fill ( 0 ) ; v [ m ] = 1 ; while ( 1 ) { if ( m == 0 ) return cnt ; m = ( ( ( m * ( 10 % k ) ) % k ) + ( d % k ) ) % k ; if ( v [ m ] == 1 ) return - 1 ; v [ m ] = 1 ; cnt ++ ; } return - 1 ; } let d = 1 ; let k = 41 ; document . write ( smallest ( k , d ) ) ;
Fibonacci Cube Graph | function to find fibonacci number ; function for finding number of vertices in fibonacci cube graph ; return fibonacci number for f ( n + 2 ) ; driver program
function fib ( n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } function findVertices ( n ) { return fib ( n + 2 ) ; } var n = 3 ; document . write ( findVertices ( n ) ) ;
Modify array such that the array does not contain any common divisors other than 1 | Function to check if it is possible to modify the array such that there is no common factor between array elements except 1 ; Stores GCD of the array ; Calculate GCD of the array ; If the current divisor is smaller than X ; Divide GCD by the current divisor ; If possible ; Prvar the modified array ; Otherwise ; Calculating gcd ; Given array ; Size of the array
function checkCommonDivisor ( arr , N , X ) { var G = 0 ; for ( i = 0 ; i < N ; i ++ ) { G = gcd ( G , arr [ i ] ) ; } var copy_G = G ; for ( divisor = 2 ; divisor <= X ; divisor ++ ) { while ( G % divisor == 0 ) { G = G / divisor ; } } if ( G <= X ) { document . write ( " " ) ; for ( i = 0 ; i < N ; i ++ ) document . write ( ( arr [ i ] / copy_G ) + " " ) ; document . write ( ) ; } else document . write ( " " ) ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } var arr = [ 6 , 15 , 6 ] ; var X = 6 ; var N = arr . length ; checkCommonDivisor ( arr , N , X ) ;
Program to print Spiral Pattern | Create row and col to traverse rows and columns ; Variable to determine the movement r = right , l = left , d = down , u = upper ; Array for matrix ; Assign the value ; switch - case to determine the next index ; If right , go right ; If left , go left ; If up , go up ; If down , go down ; Check if the matrix has reached array boundary ; Add the left size for the next boundary ; If 2 rotations has been made , decrease the size left by 1 ; switch - case to rotate the movement ; If right , rotate to down ; If down , rotate to left ; If left , rotate to up ; If up , rotate to right ; Print the matrix ; Get the size of size ; Print the Spiral Pattern
function printSpiral ( size ) { let row = 0 , col = 0 ; let boundary = size - 1 ; let sizeLeft = size - 1 ; let flag = 1 ; let move = ' ' ; let matrix = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) { matrix [ i ] = new Array ( size ) . fill ( 0 ) ; } for ( let i = 1 ; i < size * size + 1 ; i ++ ) { matrix [ row ] [ col ] = i ; switch ( move ) { case ' ' : col += 1 ; break ; case ' ' : col -= 1 ; break ; case ' ' : row -= 1 ; break ; case ' ' : row += 1 ; break ; } if ( i == boundary ) { boundary += sizeLeft ; if ( flag != 2 ) { flag = 2 ; } else { flag = 1 ; sizeLeft -= 1 ; } switch ( move ) { case ' ' : move = ' ' ; break ; case ' ' : move = ' ' ; break ; case ' ' : move = ' ' ; break ; case ' ' : move = ' ' ; break ; } } } for ( row = 0 ; row < size ; row ++ ) { for ( col = 0 ; col < size ; col ++ ) { let n = matrix [ row ] [ col ] ; if ( n < 10 ) document . write ( n + " " ) ; else document . write ( n + " " ) ; } document . write ( " " ) ; } } let size = 5 ; printSpiral ( size ) ;
K 'th Largest element in BST using constant extra space | Node structure ; helper function to create a new Node ; count variable to keep count of visited Nodes ; if right child is NULL ; first increment count and check if count = k ; otherwise move to the left child ; find inorder successor of current Node ; set left child of successor to the current Node ; move current to its right ; restoring the tree back to original binary search tree removing threaded links ; move current to its left child ; Constructed binary tree is 4 / \ 2 7 / \ / \ 1 3 6 10
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } function newNode ( data ) { var temp = new Node ( ) ; temp . data = data ; temp . right = null ; temp . left = null ; return temp ; } function KthLargestUsingMorrisTraversal ( root , k ) { var curr = root ; var Klargest = null ; var count = 0 ; while ( curr != null ) { if ( curr . right == null ) { if ( ++ count == k ) Klargest = curr ; curr = curr . left ; } else { var succ = curr . right ; while ( succ . left != null && succ . left != curr ) succ = succ . left ; if ( succ . left == null ) { succ . left = curr ; curr = curr . right ; } else { succ . left = null ; if ( ++ count == k ) Klargest = curr ; curr = curr . left ; } } } return Klargest ; } root = newNode ( 4 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 10 ) ; document . write ( " " + KthLargestUsingMorrisTraversal ( root , 2 ) . data ) ;
Sorting rows of matrix in ascending order followed by columns in descending order | Javascript implementation to sort the rows of matrix in ascending order followed by sorting the columns in descending order ; function to sort each row of the matrix according to the order specified by ascending . ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of mat [ ] [ ] ; get transpose of mat [ ] [ ] ; again sort rows of mat [ ] [ ] in descending order . ; again get transpose of mat [ ] [ ] ; function to print the matrix ; Driver code
let MAX_SIZE = 10 ; function sortByRow ( mat , n , ascending ) { for ( let i = 0 ; i < n ; i ++ ) { if ( ascending ) mat [ i ] . sort ( function ( a , b ) { return a - b ; } ) ; else mat [ i ] . sort ( function ( a , b ) { return b - a ; } ) ; } } function transpose ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) { let temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ j ] [ i ] ; mat [ j ] [ i ] = temp ; } } function sortMatRowAndColWise ( mat , n ) { sortByRow ( mat , n , true ) ; transpose ( mat , n ) ; sortByRow ( mat , n , false ) ; transpose ( mat , n ) ; } function printMat ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) document . write ( mat [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let n = 3 ; let mat = [ [ 3 , 2 , 1 ] , [ 9 , 8 , 7 ] , [ 6 , 5 , 4 ] ] ; document . write ( " " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; document . write ( " " ) ; printMat ( mat , n ) ;
Sort the matrix row | Javascript implementation to sort the matrix row - wise and column - wise ; function to sort each row of the matrix ; sorting row number ' i ' ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of mat [ ] [ ] ; get transpose of mat [ ] [ ] ; again sort rows of mat [ ] [ ] ; again get transpose of mat [ ] [ ] ; function to print the matrix ; Driver code
let MAX_SIZE = 10 ; function sortByRow ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) mat [ i ] . sort ( function ( a , b ) { return a - b ; } ) ; } function transpose ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) { let temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ j ] [ i ] ; mat [ j ] [ i ] = temp ; } } function sortMatRowAndColWise ( mat , n ) { sortByRow ( mat , n ) ; transpose ( mat , n ) ; sortByRow ( mat , n ) ; transpose ( mat , n ) ; } function printMat ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) document . write ( mat [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let mat = [ [ 4 , 1 , 3 ] , [ 9 , 6 , 8 ] , [ 5 , 2 , 7 ] ] ; let n = 3 ; document . write ( " " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; document . write ( " " ) ; printMat ( mat , n ) ;
Magic Square | Even Order | Function for calculating Magic square ; filling matrix with its count value starting from 1 ; ; change value of Array elements at fix location as per rule ( n * n + 1 ) - arr [ i ] [ j ] Top Left corner of Matrix ( order ( parseInt ( n / 4 ) ) * ( parseInt ( n / 4 ) ) ) ; Top Right corner of Matrix ( order ( parseInt ( n / 4 ) ) * ( parseInt ( n / 4 ) ) ) ; Bottom Left corner of Matrix ( order ( parseInt ( n / 4 ) ) * ( parseInt ( n / 4 ) ) ) ; Bottom Right corner of Matrix ( order ( parseInt ( n / 4 ) ) * ( parseInt ( n / 4 ) ) ) ; Centre of Matrix ( order ( n / 2 ) * ( n / 2 ) ) ; Printing the magic - square ; driver program ; Function call
function doublyEven ( n ) { var arr = Array ( n ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; var i , j ; for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = ( n * i ) + j + 1 ; for ( i = 0 ; i < parseInt ( n / 4 ) ; i ++ ) for ( j = 0 ; j < parseInt ( n / 4 ) ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 0 ; i < parseInt ( n / 4 ) ; i ++ ) for ( j = 3 * ( parseInt ( n / 4 ) ) ; j < n ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 3 * parseInt ( n / 4 ) ; i < n ; i ++ ) for ( j = 0 ; j < parseInt ( n / 4 ) ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 3 * parseInt ( n / 4 ) ; i < n ; i ++ ) for ( j = 3 * parseInt ( n / 4 ) ; j < n ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = parseInt ( n / 4 ) ; i < 3 * parseInt ( n / 4 ) ; i ++ ) for ( j = parseInt ( n / 4 ) ; j < 3 * parseInt ( n / 4 ) ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) document . write ( arr [ i ] [ j ] + " " ) ; document . write ( ' ' ) ; } } var n = 8 ; doublyEven ( n ) ;
Kronecker Product of two matrices | rowa and cola are no of rows and columns of matrix A rowb and colb are no of rows and columns of matrix B ; Function to computes the Kronecker Product of two matrices ; i loops till rowa ; k loops till rowb ; j loops till cola ; l loops till colb ; Each element of matrix A is multiplied by whole Matrix B resp and stored as Matrix C ; Driver Code
let cola = 2 , rowa = 3 , colb = 3 , rowb = 2 ; function Kroneckerproduct ( A , B ) { let C = new Array ( rowa * rowb ) for ( let i = 0 ; i < ( rowa * rowb ) ; i ++ ) { C [ i ] = new Array ( cola * colb ) ; for ( let j = 0 ; j < ( cola * colb ) ; j ++ ) { C [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < rowa ; i ++ ) { for ( let k = 0 ; k < rowb ; k ++ ) { for ( let j = 0 ; j < cola ; j ++ ) { for ( let l = 0 ; l < colb ; l ++ ) { C [ i + l + 1 ] [ j + k + 1 ] = A [ i ] [ j ] * B [ k ] [ l ] ; document . write ( C [ i + l + 1 ] [ j + k + 1 ] + " " ) ; } } document . write ( " " ) ; } } } let A = [ [ 1 , 2 ] , [ 3 , 4 ] , [ 1 , 0 ] ] ; let B = [ [ 0 , 5 , 2 ] , [ 6 , 7 , 3 ] ] ; Kroneckerproduct ( A , B ) ;
Program to check if matrix is lower triangular | Java script Program to check for a lower triangular matrix . ; Function to check matrix is in lower triangular form or not . ; Driver function . ; Function call
let N = 4 ; function isLowerTriangularMatrix ( mat ) { for ( let i = 0 ; i < N ; i ++ ) for ( let j = i + 1 ; j < N ; j ++ ) if ( mat [ i ] [ j ] != 0 ) return false ; return true ; } let mat = [ [ 1 , 0 , 0 , 0 ] , [ 1 , 4 , 0 , 0 ] , [ 4 , 6 , 2 , 0 ] , [ 0 , 4 , 7 , 6 ] ] ; if ( isLowerTriangularMatrix ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to check if matrix is upper triangular | Java script Program to check upper triangular matrix . ; Function to check matrix is in upper triangular form or not . ; driver function
let N = 4 ; function isUpperTriangularMatrix ( mat ) { for ( let i = 1 ; i < N ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != 0 ) return false ; return true ; } let mat = [ [ 1 , 3 , 5 , 3 ] , [ 0 , 4 , 6 , 2 ] , [ 0 , 0 , 2 , 5 ] , [ 0 , 0 , 0 , 6 ] ] ; if ( isUpperTriangularMatrix ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;