codeparrot/xlcost-text-to-code huggingface dataset:
Print all paths from a source point to all the 4 corners of a Matrix | Function to check if we reached on of the entry / exit ( corner ) point . ; Function to check if the index is within the matrix boundary . ; Recursive helper function ; If any corner is reached push the string t into ans and return ; For all the four directions ; The new ith index ; The new jth index ; The direction R / L / U / D ; If the new cell is within the matrix boundary and it is not previously visited in same path ; mark the new cell visited ; Store the direction ; Backtrack to explore other paths ; Function to find all possible paths ; Create a direction array for all the four directions ; stores the result ; Initialise variable ; function call ; Print the result
function isCorner ( i , j , M , N ) { if ( ( i == 0 && j == 0 ) || ( i == 0 && j == N - 1 ) || ( i == M - 1 && j == N - 1 ) || ( i == M - 1 && j == 0 ) ) return true ; return false ; } function isValid ( i , j , M , N ) { if ( i < 0 i >= M j < 0 j >= N ) return false ; return true ; } function solve ( i , j , M , N , Dir , maze , t , ans ) { if ( isCorner ( i , j , M , N ) ) { ans . push ( t ) ; return ; } for ( let k = 0 ; k < 4 ; k ++ ) { let x = i + Dir [ k ] [ 0 ] ; let y = j + Dir [ k ] [ 1 ] ; let c = Dir [ k ] [ 2 ] ; if ( isValid ( x , y , M , N ) && maze [ x ] [ y ] == 1 ) { maze [ x ] [ y ] = 0 ; t += c ; solve ( x , y , M , N , Dir , maze , t , ans ) ; t = t . substr ( 0 , t . length - 1 ) ; maze [ x ] [ y ] = 1 ; } } return ; } function possiblePaths ( src , maze ) { let Dir = [ [ - 1 , 0 , " " ] , [ 0 , 1 , " " ] , [ 1 , 0 , " " ] , [ 0 , - 1 , " " ] , ] ; let temp = " " ; let ans = [ ] ; solve ( src [ 0 ] , src [ 1 ] , maze . length , maze [ 0 ] . length , Dir , maze , temp , ans ) ; return ans ; } let maze = [ [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 ] , [ 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 ] , ] ; let src = [ 4 , 2 ] ; let paths = possiblePaths ( src , maze ) ; if ( paths . length == 0 ) { document . write ( " " ) ; } else { for ( let i = 0 ; i < paths . length ; i ++ ) { if ( paths [ i ] ) document . write ( paths [ i ] + " " ) ; } }
Remove all subtrees consisting only of even valued nodes from a Binary Tree | Node of the tree ; Function to create a new node ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer
class node { constructor ( key ) { this . left = null ; this . right = null ; this . data = key ; } } function newNode ( key ) { let temp = new node ( key ) ; return ( temp ) ; } function printLevelOrder ( root ) { if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( q . length != 0 ) { let temp = q [ 0 ] ; document . write ( temp . data + " " ) ; q . shift ( ) ; if ( temp . left != null ) { q . push ( temp . left ) ; } else if ( temp . right != null ) { document . write ( " " ) ; } if ( temp . right != null ) { q . push ( temp . right ) ; } else if ( temp . left != null ) { document . write ( " " ) ; } } } function pruneTree ( root ) { if ( root == null ) { return null ; } root . left = pruneTree ( root . left ) ; root . right = pruneTree ( root . right ) ; if ( root . data % 2 == 0 && root . right == null && root . left == null ) return null ; return root ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 10 ) ; root . right = newNode ( 7 ) ; root . right . left = newNode ( 12 ) ; root . right . right = newNode ( 5 ) ; let newRoot = pruneTree ( root ) ; printLevelOrder ( newRoot ) ;
Count total ways to reach destination from source in an undirected Graph | Utility Function to count total ways ; Base condition When reach to the destination ; Make vertex visited ; Recursive function , for count ways ; Backtracking Make vertex unvisited ; Return total ways ; Function to count total ways to reach destination ; Loop to make all vertex unvisited , Initially ; Make source visited ; Print total ways
function countWays ( mtrx , vrtx , i , dest , visited ) { if ( i == dest ) { return 1 ; } let total = 0 ; for ( let j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 && ! visited [ j ] ) { visited [ j ] = true ; total += countWays ( mtrx , vrtx , j , dest , visited ) ; visited [ j ] = false ; } } return total ; } function totalWays ( mtrx , vrtx , src , dest ) { let visited = new Array ( vrtx ) ; for ( let i = 0 ; i < vrtx ; i ++ ) { visited [ i ] = false ; } visited [ src ] = true ; return countWays ( mtrx , vrtx , src , dest , visited ) ; } let vrtx = 11 ; let mtrx = [ [ 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 ] , [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ] ; let src = 3 ; let dest = 9 ; document . write ( totalWays ( mtrx , vrtx , src - 1 , dest - 1 ) ) ;
Print path from given Source to Destination in 2 | Function to print the path ; Base condition ; Pop stores elements ; Recursive call for printing stack In reverse order ; Function to store the path into The stack , if path exist ; Base condition ; Push current elements ; Condition to check whether reach to the Destination or not ; Increment ' x ' ordinate of source by ( 2 * x + y ) Keeping ' y ' constant ; Increment ' y ' ordinate of source by ( 2 * y + x ) Keeping ' x ' constant ; Pop current elements form stack ; If no path exists ; Utility function to check whether path exist or not ; To store x co - ordinate ; To store y co - ordinate ; Function to find the path ; Print - 1 , if path doesn 't exist ; Driver code ; Function call
function printExistPath ( sx , sy , last ) { if ( sx . length == 0 sy . length == 0 ) { return ; } let x = sx [ sx . length - 1 ] ; let y = sy [ sy . length - 1 ] ; sx . pop ( ) ; sy . pop ( ) ; printExistPath ( sx , sy , last ) ; if ( x == 83 ) { document . write ( " " + x + " " + y + " " ) ; } else { document . write ( " " + x + " " + y + " " ) ; } } function storePath ( srcX , srcY , destX , destY , sx , sy ) { if ( srcX > destX srcY > destY ) { return false ; } sx . push ( srcX ) ; sy . push ( srcY ) ; if ( srcX == destX && srcY == destY ) { printExistPath ( sx , sy , sx . length ) ; return true ; } if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) { return true ; } if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) { return true ; } sx . pop ( ) ; sy . pop ( ) ; return false ; } function isPathExist ( srcX , srcY , destX , destY ) { let sx = [ ] ; let sy = [ ] ; return storePath ( srcX , srcY , destX , destY , sx , sy ) ; } function printPath ( srcX , srcY , destX , destY ) { if ( ! isPathExist ( srcX , srcY , destX , destY ) ) { document . write ( " " ) ; } } let srcX = 5 , srcY = 8 ; let destX = 83 , destY = 21 ; printPath ( srcX , srcY , destX , destY ) ;
Count even paths in Binary Tree | A Tree node ; Utility function to create a new node ; Utility function to count the even path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is odd ; Increment count when encounter leaf node with all node value even ; Left recursive call , and save the value of count ; Right recursive call , and return value of count ; Function to count the even paths in a given Binary tree ; Function call with count = 0 ; Tree ; Function call
class Node { constructor ( key ) { this . key = key ; this . left = this . right = null ; } } function evenPaths ( node , count ) { if ( node == null || ( node . key % 2 != 0 ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count ) ; return evenPaths ( node . right , count ) ; } function countEvenPaths ( node ) { return evenPaths ( node , 0 ) ; } let root = new Node ( 12 ) ; root . left = new Node ( 13 ) ; root . right = new Node ( 12 ) ; root . right . left = new Node ( 14 ) ; root . right . right = new Node ( 16 ) ; root . right . left . left = new Node ( 21 ) ; root . right . left . right = new Node ( 22 ) ; root . right . right . left = new Node ( 22 ) ; root . right . right . right = new Node ( 24 ) ; root . right . right . right . left = new Node ( 8 ) ; document . write ( countEvenPaths ( root ) ) ;
Sum of subsets of all the subsets of an array | O ( 3 ^ N ) | Javascript implementation of the approach ; To store the final ans ; Function to sum of all subsets of a given array ; Base case ; Recursively calling subsetSum ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
var ans = 0 ; var c = [ ] ; var ans = 0 ; var c = [ ] ; subsetGen ( arr , 0 , n , ans ) ; document . write ( ans ) ; function subsetSum ( i , curr ) { if ( i == c . length ) { ans += curr ; return ; } subsetSum ( i + 1 , curr + c [ i ] ) ; subsetSum ( i + 1 , curr ) ; } function subsetGen ( arr , i , n , ans ) { if ( i == n ) { subsetSum ( 0 , ans , 0 ) ; return ; } subsetGen ( arr , i + 1 , n , ans ) ; c . push ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , ans ) ; c . pop ( ) ; } var arr = [ 1 , 1 ] ; var n = arr . length ;
Print the DFS traversal step | Javascript program to print the complete DFS - traversal of graph using back - tracking ; Function to print the complete DFS - traversal ; Check if all th node is visited or not and count unvisited nodes ; If all the node is visited return ; ; Mark not visited node as visited ; Track the current edge ; Print the node ; Check for not visited node and proceed with it . ; call the DFs function if not visited ; Backtrack through the last visited nodes ; Function to call the DFS function which prints the DFS - traversal stepwise ; Create a array of visited node ; Vector to track last visited road ; Initialize all the node with false ; call the function ; Function to insert edges in Graph ; number of nodes and edges in the graph ; Function call to create the graph ; Call the function to print
let N = 1000 ; let adj = [ ] ; function dfsUtil ( u , node , visited , road_used , parent , it ) { let c = 0 ; for ( let i = 0 ; i < node ; i ++ ) if ( visited [ i ] ) c ++ ; if ( c == node ) return ; visited [ u ] = true ; road_used . push ( [ parent , u ] ) ; document . write ( u + " " ) ; for ( let x = 0 ; x < adj [ u ] . length ; x ++ ) { if ( ! visited [ adj [ u ] [ x ] ] ) { dfsUtil ( adj [ u ] [ x ] , node , visited , road_used , u , it + 1 ) ; } } for ( let y = 0 ; y < road_used . length ; y ++ ) { if ( road_used [ y ] [ 1 ] == u ) { dfsUtil ( road_used [ y ] [ 0 ] , node , visited , road_used , u , it + 1 ) ; } } } function dfs ( node ) { let visited = new Array ( node ) ; let road_used = [ ] ; for ( let i = 0 ; i < node ; i ++ ) { visited [ i ] = false ; } dfsUtil ( 0 , node , visited , road_used , - 1 , 0 ) ; } function insertEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } let node = 11 , edge = 13 ; for ( let i = 0 ; i < N ; i ++ ) { adj . push ( [ ] ) ; } insertEdge ( 0 , 1 ) ; insertEdge ( 0 , 2 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 2 , 9 ) ; insertEdge ( 6 , 7 ) ; insertEdge ( 6 , 8 ) ; insertEdge ( 7 , 8 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 3 , 9 ) ; insertEdge ( 3 , 10 ) ; insertEdge ( 9 , 10 ) ; dfs ( node ) ; 9
Find Maximum number possible by doing at | Javascript program to find maximum integer possible by doing at - most K swap operations on its digits . ; Function to find maximum integer possible by doing at - most K swap operations on its digits ; Return if no swaps left ; Consider every digit ; Compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap str [ i ] with str [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; Backtrack ; Driver code
let max ; function findMaximumNum ( str , k ) { if ( k == 0 ) return ; let n = str . length ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( str [ i ] < str [ j ] ) { let t = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = t ; if ( ( str ) . join ( " " ) > ( max ) ) max = ( str ) . join ( " " ) ; findMaximumNum ( str , k - 1 ) ; let c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } } } } let str = " " ; let k = 4 ; max = str ; findMaximumNum ( str . split ( " " ) , k ) ; document . write ( max + " " ) ;
Reduce the array by deleting elements which are greater than all elements to its left | Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Given array arr [ ]
function merge ( x , y ) { for ( let i = 0 ; i < y . length ; i ++ ) { if ( x [ x . length - 1 ] > y [ i ] ) x . push ( y [ i ] ) ; } return x ; } function mergeDel ( l ) { if ( l . length == 1 ) return l ; let m = Math . floor ( l . length / 2 ) ; let temp1 = l . slice ( 0 , m ) ; let temp2 = l . slice ( m , l . length ) ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } let arr = [ 5 , 4 , 3 , 2 , 1 ] ; let ans = mergeDel ( arr ) ; document . write ( " " ) ; document . write ( ans . join ( " " ) ) ; document . write ( " " ) ;
Significant Inversions in an Array | Function that sorts the input array and returns the number of inversions in the array ; Recursive function that sorts the input array and returns the number of inversions in the array ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of the inversions in the left - part , the right - part and the number of inversions in merging ; Merge the two parts ; Function that merges the two sorted arrays and returns the inversion count in the arrays ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; First pass to count number of significant inversions ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; Second pass to merge the two sorted arrays ; Copy the remaining elements of the left subarray ( if there are any ) to temp ; Copy the remaining elements of the right subarray ( if there are any ) to temp ; Copy back the merged elements to the original array ; Driver code
function mergeSort ( arr , array_size ) { let temp = new Array ( array_size ) ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } function _mergeSort ( arr , temp , left , right ) { let mid , inv_count = 0 ; if ( right > left ) { mid = parseInt ( ( right + left ) / 2 , 10 ) ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } function merge ( arr , temp , left , mid , right ) { let i , j , k ; let inv_count = 0 ; i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] > 2 * arr [ j ] ) { inv_count += ( mid - i ) ; j ++ ; } else { i ++ ; } } i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } let arr = [ 1 , 20 , 6 , 4 , 5 ] ; let n = arr . length ; document . write ( mergeSort ( arr , n ) ) ;
Find the number of different numbers in the array after applying the given operation q times | JavaScript implementation for above approach ; To store the tree in lazy propagation ; To store the different numbers ; Function to update in the range [ x , y ) with given value ; check out of bound ; check for complete overlap ; find the mid number ; check for pending updates ; make lazy [ id ] = 0 , so that it has no pending updates ; call for two child nodes ; Function to find non - zero integers in the range [ l , r ) ; if id contains positive number ; There is no need to see the children , because all the interval have same number ; check for out of bound ; find the middle number ; call for two child nodes ; size of the array and number of queries ; Update operation for l , r , x , id , 0 , n ; Query operation to get answer in the range [ 0 , n - 1 ] ; Print the count of non - zero elements
var N = 100005 ; var lazy = Array ( 4 * N ) . fill ( 0 ) ; var se = new Set ( ) ; function update ( x , y , value , id , l , r ) { if ( x >= r l >= y ) return ; if ( x <= l && r <= y ) { lazy [ id ] = value ; return ; } var mid = parseInt ( ( l + r ) / 2 ) ; if ( lazy [ id ] != 0 ) lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; lazy [ id ] = 0 ; update ( x , y , value , 2 * id , l , mid ) ; update ( x , y , value , 2 * id + 1 , mid , r ) ; } function query ( id , l , r ) { if ( lazy [ id ] != 0 ) { se . add ( lazy [ id ] ) ; return ; } if ( r - l < 2 ) return ; var mid = parseInt ( ( l + r ) / 2 ) ; query ( 2 * id , l , mid ) ; query ( 2 * id + 1 , mid , r ) ; } var n = 5 , q = 3 ; update ( 1 , 4 , 1 , 1 , 0 , n ) ; update ( 0 , 2 , 2 , 1 , 0 , n ) ; update ( 3 , 4 , 3 , 1 , 0 , n ) ; query ( 1 , 0 , n ) ; document . write ( se . size ) ;
Find Nth term ( A matrix exponentiation example ) | Javascript program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence will be first row of res * Initial Vector . ; Driver code
var MOD = 1000000009 ; function power ( n ) { if ( n <= 1 ) return 1 ; n -- ; var res = [ [ 1 , 0 , ] , [ 0 , 1 ] ] ; var tMat = [ [ 2 , 3 ] , [ 1 , 0 ] ] ; while ( n ) { if ( n & 1 ) { var tmp = Array . from ( Array ( 2 ) , ( ) => Array ( 2 ) ) ; tmp [ 0 ] [ 0 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 0 ] [ 1 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tmp [ 1 ] [ 0 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 1 ] [ 1 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; res [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; res [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; res [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; res [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; } n = parseInt ( n / 2 ) ; var tmp = Array . from ( Array ( 2 ) , ( ) => Array ( 2 ) ) ; tmp [ 0 ] [ 0 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 0 ] [ 1 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tmp [ 1 ] [ 0 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 1 ] [ 1 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tMat [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; tMat [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; tMat [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; tMat [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; } return ( res [ 0 ] [ 0 ] * 1 + res [ 0 ] [ 1 ] * 1 ) % MOD ; } var n = 3 ; document . write ( power ( n ) ) ;
Shuffle 2 n integers in format { a1 , b1 , a2 , b2 , a3 , b3 , ... ... , an , bn } without using extra space | method to shuffle an array of size 2 n ; Rotate the element to the left ; swap a [ j - 1 ] , a [ j ] ; Driver Method
function shuffleArray ( a , n ) { for ( let i = 0 , q = 1 , k = n ; i < n ; i ++ , k ++ , q ++ ) for ( let j = k ; j > i + q ; j -- ) { let temp = a [ j - 1 ] ; a [ j - 1 ] = a [ j ] ; a [ j ] = temp ; } } let a = [ 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 ] ; shuffleArray ( a , a . length / 2 ) ; document . write ( a . join ( " " ) ) ;
Place k elements such that minimum distance is maximized | Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid if all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; driver code
function isFeasible ( mid , arr , n , k ) { let pos = arr [ 0 ] ; let elements = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - pos >= mid ) { pos = arr [ i ] ; elements ++ ; if ( elements == k ) return true ; } } return false ; } function largestMinDist ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b } ) ; let res = - 1 ; let left = 1 , right = arr [ n - 1 ] ; while ( left < right ) { let mid = parseInt ( ( left + right ) / 2 , 10 ) ; if ( isFeasible ( mid , arr , n , k ) ) { res = Math . max ( res , mid ) ; left = mid + 1 ; } else right = mid ; } return res ; } let arr = [ 1 , 2 , 8 , 4 , 9 ] ; let n = arr . length ; let k = 3 ; document . write ( largestMinDist ( arr , n , k ) ) ;
Find frequency of each element in a limited range array in less than O ( n ) time | It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; Traverse the array ; Update the frequency ; Traverse the hashmap ; Driver code
function findFrequency ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 0 ) ; mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } for ( let [ key , value ] of mp . entries ( ) ) { document . write ( " " + key + " " + value + " " ) ; } } let arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] ; let n = arr . length ; findFrequency ( arr , n ) ;
Square root of an integer | Returns floor of square root of x ; Base cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code
function floorSqrt ( x ) { if ( x == 0 x == 1 ) return x ; let start = 1 ; let end = x ; let ans ; while ( start <= end ) { let mid = ( start + end ) / 2 ; if ( mid * mid == x ) return mid ; if ( mid * mid < x ) { start = mid + 1 ; ans = mid ; } else end = mid - 1 ; } return ans ; } let x = 11 ; document . write ( floorSqrt ( x ) + " " ) ;