codeparrot/xlcost-text-to-code huggingface dataset:
Check if all substrings of length K of a Binary String has equal count of 0 s and 1 s | Function to check if the substring of length K has equal 0 and 1 ; Traverse the String ; Check if every K - th character is the same or not ; Traverse subString of length K ; If current character is 0 ; Increment count ; Otherwise ; Decrement count ; Check for equal 0 s and 1 s ; Driver Code
function check ( s , k ) { let n = s . length ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = i ; j < n ; j += k ) { if ( s [ i ] != s [ j ] ) return false ; } } let c = 0 ; for ( let i = 0 ; i < k ; i ++ ) { if ( s [ i ] == ' ' ) c ++ ; else c -- ; } if ( c == 0 ) return true ; else return false ; } let s = " " ; let k = 2 ; if ( check ( s , k ) ) document . write ( " " + " " ) ; else document . write ( " " ) ;
Check if characters of a given string can be used to form any N equal strings | Function to check if the freq of any character is divisible by N ; Stores the frequency of characters ; If frequency of a character is not divisible by n ; If no character has frequency at least N ; Driver Code ; Function Call
function isSame ( str , n ) { var mp = { } ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( mp . hasOwnProperty ( str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) { mp [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = mp [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] + 1 ; } else { mp [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = 1 ; } } for ( const [ key , value ] of Object . entries ( mp ) ) { if ( value >= n ) { return true ; } } return false ; } var str = " " ; var n = 4 ; if ( isSame ( str , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find the root of given non decreasing function between A and B | Javascript program for the above approach ; Given function ; Function to find the root of the given non - decreasing Function ; To get the minimum possible answer for the root ; Find mid ; Search in [ low , x ] ; Search in [ x , high ] ; Return the required answer ; Function to find the roots of the given equation within range [ a , b ] ; If root doesn 't exists ; Else find the root upto 4 decimal places ; Given range ; Function call
let eps = 1e-6 ; function func ( a , b , c , x ) { return a * x * x + b * x + c ; } function findRoot ( a , b , c , low , high ) { let x = - 1 ; while ( Math . abs ( high - low ) > eps ) { x = ( low + high ) / 2 ; if ( func ( a , b , c , low ) * func ( a , b , c , x ) <= 0 ) { high = x ; } else { low = x ; } } return x ; } function solve ( a , b , c , A , B ) { if ( func ( a , b , c , A ) * func ( a , b , c , B ) > 0 ) { document . write ( " " ) ; } else { document . write ( findRoot ( a , b , c , A , B ) ) ; } } let a = 2 , b = - 3 , c = - 2 , A = 0 , B = 3 ; solve ( a , b , c , A , B ) ;
Median of difference of all pairs from an Array | Function check if mid can be median index of the difference array ; Size of the array ; Total possible no of pair possible ; The index of the element in the difference of all pairs from the array ; Count the number of pairs having difference <= mid ; If the difference between end and first element is less then or equal to mid ; Checking for the no of element less than or equal to mid is greater than median or not ; Function to calculate the median of differences of all pairs from the array ; Size of the array ; Initialising the low and high ; Binary search ; Calculate mid ; If mid can be the median of the array ; Returning the median of the differences of pairs from the array ; Driver Code
function possible ( mid , a ) { let n = a . length ; let total = parseInt ( ( n * ( n - 1 ) ) / 2 ) ; let need = parseInt ( ( total + 1 ) / 2 ) ; let count = 0 ; let start = 0 , end = 1 ; while ( end < n ) { if ( a [ end ] - a [ start ] <= mid ) { end ++ ; } else { count += ( end - start - 1 ) ; start ++ ; } } if ( end == n && start < end && a [ end - 1 ] - a [ start ] <= mid ) { let t = end - start - 1 ; count += parseInt ( t * ( t + 1 ) / 2 ) ; } if ( count >= need ) return true ; else return false ; } function findMedian ( a ) { let n = a . length ; let low = 0 , high = a [ n - 1 ] - a [ 0 ] ; while ( low <= high ) { let mid = ( low + high ) / 2 ; if ( possible ( mid , a ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } let a = [ 1 , 7 , 5 , 2 ] ; a . sort ( ) ; document . write ( findMedian ( a ) ) ;
Print all Strings from array A [ ] having all strings from array B [ ] as subsequence | Function to find strings from A [ ] having all strings in B [ ] as subsequence ; Calculate respective sizes ; Stores the answer ; Stores the frequency of each character in strings of A [ ] ; Compute the frequencies of characters of all strings ; Stores the frequency of each character in strings of B [ ] each character of a string in B [ ] ; If the frequency of a character in B [ ] exceeds that in A [ ] ; A string exists in B [ ] which is not a proper subset of A [ i ] ; If all strings in B [ ] are proper subset of A [ ] ; Push the string in resultant vector ; If any string is found ; Print those strings ; Otherwise ; Driver code
function UniversalSubset ( A , B ) { var n1 = A . length ; var n2 = B . length ; var res = [ ] ; var A_fre = Array . from ( Array ( n1 ) , ( ) => Array ( 26 ) ) ; for ( var i = 0 ; i < n1 ; i ++ ) { for ( var j = 0 ; j < 26 ; j ++ ) A_fre [ i ] [ j ] = 0 ; } for ( var i = 0 ; i < n1 ; i ++ ) { for ( var j = 0 ; j < A [ i ] . length ; j ++ ) { A_fre [ i ] [ A [ i ] . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) ] ++ ; } } var B_fre = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n2 ; i ++ ) { var arr = Array ( 26 ) . fill ( 0 ) ; for ( var j = 0 ; j < B [ i ] . length ; j ++ ) { arr [ B [ i ] . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) ] ++ ; B_fre [ B [ i ] . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) ] = Math . max ( B_fre [ B [ i ] . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) ] , arr [ B [ i ] . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) ] ) ; } } for ( var i = 0 ; i < n1 ; i ++ ) { var flag = 0 ; for ( var j = 0 ; j < 26 ; j ++ ) { if ( A_fre [ i ] [ j ] < B_fre [ j ] ) { flag = 1 ; break ; } } if ( flag == 0 ) res . push ( A [ i ] ) ; } if ( res . length > 0 ) { for ( var i = 0 ; i < res . length ; i ++ ) { for ( var j = 0 ; j < res [ i ] . length ; j ++ ) document . write ( res [ i ] [ j ] ) ; } document . write ( " " ) ; } else document . write ( " " ) ; } var A = [ " " , " " , " " ] ; var B = [ " " , " " ] ; UniversalSubset ( A , B ) ;
Closest pair in an Array such that one number is multiple of the other | Function to find the minimum distance pair where one is the multiple of the other ; Initialize the variables ; Iterate for all the elements ; Loop to make pairs ; Check for minimum distance ; Check if one is a multiple of other ; Update the distance ; Store indexes ; If no such pair exists ; Print the answer ; Given array arr [ ] ; Function Call
function findPair ( a , n ) { let min_dist = Number . MAX_VALUE ; let index_a = - 1 , index_b = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( j - i < min_dist ) { if ( a [ i ] % a [ j ] == 0 a [ j ] % a [ i ] == 0 ) { min_dist = j - i ; index_a = i ; index_b = j ; } } } } if ( index_a == - 1 ) { document . write ( " " ) ; } else { document . write ( " " + a [ index_a ] + " " + a [ index_b ] + " " ) ; } } let a = [ 2 , 3 , 4 , 5 , 6 ] ; let n = a . length ; findPair ( a , n ) ;
Print all numbers in given range having digits in strictly increasing order | Function to print all numbers in the range [ L , R ] having digits in strictly increasing order ; Iterate over the range ; Iterate over the digits ; Check if the current digit is >= the previous digit ; If the digits are in ascending order ; Given range L and R ; Function call
function printNum ( L , R ) { for ( let i = L ; i <= R ; i ++ ) { let temp = i ; let c = 10 ; let flag = 0 ; while ( temp > 0 ) { if ( temp % 10 >= c ) { flag = 1 ; break ; } c = temp % 10 ; temp /= 10 ; } if ( flag == 0 ) document . write ( i + " " ) ; } } let L = 10 , R = 15 ; printNum ( L , R ) ;
Find the missing number in unordered Arithmetic Progression | Function to find the missing element ; Fix left and right boundary for binary search ; Find index of middle element ; Check if the element just after the middle element is missing ; Check if the element just before mid is missing ; Check if the elements till mid follow the AP , then recur for right half ; Else recur for left half ; Function to find the missing element in AP series ; Sort the array [ ] arr ; Calculate Common Difference ; Binary search for the missing ; Given array [ ] arr ; Function Call
function findMissing ( arr , left , right , diff ) { if ( right <= left ) return 0 ; let mid = left + parseInt ( ( right - left ) / 2 , 10 ) ; if ( arr [ mid + 1 ] - arr [ mid ] != diff ) return ( arr [ mid ] + diff ) ; if ( mid > 0 && arr [ mid ] - arr [ mid - 1 ] != diff ) return ( arr [ mid - 1 ] + diff ) ; if ( arr [ mid ] == arr [ 0 ] + mid * diff ) return findMissing ( arr , mid + 1 , right , diff ) ; return findMissing ( arr , left , mid - 1 , diff ) ; } function missingElement ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let diff = parseInt ( ( arr [ n - 1 ] - arr [ 0 ] ) / n , 10 ) ; return findMissing ( arr , 0 , n - 1 , diff ) ; } let arr = [ 2 , 8 , 6 , 10 ] ; let n = arr . length ; document . write ( missingElement ( arr , n ) ) ;
Floor value Kth root of a number using Recursive Binary Search | Function to calculate x raised to the power y in O ( logn ) ; Function to find the Kth root of the number N using BS ; If the range is still valid ; Find the mid - value of range ; Base Case ; Condition to check if the left search space is useless ; Given N and K ; Function Call
function power ( x , y ) { let temp ; if ( y == 0 ) return 1 ; temp = power ( x , Math . floor ( y / 2 ) ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } function nthRootSearch ( low , high , N , K ) { if ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( ( power ( mid , K ) <= N ) && ( power ( mid + 1 , K ) > N ) ) { return mid ; } else if ( power ( mid , K ) < N ) { return nthRootSearch ( mid + 1 , high , N , K ) ; } else { return nthRootSearch ( low , mid - 1 , N , K ) ; } } return low ; } let N = 16 , K = 4 ; document . write ( nthRootSearch ( 0 , N , N , K ) ) ;
Count of subsets having sum of min and max element less than K | Function that return the count of subset such that Math . min ( S ) + Math . max ( S ) < K ; Sorting the array ; ans stores total number of subsets ; Add all possible subsets between i and j ; Decrease the sum ; Driver code
function get_subset_count ( arr , K , N ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; let left , right ; left = 0 ; right = N - 1 ; let ans = 0 ; while ( left <= right ) { if ( arr [ left ] + arr [ right ] < K ) { ans += 1 << ( right - left ) ; left ++ ; } else { right -- ; } } return ans ; } let arr = [ 2 , 4 , 5 , 7 ] ; let K = 8 ; let N = arr . length ; document . write ( get_subset_count ( arr , K , N ) ) ;
Minimize the maximum difference of adjacent elements after at most K insertions | javascript Program to find the minimum of maximum differerence between adjacent elements after at most K insertions ; Calculate the maximum adjacent difference ; If the maximum adjacent difference is already zero ; best and worst specifies range of the maximum adjacent difference ; To store the no of insertions required for respective values of mid ; If the number of insertions required exceeds K ; Otherwise ; Driver code
function minMaxDiff ( arr , n , k ) { var max_adj_dif = - 1000000000 ; for ( var i = 0 ; i < n - 1 ; i ++ ) max_adj_dif = Math . max ( max_adj_dif , Math . abs ( arr [ i ] - arr [ i + 1 ] ) ) ; if ( max_adj_dif == 0 ) return 0 ; var best = 1 ; var worst = max_adj_dif ; var mid , required ; while ( best < worst ) { mid = ( best + worst ) / 2 ; required = 0 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { required += parseInt ( ( Math . abs ( arr [ i ] - arr [ i + 1 ] ) - 1 ) / mid ) ; } if ( required > k ) best = mid + 1 ; else worst = mid ; } return worst ; } var arr = [ 3 , 12 , 25 , 50 ] ; var n = arr . length ; var k = 7 ; document . write ( minMaxDiff ( arr , n , k ) ) ;
Check if minimum element in array is less than or equals half of every other element | Function to Check if the minimum element in the array is greater than or equal to half of every other element ; Initialise the variables to store smallest and second smallest ; Check if current element is smaller than smallest , the current smallest will become secondSmallest and current element will be the new smallest ; Check if current element is smaller than secondSmallest simply update the latter ; Driver code
function checkMin ( arr , len ) { var smallest = Number . INFINITY , secondSmallest = Number . INFINITY ; for ( var i = 0 ; i < len ; i ++ ) { if ( arr [ i ] < smallest ) { secondSmallest = smallest ; smallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest ) { secondSmallest = arr [ i ] ; } } if ( 2 * smallest <= secondSmallest ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 2 , 3 , 4 , 5 ] ; var len = 4 ; checkMin ( arr , len ) ;
Largest and smallest Fibonacci numbers in an Array | Function to create hash table to check Fibonacci numbers ; Insert initial two numbers in the hash table ; Sum of previous two numbers ; Update the variable each time ; Function to find minimum and maximum fibonacci number in given array ; Find maximum value in the array ; Creating a set containing all Fibonacci numbers up to maximum value in the array ; For storing the Minimum and Maximum Fibonacci number ; Check if current element is a fibonacci number ; Update the maximum and minimum accordingly ; Driver code
function createHash ( hash , maxElement ) { let prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { let temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } function fibonacci ( arr , n ) { let max_val = Math . max ( ... arr ) ; let hash = new Set ( ) ; createHash ( hash , max_val ) ; let minimum = Number . MAX_VALUE ; let maximum = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( hash . has ( arr [ i ] ) ) { minimum = Math . min ( minimum , arr [ i ] ) ; maximum = Math . max ( maximum , arr [ i ] ) ; } } document . write ( minimum + " " + maximum + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; let n = arr . length ; fibonacci ( arr , n ) ;
Longest substring with K unique characters using Binary Search | Function that returns true if there is a substring of length len with <= k unique characters ; Size of the string ; Map to store the characters and their frequency ; Update the map for the first substring ; Check for the rest of the substrings ; Add the new character ; Remove the first character of the previous window ; Update the map ; Function to return the length of the longest substring which has K unique characters ; Check if the complete string contains K unique characters ; Size of the string ; Apply binary search ; Driver code
function isValidLen ( s , len , k ) { var n = s . length ; var mp = new Map ( ) ; var right = 0 ; while ( right < len ) { if ( mp . has ( s [ right ] ) ) mp . set ( s [ right ] , mp . get ( s [ right ] ) + 1 ) else mp . set ( s [ right ] , 1 ) right ++ ; } if ( mp . size <= k ) return true ; while ( right < n ) { if ( mp . has ( s [ right ] ) ) mp . set ( s [ right ] , mp . get ( s [ right ] ) + 1 ) else mp . set ( s [ right ] , 1 ) if ( mp . has ( s [ right - len ] ) ) mp . set ( s [ right - len ] , mp . get ( s [ right - len ] ) - 1 ) if ( mp . has ( s [ right - len ] ) && mp . get ( s [ right - len ] ) == 0 ) mp . delete ( s [ right - len ] ) ; if ( mp . size <= k ) return true ; right ++ ; } return mp . size <= k ; } function maxLenSubStr ( s , k ) { var uni = new Set ( ) ; s . split ( ' ' ) . forEach ( x => { uni . add ( x ) ; } ) ; if ( uni . size < k ) return - 1 ; var n = s . length ; var lo = - 1 , hi = n + 1 ; while ( hi - lo > 1 ) { var mid = lo + hi >> 1 ; if ( isValidLen ( s , mid , k ) ) lo = mid ; else hi = mid ; } return lo ; } var s = " " ; var k = 3 ; document . write ( maxLenSubStr ( s , k ) ) ;
Largest area square in an array when elements can be shuffled | Function that returns true if it is possible to make a square with side equal to l ; To store the count of elements greater than or equal to l ; Increment the count ; If the count becomes greater than or equal to l ; Function to return the maximum area of the square that can be obtained ; If square is possible with side length m ; Try to find a square with smaller side length ; Return the area ; Driver code
function isSquarePossible ( arr , n , l ) { let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l ) cnt ++ ; if ( cnt >= l ) return true ; } return false ; } function maxArea ( arr , n ) { let l = 0 , r = n ; let len = 0 ; while ( l <= r ) { let m = l + Math . floor ( ( r - l ) / 2 ) ; if ( isSquarePossible ( arr , n , m ) ) { len = m ; l = m + 1 ; } else r = m - 1 ; } return ( len * len ) ; } let arr = [ 1 , 3 , 4 , 5 , 5 ] ; let n = arr . length ; document . write ( maxArea ( arr , n ) ) ;
Check duplicates in a stream of strings | Function to insert the names and check whether they appear for the first time ; To store the names of the employees ; If current name is appearing for the first time ; Driver code
function insertNames ( arr , n ) { let set = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! set . has ( arr [ i ] ) ) { document . write ( " " + " " ) ; set . add ( arr [ i ] ) ; } else { document . write ( " " + " " ) ; } } } let arr = [ " " , " " , " " ] ; let n = arr . length ; insertNames ( arr , n ) ;
Count the triplets such that A [ i ] < B [ j ] < C [ k ] | Function to return the count of elements in arr [ ] which are less than the given key ; Modified binary search ; Function to return the count of elements in arr [ ] which are greater than the given key ; Modified binary search ; Function to return the count of the required triplets ; Sort all three arrays ; Iterate for all the elements of array B ; Count of elements in A [ ] which are less than the chosen element from B [ ] ; Count of elements in C [ ] which are greater than the chosen element from B [ ] ; Update the count ; Driver code
function countLessThan ( arr , n , key ) { let l = 0 , r = n - 1 ; let index = - 1 ; while ( l <= r ) { let m = Math . floor ( ( l + r ) / 2 ) ; if ( arr [ m ] < key ) { l = m + 1 ; index = m ; } else { r = m - 1 ; } } return ( index + 1 ) ; } function countGreaterThan ( arr , n , key ) { let l = 0 , r = n - 1 ; let index = - 1 ; while ( l <= r ) { let m = Math . floor ( ( l + r ) / 2 ) ; if ( arr [ m ] <= key ) { l = m + 1 ; } else { r = m - 1 ; index = m ; } } if ( index == - 1 ) return 0 ; return ( n - index ) ; } function countTriplets ( n , a , b , c ) { a . sort ( function ( e , f ) { return e - f ; } ) ; b . sort ( function ( e , f ) { return e - f ; } ) ; c . sort ( function ( e , f ) { return e - f ; } ) ; let count = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let current = b [ i ] ; let low = countLessThan ( a , n , current ) ; let high = countGreaterThan ( c , n , current ) ; count += ( low * high ) ; } return count ; } let a = [ 1 , 5 ] ; let b = [ 2 , 4 ] ; let c = [ 3 , 6 ] ; let size = a . length ; document . write ( countTriplets ( size , a , b , c ) ) ;
Cost to Balance the parentheses | javascript code to calculate the minimum cost to make the given parentheses balanced ; To store absolute count of balanced and unbalanced parenthesis ; o ( open bracket ) stores count of ' ( ' and c ( close bracket ) stores count of ' ) ' ; Driver code
function costToBalance ( s ) { if ( s . length == 0 ) document . write ( 0 ) ; var ans = 0 ; var o = 0 , c = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) o ++ ; if ( s [ i ] == ' ' ) c ++ ; } if ( o != c ) return - 1 ; var a = new Array ( s . Length ) ; if ( s [ 0 ] == ' ' ) a [ 0 ] = 1 ; else a [ 0 ] = - 1 ; if ( a [ 0 ] < 0 ) ans += Math . abs ( a [ 0 ] ) ; for ( var i = 1 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) a [ i ] = a [ i - 1 ] + 1 ; else a [ i ] = a [ i - 1 ] - 1 ; if ( a [ i ] < 0 ) ans += Math . abs ( a [ i ] ) ; } return ans ; } var s ; s = " " ; document . write ( costToBalance ( s ) + " " ) ; s = " " ; document . write ( costToBalance ( s ) + " " ) ;
Middle of three using minimum comparisons | Function to find the middle of three number ; x is positive if a is greater than b . x is negative if b is greater than a . ; Similar to x ; Similar to x and y . ; Checking if b is middle ( x and y both are positive ) ; Checking if c is middle ( x and z both are positive ) ; Driver code
function middleOfThree ( a , b , c ) { let x = a - b ; let y = b - c ; let z = a - c ; if ( x * y > 0 ) return b ; else if ( x * z > 0 ) return c ; else return a ; } let a = 20 , b = 30 , c = 40 ; document . write ( middleOfThree ( a , b , c ) ) ;
Find four missing numbers in an array containing elements from 1 to N | Finds missing 4 numbers in O ( N ) time and O ( 1 ) auxiliary space . ; To keep track of 4 possible numbers greater than length of input array In Java , helper is automatically initialized as 0. ; Traverse the input array and mark visited elements either by marking them as negative in arr [ ] or in helper [ ] . ; If element is smaller than or equal to length , mark its presence in arr [ ] ; Mark presence in helper [ ] ; Print all those elements whose presence is not marked . ; Driver code
function missing4 ( arr ) { let helper = [ ] ; for ( let i = 0 ; i < 4 ; i ++ ) { helper [ i ] = 0 ; } for ( let i = 0 ; i < arr . length ; i ++ ) { let temp = Math . abs ( arr [ i ] ) ; if ( temp <= arr . length ) arr [ temp - 1 ] = Math . floor ( arr [ temp - 1 ] * ( - 1 ) ) ; else if ( temp > arr . length ) { if ( temp % arr . length != 0 ) helper [ temp % arr . length - 1 ] = - 1 ; else helper [ ( temp % arr . length ) + arr . length - 1 ] = - 1 ; } } for ( let i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] > 0 ) document . write ( i + 1 + " " ) ; for ( let i = 0 ; i < helper . length ; i ++ ) if ( helper [ i ] >= 0 ) document . write ( arr . length + i + 1 + " " ) ; return ; } let arr = [ 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 ] ; missing4 ( arr ) ;
Permutation present at the middle of lexicographic ordering of permutations of at most length N made up integers up to K | Function that finds the middle the lexicographical smallest sequence ; If K is even ; First element is K / 2 ; Remaining elements of the sequence are all integer K ; Stores the sequence when K is odd ; Iterate over the range [ 0 , N / 2 ] ; Check if the sequence ends with in 1 or not ; Remove the sequence ending in 1 ; If it doesn 't end in 1 ; Decrement by 1 ; Insert K to the sequence till its size is N ; Print the sequence stored in the vector ; Driver Code
function lexiMiddleSmallest ( K , N ) { if ( K % 2 == 0 ) { document . write ( K / 2 + " " ) ; for ( let i = 0 ; i < N - 1 ; ++ i ) { document . write ( K + " " ) ; } document . write ( " " ) ; return ; } let a = [ ] ; for ( let i = 0 ; i < N / 2 ; ++ i ) { if ( a [ a . length - 1 ] == 1 ) { a . pop ( a . length - 1 ) ; } else { a [ a . length - 1 ] -= 1 ; while ( a . length < N ) { a . push ( K ) ; } } } for ( let i in a ) { document . write ( i + " " ) ; } document . write ( " " ) ; } let K = 2 , N = 4 ; lexiMiddleSmallest ( K , N ) ;
Remaining array element after repeated removal of the smallest element from pairs with absolute difference of 2 or 0 | Function to find the last remaining array element after repeatedly removing the smallest from pairs having absolute difference 2 or 0 ; Sort the given array in ascending order ; Traverse the array ; If difference between adjacent elements is not equal to 0 or 2 ; If operations can be performed ; Driver Code
function findLastElement ( arr , N ) { arr . sort ( ) ; let i = 0 ; for ( i = 1 ; i < N ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != 0 && arr [ i ] - arr [ i - 1 ] != 2 ) { document . write ( " " + " " ) ; return ; } } document . write ( arr [ N - 1 ] + " " ) ; } let arr = [ 2 , 4 , 6 , 8 , 0 , 8 ] ; let N = arr . length ; findLastElement ( arr , N ) ;