codeparrot/xlcost-text-to-code huggingface dataset:
Unbounded Fractional Knapsack | Function to return the maximum required value ; maxratio will store the maximum value to weight ratio we can have for any item and maxindex will store the index of that element ; Find the maximum ratio ; The item with the maximum value to weight ratio will be put into the knapsack repeatedly until full ; Driver code
function knapSack ( W , wt , val , n ) { var maxratio = - 1000000000 ; var maxindex = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( parseInt ( val [ i ] / wt [ i ] ) > maxratio ) { maxratio = ( val [ i ] / wt [ i ] ) ; maxindex = i ; } } return ( W * maxratio ) ; } var val = [ 14 , 27 , 44 , 19 ] ; var wt = [ 6 , 7 , 9 , 8 ] ; var n = val . length ; var W = 50 ; document . write ( knapSack ( W , wt , val , n ) . toFixed ( 3 ) ) ;
Sort an array without changing position of negative numbers | Function to sort the array such that negative values do not get affected ; Store all non - negative values ; Sort non - negative values ; If current element is non - negative then update it such that all the non - negative values are sorted ; Print the sorted array ; Driver code
function sortArray ( a , n ) { var ans = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) ans . push ( a [ i ] ) ; } ans . sort ( ( a , b ) => a - b ) ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { a [ i ] = ans [ j ] ; j ++ ; } } for ( var i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; } var arr = [ 2 , - 6 , - 3 , 8 , 4 , 1 ] ; var n = arr . length ; sortArray ( arr , n ) ;
Find a triplet in an array whose sum is closest to a given number | Function to return the sum of a triplet which is closest to x ; Sort the array ; To store the closest sum not using INT_MAX to avoid overflowing condition ; Fix the smallest number among the three integers ; Two pointers initially pointing at the last and the element next to the fixed element ; While there could be more pairs to check ; Calculate the sum of the current triplet ; If the sum is more closer than the current closest sum ; If sum is greater then x then decrement the second pointer to get a smaller sum ; Else increment the first pointer to get a larger sum ; Return the closest sum found ; Driver code
function solution ( arr , x ) { arr . sort ( ( a , b ) => a - b ) ; let closestSum = 1000000000 ; for ( let i = 0 ; i < arr . length - 2 ; i ++ ) { let ptr1 = i + 1 , ptr2 = arr . length - 1 ; while ( ptr1 < ptr2 ) { let sum = arr [ i ] + arr [ ptr1 ] + arr [ ptr2 ] ; if ( Math . abs ( 1 * x - sum ) < Math . abs ( 1 * x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return closestSum ; } let arr = [ - 1 , 2 , 1 , - 4 ] ; let x = 1 ; document . write ( solution ( arr , x ) ) ;
Merge two BSTs with constant extra space | Structure of a BST Node ; A utility function to print Inorder traversal of a Binary Tree ; The function to print data of two BSTs in sorted order ; Base cases ; If the first tree is exhausted simply print the inorder traversal of the second tree ; If second tree is exhausted simply print the inoreder traversal of the first tree ; A temporary pointer currently pointing to root of first tree ; previous pointer to store the parent of temporary pointer ; Traverse through the first tree until you reach the leftmost element , which is the first element of the tree in the inorder traversal . This is the least element of the tree ; Another temporary pointer currently pointing to root of second tree ; Previous pointer to store the parent of second temporary pointer ; Traverse through the second tree until you reach the leftmost element , which is the first element of the tree in inorder traversal . This is the least element of the tree . ; Compare the least current least elements of both the tree ; If first tree 's element is  smaller print it ; If the node has no parent , that means this node is the root ; Simply make the right child of the root as new root ; If node has a parent ; As this node is the leftmost node , it is certain that it will not have a let child so we simply assign this node ' s ▁ right ▁ pointer , ▁ which ▁ can ▁ be ▁ ▁ either ▁ null ▁ or ▁ not , ▁ to ▁ its ▁ parent ' s left pointer . This statement is just doing the task of deleting the node ; recursively call the merge function with updated tree ; If the node has no parent , that means this node is the root ; Simply make the right child of root as new root ; If node has a parent ; Recursively call the merge function with updated tree ; Driver Code ; Print sorted nodes of both trees
class Node { constructor ( num ) { this . data = num ; this . left = this . right = null ; } } function inorder ( root ) { if ( root != null ) { inorder ( root . left ) ; document . write ( root . data + " " ) ; inorder ( root . right ) ; } } function merge ( root1 , root2 ) { if ( root1 == null && root2 == null ) return ; if ( root1 == null ) { inorder ( root2 ) ; return ; } if ( root2 == null ) { inorder ( root1 ) ; return ; } let temp1 = root1 ; let prev1 = null ; while ( temp1 . left != null ) { prev1 = temp1 ; temp1 = temp1 . left ; } let temp2 = root2 ; let prev2 = null ; while ( temp2 . left != null ) { prev2 = temp2 ; temp2 = temp2 . left ; } if ( temp1 . data <= temp2 . data ) { document . write ( temp1 . data + " " ) ; if ( prev1 == null ) { merge ( root1 . right , root2 ) ; } else { prev1 . left = temp1 . right ; merge ( root1 , root2 ) ; } } else { document . write ( temp2 . data + " " ) ; if ( prev2 == null ) { merge ( root1 , root2 . right ) ; } else { prev2 . left = temp2 . right ; merge ( root1 , root2 ) ; } } } let root1 = null , root2 = null ; root1 = new Node ( 3 ) ; root1 . left = new Node ( 1 ) ; root1 . right = new Node ( 5 ) ; root2 = new Node ( 4 ) ; root2 . left = new Node ( 2 ) ; root2 . right = new Node ( 6 ) ; merge ( root1 , root2 ) ;
Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | Function to check whether both the array can be sorted in ( strictlyincreasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver Code
function IsSorted ( A , B , n ) { for ( var i = 0 ; i < n ; i ++ ) { var x , y ; x = Math . max ( A [ i ] , B [ i ] ) ; y = Math . min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( var i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } var A = [ 1 , 4 , 3 , 5 , 7 ] ; var B = [ 2 , 2 , 5 , 8 , 9 ] ; var n = A . length ; document . write ( IsSorted ( A , B , n ) ? " " : " " ) ;
Find the largest possible k | Function to find the largest possible k - multiple set ; Sort the given array ; To store k - multiple set ; Traverse through the whole array ; Check if x / k is already present or not ; Print the k - multiple set ; Driver code ; Function call
function K_multiple ( a , n , k ) { a . sort ( ( a , b ) => a - b ) ; let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] % k == 0 && ! s . has ( a [ i ] / k ) ) a [ i ] % k != 0 ) s . add ( a [ i ] ) ; } for ( let i of s ) { document . write ( i + " " ) ; } } let a = [ 2 , 3 , 4 , 5 , 6 , 10 ] ; let k = 2 ; let n = a . length ; K_multiple ( a , n , k ) ;
Maximum water that can be stored between two buildings | Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code
function maxWater ( height , n ) { var maximum = 0 ; var i = 0 , j = n - 1 ; while ( i < j ) { if ( height [ i ] < height [ j ] ) { maximum = Math . max ( maximum , ( j - i - 1 ) * height [ i ] ) ; i ++ ; } else if ( height [ j ] < height [ i ] ) { maximum = Math . max ( maximum , ( j - i - 1 ) * height [ j ] ) ; j -- ; } else { maximum = Math . max ( maximum , ( j - i - 1 ) * height [ i ] ) ; i ++ ; j -- ; } } return maximum ; } var height = [ 2 , 1 , 3 , 4 , 6 , 5 ] ; var n = height . length ; document . write ( maxWater ( height , n ) )
Count the maximum number of elements that can be selected from the array | Function to return the maximum count of selection possible from the given array following the given process ; Initialize result ; Sorting the array ; Initialize the select variable ; Loop through array ; If selection is possible ; Increment result ; Increment selection variable ; Driver Code
function maxSelectionCount ( a , n ) { var res = 0 ; a . sort ( ) ; var select = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } var arr = [ 4 , 2 , 1 , 3 , 5 , 1 , 4 ] ; var N = arr . length ; document . write ( maxSelectionCount ( arr , N ) ) ;
Print combinations of distinct numbers which add up to give sum N | arr [ ] to store all the distinct elements index - next location in array num - given number reducedNum - reduced number ; Set to store all the distinct elements ; Base condition ; Iterate over all the elements and store it into the set ; Calculate the sum of all the elements of the set ; Compare whether the sum is equal to n or not , if it is equal to n print the numbers ; Find previous number stored in the array ; Store all the numbers recursively into the arr [ ] ; Function to find all the distinct combinations of n ; Driver code
function findCombinationsUtil ( arr , index , n , red_num ) { var s = new Set ( ) ; var sum = 0 ; if ( red_num < 0 ) { return ; } if ( red_num === 0 ) { for ( var i = 0 ; i < index ; i ++ ) { s . add ( arr [ i ] ) ; } for ( const itr of s ) { sum = sum + itr ; } if ( sum === n ) { for ( const i of s ) { document . write ( i + " " ) ; } document . write ( " " ) ; return ; } } var prev = index === 0 ? 1 : arr [ index - 1 ] ; for ( var k = prev ; k <= n ; k ++ ) { if ( index < n ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , n , red_num - k ) ; } } } function findCombinations ( n ) { var a = new Array ( n ) . fill ( 0 ) ; findCombinationsUtil ( a , 0 , n , n ) ; } var n = 7 ; findCombinations ( n ) ;
Check whether an array can be made strictly increasing by modifying atmost one element | Function that returns true if arr [ ] can be made strictly increasing after modifying at most one element ; To store the number of modifications required to make the array strictly increasing ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; Check whether the last element needs to be modify or not ; If more than 1 modification is required ; Driver code
function check ( arr , n ) { var modify = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] / 2 ; modify ++ ; } for ( var i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] < arr [ i ] && arr [ i + 1 ] < arr [ i ] ) || ( arr [ i - 1 ] > arr [ i ] && arr [ i + 1 ] > arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; modify ++ ; } } if ( arr [ n - 1 ] < arr [ n - 2 ] ) modify ++ ; if ( modify > 1 ) return false ; return true ; } var arr = [ 2 , 4 , 8 , 6 , 9 , 12 ] ; var n = arr . length ; if ( check ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if the given matrix is increasing row and column wise | Java Script implementation of the approach ; Function that returns true if the matrix is strictly increasing ; Check if the matrix is strictly increasing ; Out of bound condition ; Out of bound condition ; Driver code
let N = 2 ; let M = 2 ; function isMatrixInc ( a ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( i - 1 >= 0 ) { if ( a [ i ] [ j ] <= a [ i - 1 ] [ j ] ) return false ; } if ( j - 1 >= 0 ) { if ( a [ i ] [ j ] <= a [ i ] [ j - 1 ] ) return false ; } } } return true ; } let a = [ [ 2 , 10 ] , [ 11 , 20 ] ] ; if ( isMatrixInc ( a ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize the size of array by deleting exactly k sub | Javascript implementation of the approach ; Sieve of Eratosthenes ; Function to return the size of the maximized array ; Insert the indices of composite numbers ; Compute the number of prime between two consecutive composite ; Sort the diff vector ; Compute the prefix sum of diff vector ; Impossible case ; Delete sub - arrays of length 1 ; Find the number of primes to be deleted when deleting the sub - arrays ; Driver code
const N = 1e7 + 5 ; let prime = new Array ( N ) ; function seive ( ) { for ( let i = 2 ; i < N ; i ++ ) { if ( ! prime [ i ] ) { for ( let j = i + i ; j < N ; j += i ) { prime [ j ] = 1 ; } } } prime [ 1 ] = 1 ; } function maxSizeArr ( arr , n , k ) { let v = new Array ( ) ; let diff = new Array ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) v . push ( i ) ; } for ( let i = 1 ; i < v . length ; i ++ ) { diff . push ( v [ i ] - v [ i - 1 ] - 1 ) ; } diff . sort ( ( a , b ) => a - b ) ; for ( let i = 1 ; i < diff . length ; i ++ ) { diff [ i ] += diff [ i - 1 ] ; } if ( k > n || ( k == 0 && v . length ) ) { return - 1 ; } else if ( v . length <= k ) { return ( n - k ) ; } else if ( v . length > k ) { let tt = v . length - k ; let sum = 0 ; sum += diff [ tt - 1 ] ; let res = n - ( v . length + sum ) ; return res ; } } seive ( ) ; let arr = [ 2 , 4 , 2 , 2 , 4 , 2 , 4 , 2 ] ; let n = arr . length ; let k = 2 ; document . write ( maxSizeArr ( arr , n , k ) ) ;
Sort an array according to count of set bits | Set 2 | function to sort the array according to the number of set bits in elements ; Counting no of setBits in arr [ i ] ; The count is subtracted from 32 because the result needs to be in descending order ; Printing the numbers in descending order of set bit count ; Driver code
function sortArr ( arr , n ) { let map = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { let count = 0 ; let k = arr [ i ] ; while ( k ) { k = k & k - 1 ; count ++ ; } map . push ( [ 32 - count , arr [ i ] ] ) ; } map . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] ; } ) ; for ( let i = 0 ; i < map . length ; i ++ ) { document . write ( map [ i ] [ 1 ] + " " ) ; } } let arr = [ 5 , 2 , 3 , 9 , 4 , 6 , 7 , 15 , 32 ] ; let n = arr . length ; sortArr ( arr , n ) ;
Count number of subsets whose median is also present in the same subset | JavaScript implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nCr ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
const mod = 1000000007 ; function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function nCr ( n , r ) { return parseInt ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) ; } function powmod ( a , n ) { if ( ! n ) return 1 ; let pt = powmod ( a , parseInt ( n / 2 ) ) ; pt = ( pt * pt ) % mod ; if ( n % 2 ) return ( pt * a ) % mod ; else return pt ; } function CountSubset ( arr , n ) { let ans = powmod ( 2 , n - 1 ) ; arr . sort ( ) ; for ( let i = 0 ; i < n ; ++ i ) { let j = i + 1 ; while ( j < n && arr [ j ] == arr [ i ] ) { let r = n - 1 - j ; let l = i ; ans = ( ans + nCr ( l + r , l ) ) % mod ; j ++ ; } } return ans ; } let arr = [ 2 , 3 , 2 ] ; let n = arr . length ; document . write ( CountSubset ( arr , n ) ) ;
Count number of subsets whose median is also present in the same subset | Js implementation of the approach ; Function to store pascal triangle in 2 - d array ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
let mod = 1000000007 ; let arr = [ ] ; for ( let i = 0 ; i < 1001 ; i ++ ) { arr [ i ] = [ ] ; for ( let j = 0 ; j < 1001 ; j ++ ) { arr [ i ] [ j ] = 0 ; } } function Preprocess ( ) { arr [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i <= 1000 ; ++ i ) { arr [ i ] [ 0 ] = 1 ; for ( let j = 1 ; j < i ; ++ j ) { arr [ i ] [ j ] = ( arr [ i - 1 ] [ j - 1 ] + arr [ i - 1 ] [ j ] ) % mod ; } arr [ i ] [ i ] = 1 ; } } function powmod ( a , n ) { if ( ! n ) return 1 ; let pt = powmod ( a , Math . floor ( n / 2 ) ) ; pt = ( pt * pt ) % mod ; if ( n % 2 ) return ( pt * a ) % mod ; else return pt ; } function CountSubset ( val , n ) { let ans = powmod ( 2 , n - 1 ) ; val . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < n ; ++ i ) { let j = i + 1 ; while ( j < n && val [ j ] == val [ i ] ) { let r = n - 1 - j ; let l = i ; ans = ( ans + arr [ l + r ] [ l ] ) % mod ; j ++ ; } } return ans ; } Preprocess ( ) ; let val = [ 2 , 3 , 2 ] ; let n = val . length ; document . write ( CountSubset ( val , n ) , ' ' ) ;
Reorder the position of the words in alphabetical order | Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code
function reArrange ( words , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) mp . set ( words [ i ] , i + 1 ) ; words . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( mp . get ( words [ i ] ) + " " ) ; } } var words = [ " " , " " , " " , " " , " " ] ; var n = words . length ; reArrange ( words , n ) ;
Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code
function findSumofEle ( arr1 , m , arr2 , n ) { let MAX = 100000 ; let hash = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) hash [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) hash [ arr2 [ i ] ] ++ ; for ( let i = 1 ; i < MAX ; i ++ ) hash [ i ] = hash [ i ] + hash [ i - 1 ] ; let maximumFreq = 0 ; for ( let i = 0 ; i < m ; i ++ ) { maximumFreq = Math . max ( maximumFreq , hash [ arr1 [ i ] ] ) ; } let sumOfElements = 0 ; for ( let i = 0 ; i < m ; i ++ ) { if ( maximumFreq == hash [ arr1 [ i ] ] ) sumOfElements += arr1 [ i ] ; } return sumOfElements ; } let arr1 = [ 2 , 5 , 6 , 8 ] ; let arr2 = [ 4 , 10 ] ; let m = arr1 . length ; let n = arr2 . length ; document . write ( findSumofEle ( arr1 , m , arr2 , n ) ) ;
Split the array elements into strictly increasing and decreasing sequence | Function to print both the arrays ; Store both arrays ; Used for hashing ; Iterate for every element ; Increase the count ; If first occurrence ; If second occurrence ; If occurs more than 2 times ; Sort in increasing order ; Print the increasing array ; Sort ; Print the decreasing array ; Driver code
function PrletBothArrays ( a , n ) { let v1 = [ ] , v2 = [ ] ; let mpp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { mpp . set ( a [ i ] , ( mpp . get ( a [ i ] ) == null ? 0 : mpp . get ( a [ i ] ) ) + 1 ) ; if ( mpp . get ( a [ i ] ) == 1 ) v1 . push ( a [ i ] ) ; else if ( mpp . get ( a [ i ] ) == 2 ) v2 . push ( a [ i ] ) ; else { document . write ( " " ) ; return ; } } v1 . sort ( ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < v1 . length ; i ++ ) document . write ( v1 [ i ] + " " ) ; v2 . sort ( ) ; v2 . reverse ( ) ; document . write ( " " + " " + " " ) ; for ( let i = 0 ; i < v2 . length ; i ++ ) document . write ( v2 [ i ] + " " ) ; } let a = [ 7 , 2 , 7 , 3 , 3 , 1 , 4 ] ; let n = a . length ; PrletBothArrays ( a , n ) ;
Iterative selection sort for linked list |  ; Traverse the List ; Traverse the unsorted sublist ; Swap Data
function selectionSort ( head ) { var temp = head ; while ( temp ) { var min = temp ; var r = temp . next ; while ( r ) { if ( min . data > r . data ) min = r ; r = r . next ; } var x = temp . data ; temp . data = min . data ; min . data = x ; temp = temp . next ; } }
Sort ugly numbers in an array at their relative positions | Function that returns true if n is an ugly number ; While divisible by 2 , keep dividing ; While divisible by 3 , keep dividing ; While divisible by 5 , keep dividing ; n must be 1 if it was ugly ; Function to sort ugly numbers in their relative positions ; To store the ugly numbers from the array ; If current element is an ugly number ; Add it to the ArrayList and set arr [ i ] to - 1 ; Sort the ugly numbers ; Position of an ugly number ; Driver code
function isUgly ( n ) { while ( n % 2 == 0 ) n = n / 2 ; while ( n % 3 == 0 ) n = n / 3 ; while ( n % 5 == 0 ) n = n / 5 ; if ( n == 1 ) return true ; return false ; } function sortUglyNumbers ( arr , n ) { var list = [ ] ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( isUgly ( arr [ i ] ) ) { list . push ( arr [ i ] ) ; arr [ i ] = - 1 ; } } list . sort ( ( a , b ) => a - b ) ; var j = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) document . write ( list [ j ++ ] + " " ) ; else document . write ( arr [ i ] + " " ) ; } } var arr = [ 1 , 2 , 3 , 7 , 12 , 10 ] ; var n = arr . length ; sortUglyNumbers ( arr , n ) ;
Sort elements of the array that occurs in between multiples of K | Utility function to print the contents of an array ; Function to sort elements in between multiples of k ; To store the index of previous multiple of k ; If it is not the first multiple of k ; Sort the elements in between the previous and the current multiple of k ; Update previous to be current ; Print the updated array ; Driver code
function printArr ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function sortArr ( arr , n , k ) { var prev = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { if ( prev != - 1 ) var tmp = arr . slice ( prev + 1 , i ) . sort ( ( a , b ) => a - b ) ; for ( var j = prev + 1 ; j < i ; j ++ ) { arr [ j ] = tmp [ j - prev - 1 ] ; } prev = i ; } } printArr ( arr , n ) ; } var arr = [ 2 , 1 , 13 , 3 , 7 , 8 , 21 , 13 , 12 ] ; var n = arr . length ; var k = 2 ; sortArr ( arr , n , k ) ;
Find A and B from list of divisors | Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code
function printNumbers ( arr , n ) { arr . sort ( ( a , b ) => { return a - b ; } ) ; var A = arr [ n - 1 ] , B = - 1 ; for ( var i = n - 2 ; i >= 0 ; i -- ) { if ( ( A % arr [ i ] ) != 0 ) { B = arr [ i ] ; break ; } if ( i - 1 >= 0 && arr [ i ] == arr [ i - 1 ] ) { B = arr [ i ] ; break ; } } document . write ( " " + A + " " + B ) ; } var arr = [ 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 ] ; var n = arr . length ; printNumbers ( arr , n ) ;