codeparrot/xlcost-text-to-code huggingface dataset:
Smallest Palindrome after replacement | Utility method to check str is possible palindrome after ignoring ; If both left and right character are not dot and they are not equal also , then it is not possible to make this string a palindrome ; Returns lexicographically smallest palindrome string , if possible ; loop through character of string ; if one of character is dot , replace dot with other character ; if both character are dot , then replace them with smallest character ' a ' ; return the result ; Driver code
function isPossiblePalindrome ( str ) { let n = str . length ; for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) { if ( str [ i ] != ' ' && str [ n - i - 1 ] != ' ' && str [ i ] != str [ n - i - 1 ] ) return false ; } return true ; } function smallestPalindrome ( str ) { if ( ! isPossiblePalindrome ( str ) ) document . write ( " " ) ; let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { if ( str [ n - i - 1 ] != ' ' ) str [ i ] = str [ n - i - 1 ] ; else str [ i ] = str [ n - i - 1 ] = ' ' ; } } for ( let i = 0 ; i < n ; i ++ ) document . write ( str [ i ] + " " ) ; } let str = " " ; let s = str . split ( " " ) ; smallestPalindrome ( s ) ;
Number of flips to make binary string alternate | Set 1 | Utility method to flip a character ; Utility method to get minimum flips when alternate string starts with expected char ; if current character is not expected , increase flip count ; flip expected character each time ; method return minimum flip to make binary string alternate ; return minimum of following two 1 ) flips when alternate string starts with 0 2 ) flips when alternate string starts with 1 ; Driver code to test above method
function flip ( ch ) { return ( ch == ' ' ) ? ' ' : ' ' ; } function getFlipWithStartingCharcter ( str , expected ) { let flipCount = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str . charAt ( i ) != expected ) flipCount ++ ; expected = flip ( expected ) ; } return flipCount ; } function minFlipToMakeStringAlternate ( str ) { return Math . min ( getFlipWithStartingCharcter ( str , ' ' ) , getFlipWithStartingCharcter ( str , ' ' ) ) ; } let str = " " ; document . write ( minFlipToMakeStringAlternate ( str ) ) ;
Remainder with 7 for large numbers | Function which returns Remainder after dividing the number by 7 ; This series is used to find remainder with 7 ; Index of next element in series ; Traverse num from end ; Find current digit of nun ; Add next term to result ; Move to next term in series ; Make sure that result never goes beyond 7. ; Make sure that remainder is positive ; Driver program
function remainderWith7 ( num ) { series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ] var series_index = 0 ; for ( var i = num . length - 1 ; i >= 0 ; i -- ) { var digit = num [ i ] - ' ' ; result += digit * series [ series_index ] ; series_index = ( series_index + 1 ) % 6 ; result %= 7 ; } if ( result < 0 ) result = ( result + 7 ) % 7 ; return result ; } var str = " " ; document . write ( " " + remainderWith7 ( str ) ) ;
Check if a string can become empty by recursively deleting a given sub | Returns true if str can be made empty by recursively removing sub_str ; idx : to store starting index of sub - string found in the original string ; Erasing the found sub - string from the original string ; Driver code
function canBecomeEmpty ( str , sub_str ) { while ( str . length > 0 ) { let idx = str . indexOf ( sub_str ) ; if ( idx == - 1 ) { break ; } str = str . replace ( sub_str , " " ) ; } return ( str . length == 0 ) ; } let str = " " , sub_str = " " ; if ( canBecomeEmpty ( str , sub_str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Perfect reversible string | This function basically checks if string is palindrome or not ; Iterate from left and right ; Driver Code
function isReversible ( str ) { var i = 0 , j = str . length - 1 ; while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } var str = " " ; if ( isReversible ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if string follows order of characters defined by a pattern or not | Set 3 | Javascript program to find if a string follows order defined by a given pattern . ; Returns true if characters of str follow order defined by a given ptr . ; Initialize all orders as - 1 ; Assign an order to pattern characters according to their appearance in pattern ; give the pattern characters order ; increment the order ; Now one by check if string characters follow above order ; If order of this character is less than order of previous , return false . ; Update last_order for next iteration ; return that str followed pat ; Driver code
let CHAR_SIZE = 256 ; function checkPattern ( str , pat ) { let label = new Array ( CHAR_SIZE ) ; for ( let i = 0 ; i < CHAR_SIZE ; i ++ ) label [ i ] = - 1 ; let order = 1 ; for ( let i = 0 ; i < pat . length ; i ++ ) { label [ pat [ i ] . charCodeAt ( 0 ) ] = order ; order ++ ; } let last_order = - 1 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( label [ str [ i ] . charCodeAt ( 0 ) ] != - 1 ) { if ( label [ str [ i ] . charCodeAt ( 0 ) ] < last_order ) return false ; last_order = label [ str [ i ] . charCodeAt ( 0 ) ] ; } } return true ; } let str = " " ; let pattern = " " ; document . write ( checkPattern ( str , pattern ) ) ;
Check if string follows order of characters defined by a pattern or not | Set 2 | Javascript program to check if characters of a string follow pattern defined by given pattern . ; Insert all characters of pattern in a hash set , ; Build modified string ( string with characters only from pattern are taken ) ; Remove more than one consecutive occurrences of pattern characters from modified string . ; After above modifications , the length of modified string must be same as pattern length ; And pattern characters must also be same as modified string characters ; Driver program
function followsPattern ( str , pattern ) { let patternSet = new Set ( ) ; for ( let i = 0 ; i < pattern . length ; i ++ ) patternSet . add ( pattern [ i ] ) ; let modifiedString = ( str ) . split ( " " ) ; for ( let i = str . length - 1 ; i >= 0 ; i -- ) if ( ! patternSet . has ( modifiedString [ i ] ) ) modifiedString . splice ( i , 1 ) ; for ( let i = modifiedString . length - 1 ; i > 0 ; i -- ) if ( modifiedString [ i ] == modifiedString [ i - 1 ] ) modifiedString . splice ( i , 1 ) ; if ( pattern . length != modifiedString . length ) return false ; for ( let i = 0 ; i < pattern . length ; i ++ ) if ( pattern [ i ] != modifiedString [ i ] ) return false ; return true ; } let str = " " ; let pattern = " " ; document . write ( " " + followsPattern ( str , pattern ) + " " ) ; str = " " ; pattern = " " ; document . write ( " " + followsPattern ( str , pattern ) + " " ) ; str = " " ; pattern = " " ; document . write ( " " + followsPattern ( str , pattern ) + " " ) ; str = " " ; pattern = " " ; document . write ( " " + followsPattern ( str , pattern ) + " " ) ;
Find k 'th character of decrypted string | Set 1 | Function to find K 'th character in  Encoded String ; expand string variable is used to store final string after decompressing string str ; let temp = " " ; Current substring let freq = 0 ; Count of current substring ; read characters until you find a number or end of string ; push character in temp ; read number for how many times string temp will be repeated in decompressed string ; generating frequency of temp ; now append string temp into expand equal to its frequency ; this condition is to handle the case when string str is ended with alphabets not with numeric value ; Driver program to test the string
function encodedChar ( str , k ) { let expand = " " ; for ( let i = 0 ; i < str . length ; ) { while ( i < str . length && str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { temp += str [ i ] ; i ++ ; } while ( i < str . length && str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { freq = freq * 10 + str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; i ++ ; } for ( let j = 1 ; j <= freq ; j ++ ) expand += temp ; } if ( freq == 0 ) expand += temp ; return expand [ k - 1 ] ; } let str = " " ; let k = 21 ; document . write ( encodedChar ( str , k ) ) ;
Converting Decimal Number lying between 1 to 3999 to Roman Numerals | Function to calculate roman equivalent ; storing roman values of digits from 0 - 9 when placed at different places ; Converting to roman ; Driver program to test above function
function intToRoman ( num ) { let m = [ " " , " " , " " , " " ] ; let c = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let x = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let i = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let thousands = m [ Math . floor ( num / 1000 ) ] ; let hundereds = c [ Math . floor ( ( num % 1000 ) / 100 ) ] ; let tens = x [ Math . floor ( ( num % 100 ) / 10 ) ] ; let ones = i [ num % 10 ] ; let ans = thousands + hundereds + tens + ones ; return ans ; } let number = 3549 ; document . write ( intToRoman ( number ) ) ;
Remove extra spaces from a string | JavaScript Program to remove extra spaces from a string
var str = " " ; document . write ( str . replace ( " \\ " , " " ) . trim ( ) ) ;
Lower case to upper case | Converts a string to uppercase ; Driver code
function to_upper ( In ) { let n = In . length ; for ( let i = 0 ; i < In . length ; i ++ ) if ( ' ' <= In [ i ] && In [ i ] <= ' ' ) In [ i ] = String . fromCharCode ( In [ i ] . charCodeAt ( 0 ) & ( ~ ( 1 << 5 ) ) ) ; return ( In ) . join ( " " ) ; } let str = " " ; document . write ( to_upper ( str . split ( " " ) ) ) ;
Longest Common Prefix using Divide and Conquer Algorithm | A Utility Function to find the common prefix between strings - str1 and str2 ; A Divide and Conquer based function to find the longest common prefix . This is similar to the merge sort technique ; Same as ( low + high ) / 2 , but avoids overflow for large low and high ; Driver code
function commonPrefixUtil ( str1 , str2 ) { let result = " " ; let n1 = str1 . length , n2 = str2 . length ; for ( let i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) { break ; } result += str1 [ i ] ; } return ( result ) ; } function commonPrefix ( arr , low , high ) { if ( low == high ) { return ( arr [ low ] ) ; } if ( high > low ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; let str1 = commonPrefix ( arr , low , mid ) ; let str2 = commonPrefix ( arr , mid + 1 , high ) ; return ( commonPrefixUtil ( str1 , str2 ) ) ; } return null ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; let ans = commonPrefix ( arr , 0 , n - 1 ) ; if ( ans . length != 0 ) { document . write ( " " + ans ) ; } else { document . write ( " " ) ; }
Longest Common Prefix using Word by Word Matching | A Utility Function to find the common prefix between strings - str1 and str2 ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; Driver program to test above function
function commonPrefixUtil ( str1 , str2 ) { let result = " " ; let n1 = str1 . length , n2 = str2 . length ; for ( let i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) { break ; } result += str1 [ i ] ; } return ( result ) ; } function commonPrefix ( arr , n ) { let prefix = arr [ 0 ] ; for ( let i = 1 ; i <= n - 1 ; i ++ ) { prefix = commonPrefixUtil ( prefix , arr [ i ] ) ; } return ( prefix ) ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; let ans = commonPrefix ( arr , n ) ; if ( ans . length > 0 ) { document . write ( " " , ans ) ; } else { document . write ( " " ) ; }
1 ' s ▁ and ▁ 2' s complement of a Binary Number | Returns '0' for '1' and '1' for '0' ; Print 1 ' s ▁ and ▁ 2' s complement of binary number represented by " bin " ; for ones complement flip every bit ; for two 's complement go from right to left in  ones complement and if we get 1 make, we make  them 0 and keep going left when we get first  0, make that 1 and go out of loop ; If No break : all are 1 as in 111 or 11111 ; in such case , add extra 1 at beginning ; Driver program
function flip ( c ) { return ( c == ' ' ) ? ' ' : ' ' ; } function printOneAndTwosComplement ( bin ) { var n = bin . length ; var i ; var ones , twos ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) ones += flip ( bin [ i ] ) ; twos = ones ; twos = twos . split ( ' ' ) for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ones [ i ] == ' ' ) twos [ i ] = ' ' ; else { twos [ i ] = ' ' ; break ; } } twos = twos . join ( ' ' ) if ( i == - 1 ) twos = ' ' + twos ; document . write ( " " + ones + " " ) ; document . write ( " " + twos + " " ) ; } var bin = " " ; printOneAndTwosComplement ( bin ) ;
Print Concatenation of Zig | Function for zig - zag Concatenation ; Check is n is less or equal to 1 ; Iterate rowNum from 0 to n - 1 ; Iterate i till s . length ( ) - 1 ; Check is rowNum is 0 or n - 1 ; Driver Code
function zigZagConcat ( s , n ) { if ( n <= 1 ) { return s ; } let result = [ ] ; for ( let rowNum = 0 ; rowNum < n ; rowNum ++ ) { let i = rowNum ; let up = true ; while ( i < s . length ) { result . push ( s [ i ] ) ; if ( rowNum == 0 rowNum == n - 1 ) { i += ( 2 * n - 2 ) ; } else { if ( up ) { i += ( 2 * ( n - rowNum ) - 2 ) ; } else { i += rowNum * 2 ; } up ^= true ; } } } return result . join ( " " ) ; } let str = " " ; let n = 3 ; document . write ( zigZagConcat ( str , n ) ) ;
Print string of odd length in ' X ' format | Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; driver code
function pattern ( str , len ) { for ( var i = 0 ; i < len ; i ++ ) { var j = len - 1 - i ; for ( var k = 0 ; k < len ; k ++ ) { if ( k == i k == j ) document . write ( str . charAt ( k ) ) ; else document . write ( " " ) ; } document . write ( ' ' ) ; } } var str = " " ; var len = str . length ; pattern ( str , len ) ;
Check if characters of a given string can be rearranged to form a palindrome | Javascript implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver program
let NO_OF_CHARS = 256 ; function canFormPalindrome ( str ) { let count = Array ( NO_OF_CHARS ) . fill ( 0 ) ; for ( let i = 0 ; i < str . length ; i ++ ) count [ str [ i ] . charCodeAt ( ) ] ++ ; let odd = 0 ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) == 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } if ( canFormPalindrome ( " " ) ) document . write ( " " ) ; else document . write ( " " ) ; if ( canFormPalindrome ( " " ) ) document . write ( " " ) ; else document . write ( " " ) ;
Generate n | This function generates all n bit Gray codes and prints the generated codes ; Base case ; Recursive case ; Append 0 to the first half ; Append 1 to the second half ; Function to generate the Gray code of N bits ; print contents of arr ; Driver Code
function generateGray ( n ) { if ( n <= 0 ) { let temp = [ " " ] ; return temp ; } if ( n == 1 ) { let temp = [ " " , " " ] ; return temp ; } let recAns = generateGray ( n - 1 ) ; let mainAns = [ ] ; for ( let i = 0 ; i < recAns . length ; i ++ ) { let s = recAns [ i ] ; mainAns . push ( " " + s ) ; } for ( let i = recAns . length - 1 ; i >= 0 ; i -- ) { let s = recAns [ i ] ; mainAns . push ( " " + s ) ; } return mainAns ; } function generateGrayarr ( n ) { let arr = [ ] ; arr = generateGray ( n ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } generateGrayarr ( 3 ) ;
Check whether two strings are anagram of each other | JAVAscript program to check if two strings are anagrams of each other ; function to check whether two strings are anagram of each other ; Create 2 count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; Compare count arrays ; Driver code ; Function call
let NO_OF_CHARS = 256 ; function areAnagram ( str1 , str2 ) { let count1 = new Array ( NO_OF_CHARS ) ; let count2 = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { count1 [ i ] = 0 ; count2 [ i ] = 0 ; } let i ; for ( i = 0 ; i < str1 . length && i < str2 . length ; i ++ ) { count1 [ str1 [ i ] . charCodeAt ( 0 ) ] ++ ; count2 [ str1 [ i ] . charCodeAt ( 0 ) ] ++ ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } let str1 = ( " " ) . split ( " " ) ; let str2 = ( " " ) . split ( " " ) ; if ( areAnagram ( str1 , str2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Find the smallest window in a string containing all characters of another string | javascript program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; Check if string ' s ▁ length ▁ is ▁ ▁ less ▁ than ▁ pattern ' s length . If yes then no such window can exist ; Store occurrence ofs characters of pattern ; Start traversing the string Count of characters ; Count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ ▁ with ▁ pattern ' s char then increment count ; If all the characters are matched ; Try to minimize the window ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver Method
var no_of_chars = 256 ; function findSubString ( str , pat ) { var len1 = str . length ; var len2 = pat . length ; if ( len1 < len2 ) { document . write ( " " ) ; return " " ; } var hash_pat = Array . from ( { length : no_of_chars } , ( _ , i ) => 0 ) ; var hash_str = Array . from ( { length : no_of_chars } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < len2 ; i ++ ) hash_pat [ pat . charAt ( i ) . charCodeAt ( 0 ) ] ++ ; var start = 0 , start_index = - 1 , min_len = Number . MAX_VALUE ; var count = 0 ; for ( var j = 0 ; j < len1 ; j ++ ) { hash_str [ str . charAt ( j ) . charCodeAt ( 0 ) ] ++ ; if ( hash_str [ str . charAt ( j ) . charCodeAt ( 0 ) ] <= hash_pat [ str . charAt ( j ) . charCodeAt ( 0 ) ] ) count ++ ; if ( count == len2 ) { while ( hash_str [ str . charAt ( start ) . charCodeAt ( 0 ) ] > hash_pat [ str . charAt ( start ) . charCodeAt ( 0 ) ] || hash_pat [ str . charAt ( start ) . charCodeAt ( 0 ) ] == 0 ) { if ( hash_str [ str . charAt ( start ) . charCodeAt ( 0 ) ] > hash_pat [ str . charAt ( start ) . charCodeAt ( 0 ) ] ) hash_str [ str . charAt ( start ) . charCodeAt ( 0 ) ] -- ; start ++ ; } var len_window = j - start + 1 ; if ( min_len > len_window ) { min_len = len_window ; start_index = start ; } } } if ( start_index == - 1 ) { document . write ( " " ) ; return " " ; } return str . substring ( start_index , start_index + min_len ) ; } var str = " " ; var pat = " " ; document . write ( " " + findSubString ( str , pat ) ) ;
Remove duplicates from a given string | JavaScript program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
function removeDuplicate ( str , n ) { var index = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var j ; for ( j = 0 ; j < i ; j ++ ) { if ( str [ i ] == str [ j ] ) { break ; } } if ( j == i ) { str [ index ++ ] = str [ i ] ; } } return str . join ( " " ) . slice ( str , index ) ; } var str = " " . split ( " " ) ; var n = str . length ; document . write ( removeDuplicate ( str , n ) ) ;
Print all Knight 's tour possible from a starting point on NxN chessboard | Stores the 8 possible combinations of moves that the knight can follow ; Function to find if ( i , j ) is a valid cell for the knight to move and it exists within the chessboard ; Stores whether there exist any valid path ; Recursive function to iterate through all the paths that the knight can follow ; Mark the current square of the chessboard ; If the number of visited squares are equal to the total number of sqares ; Prlet the current state of ChessBoard ; Backtrack to the previous move ; Iterate through all the eight possible moves for a knight ; Stores the new position of the knight after a move ; If the new position is a valid position recursively call for the next move ; Backtrack to the previous move ; Driver Code ; If no valid sequence of moves exist
let DirX = [ 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 ] ; let DirY = [ 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 ] ; function isSafe ( i , j , n , Board ) { return i >= 0 && j >= 0 && i < n && j < n && Board [ i ] [ j ] == 0 ; } let isPossible = false ; function knightTour ( ChessBoard , N , x , y , visited = 1 ) { ChessBoard [ x ] [ y ] = visited ; if ( visited == N * N ) { isPossible = true ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { document . write ( ChessBoard [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } document . write ( " " ) ; ChessBoard [ x ] [ y ] = 0 ; return ; } for ( let i = 0 ; i < 8 ; i ++ ) { let newX = x + DirX [ i ] ; let newY = y + DirY [ i ] ; if ( isSafe ( newX , newY , N , ChessBoard ) && ! ChessBoard [ newX ] [ newY ] ) { knightTour ( ChessBoard , N , newX , newY , visited + 1 ) ; } } ChessBoard [ x ] [ y ] = 0 ; } let ChessBoard = new Array ( 5 ) . fill ( 0 ) . map ( ( ) => new Array ( 5 ) . fill ( 0 ) ) ; let N = ChessBoard . length ; let X = 1 ; let Y = 1 ; knightTour ( ChessBoard , N , X - 1 , Y - 1 ) ; if ( ! isPossible ) { document . write ( " " ) ; }
Check if it ’ s possible to completely fill every container with same ball | A boolean function that returns true if it 's possible to completely fill the container else return false ; Base Case ; Backtracking ; Function to check the conditions ; Storing frequencies ; Function Call for backtracking ; Given Input ; Function Call
function getans ( i , v , q ) { if ( i == q . length ) return true ; for ( let j = 0 ; j < v . length ; j ++ ) { if ( v [ j ] >= q [ i ] ) { v [ j ] -= q [ i ] ; if ( getans ( i + 1 , v , q ) ) { return true ; } v [ j ] += q [ i ] ; } } return false ; } function Check ( c , b ) { let m = new Map ( ) ; for ( let i = 0 ; i < b . length ; i ++ ) { if ( m . has ( b [ i ] ) ) { m . set ( b [ i ] , m . get ( b [ i ] ) + 1 ) ; } else { m . set ( b [ i ] , 1 ) ; } } let v = [ ] ; for ( i of m ) { v . push ( i [ 1 ] ) ; } let check = getans ( 0 , v , c ) ; if ( check ) document . write ( " " ) ; else document . write ( " " ) ; } let c = [ 1 , 3 , 3 ] ; let b = [ 2 , 2 , 2 , 2 , 4 , 4 , 4 ] ; Check ( c , b ) ;
Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges | Function to find the maximum Bitwise XOR of any subset of the array of size K ; Number of node must K + 1 for K edges ; Stores the maximum Bitwise XOR ; Generate all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update the maximum Bitwise XOR obtained so far ; Return the maximum XOR ; Driver Code
function maximumXOR ( arr , n , K ) { K ++ ; let maxXor = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( ( i ) . toString ( 2 ) . split ( ' ' ) . filter ( x => x == ' ' ) . length == K ) { let cur_xor = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . max ( maxXor , cur_xor ) ; } } return maxXor ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; let M = 2 ; document . write ( maximumXOR ( arr , N , M ) ) ;