codeparrot/xlcost-text-to-code huggingface dataset:
Queries on XOR of XORs of all subarrays | Output for each query ; If number of element is even . ; If number of element is odd . ; if l is even ; if l is odd ; Wrapper Function ; Evaluating prefixodd and prefixeven ; Driver code
function ansQueries ( prefeven , prefodd , l , r ) { if ( ( r - l + 1 ) % 2 == 0 ) document . write ( " " ) ; else { if ( l % 2 == 0 ) document . write ( prefeven [ r ] ^ prefeven [ l - 1 ] ) ; else document . write ( prefodd [ r ] ^ prefodd [ l - 1 ] ) ; } document . write ( " " ) ; } function wrapper ( arr , n , l , r , q ) { let prefodd = [ ] ; let prefeven = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) { if ( ( i ) % 2 == 0 ) { prefeven [ i ] = arr [ i - 1 ] ^ prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ; } else { prefeven [ i ] = prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ^ arr [ i - 1 ] ; } } let i = 0 ; while ( i != q ) { ansQueries ( prefeven , prefodd , l [ i ] , r [ i ] ) ; i ++ ; } } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; let l = [ 1 , 1 , 2 ] ; let r = [ 2 , 3 , 4 ] ; let q = l . length ; wrapper ( arr , n , l , r , q ) ;
Variation in Nim Game | Function to return final grundy Number ( G ) of game ; If pile size is odd ; We XOR pile size + 1 ; If pile size is even ; We XOR pile size - 1 ; Game with 3 piles ; Pile with different sizes ; Function to return result of game ; If G is zero ; If G is non zero
function solve ( p , n ) { let G = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( p [ i ] % 2 != 0 ) G ^= ( p [ i ] + 1 ) ; else G ^= ( p [ i ] - 1 ) ; } return G ; } let n = 3 ; let p = [ 32 , 49 , 58 ] ; let res = solve ( p , n ) ; if ( res == 0 ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum AND value of a pair in an array | Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Driver Code
function checkBit ( pattern , arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } function maxAND ( arr , n ) { let res = 0 , count ; for ( let bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } return res ; } let arr = [ 4 , 8 , 6 , 2 ] ; let n = arr . length ; document . write ( " " + maxAND ( arr , n ) ) ;
Minimum flips to make all 1 s in left and 0 s in right | Set 1 ( Using Bitmask ) | Function to count minimum number of flips ; This is converting string s into integer of base 2 ( if s = '100' then num = 4 ) ; Initialize minXor with n that can be maximum number of flips ; Right shift 1 by ( n - 1 ) bits ; Calculate bitwise XOR of num and mask ; Math . min ( a , b ) returns minimum of a and b return minimum number of flips till that digit ; Function to count number of 1 s ; Driver Code
function findMiniFlip ( nums ) { let n = nums . length ; let s = " " ; for ( let i = 0 ; i < n ; i ++ ) s += nums [ i ] ; let num = parseInt ( s , 2 ) ; let minXor = n ; let mask = ( 1 << ( n - 1 ) ) ; while ( n - 1 > 0 ) { let temp = ( num ^ mask ) ; minXor = Math . min ( minXor , countones ( temp ) ) ; n -- ; mask = ( mask | ( 1 << n - 1 ) ) ; } return minXor ; } function countones ( n ) { let c = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; c ++ ; } return c ; } let nums = [ 1 , 0 , 1 , 1 , 0 , 0 , 0 ] ; let n = findMiniFlip ( nums ) ; document . write ( n ) ;
Check if a number is power of 8 or not | function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; Driver Code
function checkPowerof8 ( n ) { let i = Math . log ( n ) / Math . log ( 8 ) ; return ( i - Math . floor ( i ) < 0.000001 ) ; } let n = 65 ; if ( checkPowerof8 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the n | utility function which is used to convert binary string into integer ; convert binary string into integer ; function to find nth binary palindrome number ; stores the binary palindrome string ; base case ; add 2 nd binary palindrome string ; runs till the nth binary palindrome number ; remove curr binary palindrome string from queue ; if n == 0 then we find the n 'th binary  palindrome so we return our answer ; calculate length of curr binary palindrome string ; if length is even . so it is our first case we have two choices ; if length is odd . so it is our second case we have only one choice ; Driver code ; Function Call
function convertStringToInt ( s ) { let ans = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) ans += 1 << i ; } return ans ; } function getNthNumber ( n ) { let q = [ ] ; if ( n == 1 ) return 1 ; n = n - 1 ; q . push ( " " ) ; while ( n -- > 0 ) { let curr = q . shift ( ) ; if ( n == 0 ) return convertStringToInt ( curr ) ; let len = curr . length ; let len2 = Math . floor ( len / 2 ) ; if ( len % 2 == 0 ) { q . push ( curr . substring ( 0 , len2 ) + " " + curr . substring ( len2 ) ) ; q . push ( curr . substring ( 0 , len2 ) + " " + curr . substring ( len2 ) ) ; } else { let midChar = curr [ len2 ] ; q . push ( curr . substring ( 0 , len2 ) + midChar + curr . substring ( len2 ) ) ; } } return - 1 ; } let n = 9 ; document . write ( getNthNumber ( n ) ) ;
Check if binary representation of a given number and its complement are anagram | An efficient javascript program to check if binary representations of a number and it 's complement are anagram. ; Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code
var longSize = 8 ; var ULL_SIZE = 8 * longSize ; function bit_anagram_check ( a ) { var ans = a . toString ( 2 ) . split ( ' ' ) . join ( ' ' ) . length == ( ULL_SIZE >> 1 ) ? 1 : 0 ; return ans ; } var a = 4294967295 ; document . write ( bit_anagram_check ( a ) ) ;
Sum of numbers with exactly 2 bits set | To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver Code
function findSum ( n ) { let sum = 0 ; for ( let i = 1 ; 1 << i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { let num = ( 1 << i ) + ( 1 << j ) ; if ( num <= n ) sum += num ; } } return sum ; } let n = 10 ; document . write ( findSum ( n ) ) ;
Position of rightmost different bit | function to find rightmost different bit in two numbers . ; Driver code
function posOfRightMostDiffBit ( m , n ) { return parseInt ( Math . floor ( Math . log10 ( Math . pow ( m ^ n , 2 ) ) ) , 10 ) + 2 ; } let m = 52 , n = 4 ; document . write ( " " + posOfRightMostDiffBit ( m , n ) ) ;
Set the K | function to set the kth bit ; kth bit of n is being set by this operation ; Driver code
function setKthBit ( n , k ) { return ( ( 1 << k ) n ) ; } let n = 10 , k = 2 ; document . write ( " " + setKthBit ( n , k ) ) ;
Reverse an array without using subtract sign â €˜ | Function to reverse array ; Trick to assign - 1 to a variable ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value ; Drivers code ; print the reversed array
function reversearray ( arr , n ) { let x = parseInt ( - 2147483648 / 2147483647 , 10 ) ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) ; i ++ ) { let temp = arr [ i ] ; arr [ i ] = arr [ n + ( x * i ) + x ] ; arr [ n + ( x * i ) + x ] = temp ; } } let arr = [ 5 , 3 , 7 , 2 , 1 , 6 ] ; let n = arr . length ; reversearray ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Reverse an array without using subtract sign â €˜ | Function to reverse array ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value Note : A - B = A + ~ B + 1 So n - i = n + ~ i + 1 then n - i - 1 = ( n + ~ i + 1 ) + ~ 1 + 1 ; Driver code ; print the reversed array
function reverseArray ( arr , n ) { for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) ; i ++ ) { swap ( arr , i , ( n + ~ i + 1 ) + ~ 1 + 1 ) ; } } function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } let arr = [ 5 , 3 , 7 , 2 , 1 , 6 ] ; let n = arr . length ; reverseArray ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Maximum XOR value of a pair from a range | method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver code
function maxXORInRange ( L , R ) { let LXR = L ^ R ; let msbPos = 0 ; while ( LXR > 0 ) { msbPos ++ ; LXR >>= 1 ; } let maxXOR = 0 ; let two = 1 ; while ( msbPos -- > 0 ) { maxXOR += two ; two <<= 1 ; } return maxXOR ; } let L = 8 ; let R = 20 ; document . write ( maxXORInRange ( L , R ) ) ;
Numbers whose bitwise OR and sum with N are equal | Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to N , whose bitwise OR and SUM with N are equal . ; count number of zero bit in N ; power of 2 to count ; Driver code
function CountZeroBit ( n ) { let count = 0 ; while ( n > 0 ) { if ( n % 2 != 0 ) count ++ ; n >>= 1 ; } return count ; } function CountORandSumEqual ( N ) { let count = CountZeroBit ( N ) ; return ( 1 << count ) ; } let N = 10 ; document . write ( CountORandSumEqual ( N ) ) ;
Count smaller numbers whose XOR with n produces greater value | Javascript program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Traverse bits from LSB ( least significant bit ) to MSB ; Initialize result ; If the current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver Code
function countNumbers ( n ) { let k = 0 ; let count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += ( Math . pow ( 2 , k ) ) ; k += 1 ; n >>= 1 ; } return count ; } let n = 11 ; document . write ( countNumbers ( n ) ) ;
Count all pairs with given XOR | Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty set that stores the visiting element of array . Refer below post for details of unordered_set https : www . geeksforgeeks . org / unorderd_set - stl - uses / ; If there exist an element in set s with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Make element visited ; return total count of pairs with XOR equal to x ; Driver code
function xorPairCount ( arr , n , x ) { let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( s . has ( x ^ arr [ i ] ) ) { result ++ ; } s . add ( arr [ i ] ) ; } return result ; } let arr = [ 5 , 4 , 10 , 15 , 7 , 6 ] ; let n = arr . length ; let x = 5 ; document . write ( " " + xorPairCount ( arr , n , x ) ) ;
Multiples of 4 ( An Interesting Method ) | Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Printing multiples of 4 using above method
function isMultipleOf4 ( n ) { if ( n == 1 ) return false ; let XOR = 0 ; for ( let i = 1 ; i <= n ; i ++ ) XOR = XOR ^ i ; return ( XOR == n ) ; } for ( let n = 0 ; n <= 42 ; n ++ ) document . write ( isMultipleOf4 ( n ) ? n : " " ) ;
Multiples of 4 ( An Interesting Method ) | Returns true if n is a multiple of 4. ; Printing multiples of 4 using above method
function isMultipleOf4 ( n ) { if ( n == 0 ) return true ; return ( ( ( n >> 2 ) << 2 ) == n ) ; } for ( let n = 0 ; n <= 42 ; n ++ ) if ( isMultipleOf4 ( n ) ) document . write ( n + " " ) ;
Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
function countSetBits ( x ) { let count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } function ceilLog2 ( x ) { let count = 0 ; x -- ; while ( x > 0 ) { x = x >> 1 ; count ++ ; } return count ; } function isBleak ( n ) { for ( let x = n - ceilLog2 ( n ) ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } if ( isBleak ( 3 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; if ( isBleak ( 4 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Count strings with consecutive 1 's | Returns count of n length binary strings with consecutive 1 's ; Count binary strings without consecutive 1 's.  See the approach discussed on be  ( http:goo.gl/p8A3sW ) ; Subtract a [ n - 1 ] + b [ n - 1 ] from 2 ^ n ; Driver Code
function countStrings ( n ) { let a = [ ] , b = [ ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } document . write ( countStrings ( 5 ) ) ;
How to swap two bits in a given integer ? | JavaScript code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver code
function swapBits ( n , p1 , p2 ) { n ^= 1 << p1 ; n ^= 1 << p2 ; return n ; } document . write ( " " + swapBits ( 28 , 0 , 3 ) ) ;
Maximum length sub | Function to return the maximum length of the required sub - array ; To store the maximum length for a valid subarray ; To store the count of contiguous similar elements for previous group and the current group ; If current element is equal to the previous element then it is a part of the same group ; Else update the previous group and start counting elements for the new group ; Update the maximum possible length for a group ; Return the maximum length of the valid subarray ; Driver code
function maxLength ( a , n ) { let maxLen = 0 ; let prev_cnt = 0 , curr_cnt = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) curr_cnt ++ ; else { prev_cnt = curr_cnt ; curr_cnt = 1 ; } maxLen = Math . max ( maxLen , Math . min ( prev_cnt , curr_cnt ) ) ; } return ( 2 * maxLen ) ; } let arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ] ; let n = arr . length ; document . write ( maxLength ( arr , n ) ) ;
LIFO ( Last | Pushing element on the top of the stack ; Popping element from the top of the stack ; Displaying element on the top of the stack ; Searching element in the stack ; Driver code
function stack_push ( stack ) { for ( var i = 0 ; i < 5 ; i ++ ) { stack . push ( i ) ; } return stack ; } function stack_pop ( stack ) { document . write ( " " ) ; for ( var i = 0 ; i < 5 ; i ++ ) { var y = parseInt ( stack [ stack . length - 1 ] ) ; stack . pop ( ) ; document . write ( y + " " ) ; } return stack ; } function stack_peek ( stack ) { var element = parseInt ( stack [ stack . length - 1 ] ) ; document . write ( " " + element + " " ) ; } function stack_search ( stack , element ) { var pos = - 1 , co = 0 ; while ( stack . length > 0 ) { co ++ ; if ( stack [ stack . length - 1 ] == element ) { pos = co ; break ; } stack . pop ( ) ; } if ( pos == - 1 ) document . write ( " " + " " ) ; else document . write ( " " + pos + " " ) ; } stack = [ ] ; stack = stack_push ( stack ) ; stack = stack_pop ( stack ) ; stack = stack_push ( stack ) ; stack_peek ( stack ) ; stack_search ( stack , 2 ) ; stack_search ( stack , 6 ) ;
Check if removing an edge can divide a Binary Tree in two halves | Javascript program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns true if there is an edge whose removal can divide the tree in two halves n is size of tree ; Base cases ; Check for root ; Check for rest of the nodes ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Now recursively check all nodes ; Driver code
class Node { constructor ( key ) { this . key = key ; this . left = this . right = null ; } } function count ( node ) { if ( node == null ) return 0 ; return count ( node . left ) + count ( node . right ) + 1 ; } function checkRec ( node , n ) { if ( node == null ) return false ; if ( count ( node ) == n - count ( node ) ) return true ; return checkRec ( node . left , n ) || checkRec ( node . right , n ) ; } function check ( node ) { let n = count ( node ) ; return checkRec ( node , n ) ; } let root = new Node ( 5 ) ; root . left = new Node ( 1 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 3 ) ; root . right . left = new Node ( 7 ) ; root . right . right = new Node ( 4 ) ; if ( check ( root ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Check if removing an edge can divide a Binary Tree in two halves | javascript program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns size of tree rooted with given root . It also set " res " as true if there is an edge whose removal divides tree in two halves . n is size of tree ; Base case ; Compute sizes of left and right children ; If required property is true for current node set " res " as true ; Return size ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Initialize result and recursively check all nodes ; Driver code
class Node { constructor ( key ) { this . key = key ; this . left = this . right = null ; } } class Res { constructor ( ) { this . res = false ; } } var root ; function count ( node ) { if ( node == null ) return 0 ; return count ( node . left ) + count ( node . right ) + 1 ; } function checkRec ( root , n , res ) { if ( root == null ) return 0 ; var c = checkRec ( root . left , n , res ) + 1 + checkRec ( root . right , n , res ) ; if ( c == n - c ) res . res = true ; return c ; } function check ( root ) { var n = count ( root ) ; res = new Res ( ) ; checkRec ( root , n , res ) ; return res . res ; } root = new Node ( 5 ) ; root . left = new Node ( 1 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 3 ) ; root . right . left = new Node ( 7 ) ; root . right . right = new Node ( 4 ) ; if ( check ( root ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Distance of nearest cell having 1 in a binary matrix | Javascript program to find distance of nearest cell having 1 in a binary matrix . ; Print the distance of nearest cell having 1 for each cell . ; Initialise the answer matrix with MaxValue . ; For each cell ; Traversing the whole matrix to find the minimum distance . ; If cell contain 1 , check for minimum distance . ; Printing the answer . ; Driven Program
let N = 3 ; let M = 4 ; function printDistance ( mat ) { let ans = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { ans [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { ans [ i ] [ j ] = Number . MAX_VALUE ; } } for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < M ; j ++ ) { for ( let k = 0 ; k < N ; k ++ ) for ( let l = 0 ; l < M ; l ++ ) { if ( mat [ k ] [ l ] == 1 ) ans [ i ] [ j ] = Math . min ( ans [ i ] [ j ] , Math . abs ( i - k ) + Math . abs ( j - l ) ) ; } } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) document . write ( ans [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 1 , 0 ] ] printDistance ( mat ) ;
Check if given Preorder , Inorder and Postorder traversals are of same tree | Javascript program to check if all three given traversals are of the same tree ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find index of value in arr [ start ... end ] The function assumes that value is present in in [ ] ; Recursive function to construct binary tree of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for cases where inorder and preorder do not form a tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; function to compare Postorder traversal on constructed tree and given Postorder ; first recur on left child ; now recur on right child ; Compare if data at current index in both Postorder traversals are same ; Driver program to test above functions ; build tree from given Inorder and Preorder traversals ; compare postorder traversal on constructed tree with given Postorder traversal ; If both postorder traversals are same
let preIndex = 0 ; class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function search ( arr , strt , end , value ) { for ( let i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } return - 1 ; } function buildTree ( In , pre , inStrt , inEnd ) { if ( inStrt > inEnd ) return null ; let tNode = new Node ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; let inIndex = search ( In , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( In , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( In , pre , inIndex + 1 , inEnd ) ; return tNode ; } function checkPostorder ( node , postOrder , index ) { if ( node == null ) return index ; index = checkPostorder ( node . left , postOrder , index ) ; index = checkPostorder ( node . right , postOrder , index ) ; if ( node . data == postOrder [ index ] ) index ++ ; else return - 1 ; return index ; } let inOrder = [ 4 , 2 , 5 , 1 , 3 ] ; let preOrder = [ 1 , 2 , 4 , 5 , 3 ] ; let postOrder = [ 4 , 5 , 2 , 3 , 1 ] ; let len = inOrder . length ; let root = buildTree ( inOrder , preOrder , 0 , len - 1 ) ; let index = checkPostorder ( root , postOrder , 0 ) ; if ( index == len ) document . write ( " " ) ; else document . write ( " " ) ;
Check if X can give change to every person in the Queue | Function to check if every person will get the change from X ; To count the 5 $ and 10 & notes ; Serve the customer in order ; Increase the number of 5 $ note by one ; decrease the number of note 5 $ and increase 10 $ note by one ; decrease 5 $ and 10 $ note by one ; decrease 5 $ note by three ; queue of customers with available notes . ; Calling function
function isChangeable ( notes , n ) { let fiveCount = 0 ; let tenCount = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; } else { if ( fiveCount > 0 && tenCount > 0 ) { fiveCount -- ; tenCount -- ; } else if ( fiveCount >= 3 ) { fiveCount -= 3 ; } else return 0 ; } } return 1 ; } let a = [ 5 , 5 , 5 , 10 , 20 ] ; let n = a . length ; if ( isChangeable ( a , n ) > 0 ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum distance between two occurrences of same element in array | Function to find maximum distance between equal elements ; Used to store element to first index mapping ; Traverse elements and find maximum distance between same occurrences with the help of map . ; If this is first occurrence of element , insert its index in map ; Else update max distance ; Driver program
function maxDistance ( arr , n ) { let map = new Map ( ) ; let max_dist = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! map . has ( arr [ i ] ) ) map . set ( arr [ i ] , i ) ; else max_dist = Math . max ( max_dist , i - map . get ( arr [ i ] ) ) ; } return max_dist ; } let arr = [ 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 4 , 2 ] ; let n = arr . length ; document . write ( maxDistance ( arr , n ) ) ;