codeparrot/xlcost-text-to-code huggingface dataset:
Find array elements with frequencies in range [ l , r ] | Javascript program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver Code
function findElements ( arr , n , l , r ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { let a = 0 ; if ( mp . get ( arr [ i ] ) == null ) { a = 1 ; } else { a = mp . get ( arr [ i ] ) + 1 ; } mp . set ( arr [ i ] , a ) ; } for ( let i = 0 ; i < n ; ++ i ) { if ( l <= mp . get ( arr [ i ] ) && ( mp . get ( arr [ i ] ) <= r ) ) { document . write ( arr [ i ] + " " ) ; } } } let arr = [ 1 , 2 , 3 , 3 , 2 , 2 , 5 ] ; let n = arr . length ; let l = 2 , r = 3 ; findElements ( arr , n , l , r ) ;
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Function to find the quadratic equation whose roots are a and b ; iterate for all triples pairs ( i , j , l ) ; if the condition is satisfied ; Driver Code
function count_triples ( n , k ) { var i = 0 , j = 0 , l = 0 ; var count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { for ( l = 1 ; l <= n ; l ++ ) { if ( ( i + j ) % k == 0 && ( i + l ) % k == 0 && ( j + l ) % k == 0 ) count ++ ; } } } return count ; } var n = 3 ; var k = 2 ; var ans = count_triples ( n , k ) ; document . write ( ans ) ;
kth smallest / largest in a small range unsorted array | Javascript program of kth smallest / largest in a small range unsorted array ; Storing counts of elements ; Traverse hash array build above until we reach k - th smallest element . ; Driver code
let maxs = 1000001 ; function kthSmallestLargest ( arr , n , k ) { let max_val = Math . max ( ... arr ) ; let hash = new Array ( max_val + 1 ) ; for ( let i = 0 ; i < hash . length ; i ++ ) { hash [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } let count = 0 ; for ( let i = 0 ; i <= max_val ; i ++ ) { while ( hash [ i ] > 0 ) { count ++ ; if ( count == k ) { return i ; } hash [ i ] -- ; } } return - 1 ; } let arr = [ 11 , 6 , 2 , 9 , 4 , 3 , 16 ] ; let n = arr . length , k = 3 ; document . write ( " " + kthSmallestLargest ( arr , n , k ) ) ;
Meta Binary Search | One | Function to show the working of Meta binary search ; Set number of bits to represent largest array index ; while ( ( 1 << lg ) < n - 1 ) lg += 1 ; ; Incrementally construct the index of the target value ; find the element in one direction and update position ; if element found return pos otherwise - 1 ; Driver code
function bsearch ( A , key_to_search ) { let n = A . length ; let lg = parseInt ( Math . log ( n - 1 ) / Math . log ( 2 ) ) + 1 ; let pos = 0 ; for ( let i = lg ; i >= 0 ; i -- ) { if ( A [ pos ] == key_to_search ) return pos ; let new_pos = pos | ( 1 << i ) ; if ( ( new_pos < n ) && ( A [ new_pos ] <= key_to_search ) ) pos = new_pos ; } return ( ( A [ pos ] == key_to_search ) ? pos : - 1 ) ; } let A = [ - 2 , 10 , 100 , 250 , 32315 ] ; document . write ( bsearch ( A , 10 ) ) ;
Queries to check if a number lies in N ranges of L | Function that answers every query ; container to store all range ; hash the L and R ; Push the element to container and hash the L and R ; sort the elements in container ; each query ; get the number same or greater than integer ; if it lies ; check if greater is hashed as 2 ; else check if greater is hashed as 1 ; Lower bound implementation ; Driver Code ; function call to answer queries
function answerQueries ( a , n , queries , q ) { let v = [ ] ; let mpp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { v . push ( a [ i ] [ 0 ] ) ; mpp . set ( a [ i ] [ 0 ] , 1 ) ; v . push ( a [ i ] [ 1 ] ) ; mpp . set ( a [ i ] [ 1 ] , 2 ) ; } v . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < q ; i ++ ) { let num = queries [ i ] ; let ind = lowerBound ( v , num ) ; if ( ind >= 0 && v [ ind ] == num ) document . write ( " " ) ; else { if ( ind >= 0 && mpp . get ( v [ ind ] ) == 2 ) document . write ( " " ) ; document . write ( " " ) ; } } } function lowerBound ( array , value ) { let low = 0 ; let high = array . length ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } let a = [ [ 5 , 6 ] , [ 1 , 3 ] , [ 8 , 10 ] ] ; let n = 3 ; let queries = [ 2 , 3 , 4 , 7 ] ; let q = queries . length ; answerQueries ( a , n , queries , q ) ;
Median of two sorted arrays of different sizes | Set 1 ( Linear ) | A utility function to find median of two integers This function returns median of a [ ] and b [ ] . Assumptions in this function : Both a [ ] and b [ ] are sorted arrays ; Current index of i / p array a [ ] ; Current index of i / p array b [ ] ; Below is to handle the case where all elements of a [ ] are smaller than smallest ( or first ) element of b [ ] or a [ ] is empty ; Below is to handle case where all elements of b [ ] are smaller than smallest ( or first ) element of a [ ] or b [ ] is empty ; Below is to handle the case where sum of number of elements of the arrays is even ; Below is to handle the case where sum of number of elements of the arrays is odd ; Driver code
function findmedian ( a , n1 , b , n2 ) { let i = 0 ; let j = 0 ; let k ; let m1 = - 1 , m2 = - 1 ; for ( k = 0 ; k <= ( n1 + n2 ) / 2 ; k ++ ) { if ( i < n1 && j < n2 ) { if ( a [ i ] < b [ j ] ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } else { m2 = m1 ; m1 = b [ j ] ; j ++ ; } } else if ( i == n1 ) { m2 = m1 ; m1 = b [ j ] ; j ++ ; } else if ( j == n2 ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } } if ( ( n1 + n2 ) % 2 == 0 ) { return ( m1 + m2 ) * 1.0 / 2 ; } return m1 ; } let a = [ 1 , 12 , 15 , 26 , 38 ] ; let b = [ 2 , 13 , 24 ] ; let n1 = a . length ; let n2 = b . length ; document . write ( findmedian ( a , n1 , b , n2 ) ) ;
Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; Driver Code
function printNSE ( arr , n ) { var next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { next = arr [ j ] ; break ; } } document . write ( arr [ i ] + " " + next + " " ) ; } } var arr = [ 11 , 13 , 21 , 3 ] ; var n = arr . length ; printNSE ( arr , n ) ;
Longest subarray such that the difference of max and min is at | Javascript code to find longest subarray with difference between max and min as at - most 1. ; longest constant range length ; first number ; If we see same number ; If we see different number , but same as previous . ; If number is neither same as previous nor as current . ; Driver code
function longestSubarray ( input ) { let prev = - 1 ; let current , next ; let prevCount = 0 , currentCount = 1 ; let longest = 1 ; current = input [ 0 ] ; for ( let i = 1 ; i < input . length ; i ++ ) { next = input [ i ] ; if ( next == current ) { currentCount ++ ; } else if ( next == prev ) { prevCount += currentCount ; prev = current ; current = next ; currentCount = 1 ; } else { longest = Math . max ( longest , currentCount + prevCount ) ; prev = current ; prevCount = currentCount ; current = next ; currentCount = 1 ; } } return Math . max ( longest , currentCount + prevCount ) ; } let input = [ 5 , 5 , 6 , 7 , 6 ] ; document . write ( longestSubarray ( input ) ) ;
Longest Subarray with first element greater than or equal to Last element | Search function for searching the first element of the subarray which is greater or equal to the last element ( num ) ; Returns length of the longest array with first element smaller than the last element . ; Search space for the potential first elements . ; It will store the Indexes of the elements of search space in the original array . ; Initially the search space is empty . ; We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space . ; We will search for the index first element in the search space and we will use it find the index of it in the original array . ; Update the answer if the length of the subarray is greater than the previously calculated lengths . ; Driver code
function binarySearch ( searchSpace , s , e , num ) { let ans = 0 ; while ( s <= e ) { let mid = Math . floor ( ( s + e ) / 2 ) ; if ( searchSpace [ mid ] >= num ) { ans = mid ; e = mid - 1 ; } else s = mid + 1 ; } return ans ; } function longestSubArr ( arr , n ) { let searchSpace = new Array ( n ) ; let index = new Array ( n ) ; let j = 0 ; let ans = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( j == 0 searchSpace [ j - 1 ] < arr [ i ] ) { searchSpace [ j ] = arr [ i ] ; index [ j ] = i ; j ++ ; } let idx = binarySearch ( searchSpace , 0 , j - 1 , arr [ i ] ) ; ans = Math . max ( ans , i - index [ idx ] + 1 ) ; } return ans ; } let arr = [ - 5 , - 1 , 7 , 5 , 1 , - 2 ] ; let n = arr . length ; document . write ( longestSubArr ( arr , n ) ) ;
Print all pairs with given sum | Javascript code to implement the above approach ; Driver code
function pairedElements ( arr , sum , n ) { var low = 0 ; var high = n - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { document . write ( " " + arr [ low ] + " " + arr [ high ] + " " ) ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } } var arr = [ 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 ] var n = arr . length ; arr . sort ( function ( a , b ) { return a - b ; } ) ; pairedElements ( arr , 6 , n ) ;
Check if a string is suffix of another | Javascript program to find if a string is suffix of another ; Driver code ; Test case - sensitive implementation of endsWith function
function isSuffix ( s1 , s2 ) { let n1 = s1 . length , n2 = s2 . length ; if ( n1 > n2 ) return false ; for ( let i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } let s1 = " " , s2 = " " ; let result = isSuffix ( s1 , s2 ) ; if ( result ) document . write ( " " ) ; else document . write ( " " ) ;
Check if all occurrences of a character appear together | Javascript program to find if all occurrences of a character appear together in a string . ; function to find if all occurrences of a character appear together in a string . ; To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver code
function checkIfAllTogether ( s , c ) { function checkIfAllTogether ( s , c ) { let oneSeen = false ; let i = 0 , n = s . length ; while ( i < n ) { if ( s [ i ] == c ) { if ( oneSeen == true ) return false ; while ( i < n && s [ i ] == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } let s = " " ; if ( checkIfAllTogether ( s , ' ' ) ) document . write ( " " ) ; else document . write ( " " ) ;
Front and Back Search in unsorted array | JavaScript program to implement front and back ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver Code
function search ( arr , n , x ) { let front = 0 , back = n - 1 ; while ( front <= back ) { if ( arr [ front ] == x arr [ back ] == x ) return true ; front ++ ; back -- ; } return false ; } let arr = [ 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ] ; let x = 130 ; let n = arr . length ; if ( search ( arr , n , x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Even | Javascript program to find max ( X , Y ) / min ( X , Y ) after P turns ; 1 st test case ; 2 nd test case
function findValue ( X , Y , P ) { if ( P % 2 == 0 ) return parseInt ( ( Math . max ( X , Y ) / Math . min ( X , Y ) ) , 10 ) ; else return parseInt ( ( Math . max ( 2 * X , Y ) / Math . min ( 2 * X , Y ) ) , 10 ) ; } let X = 1 , Y = 2 , P = 1 ; document . write ( findValue ( X , Y , P ) + " " ) ; X = 3 , Y = 7 , P = 2 ; document . write ( findValue ( X , Y , P ) ) ;
The painter 's partition problem | function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions for ( let i = 2 ; i <= k ; i ++ ) { 2 to n boards ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver code ; Calculate size of array .
function sum ( arr , from , to ) { let total = 0 ; for ( let i = from ; i <= to ; i ++ ) total += arr [ i ] ; return total ; } function findMax ( arr , n , k ) { let dp = new Array ( k + 1 ) ; for ( let i = 0 ; i < k + 1 ; i ++ ) { dp [ i ] = new Array ( n + 1 ) ; } for ( let i = 1 ; i <= n ; i ++ ) dp [ 1 ] [ i ] = sum ( arr , 0 , i - 1 ) ; for ( let i = 1 ; i <= k ; i ++ ) dp [ i ] [ 1 ] = arr [ 0 ] ; for ( let j = 2 ; j <= n ; j ++ ) { let best = Number . MAX_VALUE ; for ( let p = 1 ; p <= j ; p ++ ) best = Math . min ( best , Math . max ( dp [ i - 1 ] [ p ] , sum ( arr , p , j - 1 ) ) ) ; dp [ i ] [ j ] = best ; } } return dp [ k ] [ n ] ; } let arr = [ 10 , 20 , 60 , 50 , 30 , 40 ] ; let n = arr . length ; let k = 3 ; document . write ( findMax ( arr , n , k ) ) ;
Counting cross lines in an array | function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; driver code
function countCrossLine ( arr , n ) { let count_crossline = 0 ; let i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } let arr = [ 4 , 3 , 1 , 2 ] ; let n = arr . length ; document . write ( countCrossLine ( arr , n ) + " " ) ;
Recursive Programs to find Minimum and Maximum elements of array | Function to return maximum element using recursion ; If size = 0 means whole array has been traversed ; Driver code ; Function calling
function findMaxRec ( A , n ) { if ( n == 1 ) return A [ 0 ] ; return Math . max ( A [ n - 1 ] , findMaxRec ( A , n - 1 ) ) ; } let A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ] ; let n = A . length ; document . write ( findMaxRec ( A , n ) ) ;
Maximum money that can be collected by both the players in a game of removal of coins | Function to find the maximum score obtained by the players A and B ; Sort the array in descending order ; Stores the maximum amount of money obtained by A ; Stores the maximum amount of money obtained by B ; If the value of N is 1 ; Update the amountA ; Print the amount of money obtained by both players ; Update the amountA ; Update the amountB ; Traverse the array arr [ ] ; If i is an odd number ; Update the amountB ; Update the amountA ; Print the amount of money obtained by both the players ; Driver Code
function findAmountPlayers ( arr , N ) { arr . sort ( ( a , b ) => b - a ) let amountA = 0 ; let amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; document . write ( " " + amountA + " " + " " + amountB + " " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( let i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } document . write ( " " + amountA + " " + " " + amountB + " " ) ; } let arr = [ 1 , 1 , 1 ] ; let N = arr . length findAmountPlayers ( arr , N ) ;
Minimum number of swaps required to minimize sum of absolute differences between adjacent array elements | Comparator to sort in the descending order ; Function to find the minimum number of swaps required to sort the array in increasing order ; Stores the array elements with its index ; Sort the array in the increasing order ; Keeps the track of visited elements ; Stores the count of swaps required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of nodes in this cycle ; Update the value of j ; Move to the next element ; Increment cycle_size ; Update the ans by adding current cycle ; Function to find the minimum number of swaps required to sort the array in decreasing order ; Stores the array elements with its index ; Sort the array in the descending order ; Keeps track of visited elements ; Stores the count of resultant swap required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of node in this cycle ; Update the value of j ; Move to the next element ; Increment the cycle_size ; Update the ans by adding current cycle size ; Function to find minimum number of swaps required to minimize the sum of absolute difference of adjacent elements ; Sort in ascending order ; Sort in descending order ; Return the minimum value ; Drive Code
function mycmp ( a , b ) { return a [ 0 ] > b [ 0 ] ; } function minSwapsAsc ( arr , n ) { let arrPos = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) arrPos [ i ] = new Array ( 2 ) ; for ( let i = 0 ; i < n ; i ++ ) { arrPos [ i ] [ 0 ] = arr [ i ] ; arrPos [ i ] [ 1 ] = i ; } arrPos . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) let vis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) vis [ i ] = false let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] [ 1 ] == i ) continue ; let cycle_size = 0 ; let j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] [ 1 ] ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } function minSwapsDes ( arr , n ) { let arrPos = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) arrPos [ i ] = new Array ( 2 ) ; for ( let i = 0 ; i < n ; i ++ ) { arrPos [ i ] [ 0 ] = arr [ i ] ; arrPos [ i ] [ 1 ] = i ; } arrPos . sort ( function ( a , b ) { return b [ 0 ] - a [ 0 ] } ) let vis = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) vis [ i ] = false let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] [ 1 ] == i ) continue ; let cycle_size = 0 ; let j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] [ 1 ] ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } function minimumSwaps ( arr ) { let S1 = minSwapsAsc ( arr , arr . length ) ; let S2 = minSwapsDes ( arr , arr . length ) ; return Math . min ( S1 , S2 ) ; } let arr = [ 3 , 4 , 2 , 5 , 1 ] ; document . write ( minimumSwaps ( arr ) ) ;
Sort an array of 0 s , 1 s , 2 s and 3 s | Function to sort the array having array element only 0 , 1 , 2 , and 3 ; Iterate until mid <= j ; If arr [ mid ] is 0 ; Swap integers at indices i and mid ; Increment i ; Increment mid ; Otherwise if the value of arr [ mid ] is 3 ; Swap arr [ mid ] and arr [ j ] ; Decrement j ; Otherwise if the value of arr [ mid ] is either 1 or 2 ; Increment the value of mid ; Iterate until i <= j ; If arr [ i ] the value of is 2 ; Swap arr [ i ] and arr [ j ] ; Decrement j ; Otherwise , increment i ; Print the sorted array arr [ ] ; Driver Code
function sortArray ( arr , N ) { var i = 0 , j = N - 1 , mid = 0 ; while ( mid <= j ) { if ( arr [ mid ] == 0 ) { var temp = arr [ i ] ; arr [ i ] = arr [ mid ] ; arr [ mid ] = temp ; i ++ ; mid ++ ; } else if ( arr [ mid ] == 3 ) { var temp = arr [ mid ] ; arr [ mid ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } else if ( arr [ mid ] == 1 arr [ mid ] == 2 ) { mid ++ ; } } while ( i <= j ) { if ( arr [ i ] == 2 ) { var temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } else { i ++ ; } } for ( i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } var arr = [ 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 ] ; var N = arr . length ; sortArray ( arr , N ) ;
Check whether an array can be made strictly increasing by removing at most one element | Function to find if is it possible to make the array strictly increasing by removing at most one element ; Stores the count of numbers that are needed to be removed ; Store the index of the element that needs to be removed ; Traverse the range [ 1 , N - 1 ] ; If arr [ i - 1 ] is greater than or equal to arr [ i ] ; Increment the count by 1 ; Update index ; If count is greater than one ; If no element is removed ; If only the last or the first element is removed ; If a [ index ] is removed ; If a [ index - 1 ] is removed ; Driver Code
function check ( arr , n ) { var count = 0 ; var index = - 1 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] ) { count ++ ; index = i ; } } if ( count > 1 ) return false ; if ( count == 0 ) return true ; if ( index == n - 1 index == 1 ) return true ; if ( arr [ index - 1 ] < arr [ index + 1 ] ) return true ; if ( arr [ index - 2 ] < arr [ index ] ) return true ; return false ; } var arr = [ 1 , 2 , 5 , 3 , 5 ] ; var N = arr . length ; if ( check ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Modify string by rearranging vowels in alphabetical order at their respective indices | Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code
function sortVowels ( S ) { var n = S . length ; var vow = " " ; for ( var i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' ) { vow += S [ i ] ; } } if ( vow . length == 0 ) { document . write ( S ) ; return ; } vow = vow . split ( ' ' ) . sort ( ) ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' S [ i ] == ' ' ) { S [ i ] = vow [ j ++ ] ; } } document . write ( S . join ( ' ' ) ) ; } var S = " " . split ( ' ' ) ; sortVowels ( S ) ;
Maximum number of buckets that can be filled | Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver code
function getBuckets ( arr , N ) { let availableWater = N * ( N - 1 ) / 2 ; arr . sort ( function ( a , b ) { return a - b } ) ; let i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } document . write ( i - 1 ) ; } let arr = [ 1 , 5 , 3 , 4 , 7 , 9 ] ; let N = arr . length ; getBuckets ( arr , N ) ;
Minimize the number of strictly increasing subsequences in an array | Set 2 | Function to find the number of strictly increasing subsequences in an array ; Sort the array ; Stores readonly count of subsequences ; Traverse the array ; Stores current element ; Stores frequency of the current element ; Count frequency of the current element ; If current element frequency is greater than count ; Print the readonly count ; Given array ; Size of the array ; Function call to find the number of strictly increasing subsequences
function minimumIncreasingSubsequences ( arr , N ) { arr . sort ( function ( a , b ) { return a - b } ) ; let count = 0 ; let i = 0 ; while ( i < N ) { let x = arr [ i ] ; let freqX = 0 ; while ( i < N && arr [ i ] == x ) { freqX ++ ; i ++ ; } count = Math . max ( count , freqX ) ; } document . write ( count ) ; } let arr = [ 2 , 1 , 2 , 1 , 4 , 3 ] ; let N = arr . length ; minimumIncreasingSubsequences ( arr , N ) ;