codeparrot/xlcost-text-to-code huggingface dataset:
Check if given Sudoku solution is valid or not | JavaScript program to implement the above approach ; Function to check if all elements of the board [ ] [ ] array store value in the range [ 1 , 9 ] ; Traverse board [ ] [ ] array ; Check if board [ i ] [ j ] lies in the range ; Function to check if the solution of sudoku puzzle is valid or not ; Check if all elements of board [ ] [ ] stores value in the range [ 1 , 9 ] ; Stores unique value from 1 to N ; Traverse each row of the given array ; Initialize unique [ ] array to false ; Traverse each column of current row ; Stores the value of board [ i ] [ j ] ; Check if current row stores duplicate value ; Traverse each column of the given array ; Traverse each row of current column ; Stores the value of board [ j ] [ i ] ; Check if current column stores duplicate value ; Traverse each block of size 3 * 3 in board [ ] [ ] array ; j stores first column of each 3 * 3 block ; Initialize unique [ ] array to false ; Traverse current block ; Stores row number of current block ; Stores column number of current block ; Stores the value of board [ X ] [ Y ] ; Check if current block stores duplicate value ; If all conditions satisfied ; Driver Code
var N = 9 ; function isinRange ( board ) { for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( board [ i ] [ j ] <= 0 board [ i ] [ j ] > 9 ) { return false ; } } } return true ; } function isValidSudoku ( board ) { if ( isinRange ( board ) == false ) { return false ; } var unique = Array ( N + 1 ) . fill ( false ) ; for ( var i = 0 ; i < N ; i ++ ) { unique = Array ( N + 1 ) . fill ( false ) ; unique = Array ( N + 1 ) . fill ( false ) ; for ( var j = 0 ; j < N ; j ++ ) { var Z = board [ i ] [ j ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { var Z = board [ j ] [ i ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( var i = 0 ; i < N - 2 ; i += 3 ) { for ( var j = 0 ; j < N - 2 ; j += 3 ) { unique = Array ( N + 1 ) . fill ( false ) ; for ( var k = 0 ; k < 3 ; k ++ ) { for ( var l = 0 ; l < 3 ; l ++ ) { var X = i + k ; var Y = j + l ; var Z = board [ X ] [ Y ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } } } return true ; } var board = [ [ 7 , 9 , 2 , 1 , 5 , 4 , 3 , 8 , 6 ] , [ 6 , 4 , 3 , 8 , 2 , 7 , 1 , 5 , 9 ] , [ 8 , 5 , 1 , 3 , 9 , 6 , 7 , 2 , 4 ] , [ 2 , 6 , 5 , 9 , 7 , 3 , 8 , 4 , 1 ] , [ 4 , 8 , 9 , 5 , 6 , 1 , 2 , 7 , 3 ] , [ 3 , 1 , 7 , 4 , 8 , 2 , 9 , 6 , 5 ] , [ 1 , 3 , 6 , 7 , 4 , 8 , 5 , 9 , 2 ] , [ 9 , 7 , 4 , 2 , 1 , 5 , 6 , 3 , 8 ] , [ 5 , 2 , 8 , 6 , 3 , 9 , 4 , 1 , 7 ] ] ; if ( isValidSudoku ( board ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Subarray of length K whose concatenation forms a palindrome | Function to check if a number is Palindrome or not here i is the starting index and j is the last index of the subarray ; If the integer at i is not equal to j then the subarray is not palindrome ; Otherwise ; all a [ i ] is equal to a [ j ] then the subarray is palindrome ; Function to find a subarray whose concatenation forms a palindrome and return its starting index ; Iterating over subarray of length k and checking if that subarray is palindrome ; If no subarray is palindrome ; Driver Code
function palindrome ( a , i , j ) { while ( i < j ) { if ( a [ i ] != a [ j ] ) return false ; i ++ ; j -- ; } return true ; } function findSubArray ( arr , k ) { let n = arr . length ; for ( let i = 0 ; i <= n - k ; i ++ ) { if ( palindrome ( arr , i , i + k - 1 ) ) return i ; } return - 1 ; } let arr = [ 2 , 3 , 5 , 1 , 3 ] ; let k = 4 ; let ans = findSubArray ( arr , k ) ; if ( ans == - 1 ) document . write ( - 1 + " " ) ; else { for ( let i = ans ; i < ans + k ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; }
Check if a sequence of path visits any coordinate twice or not | Function to check if the man crosses previous visited coordinate or not ; Stores the count of crossed vertex ; Stores ( x , y ) coordinates ; The coordinates for the origin ; Iterate over the String ; Condition to increment X or Y co - ordinates respectively ; Check if ( x , y ) is already visited ; Print the result ; Given String ; Function call
function isCrossed ( path ) { if ( path . length == 0 ) return ; let ans = false ; let mySet = new Set ( ) ; let x = 0 , y = 0 ; mySet . add ( [ x , y ] ) ; for ( let i = 0 ; i < path . length ; i ++ ) { if ( path [ i ] == ' ' ) mySet . add ( [ x , y ++ ] ) ; if ( path [ i ] == ' ' ) mySet . add ( [ x , y -- ] ) ; if ( path [ i ] == ' ' ) mySet . add ( [ x ++ , y ] ) ; if ( path [ i ] == ' ' ) mySet . add ( [ x -- , y ] ) ; if ( ! mySet . has ( [ x , y ] ) ) { ans = true ; break ; } } if ( ans ) document . write ( " " ) ; else document . write ( " " ) ; } let path = " " ; isCrossed ( path ) ;
Maximum width of an N | Function to find the maximum width of the tree using level order traversal ; Store the edges of the tree ; Stores maximum width of the tree ; Stores the nodes of each level ; Insert root node ; Perform level order traversal on the tree ; Stores the size of the queue ; Update maximum width ; Push the nodes of the next level and pop the elements of the current level ; Get element from the front the Queue ; Push all nodes of the next level . ; Return the result . ; Driver Code ; Constructed tree is : 1 / | \ 2 - 1 3 / \ \ 4 5 8 / / | \ 2 6 12 7
function maxWidth ( N , M , cost , s ) { let adj = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { adj . push ( [ ] ) ; } for ( let i = 0 ; i < M ; i ++ ) { adj [ s [ i ] [ 0 ] ] . push ( s [ i ] [ 1 ] ) ; } let result = 0 ; let q = [ ] ; q . push ( 0 ) ; while ( q . length != 0 ) { let count = q . length ; result = Math . max ( count , result ) ; while ( count -- > 0 ) { let temp = q . shift ( ) ; for ( let i = 0 ; i < adj [ temp ] . length ; i ++ ) { q . push ( adj [ temp ] [ i ] ) ; } } } return result ; } let N = 11 , M = 10 ; let edges = [ ] ; edges . push ( [ 0 , 1 ] ) ; edges . push ( [ 0 , 2 ] ) ; edges . push ( [ 0 , 3 ] ) ; edges . push ( [ 1 , 4 ] ) ; edges . push ( [ 1 , 5 ] ) ; edges . push ( [ 3 , 6 ] ) ; edges . push ( [ 4 , 7 ] ) ; edges . push ( [ 6 , 10 ] ) ; edges . push ( [ 6 , 8 ] ) ; edges . push ( [ 6 , 9 ] ) ; let cost = [ 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 2 , 6 , 12 , 7 ] ; document . write ( maxWidth ( N , M , cost , edges ) ) ;
Minimize sum of prime numbers added to make an array non | Javascript Program to implement the above approach ; Stores if an index is a prime / non - prime value ; Stores the prime ; Function to generate all prime numbers ; If current element is prime ; Set all its multiples non - prime ; Store all prime numbers ; Function to find the closest prime to a particular number ; Applying binary search on primes vector ; If the prime added makes the elements equal ; Return this as the closest prime ; If the array remains non - decreasing ; Search for a bigger prime number ; Otherwise ; Check if a smaller prime can make array non - decreasing or not ; Return closest number ; Function to find the minimum cost ; Find all primes ; Store the result ; Iterate over the array ; Current element is less than the previous element ; Find the closest prime which makes the array non decreasing ; Add to overall cost ; Update current element ; Return the minimum cost ; Given array ; Function Call
let MAX = 10000000 ; let isPrime = new Array ( MAX ) ; let primes = new Array ( ) ; function SieveOfEratosthenes ( ) { isPrime . fill ( true ) ; for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( let i = p * p ; i <= MAX ; i += p ) isPrime [ i ] = false ; } } for ( let p = 2 ; p <= MAX ; p ++ ) if ( isPrime [ p ] ) primes . push ( p ) ; } function prime_search ( primes , diff ) { let low = 0 ; let high = primes . length - 1 ; let res = 0 ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( primes [ mid ] == diff ) { return primes [ mid ] ; } else if ( primes [ mid ] < diff ) { low = mid + 1 ; } else { res = primes [ mid ] ; high = mid - 1 ; } } return res ; } function minCost ( arr , n ) { SieveOfEratosthenes ( ) ; let res = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { let diff = arr [ i - 1 ] - arr [ i ] ; let closest_prime = prime_search ( primes , diff ) ; res += closest_prime ; arr [ i ] += closest_prime ; } } return res ; } let arr = [ 2 , 1 , 5 , 4 , 3 ] ; let n = 5 ; document . write ( minCost ( arr , n ) )
Count ways to split a Binary String into three substrings having equal count of zeros | Function to return ways to split a string into three parts with the equal number of 0 ; Store total count of 0 s ; Count total no . of 0 s character in given string ; If total count of 0 character is not divisible by 3 ; Initialize mp to store frequency of k ; Traverse string to find ways to split string ; Increment count if 0 appears ; Increment result if sum equal to 2 * k and k exists in mp ; Insert sum in mp ; Return result ; Given string ; Function call
function count ( s ) { var cnt = 0 ; s . split ( ' ' ) . forEach ( c => { cnt += ( c == ' ' ) ? 1 : 0 ; } ) ; if ( cnt % 3 != 0 ) return 0 ; var res = 0 , k = parseInt ( cnt / 3 ) , sum = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { sum += ( s [ i ] == ' ' ) ? 1 : 0 ; if ( sum == 2 * k && mp . has ( k ) && i < s . length - 1 && i > 0 ) { res += mp . get ( k ) ; } if ( mp . has ( sum ) ) mp . set ( sum , mp . get ( sum ) + 1 ) else mp . set ( sum , 1 ) ; } return res ; } var str = " " ; document . write ( count ( str ) ) ;
Count ways to split a Binary String into three substrings having equal count of zeros | Function to calculate the number of ways to split ; Calculating the total number of zeros ; Case1 If total count of zeros is not divisible by 3 ; Case2 if total count of zeros is zero ; Number of zeros in each substring ; Initialising zero to the number of ways for first and second cut ; Initializing the count ; Traversing from the beginning ; Incrementing the count if the element is '0' ; Incrementing the ways for the 1 st cut if count is equal to zeros required in each substring ; Incrementing the ways for the 2 nd cut if count is equal to 2 * ( zeros required in each substring ) ; Total number of ways to split is multiplication of ways for the 1 st and 2 nd cut ; Driver code ; Function call
function splitstring ( s ) { let n = s . length ; let zeros = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' ' ) zeros ++ ; if ( zeros % 3 != 0 ) return 0 ; if ( zeros == 0 ) return parseInt ( ( ( n - 1 ) * ( n - 2 ) ) / 2 , 10 ) ; let zerosInEachSubstring = parseInt ( zeros / 3 , 10 ) ; let waysOfFirstCut = 0 ; let waysOfSecondCut = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) count ++ ; if ( count == zerosInEachSubstring ) waysOfFirstCut ++ ; else if ( count == 2 * zerosInEachSubstring ) waysOfSecondCut ++ ; } return waysOfFirstCut * waysOfSecondCut ; } let s = " " ; document . write ( " " + " " + splitstring ( s ) ) ;
Check if a string can be converted to another by swapping of adjacent characters of given type | Function to check if it is possible to transform start to end ; Check the sequence of A , B in both Strings str1 and str2 ; If both the Strings are not equal ; Traverse the Strings ; Check for indexes of A and B ; Driver Code ; Function call
function canTransform ( str1 , str2 ) { var s1 = " " ; var s2 = " " ; for ( const c of str1 ) { if ( c !== " " ) { s1 += c ; } } for ( const c of str2 ) { if ( c !== " " ) { s2 += c ; } } if ( s1 !== s2 ) return false ; var i = 0 ; var j = 0 ; var n = str1 . length ; while ( i < n && j < n ) { if ( str1 [ i ] === " " ) { i ++ ; } else if ( str2 [ j ] === " " ) { j ++ ; } else { if ( ( str1 [ i ] === " " && i < j ) || ( str1 [ i ] === " " && i > j ) ) { return false ; } i ++ ; j ++ ; } } return true ; } var str1 = " " ; var str2 = " " ; if ( canTransform ( str1 . split ( " " ) , str2 . split ( " " ) ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Longest Substring having equal count of Vowels and Consonants | Function to return the length of the longest subString having equal number of vowel and consonant ; Generate the array ; Initialize variable to store result ; Stores the sum of subarray ; Map to store indices of the sum ; Loop through the array ; If sum is 0 ; Count of vowels and consonants are equal ; Update the maximum length of subString in HashMap ; Store the index of the sum ; Return the maximum length of required subString ; Driver code
function maxsubStringLength ( S , N ) { let arr = Array . from ( { length : N } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) if ( S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; let maxLen = 0 ; let curr_sum = 0 ; let hash = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { curr_sum += arr [ i ] ; if ( curr_sum == 0 ) maxLen = Math . max ( maxLen , i + 1 ) ; if ( hash . has ( curr_sum ) ) maxLen = Math . max ( maxLen , i - hash . get ( curr_sum ) ) ; else hash . set ( curr_sum , i ) ; } return maxLen ; } let S = " " ; let n = S . length ; document . write ( maxsubStringLength ( S . split ( ' ' ) , n ) ) ;
Minimum Distance from a given Cell to all other Cells of a Matrix | Javascript Program to implement the above approach ; Stores the accessible directions ; Function to find the minimum distance from a given cell to all other cells in the matrix ; Stores the accessible cells from current cell ; Insert pair ( x , y ) ; Iterate untill queue is empty ; Extract the pair ; Pop them ; Checking boundary condition ; If the cell is not visited ; Assign the minimum distance ; Insert the traversed neighbour into the queue ; Driver Code ; Print the required distances
let mat = new Array ( 1001 ) ; for ( let i = 0 ; i < 1001 ; i ++ ) { mat [ i ] = new Array ( 1001 ) ; for ( let j = 0 ; j < 1001 ; j ++ ) { mat [ i ] [ j ] = 0 ; } } let r , c , x , y ; let dx = [ 0 , - 1 , - 1 , - 1 , 0 , 1 , 1 , 1 ] ; let dy = [ 1 , 1 , 0 , - 1 , - 1 , - 1 , 0 , 1 ] ; function FindMinimumDistance ( ) { let q = [ ] ; q . push ( [ x , y ] ) ; mat [ x ] [ y ] = 0 ; while ( q . length > 0 ) { x = q [ 0 ] [ 0 ] ; y = q [ 0 ] [ 1 ] ; q . shift ( ) ; for ( let i = 0 ; i < 8 ; i ++ ) { let a = x + dx [ i ] ; let b = y + dy [ i ] ; if ( a < 0 a >= r b >= c b < 0 ) continue ; if ( mat [ a ] [ b ] == 0 ) { mat [ a ] [ b ] = mat [ x ] [ y ] + 1 ; q . push ( [ a , b ] ) ; } } } } r = 5 , c = 5 , x = 1 , y = 1 ; let t = x ; let l = y ; mat [ x ] [ y ] = 0 ; FindMinimumDistance ( ) ; mat [ t ] [ l ] = 0 ; for ( let i = 0 ; i < r ; i ++ ) { for ( let j = 0 ; j < c ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; }
Minimum flips required to convert given string into concatenation of equal substrings of length K | Function that returns the minimum number of flips to convert the s into a concatenation of K - length sub - string ; Stores the result ; Iterate through string index ; Stores count of 0 s & 1 s ; Iterate making K jumps ; Count 0 's ; Count 1 's ; Add minimum flips for index i ; Return minimum number of flips ; Driver Code
function minOperations ( S , K ) { var ans = 0 ; for ( var i = 0 ; i < K ; i ++ ) { var zero = 0 , one = 0 ; for ( var j = i ; j < S . length ; j += K ) { if ( S [ j ] === " " ) zero ++ ; else one ++ ; } ans += Math . min ( zero , one ) ; } return ans ; } var S = " " ; var K = 3 ; document . write ( minOperations ( S , K ) ) ;
Find the missing number in unordered Arithmetic Progression | Function to get the missing element ; For maximum Element in the array ; For minimum Element in the array ; For xor of all elements ; Common difference of AP series ; find maximum and minimum element ; Calculating common difference ; Calculate the XOR of all elements ; Perform XOR with actual AP series resultant x will be the ans ; Return the missing element ; Given array ; Function Call ; Print the missing element
function missingElement ( arr , n ) { let max_ele = arr [ 0 ] ; let min_ele = arr [ 0 ] ; let x = 0 ; let d ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_ele ) max_ele = arr [ i ] ; if ( arr [ i ] < min_ele ) min_ele = arr [ i ] ; } d = parseInt ( ( max_ele - min_ele ) / n , 10 ) ; for ( let i = 0 ; i < n ; i ++ ) { x = x ^ arr [ i ] ; } for ( let i = 0 ; i <= n ; i ++ ) { x = x ^ ( min_ele + ( i * d ) ) ; } return x ; } let arr = [ 12 , 3 , 6 , 15 , 18 ] ; let n = arr . length ; let element = missingElement ( arr , n ) ; document . write ( element ) ;
Given a string and an integer k , find the kth sub | Function to prints kth sub - string ; Total sub - strings possible ; If k is greater than total number of sub - strings ; To store number of sub - strings starting with ith character of the string ; Compute the values ; substring [ i - 1 ] is added to store the cumulative sum ; Binary search to find the starting index of the kth sub - string ; To store the ending index of the kth sub - string ; Print the sub - string ; Driver code
function Printksubstring ( str , n , k ) { let total = parseInt ( ( n * ( n + 1 ) ) / 2 , 10 ) ; if ( k > total ) { document . write ( " " + " " ) ; return ; } let substring = new Array ( n + 1 ) ; substring [ 0 ] = 0 ; let temp = n ; for ( let i = 1 ; i <= n ; i ++ ) { substring [ i ] = substring [ i - 1 ] + temp ; temp -- ; } let l = 1 ; let h = n ; let start = 0 ; while ( l <= h ) { let m = parseInt ( ( l + h ) / 2 , 10 ) ; if ( substring [ m ] > k ) { start = m ; h = m - 1 ; } else if ( substring [ m ] < k ) { l = m + 1 ; } else { start = m ; break ; } } let end = n - ( substring [ start ] - k ) ; for ( let i = start - 1 ; i < end ; i ++ ) { document . write ( str [ i ] ) ; } } let str = " " ; let k = 4 ; let n = str . length ; Printksubstring ( str , n , k ) ;
Lower Insertion Point | Function to return the lower insertion point of an element in a sorted array ; Base cases ; Final check for the remaining elements which are < X ; Driver code
function LowerInsertionPoint ( arr , n , X ) { if ( X < arr [ 0 ] ) return 0 ; else if ( X > arr [ n - 1 ] ) return n ; let lowerPnt = 0 ; let i = 1 ; while ( i < n && arr [ i ] < X ) { lowerPnt = i ; i = i * 2 ; } while ( lowerPnt < n && arr [ lowerPnt ] < X ) lowerPnt ++ ; return lowerPnt ; } let arr = [ 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 ] ; let n = arr . length ; let X = 4 ; document . write ( LowerInsertionPoint ( arr , n , X ) ) ;
Number of positions with Same address in row major and column major order | Returns count of required positions ; horizontal 1D array ; vertical 1D array ; iterating for all possible i ; checking if j is integer ; checking if j lies b / w 1 to N ; iterating for all possible j ; checking if i is integer ; checking if i lies b / w 1 to M ; Driver Code
function getCount ( M , N ) { let count = 0 ; if ( M == 1 ) return N ; if ( N == 1 ) return M ; if ( N > M ) { for ( let i = 1 ; i <= M ; i ++ ) { let numerator = N * i - N + M - i ; let denominator = M - 1 ; if ( numerator % denominator == 0 ) { let j = parseInt ( numerator / denominator , 10 ) ; if ( j >= 1 && j <= N ) count ++ ; } } } else { for ( let j = 1 ; j <= N ; j ++ ) { let numerator = M * j - M + N - j ; let denominator = N - 1 ; if ( numerator % denominator == 0 ) { let i = parseInt ( numerator / denominator , 10 ) ; if ( i >= 1 && i <= M ) count ++ ; } } } return count ; } let M = 3 , N = 5 ; document . write ( getCount ( M , N ) ) ;
Maximum in an array that can make another array sorted | Function to check whether there is any swappable element present to make the first array sorted ; wrongIdx is the index of the element which is making the first array unsorted ; Find the maximum element which satisfies the above mentioned neighboring conditions ; if res is true then swap the element and make the first array sorted ; Function to print the sorted array if elements are swapped . ; Driver Code
function swapElement ( arr1 , arr2 , n ) { let wrongIdx = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr1 [ i ] < arr1 [ i - 1 ] ) { wrongIdx = i ; } } let maximum = Number . MIN_VALUE ; let maxIdx = - 1 ; let res = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr2 [ i ] > maximum && arr2 [ i ] >= arr1 [ wrongIdx - 1 ] ) { if ( wrongIdx + 1 <= n - 1 && arr2 [ i ] <= arr1 [ wrongIdx + 1 ] ) { maximum = arr2 [ i ] ; maxIdx = i ; res = true ; } } } if ( res ) { swap ( arr1 , wrongIdx , arr2 , maxIdx ) ; } return res ; } function swap ( a , wrongIdx , b , maxIdx ) { let c = a [ wrongIdx ] ; a [ wrongIdx ] = b [ maxIdx ] ; b [ maxIdx ] = c ; } function getSortedArray ( arr1 , arr2 , n ) { if ( swapElement ( arr1 , arr2 , n ) ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr1 [ i ] + " " ) ; } } else { document . write ( " " ) ; } } let arr1 = [ 1 , 3 , 7 , 4 , 10 ] ; let arr2 = [ 2 , 1 , 6 , 8 , 9 ] ; let n = arr1 . length ; getSortedArray ( arr1 , arr2 , n ) ;
Middle of three using minimum comparisons | Function to find the middle of three number ; Compare each three number to find middle number . Enter only if a > b ; Decided a is not greater than b . ; Driver Code
function middleOfThree ( a , b , c ) { if ( a > b ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } } let a = 20 , b = 30 , c = 40 ; document . write ( middleOfThree ( a , b , c ) ) ;
Sort the matrix column | Function to find the transpose of the matrix mat [ ] ; Stores the transpose of matrix mat [ ] [ ] ; Traverse each row of the matrix ; Traverse each column of the matrix ; Transpose matrix elements ; Function to sort the given matrix in row wise manner ; Traverse the row ; Row - Wise Sorting ; Function to print the matrix in column wise sorted manner ; Function call to find transpose of the the matrix mat [ ] [ ] ; Sorting the matrix row - wise ; Calculate transpose of B [ ] [ ] ; Print the matrix mat [ ] [ ] ; Input ; Function call to print the matrix in column wise sorted manner
function transpose ( mat , row , col ) { let tr = new Array ( col ) ; for ( let i = 0 ; i < col ; i ++ ) { tr [ i ] = new Array ( row ) ; } for ( let i = 0 ; i < row ; i ++ ) { for ( let j = 0 ; j < col ; j ++ ) { tr [ j ] [ i ] = mat [ i ] [ j ] ; } } return tr ; } function RowWiseSort ( B ) { for ( let i = 0 ; i < B . length ; i ++ ) { ( B [ i ] ) . sort ( function ( a , b ) { return a - b ; } ) ; } } function sortCol ( mat , n , M ) { let B = transpose ( mat , N , M ) ; RowWiseSort ( B ) ; mat = transpose ( B , M , N ) ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 1 , 6 , 10 ] , [ 8 , 5 , 9 ] , [ 9 , 4 , 15 ] , [ 7 , 3 , 60 ] ] ; let N = mat . length ; let M = mat [ 0 ] . length ; sortCol ( mat , N , M ) ;