codeparrot/xlcost-text-to-code huggingface dataset:
Divide a sorted array in K parts with sum of difference of max and min minimized in each part | Function to find the minimum sum of differences possible for the given array when the array is divided leto K subarrays ; Array to store the differences between two adjacent elements ; Iterating through the array ; Storing differences to p ; Sorting p in descending order ; Sum of the first k - 1 values of p ; Computing the result ; Driver Code
function calculate_minimum_split ( n , a , k ) { let p = Array . from ( { length : n - 1 } , ( _ , i ) => 0 ) ; for ( let i = 1 ; i < n ; i ++ ) p [ i - 1 ] = a [ i ] - a [ i - 1 ] ; p . sort ( ( a , b ) => a - b ) ; p . reverse ( ) ; let min_sum = 0 ; for ( let i = 0 ; i < k - 1 ; i ++ ) min_sum += p [ i ] ; let res = a [ n - 1 ] - a [ 0 ] - min_sum ; return res ; } let arr = [ 4 , 8 , 15 , 16 , 23 , 42 ] ; let k = 3 ; let n = arr . length ; document . write ( calculate_minimum_split ( n , arr , k ) ) ;
Sort an Array of dates in ascending order using Custom Comparator | Comparator to sort the array of dates ; Condition to check the year ; Condition to check the month ; Condition to check the day ; Function that prints the dates in ascensding order ; Sort the dates using library sort function with custom Comparator ; Loop to print the dates ; Driver Code
function myCompare ( date1 , date2 ) { var day1 = date1 . substr ( 0 , 2 ) ; var month1 = date1 . substr ( 3 , 2 ) ; var year1 = date1 . substr ( 6 , 4 ) ; var day2 = date2 . substr ( 0 , 2 ) ; var month2 = date2 . substr ( 3 , 2 ) ; var year2 = date2 . substr ( 6 , 4 ) ; if ( year1 < year2 ) return - 1 ; if ( year1 > year2 ) return 1 ; if ( month1 < month2 ) return - 1 ; if ( month1 > month2 ) return 1 ; if ( day1 < day2 ) return - 1 ; if ( day1 > day2 ) return 1 ; } function printDatesAscending ( arr ) { var n = arr . length ; arr . sort ( myCompare ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ ] ; arr . push ( " " ) ; arr . push ( " " ) ; arr . push ( " " ) ; arr . push ( " " ) ; arr . push ( " " ) ; printDatesAscending ( arr ) ;
Sort an Array of Strings according to the number of Vowels in them | Function to check the Vowel ; Returns count of vowels in str ; if ( isVowel ( str [ i ] ) ) Check for vowel ; Function to sort the array according to the number of the vowels ; Vector to store the number of vowels with respective elements ; Inserting number of vowels with respective strings in the vector pair ; Sort the vector , this will sort the pair according to the number of vowels ; Print the sorted vector content ; Driver code
function isVowel ( ch ) { ch = ch . toUpperCase ( ) ; return ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) ; } function countVowels ( str ) { let count = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) ++ count ; return count ; } function sortArr ( arr , n ) { let vp = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { vp . push ( [ countVowels ( arr [ i ] ) , arr [ i ] ] ) ; } vp . sort ( ) ; for ( let i = 0 ; i < vp . length ; i ++ ) document . write ( vp [ i ] [ 1 ] + " " ) ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; sortArr ( arr , n ) ;
Find the minimum cost to cross the River | Function to return the minimum cost ; Sort the price array ; Calculate minimum price of n - 2 most costly person ; Both the ways as discussed above ; Calculate the minimum price of the two cheapest person ; Driver code
function minimumCost ( price , n ) { price . sort ( ) ; let totalCost = 0 ; for ( let i = n - 1 ; i > 1 ; i -= 2 ) { if ( i == 2 ) { totalCost += price [ 2 ] + price [ 0 ] ; } else { let price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] ; let price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] ; totalCost += Math . min ( price_first , price_second ) ; } } if ( n == 1 ) { totalCost += price [ 0 ] ; } else { totalCost += price [ 1 ] ; } return totalCost ; } let price = [ 30 , 40 , 60 , 70 ] ; let n = price . length ; document . write ( minimumCost ( price , n ) ) ;
Minimize the sum of differences of consecutive elements after removing exactly K elements | Javascript implementation of the above approach . ; states of DP ; function to find minimum sum ; base - case ; if state is solved before , return ; marking the state as solved ; recurrence relation ; input values ; calling the required function ;
var N = 100 ; var INF = 1000000 ; var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) ) ; var vis = Array . from ( Array ( N ) , ( ) => Array ( N ) ) ; function findSum ( arr , n , k , l , r ) { if ( ( l ) + ( n - 1 - r ) == k ) return arr [ r ] - arr [ l ] ; if ( vis [ l ] [ r ] ) return dp [ l ] [ r ] ; vis [ l ] [ r ] = 1 ; dp [ l ] [ r ] = Math . min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; return dp [ l ] [ r ] ; } var arr = [ 1 , 2 , 100 , 120 , 140 ] ; var k = 2 ; var n = arr . length ; document . write ( findSum ( arr , n , k , 0 , n - 1 ) ) ;
Find Kth element in an array containing odd elements first and then even elements | Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code
function getNumber ( n , k ) { let pos ; if ( ( n % 2 ) == 0 ) { pos = parseInt ( n / 2 , 10 ) ; } else { pos = parseInt ( n / 2 , 10 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } let n = 8 , k = 5 ; document . write ( getNumber ( n , k ) ) ;
All unique combinations whose sum equals to K | Function to find all unique combination of given elements such that their sum is K ; If a unique combination is found ; For all other combinations ; Check if the sum exceeds K ; Check if it is repeated or not ; Take the element into the combination ; Recursive call ; Remove element from the combination ; Function to find all combination of the given elements ; Sort the given elements ; To store combination ; Driver code ; Function call
function unique_combination ( l , sum , K , local , A ) { if ( sum == K ) { document . write ( " " ) ; for ( let i = 0 ; i < local . length ; i ++ ) { if ( i != 0 ) document . write ( " " ) ; document . write ( local [ i ] ) ; if ( i != local . length - 1 ) document . write ( " " ) ; } document . write ( " " + " " ) ; return ; } for ( let i = l ; i < A . length ; i ++ ) { if ( sum + A [ i ] > K ) continue ; if ( i > l && A [ i ] == A [ i - 1 ] ) continue ; local . push ( A [ i ] ) ; unique_combination ( i + 1 , sum + A [ i ] , K , local , A ) ; local . pop ( ) ; } } function Combination ( A , K ) { A . sort ( ( a , b ) => a - b ) ; let local = [ ] ; unique_combination ( 0 , 0 , K , local , A ) ; } let A = [ 10 , 1 , 2 , 7 , 6 , 1 , 5 ] ; let K = 8 ; Combination ( A , K ) ;
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 ; To store the closest sum ; Run three nested loops each loop for each element of triplet ; Update the closestSum ; Return the closest sum found ; Driver code
function solution ( arr , x ) { let closestSum = Number . MAX_VALUE ; for ( let i = 0 ; i < arr . length ; i ++ ) { for ( let j = i + 1 ; j < arr . length ; j ++ ) { for ( let k = j + 1 ; k < arr . length ; k ++ ) { if ( Math . abs ( x - closestSum ) > Math . abs ( x - ( arr [ i ] + arr [ j ] + arr [ k ] ) ) ) closestSum = ( arr [ i ] + arr [ j ] + arr [ k ] ) ; } } } return closestSum ; } let arr = [ - 1 , 2 , 1 , - 4 ] ; let x = 1 ; document . write ( solution ( arr , x ) ) ;
Build original array from the given sub | Function to add edge to graph ; Function to calculate indegrees of all the vertices ; If there is an edge from i to x then increment indegree of x ; Function to perform topological sort ; Push every node to the queue which has no incoming edge ; Since edge u is removed , update the indegrees of all the nodes which had an incoming edge from u ; Function to generate the array from the given sub - sequences ; Create the graph from the input sub - sequences ; Add edge between every two consecutive elements of the given sub - sequences ; Get the indegrees for all the vertices ; Get the topological order of the created graph ; Size of the required array ; Given sub - sequences of the array ; Get the resultant array as vector ; Printing the array
function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; } function getindeg ( adj , V , indeg ) { for ( let i = 0 ; i < V ; i ++ ) { for ( let x = 0 ; x < adj [ i ] . length ; x ++ ) { indeg [ adj [ i ] [ x ] ] = indeg [ adj [ i ] [ x ] ] + 1 ; } } } function topo ( adj , V , indeg ) { let q = [ ] ; for ( let i = 0 ; i < V ; i ++ ) { if ( indeg [ i ] == 0 ) { q . push ( i ) ; } } let res = [ ] ; while ( q . length > 0 ) { let u = q . shift ( ) ; res . push ( u ) ; for ( let x = 0 ; x < adj [ u ] . length ; x ++ ) { indeg [ adj [ u ] [ x ] ] = indeg [ adj [ u ] [ x ] ] - 1 ; if ( indeg [ adj [ u ] [ x ] ] == 0 ) { q . push ( adj [ u ] [ x ] ) ; } } } return res ; } function makearray ( v , V ) { let adj = [ ] ; for ( let i = 0 ; i < V ; i ++ ) { adj . push ( [ ] ) ; } for ( let i = 0 ; i < v . length ; i ++ ) { for ( let j = 0 ; j < v [ i ] . length - 1 ; j ++ ) { addEdge ( adj , v [ i ] [ j ] , v [ i ] [ j + 1 ] ) ; } } let indeg = [ ] ; for ( let i = 0 ; i < V ; i ++ ) { indeg . push ( 0 ) ; } getindeg ( adj , V , indeg ) ; let res = topo ( adj , V , indeg ) ; return res ; } let n = 10 ; let subseqs = [ [ 9 , 1 , 2 , 8 , 3 ] , [ 6 , 1 , 2 ] , [ 9 , 6 , 3 , 4 ] , [ 5 , 2 , 7 ] , [ 0 , 9 , 5 , 4 ] ] ; let res = makearray ( subseqs , n ) ; for ( let x = 0 ; x < res . length ; x ++ ) { document . write ( res [ x ] + " " ) ; }
Sum of Semi | Vector to store the primes ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all prime values to be true ; If prime [ p ] is not changed then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked ; Print all prime numbers ; Function to return the semi - prime sum ; Variable to store the sum of semi - primes ; Iterate over the prime values ; Break the loop once the product exceeds N ; Add valid products which are less than or equal to N each product is a semi - prime number ; Driver code
let pr = [ ] ; let prime = new Array ( 10000000 + 1 ) ; function sieve ( n ) { for ( let i = 2 ; i <= n ; i += 1 ) { prime [ i ] = 1 ; } for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) pr . push ( p ) ; } function SemiPrimeSum ( N ) { let ans = 0 ; for ( let i = 0 ; i < pr . length ; i += 1 ) { for ( let j = i ; j < pr . length ; j += 1 ) { if ( pr [ i ] * pr [ j ] > N ) break ; ans += pr [ i ] * pr [ j ] ; } } return ans ; } let N = 6 ; sieve ( N ) ; document . write ( SemiPrimeSum ( N ) ) ;
Compress the array into Ranges | Function to compress the array ranges ; start iteration from the ith array element ; loop until arr [ i + 1 ] == arr [ i ] and increment j ; if the program do not enter into the above while loop this means that ( i + 1 ) th element is not consecutive to i th element ; increment i for next iteration ; print the consecutive range found ; move i jump directly to j + 1 ; Driver code
function compressArr ( arr , n ) { let i = 0 , j = 0 ; arr . sort ( function ( a , b ) { return a - b } ) ; while ( i < n ) { j = i ; while ( ( j + 1 < n ) && ( arr [ j + 1 ] == arr [ j ] + 1 ) ) { j ++ ; } if ( i == j ) { document . write ( arr [ i ] + " " ) ; i ++ ; } else { document . write ( arr [ i ] + " " + arr [ j ] + " " ) ; i = j + 1 ; } } } let n = 7 ; let arr = [ 1 , 3 , 4 , 5 , 6 , 9 , 10 ] ; compressArr ( arr , n ) ;
Remove elements to make array sorted | Function to sort the array by removing misplaced elements ; brr [ ] is used to store the sorted array elements ; Print the sorted array ; Driver code
function removeElements ( arr , n ) { let brr = new Array ( n ) ; brr . fill ( 0 ) ; let l = 1 ; brr [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( brr [ l - 1 ] <= arr [ i ] ) { brr [ l ] = arr [ i ] ; l ++ ; } } for ( let i = 0 ; i < l ; i ++ ) document . write ( brr [ i ] + " " ) ; } let arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ] ; let n = arr . length ; removeElements ( arr , n ) ;
Remove elements to make array sorted | Function to sort the array by removing misplaced elements ; l stores the index ; Print the sorted array ; Driver code
function removeElements ( arr , n ) { let l = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ l - 1 ] <= arr [ i ] ) { arr [ l ] = arr [ i ] ; l ++ ; } } for ( let i = 0 ; i < l ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ] ; let n = arr . length ; removeElements ( arr , n ) ;
Find number from its divisors | Function that returns X ; Sort the given array ; Get the possible X ; Container to store divisors ; Find the divisors of x ; Check if divisor ; sort the vec because a is sorted and we have to compare all the elements ; if size of both vectors is not same then we are sure that both vectors can 't be equal ; Check if a and vec have same elements in them ; Driver code ; Function call
function findX ( a , n ) { a . sort ( ( x , y ) => x - y ) ; let x = a [ 0 ] * a [ n - 1 ] ; let vec = [ ] ; for ( let i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { vec . push ( i ) ; if ( parseInt ( x / i ) != i ) vec . push ( parseInt ( x / i ) ) ; } } vec . sort ( ( x , y ) => x - y ) ; if ( vec . length != n ) return - 1 ; else { let i = 0 ; for ( let j = 0 ; j < vec . length ; j ++ ) { if ( a [ i ++ ] != vec [ j ] ) return - 1 ; } } return x ; } let a = [ 2 , 5 , 4 , 10 ] ; let n = a . length ; document . write ( findX ( a , n ) ) ;
Program to print an array in Pendulum Arrangement with constant space | Function to print the Pendulum arrangement of the given array ; Sort the array sort ( arr , arr + n ) ; ; pos stores the index of the last element of the array ; odd stores the last odd index in the array ; Move all odd index positioned elements to the right ; Shift the elements by one position from odd to pos ; Reverse the element from 0 to ( n - 1 ) / 2 ; Printing the pendulum arrangement ; Driver code
function pendulumArrangement ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let odd , temp , p , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; p = odd ; while ( p != pos ) { arr [ p ] = arr [ p + 1 ] ; p ++ ; } arr [ p ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } let start = 0 , end = parseInt ( ( n - 1 ) / 2 , 10 ) ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 11 , 2 , 4 , 55 , 6 , 8 ] ; let n = arr . length ; pendulumArrangement ( arr , n ) ;
Find all the pairs with given sum in a BST | Set 2 | A binary tree node ; Function to add a node to the BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Function to find the target pairs ; LeftList which stores the left side values ; RightList which stores the right side values ; curr_left pointer is used for left side execution and curr_right pointer is used for right side execution ; Storing the left side values into LeftList till leaf node not found ; Storing the right side values into RightList till leaf node not found ; Last node of LeftList ; Last node of RightList ; To prevent repetition like 2 , 6 and 6 , 2 ; Delete the last value of LeftList and make the execution to the right side ; Delete the last value of RightList and make the execution to the left side ; ( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side ; Driver code
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function AddNode ( root , data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( root . data < data ) root . right = AddNode ( root . right , data ) ; else if ( root . data > data ) root . left = AddNode ( root . left , data ) ; return root ; } function TargetPair ( node , tar ) { let LeftList = [ ] ; let RightList = [ ] ; let curr_left = node ; let curr_right = node ; while ( curr_left != null curr_right != null LeftList . length > 0 && RightList . length > 0 ) { while ( curr_left != null ) { LeftList . push ( curr_left ) ; curr_left = curr_left . left ; } while ( curr_right != null ) { RightList . push ( curr_right ) ; curr_right = curr_right . right ; } let LeftNode = LeftList [ LeftList . length - 1 ] ; let RightNode = RightList [ RightList . length - 1 ] ; let leftVal = LeftNode . data ; let rightVal = RightNode . data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . pop ( ) ; curr_left = LeftNode . right ; } else if ( leftVal + rightVal > tar ) { RightList . pop ( ) ; curr_right = RightNode . left ; } else { document . write ( LeftNode . data + " " + RightNode . data + " " ) ; RightList . pop ( ) ; LeftList . pop ( ) ; curr_left = LeftNode . right ; curr_right = RightNode . left ; } } } let root = null ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; let sum = 8 ; TargetPair ( root , sum ) ;
Minimum number greater than the maximum of array which cannot be formed using the numbers in the array | Function that returns the minimum number greater than maximum of the array that cannot be formed using the elements of the array ; Sort the given array ; Maximum number in the array ; table [ i ] will store the minimum number of elements from the array to form i ; Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * max ) ; If there exists a number greater than the maximum element of the array that can be formed using the numbers of array ; Driver code
function findNumber ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; var max = arr [ n - 1 ] ; var table = Array ( ( 2 * max ) + 1 ) . fill ( 0 ) ; table [ 0 ] = 0 ; for ( i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) table [ i ] = Number . MAX_VALUE ; var ans = - 1 ; for ( i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( arr [ j ] <= i ) { var res = table [ i - arr [ j ] ] ; if ( res != Number . MAX_VALUE && res + 1 < table [ i ] ) table [ i ] = res + 1 ; } } if ( i > arr [ n - 1 ] && table [ i ] == Number . MAX_VALUE ) { ans = i ; break ; } } return ans ; } var arr = [ 6 , 7 , 15 ] ; var n = arr . length ; document . write ( findNumber ( arr , n ) ) ;
Product of all Subsequences of size K except the minimum and maximum Elements | Javascript program to find product of all Subsequences of size K except the minimum and maximum Elements ; 2D array to store value of combinations nCr ; Function to pre - calculate value of all combinations nCr ; Function to calculate product of all subsequences except the minimum and maximum elements ; Sorting array so that it becomes easy to calculate the number of times an element will come in first or last place ; An element will occur ' powa ' times in total of which ' powla ' times it will be last element and ' powfa ' times it will be first element ; In total it will come powe = powa - powla - powfa times ; Multiplying a [ i ] powe times using Fermat Little Theorem under MODulo MOD for fast exponentiation ; pre - calculation of all combinations
let MOD = 1000000007 ; let max = 101 ; let C = new Array ( max ) ; for ( let i = 0 ; i < max ; i ++ ) { C [ i ] = new Array ( max ) ; for ( let j = 0 ; j < max ; j ++ ) { C [ i ] [ j ] = 0 ; } } function power ( x , y ) { let res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % MOD ; } y = y >> 1 ; x = ( x * x ) % MOD ; } return res % MOD ; } function combi ( n , k ) { let i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] % MOD + C [ i - 1 ] [ j ] % MOD ) % MOD ; } } } function product ( a , n , k ) { let ans = 1 ; a . sort ( function ( a , b ) { return a - b } ) ; let powa = C [ n - 1 ] [ k - 1 ] ; for ( let i = 0 ; i < n ; i ++ ) { let powla = C [ i ] [ k - 1 ] ; let powfa = C [ n - i - 1 ] [ k - 1 ] ; let powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD ; let mul = power ( a [ i ] , powe ) % MOD ; ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD ; } return ans % MOD ; } combi ( 100 , 100 ) ; let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; let k = 3 ; let ans = product ( arr , n , k ) ; document . write ( ans ) ;
Bitwise AND of N binary strings | Helper method : given two unequal sized bit strings , converts them to same length by adding leading 0 s in the smaller string . Returns the the new length ; Return len_b which is highest . No need to proceed further ! ; Return len_a which is greater or equal to len_b ; The main function that performs AND operation of two - bit sequences and returns the resultant string ; Make both strings of same length with the maximum length of s1 & s2 . ; Initialize res as NULL string ; We start from left to right as we have made both strings of same length . ; Convert s1 [ i ] and s2 [ i ] to int and perform bitwise AND operation , append to " res " string ; Driver code ; Check corner case : If there is just one binary string , then print it and return .
let s1 , s2 ; function makeEqualLength ( ) { let len_a = s1 . length ; let len_b = s2 . length ; let num_zeros = Math . abs ( len_a - len_b ) ; if ( len_a < len_b ) { for ( let i = 0 ; i < num_zeros ; i ++ ) { s1 = ' ' + s1 ; } return len_b ; } else { for ( let i = 0 ; i < num_zeros ; i ++ ) { s2 = ' ' + s2 ; } } return len_a ; } function andOperationBitwise ( ) { let length = makeEqualLength ( ) ; let res = " " ; for ( let i = 0 ; i < length ; i ++ ) { res = res + String . fromCharCode ( ( s1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) & s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) + ' ' . charCodeAt ( ) ) ; } return res ; } let arr = [ " " , " " , " " ] ; let n = arr . length ; let result ; if ( n < 2 ) { document . write ( arr [ n - 1 ] + " " ) ; } result = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { s1 = result ; s2 = arr [ i ] ; result = andOperationBitwise ( ) ; } document . write ( result ) ;
Maximum number of segments that can contain the given points | Function to return the maximum number of segments ; Sort both the vectors ; Initially pointing to the first element of b [ ] ; Try to find a match in b [ ] ; The segment ends before b [ j ] ; The point lies within the segment ; The segment starts after b [ j ] ; Return the required count ; Driver code
function countPoints ( n , m , a , b , x , y ) { a . sort ( function ( a , b ) { return a - b } ) ; b . sort ( function ( a , b ) { return a - b } ) ; let j = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( a [ i ] + y < b [ j ] ) break ; if ( b [ j ] >= a [ i ] - x && b [ j ] <= a [ i ] + y ) { count ++ ; j ++ ; break ; } else j ++ ; } } return count ; } let x = 1 , y = 4 ; let a = [ 1 , 5 ] ; let n = a . length ; let b = [ 1 , 1 , 2 ] ; let m = a . length ; document . write ( countPoints ( n , m , a , b , x , y ) ) ;
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | Javascript program to merge K sorted arrays ; Merge and sort k arrays ; Put the elements in sorted array . ; Sort the output array ; Input 2D - array ; Number of arrays ; Output array ; Print merged array
let N = 4 ; function merge_and_sort ( output , arr , n , k ) { for ( let i = 0 ; i < k ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { output [ i * n + j ] = arr [ i ] [ j ] ; } } output . sort ( function ( a , b ) { return a - b } ) ; } let arr = [ [ 5 , 7 , 15 , 18 ] , [ 1 , 8 , 9 , 17 ] , [ 1 , 4 , 7 , 7 ] ] ; let n = N ; let k = 3 ; let output = new Array ( n * k ) ; merge_and_sort ( output , arr , n , k ) ; for ( let i = 0 ; i < n * k ; i ++ ) document . write ( output [ i ] + " " ) ;