codeparrot/xlcost-text-to-code huggingface dataset:
Print the nodes with a prime degree in given Prufer sequence of a Tree | Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to print the nodes with prime degree in the tree whose Prufer sequence is given ; Hash - table to mark the degree of every node ; Initially let all the degrees be 1 ; Increase the count of the degree ; Print the nodes with prime degree ; Driver Code
function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function PrimeDegreeNodes ( prufer , n ) { let nodes = n + 2 ; let prime = new Array ( nodes + 1 ) ; prime . fill ( true ) ; SieveOfEratosthenes ( prime , nodes + 1 ) ; let degree = new Array ( n + 2 + 1 ) ; for ( let i = 1 ; i <= nodes ; i ++ ) degree [ i ] = 1 ; for ( let i = 0 ; i < n ; i ++ ) degree [ prufer [ i ] ] ++ ; for ( let i = 1 ; i <= nodes ; i ++ ) { if ( prime [ degree [ i ] ] ) { document . write ( i + " " ) ; } } } let a = [ 4 , 1 , 3 , 4 ] ; let n = a . length PrimeDegreeNodes ( a , n ) ;
Find K 'th smallest number such that A + B = A | B | Function to find k 'th smallest number such that A + B = A | B ; res will store final answer ; Skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position will be 0. ; j 'th bit is set ; If i ' th ▁ bit ▁ of ▁ k ▁ is ▁ 1 ▁ ▁ and ▁ i ' th bit of j is 0 then set i 'th bit in res. ; Proceed to next bit ; Driver Code
function kthSmallest ( a , k ) { let res = 0 ; let j = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) { j ++ ; } if ( k & ( 1 << i ) ) { res |= ( 1 << j ) ; } j ++ ; } return res ; } let a = 5 , k = 3 ; document . write ( kthSmallest ( a , k ) ) ;
Nodes with prime degree in an undirected Graph | JavaScript implementation of the approach ; To store Prime Numbers ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to print the nodes having prime degree ; To store Adjacency List of a Graph ; Make Adjacency List ; To precompute prime numbers till 10 ^ 5 ; Traverse each vertex ; Find size of Adjacency List ; If length of Adj [ i ] is Prime then print it ; Vertices and Edges ; Edges ; Function Call
let n = 10005 ; let Prime = new Array ( n + 1 ) . fill ( true ) ; function SieveOfEratosthenes ( ) { let i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= 10005 ; i ++ ) { if ( Prime [ i ] ) { for ( j = 2 * i ; j < 10005 ; j += i ) { Prime [ j ] = false ; } } } } function primeDegreeNodes ( N , M , edges ) { let Adj = new Array ( ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) { Adj . push ( new Array ( ) ) } for ( let i = 0 ; i < M ; i ++ ) { let x = edges [ i ] [ 0 ] ; let y = edges [ i ] [ 1 ] ; Adj [ x ] . push ( y ) ; Adj [ y ] . push ( x ) ; } SieveOfEratosthenes ( ) ; for ( let i = 1 ; i <= N ; i ++ ) { let x = Adj [ i ] . length ; if ( Prime [ x ] ) document . write ( i + ' ' ) ; } } let N = 4 , M = 6 ; let edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 2 , 4 ] , [ 3 , 4 ] ] ; primeDegreeNodes ( N , M , edges ) ;
Number of ways to color N | Javascript program for the above approach ; Recursive function to count the ways ; Base case ; Let initialise answer to 0 ; Color each uncolored block according to the given condition ; If any block is uncolored ; Check if adjacent blocks are colored or not ; Color the block ; recursively iterate for next uncolored block ; Uncolored for the next recursive call ; Return the final count ; Function to count the ways to color block ; Mark which blocks are colored in each recursive step ; Function call to count the ways ; Number of blocks ; Number of colored blocks ; Function call
let mod = 1000000007 ; function countWays ( colored , count , n ) { if ( count == n ) { return 1 ; } let answer = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { if ( colored [ i ] == 0 ) { if ( colored [ i - 1 ] == 1 colored [ i + 1 ] == 1 ) { colored [ i ] = 1 ; answer = ( answer + countWays ( colored , count + 1 , n ) ) % mod ; colored [ i ] = 0 ; } } } return answer ; } function waysToColor ( arr , n , k ) { let colored = Array . from ( { length : n + 2 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < k ; i ++ ) { colored [ arr [ i ] ] = 1 ; } return countWays ( colored , k , n ) ; } let N = 6 ; let K = 3 ; let arr = [ 1 , 2 , 6 ] ; document . write ( waysToColor ( arr , N , K ) ) ;
Shortest Palindromic Substring | Function return the shortest palindromic subString ; Finding the smallest character present in the String ; Driver code
function ShortestPalindrome ( s ) { let n = s . length ; let ans = s [ 0 ] . charCodeAt ( ) ; for ( let i = 1 ; i < n ; i ++ ) { ans = Math . min ( ans , s [ i ] . charCodeAt ( ) ) ; } return String . fromCharCode ( ans ) ; } let s = " " ; document . write ( ShortestPalindrome ( s ) ) ;
Maximum length Subsequence with alternating sign and maximum Sum | Function to find the subsequence with alternating sign having maximum size and maximum sum . ; Find whether each element is positive or negative ; Find the required subsequence ; Find the maximum element in the specified range ; prlet the result ; array declaration ; size of array
function findSubsequence ( arr , n ) { let sign = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sign [ i ] = 1 ; else sign [ i ] = - 1 ; } let k = 0 ; let result = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { let cur = arr [ i ] ; let j = i ; while ( j < n && sign [ i ] == sign [ j ] ) { cur = Math . max ( cur , arr [ j ] ) ; ++ j ; } result [ k ++ ] = cur ; i = j - 1 ; } for ( let i = 0 ; i < k ; i ++ ) document . write ( result [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 ] ; let n = arr . length ; findSubsequence ( arr , n ) ;
Longest Palindrome in a String formed by concatenating its prefix and suffix | Function to check whether the String is a palindrome ; Reverse the String to compare with the original String ; Check if both are same ; Function to find the longest palindrome in a String formed by concatenating its prefix and suffix ; Length of the String ; Finding the length upto which the suffix and prefix forms a palindrome together ; Check whether the String has prefix and suffix subStrings which are palindromes . ; Removing the suffix and prefix subStrings which already forms a palindrome and storing them in separate Strings ; Check all prefix subStrings in the remaining String str ; Check if the prefix subString is a palindrome ; If the prefix subString is a palindrome then check if it is of maximum length so far ; Check all the suffix subStrings in the remaining String str ; Check if the suffix subString is a palindrome ; If the suffix subString is a palindrome then check if it is of maximum length so far ; Combining all the thee parts of the answer ; Driver code
function isPalindrome ( r ) { let p = r ; p = reverse ( p ) ; return ( r == ( p ) ) ; } function PrefixSuffixPalindrome ( str ) { let n = str . length , len = 0 ; for ( let i = 0 ; i < n / 2 ; i ++ ) { if ( str [ i ] != str [ n - i - 1 ] ) { len = i ; break ; } } let prefix = " " , suffix = " " ; let midPal = " " ; prefix = str . substring ( 0 , len ) ; suffix = str . substring ( n - len ) ; str = str . substring ( len , ( n - 2 * len ) + len ) ; for ( let i = 1 ; i <= str . length ; i ++ ) { let y = str . substring ( 0 , i ) ; if ( isPalindrome ( y ) ) { if ( midPal . length < y . length ) { midPal = y ; } } } for ( let i = 1 ; i <= str . length ; i ++ ) { let y = str . substring ( str . length - i ) ; if ( isPalindrome ( y ) ) { if ( midPal . length < y . length ) { midPal = y ; } } } let answer = prefix + midPal + suffix ; return answer ; } function reverse ( input ) { let a = input . split ( " " ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return ( a ) . join ( " " ) ; } let str = " " ; document . write ( PrefixSuffixPalindrome ( str ) ) ;
Minimize the maximum difference between adjacent elements in an array | Javascript implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Traversing over subsets in iterative manner ; Number of elements to be taken in the subset ON bits of i represent elements not to be removed ; If the removed set is of size k ; Creating the new array after removing elements ; Maximum difference of adjacent elements of remaining array ; Driver code
function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } function minimumAdjacentDifference ( a , n , k ) { let minDiff = Number . MAX_VALUE ; for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { let cnt = countSetBits ( i ) ; if ( cnt == n - k ) { let temp = [ ] ; for ( let j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) temp . push ( a [ j ] ) ; } let maxDiff = Number . MIN_VALUE ; for ( let j = 0 ; j < temp . length - 1 ; j ++ ) { maxDiff = Math . max ( maxDiff , temp [ j + 1 ] - temp [ j ] ) ; } minDiff = Math . min ( minDiff , maxDiff ) ; } } return minDiff ; } let n = 5 ; let k = 2 ; let a = [ 3 , 7 , 8 , 10 , 14 ] ; document . write ( minimumAdjacentDifference ( a , n , k ) ) ;
Minimize the maximum difference between adjacent elements in an array | Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Iterating over all subarrays of size n - k ; Maximum difference after removing elements ; Minimum Adjacent Difference ; Driver Code
function minimumAdjacentDifference ( a , n , k ) { let minDiff = Number . MAX_VALUE ; for ( let i = 0 ; i <= k ; i ++ ) { let maxDiff = Number . MIN_VALUE ; for ( let j = 0 ; j < n - k - 1 ; j ++ ) { for ( let p = i ; p <= i + j ; p ++ ) { maxDiff = Math . max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = Math . min ( minDiff , maxDiff ) ; } return minDiff ; } let n = 5 ; let k = 2 ; let a = [ 3 , 7 , 8 , 10 , 14 ] ; document . write ( minimumAdjacentDifference ( a , n , k ) ) ;
Minimum elements inserted in a sorted array to form an Arithmetic progression | Function to find the greatest common divisor of two numbers ; Function to find the minimum the minimum number of elements required to be inserted into array ; Difference array of consecutive elements of the array ; GCD of the difference array ; Loop to calculate the minimum number of elements required ; Driver Code ; Function calling
function gcdFunc ( a , b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } function findMinimumElements ( a , n ) { let b = new Array ( n - 1 ) ; for ( let i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } let gcd = b [ 0 ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } let ans = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } let arr1 = [ 1 , 6 , 8 , 10 , 14 , 16 ] ; let n1 = arr1 . length ; document . write ( findMinimumElements ( arr1 , n1 ) + " " ) ;
Check if a Sequence is a concatenation of two permutations | Function to Check if a given sequence is a concatenation of two permutations or not ; Computing the sum of all the elements in the array ; Computing the prefix sum for all the elements in the array ; Iterating through the i from lengths 1 to n - 1 ; Sum of first i + 1 elements ; Sum of remaining n - i - 1 elements ; Lengths of the 2 permutations ; Checking if the sums satisfy the formula or not ; Driver Code
function checkPermutation ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; let prefix = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; prefix [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let lsum = prefix [ i ] ; let rsum = sum - prefix [ i ] ; let l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } let arr = [ 1 , 2 , 5 , 3 , 4 , 1 , 2 ] ; let n = arr . length ; if ( checkPermutation ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find a N | Function that print the answer ; if n == 1 then it is not possible ; loop to n - 1 times ; print 4 as last digit of the number ; Driver code ; Function call
function findTheNumber ( n ) { if ( n == 1 ) { document . write ( " " ) ; return ; } for ( var i = 0 ; i < n - 1 ; i ++ ) { document . write ( " " ) ; } document . write ( " " ) ; } var n = 12 ; findTheNumber ( n ) ;
Queries to check whether bitwise AND of a subarray is even or odd | Javascript implementation of the approach ; Function to precompute the count of even and odd numbers ; If the current element is odd then put 1 at odd [ i ] ; If the current element is even then put 1 at even [ i ] ; Taking the prefix sums of these two arrays so we can get the count of even and odd numbers in a range [ L , R ] in O ( 1 ) ; Function that returns true if the bitwise AND of the subarray a [ L ... R ] is odd ; cnt will store the count of odd numbers in the range [ L , R ] ; Check if all the numbers in the range are odd or not ; Function to perform the queries ; Perform queries ; Driver code ; Queries
var MAXN = 1000005 ; var even = Array ( MAXN ) . fill ( 0 ) ; var odd = Array ( MAXN ) . fill ( 0 ) ; function precompute ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd [ i ] = 1 ; if ( arr [ i ] % 2 == 0 ) even [ i ] = 1 ; } for ( var i = 1 ; i < n ; i ++ ) { even [ i ] = even [ i ] + even [ i - 1 ] ; odd [ i ] = odd [ i ] + odd [ i - 1 ] ; } } function isOdd ( L , R ) { var cnt = odd [ R ] ; if ( L > 0 ) cnt -= odd [ L - 1 ] ; if ( cnt == R - L + 1 ) return true ; return false ; } function performQueries ( a , n , q , m ) { precompute ( a , n ) ; for ( var i = 0 ; i < m ; i ++ ) { var L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; if ( isOdd ( L , R ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } } var a = [ 2 , 1 , 5 , 7 , 6 , 8 , 9 ] ; var n = a . length ; var q = [ [ 0 , 2 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 6 ] ] ; var m = q . length ; performQueries ( a , n , q , m ) ;
Find distinct integers for a triplet with given product | Function to find the required triplets ; To store the factors ; Find factors in Math . sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Prlet the valid triplet ; Triplet found ; Triplet not found ; Driver code
function findTriplets ( x ) { let fact = [ ] ; let factors = new Set ( ) ; for ( let i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . push ( i ) ; if ( x / i != i ) fact . push ( x / i ) ; factors . add ( i ) ; factors . add ( x / i ) ; } } let found = false ; let k = fact . length ; for ( let i = 0 ; i < k ; i ++ ) { let a = fact [ i ] ; for ( let j = 0 ; j < k ; j ++ ) { let b = fact [ j ] ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { document . write ( a + " " + b + " " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) document . write ( " " ) ; } let x = 105 ; findTriplets ( x ) ;
Count of pairs satisfying the given condition | Function to return the number of pairs satisfying the equation ; Converting integer b to string by using to_string function ; Loop to check if all the digits of b are 9 or not ; If '9' doesn 't appear  then break the loop ; If all the digits of b contain 9 then multiply a with string length else multiply a with string length - 1 ; Return the number of pairs ; Driver code
function countPair ( a , b ) { var s = ( b . toString ( ) ) ; var i ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != ' ' ) break ; } var result ; if ( i == s . length ) result = a * s . length ; else result = a * ( s . length - 1 ) ; return result ; } var a = 5 , b = 101 ; document . write ( countPair ( a , b ) ) ;
Area of the circle that has a square and a circle inscribed in it | Function to return the required area ; Calculate the area ; Driver Code
function getArea ( a ) { var area = ( Math . PI * a * a ) / 4 ; return area ; } var a = 3 ; document . write ( getArea ( a ) ) ;
Check if all the elements can be made of same parity by inverting adjacent elements | Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code
function flipsPossible ( a , n ) { let count_odd = 0 ; let count_even = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } let a = [ 1 , 0 , 1 , 1 , 0 , 1 ] ; let n = a . length ; if ( flipsPossible ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Queries On Array with disappearing and reappearing elements | Function to perform the queries ; Size of the array with 1 - based indexing ; Number of queries ; Iterating through the queries ; If m is more than the size of the array ; Count of turns ; Find the remainder ; If the remainder is 0 and turn is odd then the array is empty ; If the remainder is 0 and turn is even then array is full and is in its initial state ; If the remainder is not 0 and the turn is even ; Current size of the array ; Current size of the array ; Print the result ; The initial array , - 1 is for 1 base indexing ; Queries in the form of the pairs of ( t , M )
function PerformQueries ( a , vec ) { let ans = new Array ( ) ; let n = a . length - 1 ; let q = vec . length ; for ( let i = 0 ; i < q ; ++ i ) { let t = vec [ i ] [ 0 ] ; let m = vec [ i ] [ 1 ] ; if ( m > n ) { ans . push ( - 1 ) ; continue ; } let turn = Math . floor ( t / n ) ; let rem = t % n ; if ( rem == 0 && turn % 2 == 1 ) { ans . push ( - 1 ) ; continue ; } if ( rem == 0 && turn % 2 == 0 ) { ans . push ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { let cursize = n - rem ; if ( cursize < m ) { ans . push ( - 1 ) ; continue ; } ans . push ( a [ m + rem ] ) ; } else { let cursize = rem ; if ( cursize < m ) { ans . push ( - 1 ) ; continue ; } ans . push ( a [ m ] ) ; } } for ( let i of ans ) document . write ( i + " " ) ; } let a = [ - 1 , 1 , 2 , 3 , 4 , 5 ] ; let vec = [ [ 1 , 4 ] , [ 6 , 1 ] , [ 3 , 5 ] ] ; PerformQueries ( a , vec ) ;
Number of pairs in an array having sum equal to product | Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code
function sumEqualProduct ( a , n ) { var zero = 0 , two = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } var cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } var a = [ 2 , 2 , 3 , 4 , 2 , 6 ] ; var n = a . length ; document . write ( sumEqualProduct ( a , n ) ) ;
Minimize the sum of digits of A and B such that A + B = N | Function to return the minimum possible sum of digits of A and B such that A + B = n ; Find the sum of digits of n ; If num is a power of 10 ; Driver code
function minSum ( n ) { var sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n = parseInt ( n / 10 ) ; } if ( sum == 1 ) return 10 ; return sum ; } var n = 1884 ; document . write ( minSum ( n ) ) ;
Multiplication on Array : Range update query in O ( 1 ) | Creates a mul array for A and returns it after filling initial values . ; Does range update ; Prints updated Array ; Array to be updated ; Create and fill mul and div Array
function initialize ( mul , div , size ) { for ( i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } function update ( l , r , x , mul , div ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } function printArray ( ar , mul , div , n ) { for ( i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; document . write ( ar [ i ] + " " ) ; } } var ar = [ 10 , 5 , 20 , 40 ] ; var n = ar . length ; var mul = Array ( n + 1 ) . fill ( 0 ) ; var div = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ;
Optimal Strategy for a Game | Special Gold Coin | Function to return the winner of the game ; To store the count of silver coins ; Update the position of the gold coin ; First player will win the game ; Driver code
function getWinner ( str , len ) { var total = 0 ; if ( str [ 0 ] == ' ' str [ len - 1 ] == ' ' ) return " " ; else { for ( var i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) { total ++ ; } } if ( ( total % 2 ) == 1 ) return " " ; return " " ; } } var str = " " ; var len = str . length ; document . write ( getWinner ( str , len ) ) ;
Find the coordinates of a triangle whose Area = ( S / 2 ) | Javascript implementation of the approach ; Function to find the triangle with area = ( S / 2 ) ; Fix the two pairs of coordinates ; Find ( X3 , Y3 ) with integer coordinates ; Driver code
let MAX = 1000000000 ; function findTriangle ( S ) { let X1 = 0 , Y1 = 0 ; let X2 = MAX , Y2 = 1 ; let X3 = ( MAX - S % MAX ) % MAX ; let Y3 = ( S + X3 ) / MAX ; document . write ( " " + X1 + " " + Y1 + " " ) ; document . write ( " " + X2 + " " + Y2 + " " ) ; document . write ( " " + X3 + " " + Y3 + " " ) } let S = 4 ; findTriangle ( S ) ;
Rearrange array elements such that Bitwise AND of first N | Utility function to print the elements of an array ; Function to find the required arrangement ; There has to be atleast 2 elements ; Minimum element from the array ; Swap any occurrence of the minimum element with the last element ; Find the bitwise AND of the first ( n - 1 ) elements ; If the bitwise AND is equal to the last element then print the arrangement ; Driver code
function printArr ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function findArrangement ( arr , n ) { if ( n < 2 ) { document . write ( " " ) ; return ; } var minVal = arr . reduce ( ( a , b ) => Math . min ( a , b ) ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minVal ) { [ arr [ i ] , arr [ n - 1 ] ] = [ arr [ n - 1 ] , arr [ i ] ] ; break ; } } var andVal = arr [ 0 ] ; for ( var i = 1 ; i < n - 1 ; i ++ ) { andVal &= arr [ i ] ; } if ( andVal == arr [ n - 1 ] ) printArr ( arr , n ) ; else document . write ( " " ) ; } var arr = [ 1 , 5 , 3 , 3 ] ; var n = arr . length ; findArrangement ( arr , n ) ;
Maximum prime moves to convert X to Y | Function to return the maximum operations required to convert X to Y ; X cannot be converted to Y ; If the difference is 1 ; If the difference is even ; Add 3 to X and the new difference will be even ; Driver code
function maxOperations ( X , Y ) { if ( X > Y ) return - 1 ; let diff = Y - X ; if ( diff == 1 ) return - 1 ; if ( diff % 2 == 0 ) return ( diff / 2 ) ; return ( 1 + ( ( diff - 3 ) / 2 ) ) ; } let X = 5 , Y = 16 ; document . write ( maxOperations ( X , Y ) ) ;