codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of carry operations required to add two numbers | Function to count the number of carry operations ; Initialize the value of carry to 0 ; Counts the number of carry operations ; Initialize len_a and len_b with the sizes of strings ; Assigning the ascii value of the character ; Add both numbers / digits ; If sum > 0 , increment count and set carry to 1 ; Else , set carry to 0 ; Driver code
function count_carry ( a , b ) { let carry = 0 ; let count = 0 ; let len_a = a . length , len_b = b . length ; while ( len_a != 0 len_b != 0 ) { let x = 0 , y = 0 ; if ( len_a > 0 ) { x = a [ len_a - 1 ] - ' ' ; len_a -- ; } if ( len_b > 0 ) { y = b [ len_b - 1 ] - ' ' ; len_b -- ; } let sum = x + y + carry ; if ( sum >= 10 ) { carry = 1 ; count ++ ; } else carry = 0 ; } return count ; } let a = " " , b = " " ; let count = count_carry ( a , b ) ; if ( count == 0 ) document . write ( " " + " " ) ; else if ( count == 1 ) document . write ( " " + " " ) ; else document . write ( count ) ;
Check if a number is in given base or not | Javascript program to check if given number is in given base or not . ; Allowed base are till 16 ( Hexadecimal ) ; If bas is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver Code
function isInGivenBase ( str , bas ) { if ( bas > 16 ) return false ; else if ( bas <= 10 ) { for ( let i = 0 ; i < str . length ; i ++ ) if ( ! ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) < ( ' ' . charCodeAt ( ) + bas ) ) ) return false ; } else { for ( let i = 0 ; i < str . length ; i ++ ) if ( ! ( ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) < ( ' ' . charCodeAt ( ) + bas ) ) || ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) < ( ' ' . charCodeAt ( ) + bas - 10 ) ) ) ) return false ; } return true ; } let str = " " ; if ( isInGivenBase ( str , 16 ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Find indices of all occurrence of one string in other | JavaScript program to find indices of all occurrences of one String in other . ; Driver code
function printIndex ( str , s ) { var flag = false ; for ( var i = 0 ; i < str . length - s . length + 1 ; i ++ ) { if ( str . substring ( i , s . length + i ) == s ) { document . write ( i + " " ) ; flag = true ; } } if ( flag === false ) { document . write ( " " ) ; } } var str1 = " " ; var str2 = " " ; printIndex ( str1 , str2 ) ;
Maximum occurring character in an input string | Set | function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; to store the maximum occurring character ; length of ' str ' ; get frequency of each character of ' str ' ; for each character , where character is obtained by ( i + ' a ' ) check whether it is the maximum character so far and accodingly update ' result ' ; maximum occurring character ; Driver Code
function getMaxOccurringChar ( str ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } let max = - 1 ; let result = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ' ) { freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } } for ( let i = 0 ; i < 26 ; i ++ ) { if ( max < freq [ i ] ) { max = freq [ i ] ; result = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; } } return result ; } let str = " " . split ( " " ) ; document . write ( " " + getMaxOccurringChar ( str ) ) ;
Check for balanced parentheses in an expression | O ( 1 ) space | Function1 to match closing bracket ; Function1 to match opening bracket ; Function to check balanced parentheses ; helper variables ; Handling case of opening parentheses ; Handling case of closing parentheses ; If corresponding matching opening parentheses doesn 't  lie in given interval return 0 ; else continue ; If corresponding closing parentheses doesn 't lie in given interval  return 0 ; if found , now check for each opening and closing parentheses in this interval ; Driver Code
function matchClosing ( X , start , end , open , close ) { let c = 1 ; let i = start + 1 ; while ( i <= end ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i ++ ; } return i ; } function matchingOpening ( X , start , end , open , close ) { let c = - 1 ; let i = end - 1 ; while ( i >= start ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i -- ; } return - 1 ; } function isBalanced ( X , n ) { let i , j = 0 , k , x , start , end ; for ( i = 0 ; i < n ; i ++ ) { if ( X [ i ] == ' ' ) { j = matchClosing ( X , i , n - 1 , ' ' , ' ' ) ; } else if ( X [ i ] == ' ' ) { j = matchClosing ( X , i , n - 1 , ' ' , ' ' ) ; } else if ( X [ i ] == ' ' ) { j = matchClosing ( X , i , n - 1 , ' ' , ' ' ) ; } else { if ( X [ i ] == ' ' ) { j = matchingOpening ( X , 0 , i , ' ' , ' ' ) ; } else if ( X [ i ] == ' ' ) { j = matchingOpening ( X , 0 , i , ' ' , ' ' ) ; } else if ( X [ i ] == ' ' ) { j = matchingOpening ( X , 0 , i , ' ' , ' ' ) ; } if ( j < 0 j >= i ) { return false ; } continue ; } if ( j >= n j < 0 ) { return false ; } start = i ; end = j ; for ( k = start + 1 ; k < end ; k ++ ) { if ( X [ k ] == ' ' ) { x = matchClosing ( X , k , end , ' ' , ' ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ' ) { x = matchingOpening ( X , start , k , ' ' , ' ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' ' ) { x = matchClosing ( X , k , end , ' ' , ' ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ' ) { x = matchingOpening ( X , start , k , ' ' , ' ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' ' ) { x = matchClosing ( X , k , end , ' ' , ' ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ' ) { x = matchingOpening ( X , start , k , ' ' , ' ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } } } return true ; } let X = " " . split ( " " ) ; let n = 6 ; if ( isBalanced ( X , n ) ) document . write ( " " ) ; else document . write ( " " ) ; let Y = " " . split ( " " ) ; n = 7 ; if ( isBalanced ( Y , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sorting array with conditional swapping | Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code
function possibleToSort ( arr , n , str ) { let max_element = - 1 ; for ( let i = 0 ; i < str . length ; i ++ ) { max_element = Math . max ( max_element , arr [ i ] ) ; if ( str [ i ] == ' ' ) { if ( max_element > i + 1 ) return " " ; } } return " " ; } let arr = [ 1 , 2 , 5 , 3 , 4 , 6 ] ; let n = arr . Length ; let str = " " ; document . write ( possibleToSort ( arr , n , str ) ) ;
Find sub | Function to print indexes of substring with power as given power . ; Create an empty map ; Maintains sum of powers of characters so far . ; Add current character power to curr_power . ; If curr_power is equal to target power we found a substring starting from index 0 and ending at index i . ; If curr_power - power already exists in map then we have found a subarray with target power . ; If we reach here , then no substring exists . ; Driver Code
function findSubstring ( str , power ) { let map = new Map ( ) ; let curr_power = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { curr_power = curr_power + ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ) ; if ( curr_power == power ) { document . write ( " " + " " + i + " " + power + " " ) ; return ; } if ( map . has ( curr_power - power ) ) { document . write ( " " + ( map . get ( curr_power - power ) + 1 ) + " " + i + " " + power + " " ) ; return ; } map . set ( curr_power , i ) ; } document . write ( " " ) ; } let str = " " ; let power = 36 ; findSubstring ( str , power ) ;
Prime String | Function that checks if sum is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code
function isPrimeString ( str ) { let len = str . length , n = 0 ; for ( let i = 0 ; i < len ; i ++ ) { n += str [ i ] . charCodeAt ( ) ; } if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( let i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } let str = " " ; if ( isPrimeString ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Ways to split string such that each partition starts with distinct character | Returns the number of we can split the string ; Finding the frequency of each character . ; making frequency of first character of string equal to 1. ; Finding the product of frequency of occurrence of each character . ; Driver Code
function countWays ( s ) { let count = new Array ( 26 ) ; count . fill ( 0 ) ; for ( let i = 0 ; i < s . length ; i ++ ) count [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; count [ s [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = 1 ; let ans = 1 ; for ( let i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; } let s = " " ; document . write ( countWays ( s ) ) ;
Lexicographically next greater string using same character set | function to print output ; to store unique characters of the string ; to check uniqueness ; if mp [ s [ i ] ] = 0 then it is first time ; sort the unique characters ; simply add n - k smallest characters ; return ; end the program ; searching the first character left of index k and not equal to greatest character of the string ; finding the just next greater character than s [ i ] ; suffix with smallest character ; if we reach here then all indices to the left of k had the greatest character ; Driver code ; Function call
function lexoString ( s , k ) { var n = s . length ; var v = [ ] ; var mp = { } ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! mp . hasOwnProperty ( s [ i ] ) ) { mp [ s [ i ] ] = 1 ; v . push ( s [ i ] ) ; } } v . sort ( ( a , b ) => a - b ) ; if ( k > n ) { document . write ( s . join ( " " ) ) ; for ( var i = n ; i < k ; i ++ ) { document . write ( v [ 0 ] ) ; } } for ( var i = k - 1 ; i >= 0 ; i -- ) { if ( s [ i ] !== v [ v . length - 1 ] ) { for ( var j = 0 ; j < i ; j ++ ) { document . write ( s [ j ] ) ; } for ( var j = 0 ; j < v . length ; j ++ ) { if ( v [ j ] > s [ i ] ) { document . write ( v [ j ] ) ; break ; } } for ( var j = i + 1 ; j < k ; j ++ ) { document . write ( v [ 0 ] ) ; } return ; } } document . write ( " " + " " + k + " " ) ; } var s = " " ; var k = 3 ; lexoString ( s . split ( " " ) , k ) ;
Number of palindromic permutations | Set 1 | Javascript program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code
let MAX = 256 ; function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function countPalinPermutations ( str ) { let n = str . length ; let freq = new Array ( MAX ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) freq [ str [ i ] . charCodeAt ( ) ] ++ ; let res = fact ( n / 2 ) ; let oddFreq = false ; for ( let i = 0 ; i < MAX ; i ++ ) { let half = freq [ i ] / 2 ; if ( freq [ i ] % 2 != 0 ) { if ( oddFreq == true ) return 0 ; oddFreq = true ; } res = res / fact ( half ) ; } return res ; } let str = " " ; document . write ( countPalinPermutations ( str ) ) ;
Substrings starting with vowel and ending with consonants and vice versa | Returns true if ch is vowel ; Function to check consonant ; in case of empty string , we can 't fullfill the  required condition, hence we return ans as 0. ; co [ i ] is going to store counts of consonants from str [ len - 1 ] to str [ i ] . vo [ i ] is going to store counts of vowels from str [ len - 1 ] to str [ i ] . ; Counting consonants and vowels from end of string . ; Now we traverse string from beginning ; If vowel , then count of substrings starting with str [ i ] is equal to count of consonants after it . ; If consonant , then count of substrings starting with str [ i ] is equal to count of vowels after it . ; Driver code
function isVowel ( ch ) { return ( ch === " " ch === " " ch === " " ch === " " ch === " " ) ; } function isCons ( ch ) { return ( ch !== " " && ch !== " " && ch !== " " && ch !== " " && ch !== " " ) ; } function countSpecial ( str ) { var len = str . length ; if ( len == 0 ) return 0 ; var co = new Array ( len + 1 ) . fill ( 0 ) ; var vo = new Array ( len + 1 ) . fill ( 0 ) ; if ( isCons ( str [ len - 1 ] ) === true ) co [ len - 1 ] = 1 ; else vo [ len - 1 ] = 1 ; for ( var i = len - 2 ; i >= 0 ; i -- ) { if ( isCons ( str [ i ] ) === true ) { co [ i ] = co [ i + 1 ] + 1 ; vo [ i ] = vo [ i + 1 ] ; } else { co [ i ] = co [ i + 1 ] ; vo [ i ] = vo [ i + 1 ] + 1 ; } } var ans = 0 ; for ( var i = 0 ; i < len ; i ++ ) { if ( isVowel ( str [ i ] ) ) ans = ans + co [ i + 1 ] ; else ans = ans + vo [ i + 1 ] ; } return parseInt ( ans ) ; } var str = " " ; document . write ( countSpecial ( str . split ( " " ) ) ) ;
Convert the string into palindrome string by changing only one character | Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Function
function checkPalindrome ( str ) { let n = str . length ; let count = 0 ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) ; ++ i ) if ( str [ i ] != str [ n - i - 1 ] ) ++ count ; return ( count <= 1 ) ; } let str = " " ; if ( checkPalindrome ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Counting even decimal value substrings in a binary string | function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generate by this character produce even decimal value . ; Driver code
function evenDecimalValue ( str , n ) { let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { result += ( n - i ) ; } } return result ; } let str = " " ; let n = 5 ; document . write ( evenDecimalValue ( str , n ) ) ;
Number of substrings with odd decimal value in a binary string | Function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's before ith index ; Store count of 1 's before  i-th index ; Variable to store answer ; Traverse the string reversely to calculate number of odd substrings before i - th index ; Driver code
function countSubstr ( s ) { let n = s . length ; let auxArr = new Array ( n ) ; if ( s [ 0 ] == ' ' ) auxArr [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) auxArr [ i ] = auxArr [ i - 1 ] + 1 ; else auxArr [ i ] = auxArr [ i - 1 ] ; } let count = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) if ( s [ i ] == ' ' ) count += auxArr [ i ] ; return count ; } let s = " " ; document . write ( countSubstr ( s ) ) ;
Check whether second string can be formed from characters of first string | Javascript program to check whether second string can be formed from first string ; Create a count array and count frequencies characters in str1 . ; Now traverse through str2 to check if every character has enough counts ; Driver Code
let MAX = 256 ; function canMakeStr2 ( str1 , str2 ) { let count = new Array ( MAX ) ; count . fill ( 0 ) ; for ( let i = 0 ; i < str1 . length ; i ++ ) count [ str1 [ i ] ] ++ ; for ( let i = 0 ; i < str2 . length ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i ] ] -- ; } return true ; } let str1 = " " ; let str2 = " " ; if ( canMakeStr2 ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Position of robot after given movements | function to find final position of robot after the complete movement ; traverse the instruction string ' move ' ; for each movement increment its respective counter ; required final position of robot ; Driver code
function finalPosition ( move ) { let l = move . length ; let countUp = 0 , countDown = 0 ; let countLeft = 0 , countRight = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( move [ i ] == ' ' ) countUp ++ ; else if ( move [ i ] == ' ' ) countDown ++ ; else if ( move [ i ] == ' ' ) countLeft ++ ; else if ( move [ i ] == ' ' ) countRight ++ ; } document . write ( " " + ( countRight - countLeft ) + " " + ( countUp - countDown ) + " " ) ; } let move = " " ; finalPosition ( move ) ;
Length of longest balanced parentheses prefix | Return the length of longest balanced parentheses prefix . ; Traversing the string . ; If open bracket add 1 to sum . ; If closed bracket subtract 1 from sum ; if first bracket is closing bracket then this condition would help ; If sum is 0 , store the index value . ; Driven Program
function maxbalancedprefix ( str , n ) { var sum = 0 ; var maxi = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) sum += 1 ; else sum -= 1 ; if ( sum < 0 ) break ; if ( sum == 0 ) maxi = i + 1 ; } return maxi ; } var str = " " ; var n = str . length ; document . write ( maxbalancedprefix ( str , n ) ) ;
Minimum cost to convert string into palindrome | Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code
function cost ( str ) { let len = str . length ; let res = 0 ; for ( let i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) { if ( str [ i ] != str [ j ] ) { res += Math . min ( str [ i ] . charCodeAt ( ) , str [ j ] . charCodeAt ( ) ) - ' ' . charCodeAt ( ) + 1 ; } } return res ; } let str = " " ; document . write ( cost ( str ) ) ;
Encoding a word into Pig Latin | js program to encode a word to a Pig Latin . ; the index of the first vowel is stored . ; Pig Latin is possible only if vowels is present ; Take all characters after index ( including index ) . Append all characters which are before index . Finally append " ay " ; Driver code
function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function pigLatin ( s ) { let len = s . length ; let index = - 1 ; for ( let i = 0 ; i < len ; i ++ ) { if ( isVowel ( s [ i ] ) ) { index = i ; break ; } } if ( index == - 1 ) return " " ; return s . substring ( index ) + s . substring ( 0 , index ) + " " ; } str = pigLatin ( " " ) ; if ( str == " " ) document . write ( " " + " " + " " ) ; else document . write ( str ) ;
Possibility of a word from a given set of characters | Javascript program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; driver program
let MAX_CHAR = 256 ; function isPresent ( s , q ) { let freq = new Array ( MAX_CHAR ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < s . length ; i ++ ) freq [ s [ i ] ] ++ ; for ( let i = 0 ; i < q . length ; i ++ ) { freq [ q [ i ] ] -- ; if ( freq [ q [ i ] ] < 0 ) return false ; } return true ; } let s = " " ; let q = " " ; if ( isPresent ( s , q ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum reduce operations to convert a given string into a palindrome | Returns count of minimum character reduce operations to make palindrome . ; Compare every character of first half with the corresponding character of second half and add difference to result . ; Driver code
function countReduce ( str ) { let n = str . length ; let res = 0 ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) ; i ++ ) res += Math . abs ( str [ i ] . charCodeAt ( ) - str [ n - i - 1 ] . charCodeAt ( ) ) ; return res ; } let str = " " ; document . write ( countReduce ( str ) ) ;
Minimal operations to make a number magical | Function to calculate the minimal changes ; Maximum digits that can be changed ; Nested loops to generate all 6 digit numbers ; Counter to count the number of change required ; If first digit is equal ; If 2 nd digit is equal ; If 3 rd digit is equal ; If 4 th digit is equal ; If 5 th digit is equal ; If 6 th digit is equal ; Checks if less then the previous calculate changes ; Returns the answer ; Number stored in string ; Prints the minimum operations
function calculate ( s ) { let ans = 6 ; for ( let i = 0 ; i < 10 ; ++ i ) { for ( let j = 0 ; j < 10 ; ++ j ) { for ( let k = 0 ; k < 10 ; ++ k ) { for ( let l = 0 ; l < 10 ; ++ l ) { for ( let m = 0 ; m < 10 ; ++ m ) { for ( let n = 0 ; n < 10 ; ++ n ) { if ( i + j + k == l + m + n ) { let c = 0 ; if ( i != s [ 0 ] - ' ' ) c ++ ; if ( j != s [ 1 ] - ' ' ) c ++ ; if ( k != s [ 2 ] - ' ' ) c ++ ; if ( l != s [ 3 ] - ' ' ) c ++ ; if ( m != s [ 4 ] - ' ' ) c ++ ; if ( n != s [ 5 ] - ' ' ) c ++ ; if ( c < ans ) ans = c ; } } } } } } } return ans ; } let s = " " ; document . write ( calculate ( s ) ) ;
Check if a two character string can be made using given words | Function to check if str can be made using given words ; If str itself is present ; Match first character of str with second of word and vice versa ; If both characters found . ; Driver Code
function makeAndCheckString ( words , str ) { let n = words . length ; let first = false , second = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( words [ i ] == str ) return true ; if ( str [ 0 ] == words [ i ] [ 1 ] ) first = true ; if ( str [ 1 ] == words [ i ] [ 0 ] ) second = true ; if ( first && second ) return true ; } return false ; } let str = " " ; let words = [ " " , " " , " " , " " ] ; if ( makeAndCheckString ( words , str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print N | Function to get the binary representation of the number N ; loop for each bit ; generate numbers in the range of ( 2 ^ N ) - 1 to 2 ^ ( N - 1 ) inclusive ; longest prefix check ; if counts of 1 is greater than counts of zero ; do sub - prefixes check ; Driver code ; Function call
function getBinaryRep ( N , num_of_bits ) { let r = " " ; num_of_bits -- ; while ( num_of_bits >= 0 ) { if ( ( N & ( 1 << num_of_bits ) ) != 0 ) r += " " ; else r += " " ; num_of_bits -- ; } return r ; } function NBitBinary ( N ) { let r = [ ] ; let first = 1 << ( N - 1 ) ; let last = first * 2 ; for ( let i = last - 1 ; i >= first ; -- i ) { let zero_cnt = 0 ; let one_cnt = 0 ; let t = i ; let num_of_bits = 0 ; while ( t > 0 ) { if ( ( t & 1 ) != 0 ) one_cnt ++ ; else zero_cnt ++ ; num_of_bits ++ ; t = t >> 1 ; } if ( one_cnt >= zero_cnt ) { let all_prefix_match = true ; let msk = ( 1 << num_of_bits ) - 2 ; let prefix_shift = 1 ; while ( msk > 0 ) { let prefix = ( msk & i ) >> prefix_shift ; let prefix_one_cnt = 0 ; let prefix_zero_cnt = 0 ; while ( prefix > 0 ) { if ( ( prefix & 1 ) != 0 ) prefix_one_cnt ++ ; else prefix_zero_cnt ++ ; prefix = prefix >> 1 ; } if ( prefix_zero_cnt > prefix_one_cnt ) { all_prefix_match = false ; break ; } prefix_shift ++ ; msk = msk & ( msk << 1 ) ; } if ( all_prefix_match ) { r . push ( getBinaryRep ( i , num_of_bits ) ) ; } } } return r ; } let n = 4 ; let results = NBitBinary ( n ) ; for ( let i = 0 ; i < results . length ; ++ i ) document . write ( results [ i ] + " " ) ; document . write ( " " ) ;