codeparrot/xlcost-text-to-code huggingface dataset:
Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver program
function countValues ( n ) { let countV = 0 ; for ( let i = 0 ; i <= n ; i ++ ) if ( ( n + i ) == ( n ^ i ) ) countV ++ ; return countV ; } let n = 12 ; document . write ( countValues ( n ) ) ;
Equal Sum and XOR | Function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver Code
function countValues ( n ) { let unset_bits = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) unset_bits ++ ; n = n >> 1 ; } return 1 << unset_bits ; } let n = 12 ; document . write ( countValues ( n ) ) ;
Find profession in a special family | Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's  profession. If parent  is a Doctor, this node  will be a Doctor if it  is at odd position and an  engineer if at even position ; If parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; Driver Code
function findProffesion ( level , pos ) { if ( level == 1 ) return ' ' ; if ( findProffesion ( level - 1 , ( pos + 1 ) / 2 ) == ' ' ) return ( pos % 2 > 0 ) ? ' ' : ' ' ; return ( pos % 2 > 0 ) ? ' ' : ' ' ; } let level = 4 , pos = 2 ; if ( findProffesion ( level , pos ) == ' ' ) document . write ( " " ) ; else document . write ( " " ) ;
Print first n numbers with exactly two set bits | Prints first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits . ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver code
function printTwoSetBitNums ( n ) { let x = 1 ; while ( n > 0 ) { let y = 0 ; while ( y < x ) { document . write ( ( 1 << x ) + ( 1 << y ) + " " ) ; n -- ; if ( n == 0 ) return ; y ++ ; } x ++ ; } } printTwoSetBitNums ( 4 ) ;
Generate 0 and 1 with 25 % and 75 % probability | Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 1 with 75 % probability and 0 with 25 % probability using Bitwise OR ; Initialize random number generator
function rand50 ( ) { return Math . floor ( Math . random ( ) * 10 ) & 1 ; } function rand75 ( ) { return rand50 ( ) | rand50 ( ) ; } for ( let i = 0 ; i < 50 ; i ++ ) document . write ( rand75 ( ) ) ;
Find even occurring elements in an array of limited range | Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver Code
function printRepeatingEven ( arr , n ) { let _xor = 0 ; let pos ; for ( let i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( let i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( ( pos & _xor ) != 0 ) ) { document . write ( arr [ i ] + " " ) ; _xor ^= pos ; } } } let arr = [ 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 ] ; let n = arr . length ; printRepeatingEven ( arr , n ) ;
Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; 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 isBleak ( n ) { for ( let x = 1 ; 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 ( " " + " " ) ;
Find the maximum subset XOR of a given set | Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to  smaller i ; Put maximum element with i ' th ▁ bit ▁ set ▁ ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver code
let INT_BITS = 32 ; function maxSubarrayXOR ( set , n ) { let index = 0 ; for ( let i = INT_BITS - 1 ; i >= 0 ; i -- ) { let maxInd = index ; let maxEle = Number . MIN_VALUE ; for ( let j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) { maxEle = set [ j ] ; maxInd = j ; } } if ( maxEle == Number . MIN_VALUE ) continue ; let temp = set [ index ] ; set [ index ] = set [ maxInd ] ; set [ maxInd ] = temp ; maxInd = index ; for ( let j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } let set = [ 9 , 8 , 5 ] ; let n = set . length ; document . write ( " " ) ; document . write ( maxSubarrayXOR ( set , n ) ) ;
Given a set , find XOR of the XOR 's of all subsets. | Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver program
function findXOR ( Set , n ) { if ( n == 1 ) return Set [ 0 ] ; else return 0 ; } let Set = [ 1 , 2 , 3 ] ; let n = Set . length ; document . write ( " " + findXOR ( Set , n ) ) ;
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Assuming 32 - bit Integer ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver program to test above function
function myXOR ( x , y ) { for ( let i = 31 ; i >= 0 ; i -- ) { let b1 = ( ( x & ( 1 << i ) ) == 0 ) ? 0 : 1 ; let b2 = ( ( y & ( 1 << i ) ) == 0 ) ? 0 : 1 ; let xoredBit = ( b1 & b2 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } let x = 3 , y = 5 ; document . write ( " " + myXOR ( x , y ) ) ;
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Driver program to test above function
function myXOR ( x , y ) { return ( x y ) & ( ~ x ~ y ) ; } let x = 3 , y = 5 ; document . write ( " " + myXOR ( x , y ) ) ;
How to swap two bits in a given integer ? | This function swaps bit at positions p1 and p2 in an integer n ; Move p1 'th to rightmost side  ; Move p2 'th to rightmost side  ; XOR the two bits ; Put the xor bit back to their original positions ; XOR ' x ' with the original number so that the two sets are swapped ; Driver code
function swapBits ( n , p1 , p2 ) { var bit1 = ( n >> p1 ) & 1 ; var bit2 = ( n >> p2 ) & 1 ; var x = ( bit1 ^ bit2 ) ; x = ( x << p1 ) | ( x << p2 ) ; var result = n ^ x ; return result ; } var res = swapBits ( 28 , 0 , 3 ) ; document . write ( " " + res ) ;
Maximum length sub | Function to return the maximum length of the required sub - array ; For the first consecutive pair of elements ; While a consecutive pair can be selected ; If current pair forms a valid sub - array ; 2 is the length of the current sub - array ; To extend the sub - array both ways ; While elements at indices l and r are part of a valid sub - array ; Update the maximum length so far ; Select the next consecutive pair ; Return the maximum length ; Driver code
function maxLength ( arr , n ) { let maxLen = 0 ; let i = 0 ; let j = i + 1 ; while ( j < n ) { if ( arr [ i ] != arr [ j ] ) { maxLen = Math . max ( maxLen , 2 ) ; let l = i - 1 ; let r = j + 1 ; while ( l >= 0 && r < n && arr [ l ] == arr [ i ] && arr [ r ] == arr [ j ] ) { l -- ; r ++ ; } maxLen = Math . max ( maxLen , 2 * ( r - j ) ) ; } i ++ ; j = i + 1 ; } return maxLen ; } let arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ] ; let n = arr . length ; document . write ( maxLength ( arr , n ) ) ;
Freivaldâ €™ s Algorithm to check if a matrix is product of two | JavaScript code to implement FreivaldaTMs Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now comput B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code
let N = 2 ; function freivald ( a , b , c ) { let r = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) r [ i ] = Math . random ( ) % 2 ; let br = new Array ( N ) ; br . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) br [ i ] = br [ i ] + b [ i ] [ j ] * r [ j ] ; let cr = new Array ( N ) ; cr . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) cr [ i ] = cr [ i ] + c [ i ] [ j ] * r [ j ] ; let axbr = new Array ( N ) ; axbr . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) axbr [ i ] = axbr [ i ] + a [ i ] [ j ] * br [ j ] ; for ( let i = 0 ; i < N ; i ++ ) if ( axbr [ i ] - cr [ i ] != 0 ) false ; return true ; } function isProduct ( a , b , c , k ) { for ( let i = 0 ; i < k ; i ++ ) if ( freivald ( a , b , c ) == false ) return false ; return true ; } let a = [ [ 1 , 1 ] , [ 1 , 1 ] ] ; let b = [ [ 1 , 1 ] , [ 1 , 1 ] ] ; let c = [ [ 2 , 2 ] , [ 2 , 2 ] ] ; let k = 2 ; if ( isProduct ( a , b , c , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Expectation or expected value of an array | Function to calculate expectation ; Variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; driver function ; Function for calculating expectation ; Display expectation of given array
function calc_Expectation ( a , n ) { let prb = ( 1 / n ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } let expect , n = 6 ; let a = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; expect = calc_Expectation ( a , n ) ; document . write ( " " + expect ) ;
Sum of N | Function to calculate the sum recursively ; Base cases ; If n is odd ; If n is even ; Function to prlet the value of Sum ; First element ; Common difference ; Number of elements ; Mod value
function SumGPUtil ( r , n , m ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return ( 1 + r ) % m ; let ans ; if ( n % 2 == 1 ) { ans = ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n - 1 ) / 2 , m ) ; } else { ans = 1 + ( r * ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n / 2 ) - 1 , m ) ) ; } return ( ans % m ) ; } function SumGP ( a , r , N , M ) { let answer ; answer = a * SumGPUtil ( r , N , M ) ; answer = answer % M ; document . write ( answer ) ; } let a = 1 ; let r = 4 ; let N = 10000 ; let M = 100000 ; SumGP ( a , r , N , M ) ;
Length of longest subarray in which elements greater than K are more than elements not greater than K | Function to find the length of a longest subarray in which elements greater than K are more than elements not greater than K ; Create a new array in which we store 1 if a [ i ] > k otherwise we store - 1. ; Taking prefix sum over it ; len will store maximum length of subarray ; This indicate there is at least one subarray of length mid that has sum > 0 ; Check every subarray of length mid if it has sum > 0 or not if sum > 0 then it will satisfy our required condition ; x will store the sum of subarray of length mid ; Satisfy our given condition ; Check for higher length as we get length mid ; Check for lower length as we did not get length mid ; Driver code
function LongestSubarray ( a , n , k ) { var pre = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > k ) pre [ i ] = 1 ; else pre [ i ] = - 1 ; } for ( i = 1 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + pre [ i ] ; var len = 0 ; var lo = 1 , hi = n ; while ( lo <= hi ) { var mid = parseInt ( ( lo + hi ) / 2 ) ; var ok = false ; for ( i = mid - 1 ; i < n ; i ++ ) { var x = pre [ i ] ; if ( i - mid >= 0 ) x -= pre [ i - mid ] ; if ( x > 0 ) { ok = true ; break ; } } if ( ok == true ) { len = mid ; lo = mid + 1 ; } else hi = mid - 1 ; } return len ; } var a = [ 2 , 3 , 4 , 5 , 3 , 7 ] ; var k = 3 ; var n = a . length ; document . write ( LongestSubarray ( a , n , k ) ) ;
Choose points from two ranges such that no point lies in both the ranges | Function to find the required points ; Driver code
function findPoints ( l1 , r1 , l2 , r2 ) { var x = ( l1 != l2 ) ? Math . min ( l1 , l2 ) : - 1 ; var y = ( r1 != r2 ) ? Math . max ( r1 , r2 ) : - 1 ; document . write ( x + " " + y ) ; } var l1 = 5 , r1 = 10 , l2 = 1 , r2 = 7 ; findPoints ( l1 , r1 , l2 , r2 ) ;
Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver code
function fact ( n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } document . write ( fact ( 5 ) ) ;
Minimum number of working days required to achieve each of the given scores | Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Driver Code
function binarySeach ( P , N ) { var i = 0 ; var j = P . length - 1 ; var index = - 1 ; while ( i <= j ) { var mid = i + parseInt ( ( j - i ) / 2 ) ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } function minDays ( P , arr ) { for ( var i = 1 ; i < P . length ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( var i = 0 ; i < arr . length ; i ++ ) { var index = binarySeach ( P , arr [ i ] ) ; if ( index != - 1 ) { document . write ( ( index + 1 ) + " " ) ; } else { document . write ( - 1 + " " ) ; } } } var arr = [ 400 , 200 , 700 , 900 , 1400 ] ; var P = [ 100 , 300 , 400 , 500 , 600 ] ; minDays ( P , arr ) ;
Maximize profit that can be earned by selling an item among N buyers | Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Sort the array ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Update the maximum profit ; Return the maximum possible price ; Driver code
function maximumProfit ( arr , N ) { let ans = Number . MIN_VALUE ; let price = 0 ; arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < N ; i ++ ) { let count = ( N - i ) ; if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } let arr = [ 22 , 87 , 9 , 50 , 56 , 43 ] ; document . write ( maximumProfit ( arr , 6 ) ) ;
Maximum length palindromic substring for every index such that it starts and ends at that index | Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Driver Code
function isPalindrome ( S , i , j ) { while ( i < j ) { if ( S [ i ] !== S [ j ] ) return false ; i ++ ; j -- ; } return true ; } function printLongestPalindrome ( S , N ) { var palLength = new Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) { var maxlength = 1 ; for ( var j = 0 ; j < i ; j ++ ) { if ( S [ j ] === S [ i ] ) { if ( isPalindrome ( S , j , i ) !== false ) { maxlength = i - j + 1 ; break ; } } } for ( var j = N - 1 ; j > i ; j -- ) { if ( S [ j ] === S [ i ] ) { if ( isPalindrome ( S , i , j ) ) { maxlength = Math . max ( j - i + 1 , maxlength ) ; break ; } } } palLength [ i ] = maxlength ; } for ( var i = 0 ; i < N ; i ++ ) { document . write ( palLength [ i ] + " " ) ; } } var S = " " ; var N = S . length ; printLongestPalindrome ( S , N ) ;
Sum of array elements which are multiples of a given number | Function to find the sum of array elements which are multiples of N ; Stores the sum ; Traverse the given array ; If current element is a multiple of N ; Print total sum ; Given arr [ ]
function mulsum ( arr , n , N ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % N == 0 ) { sum = sum + arr [ i ] ; } } document . write ( sum ) ; } var arr = [ 1 , 2 , 3 , 5 , 6 ] ; var n = arr . length ; var N = 3 ; mulsum ( arr , n , N ) ;
Farthest index that can be reached from the Kth index of given array by given operations | Function to find the farthest index that can be reached ; Declare a priority queue ; Iterate the array ; If current element is greater than the next element ; Otherwise , store their difference ; Push diff into pq ; If size of pq exceeds Y ; Decrease X by the top element of pq ; Remove top of pq ; If X is exhausted ; Current index is the farthest possible ; Print N - 1 as farthest index ; Driver code ; Function Call
function farthestHill ( arr , X , Y , N , K ) { var i , diff ; var pq = [ ] ; for ( i = K ; i < N - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i + 1 ] ) continue ; diff = arr [ i + 1 ] - arr [ i ] ; pq . push ( diff ) ; pq . sort ( ) ; pq = pq . reverse ( ) ; if ( pq . length > Y ) { X -= pq [ 0 ] ; pq = pq . slice ( 1 ) ; } if ( X < 0 ) { document . write ( i ) ; return ; } } document . write ( N - 1 ) ; } var arr = [ 4 , 2 , 7 , 6 , 9 , 14 , 12 ] ; var X = 5 , Y = 1 ; var K = 0 ; var N = arr . length ; farthestHill ( arr , X , Y , N , K ) ;
Lexicographically largest string possible consisting of at most K consecutive similar characters | Function to find largest string ; Stores the frequency of characters ; Traverse the string ; Stores the resultant string ; Append larger character ; Decrease count in charset ; Increase count ; Check if count reached to charLimit ; Find nearest lower char ; If no character can be appended ; Append nearest lower character ; Reset count for next calculation ; Return new largest string ; Function to return nearest lower character ; Traverse charset from start - 1 ; If no character can be appended ; Driver Code
function newString ( originalLabel , limit ) { let n = originalLabel . length ; let charset = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { charset [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { charset [ originalLabel [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let newString = [ ] ; for ( let i = 25 ; i >= 0 ; i -- ) { let count = 0 ; while ( charset [ i ] > 0 ) { newString . push ( String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) ; charset [ i ] -- ; count ++ ; if ( charset [ i ] > 0 && count == limit ) { let next = nextAvailableChar ( charset , i ) ; if ( next == null ) return newString . join ( " " ) ; newString . push ( next ) ; count = 0 ; } } } return newString . join ( " " ) ; } function nextAvailableChar ( charset , start ) { for ( let i = start - 1 ; i >= 0 ; i -- ) { if ( charset [ i ] > 0 ) { charset [ i ] -- ; return String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; } } return null ; } let S = " " ; let K = 2 ; document . write ( newString ( S , K ) ) ;
Rearrange array by interchanging positions of even and odd elements in the given array | Function to replace odd elements with even elements and vice versa ; Traverse the given array ; If arr [ i ] is visited ; Find the next odd element ; Find next even element ; Mark them visited ; Swap them ; Print the final array ; Given array arr [ ] ; Length of the array ; Function Call
function swapEvenOdd ( arr , n ) { let o = - 1 , e = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) continue ; let r = - 1 ; if ( arr [ i ] % 2 == 0 ) { o ++ ; while ( arr [ o ] % 2 == 0 arr [ o ] < 0 ) o ++ ; r = o ; } else { e ++ ; while ( arr [ e ] % 2 == 1 arr [ e ] < 0 ) e ++ ; r = e ; } arr [ i ] *= - 1 ; arr [ r ] *= - 1 ; let tmp = arr [ i ] ; arr [ i ] = arr [ r ] ; arr [ r ] = tmp ; } for ( let i = 0 ; i < n ; i ++ ) { document . write ( ( - 1 * arr [ i ] ) + " " ) ; } } let arr = [ 1 , 3 , 2 , 4 ] ; let n = arr . length ; swapEvenOdd ( arr , n ) ;
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | JavaScript program to implement the above approach ; iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; else we could just break the loop as its not a palindrome type sequence ; we could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; we would check for the palindrome condition if yes we print True else False ; Driver code
function rev ( str ) { var st = 0 ; var ed = str . length - 1 ; var s = str . split ( " " ) ; while ( st < ed ) { var temp = s [ st ] ; s [ st ] = s [ ed ] ; s [ ed ] = temp ; st ++ ; ed -- ; } return s . join ( ) ; } function check ( a , b , n ) { var i = 0 ; var j = n - 1 ; while ( i < n ) { if ( a [ i ] !== b [ j ] ) break ; i += 1 ; j -= 1 ; var xa = a . substring ( i , j + 1 - i ) ; var xb = b . substring ( i , j + 1 - i ) ; var XA = xa . split ( " " ) ; XA . sort ( ) . reverse ( ) ; var XB = xb . split ( " " ) ; XB . sort ( ) . reverse ( ) ; if ( xa === XA . join ( " " ) || xb === XB . join ( " " ) ) return true ; } return false ; } var a = " " ; var b = " " ; if ( check ( a , b , a . length ) || check ( b , a , a . length ) ) document . write ( " " ) ; else document . write ( " " ) ;