codeparrot/xlcost-text-to-code huggingface dataset:
Encrypt string with product of number of vowels and consonants in substring of size k | isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the String ; cv to count vowel cc to count consonants ; Counting prefix count of vowel and prefix count of consonants ; generating the encrypted String . ; Driver Code
function isVowel ( c ) { return c === " " || c === " " || c === " " || c === " " || c === " " ; } function encryptString ( s , n , k ) { var cv = new Array ( n ) . fill ( 0 ) ; var cc = new Array ( n ) . fill ( 0 ) ; if ( isVowel ( s [ 0 ] ) ) cv [ 0 ] = 1 ; else cc [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { cv [ i ] = cv [ i - 1 ] + ( isVowel ( s [ i ] ) === true ? 1 : 0 ) ; cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) === true ? 0 : 1 ) ; } var ans = " " ; var prod = 0 ; prod = cc [ k - 1 ] * cv [ k - 1 ] ; ans += prod ; for ( var i = k ; i < s . length ; i ++ ) { prod = ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ; ans += prod ; } return ans ; } var s = " " ; var n = s . length ; var k = 2 ; document . write ( encryptString ( s . split ( " " ) , n , k ) + " " ) ;
Count occurrences of a word in string | Javascript program to count the number of occurrence of a word in the given string ; split the string by spaces in a ; search for pattern in a ; if match found increase count ; Driver code
function countOccurrences ( str , word ) { let a = str . split ( " " ) ; let count = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( word == ( a [ i ] ) ) count ++ ; } return count ; } let str = " " ; let word = " " ; document . write ( countOccurrences ( str , word ) ) ;
Permute a string by changing case | Function to generate permutations ; Number of permutations is 2 ^ n ; Converting string to lower case ; Using all subsequences and permuting them ; If j - th bit is set , we convert it to upper case ; Printing current combination ; Driver Program to test above function
function permute ( input ) { var n = input . length ; var max = 1 << n ; input = input . toLowerCase ( ) ; for ( var i = 0 ; i < max ; i ++ ) { var combination = input . split ( ' ' ) ; for ( var j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) combination [ j ] = String . fromCharCode ( combination [ j ] . charCodeAt ( 0 ) - 32 ) ; } document . write ( combination . join ( ' ' ) ) ; document . write ( " " ) ; } } permute ( " " ) ;
Print the string after the specified character has occurred given no . of times | Method to print the string ; If given count is 0 print the given string and return ; Start traversing the string ; Increment occ if current char is equal to given character ; Break the loop if given character has been occurred given no . of times ; Print the string after the occurrence of given character given no . of times ; Otherwise string is empty ; Driver Method
function printString ( str , ch , count ) { var occ = 0 , i ; if ( count == 0 ) { document . write ( str ) ; return ; } for ( i = 0 ; i < str . length ; i ++ ) { if ( str . charAt ( i ) == ch ) occ ++ ; if ( occ == count ) break ; } if ( i < str . length - 1 ) document . write ( str . substring ( i + 1 ) ) ; else document . write ( " " ) ; } var str = " " ; printString ( str , ' ' , 2 ) ;
Reverse vowels in a given string | utility function to check for vowel ; Function to reverse order of vowels ; Start two indexes from two corners and move toward each other ; swapping ; Driver function
function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function reverseVowel ( str ) { let i = 0 ; let j = str . length - 1 ; let str1 = str . split ( " " ) ; while ( i < j ) { if ( ! isVowel ( str1 [ i ] ) ) { i ++ ; continue ; } if ( ! isVowel ( str1 [ j ] ) ) { j -- ; continue ; } let t = str1 [ i ] ; str1 [ i ] = str1 [ j ] ; str1 [ j ] = t ; i ++ ; j -- ; } let str2 = ( str1 ) . join ( " " ) ; return str2 ; } let str = " " ; document . write ( reverseVowel ( str ) ) ;
Minimum number of palindromic subsequences to be removed to empty a binary string | A function to check if a string str is palindrome ; Start from leftmost and rightmost corners of str ; Keep comparing characters while they are same ; Returns count of minimum palindromic subseuqnces to be removed to make string empty ; If string is empty ; If string is palindrome ; If string is not palindrome ; Driver Code
function isPalindrome ( str ) { let l = 0 ; let h = str . length - 1 ; while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; } function minRemovals ( str ) { if ( str [ 0 ] == ' ' ) return 0 ; if ( isPalindrome ( str ) ) return 1 ; return 2 ; } document . write ( minRemovals ( " " ) + " " ) ; document . write ( minRemovals ( " " ) ) ;
Find the value of XXXX ... . . ( N times ) % M where N is large | Iterative function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return XXX ... . . ( N times ) % M ; Return the mod by M of smaller numbers ; Creating a string of N X 's ; Converting the string to int and calculating the modulo ; Checking the parity of N ; Dividing the number into equal half ; Utilizing the formula for even N ; Dividing the number into equal half ; Utilizing the formula for odd N ; Driver code ; Print XXX ... ( N times ) % M
function power ( x , y , p ) { var res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function findModuloByM ( X , N , M ) { if ( N < 6 ) { var temp = " " ; for ( var i = 1 ; i < N ; i ++ ) { temp += String . fromCharCode ( 48 + X ) ; } var res = parseInt ( temp ) % M ; return res ; } if ( N % 2 == 0 ) { var half = findModuloByM ( X , N / 2 , M ) % M ; var res = ( half * power ( 10 , N / 2 , M ) + half ) % M ; return res ; } else { var half = findModuloByM ( X , N / 2 , M ) % M ; var res = ( half * power ( 10 , N / 2 + 1 , M ) + half * 10 + X ) % M ; return res ; } } var X = 6 , N = 14 , M = 9 ; document . write ( findModuloByM ( X , N , M ) ) ;
Check if two circles intersect such that the third circle passes through their points of intersections and centers | Structure of the circle ; Utility function to check if given circles satisfy required criteria ; Stores the distance between the centres of C1 and C2 ; Stores the status if the given given criteria is satisfied or not ; If C1C2 is less than the sum of the radii of the first 2 circles ; If C3 is the midpoint of the centres at C1 and C2 ; Mark flag true ; Return flag ; Function to check if the given circles satisfy required criteria ; Check for the current combination of circles ; Check for the next combination ; Driver Code
class circle { constructor ( x , y , r ) { this . x = x ; this . y = y ; this . r = r ; } } function check ( C ) { let C1C2 = Math . sqrt ( ( C [ 1 ] . x - C [ 0 ] . x ) * ( C [ 1 ] . x - C [ 0 ] . x ) + ( C [ 1 ] . y - C [ 0 ] . y ) * ( C [ 1 ] . y - C [ 0 ] . y ) ) ; let flag = false ; if ( C1C2 < ( C [ 0 ] . r + C [ 1 ] . r ) ) { if ( ( C [ 0 ] . x + C [ 1 ] . x ) == 2 * C [ 2 ] . x && ( C [ 0 ] . y + C [ 1 ] . y ) == 2 * C [ 2 ] . y ) { flag = true ; } } return flag ; } function IsFairTriplet ( c ) { let f = false ; f |= check ( c ) ; for ( let i = 0 ; i < 2 ; i ++ ) { swap ( c [ 0 ] , c [ 2 ] ) ; f |= check ( c ) ; } return f ; } function swap ( circle1 , circle2 ) { let temp = circle1 ; circle1 = circle2 ; circle2 = temp ; } let C = new Array ( 3 ) ; C [ 0 ] = new circle ( 0 , 0 , 8 ) ; C [ 1 ] = new circle ( 0 , 10 , 6 ) ; C [ 2 ] = new circle ( 0 , 5 , 5 ) ; if ( IsFairTriplet ( C ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find the Eccentricity of a Hyperbola | Function to find the eccentricity of a hyperbola ; Stores the squared ratio of major axis to minor axis ; Increment r by 1 ; Return the square root of r ; Driver Code
function eccHyperbola ( A , B ) { let r = B * B / A * A ; r += 1 ; return Math . sqrt ( r ) ; } let A = 3.0 ; let B = 2.0 ; document . write ( eccHyperbola ( A , B ) ) ;
Calculate area of a cyclic quadrilateral with given side lengths | Function to find the area of cyclic quadrilateral ; Stores the value of half of the perimeter ; Stores area of cyclic quadrilateral ; Return the resultant area ; Driver Code
function calculateArea ( A , B , C , D ) { let S = ( A + B + C + D ) / 2 let area = Math . sqrt ( ( S - A ) * ( S - B ) * ( S - C ) * ( S - D ) ) return area ; } let A = 10 ; let B = 15 ; let C = 20 ; let D = 25 ; document . write ( calculateArea ( A , B , C , D ) . toFixed ( 3 ) )
Calculate ratio of area of a triangle inscribed in an Ellipse and the triangle formed by corresponding points on auxiliary circle | Function to calculate ratio of a triangle inscribed in an ellipse to the triangle on the auxiliary circle ; Stores the ratio of the semi - major to semi - minor axes ; Print the ratio ; Driver Code
function triangleArea ( a , b ) { ratio = b / a document . write ( ratio ) } var a = 1 var b = 2 triangleArea ( a , b )
Program to find the Excenters of a Triangle | Function to calculate the distance between a pair of pos ; Function to calculate the coordinates of the excenters of a triangle ; Length of the sides of the triangle ; Stores the coordinates of the excenters of the triangle ; For I1 ; For I2 ; For I3 ; Prvar the excenters of the triangle ; Driver Code
function distance ( m , n , p , q ) { return Math . sqrt ( Math . pow ( n - m , 2 ) + Math . pow ( q - p , 2 ) * 1.0 ) ; } function Excenters ( x1 , y1 , x2 , y2 , x3 , y3 ) { var a = distance ( x2 , x3 , y2 , y3 ) ; var b = distance ( x3 , x1 , y3 , y1 ) ; var c = distance ( x1 , x2 , y1 , y2 ) ; var excenter = new Array ( 4 ) ; for ( var i = 0 ; i < 4 ; i ++ ) excenter [ i ] = new Array ( 2 ) ; excenter [ 1 ] [ 0 ] = ( - ( a * x1 ) + ( b * x2 ) + ( c * x3 ) ) / ( - a + b + c ) ; excenter [ 1 ] [ 1 ] = ( - ( a * y1 ) + ( b * y2 ) + ( c * y3 ) ) / ( - a + b + c ) ; excenter [ 2 ] [ 0 ] = ( ( a * x1 ) - ( b * x2 ) + ( c * x3 ) ) / ( a - b + c ) ; excenter [ 2 ] [ 1 ] = ( ( a * y1 ) - ( b * y2 ) + ( c * y3 ) ) / ( a - b + c ) ; excenter [ 3 ] [ 0 ] = ( ( a * x1 ) + ( b * x2 ) - ( c * x3 ) ) / ( a + b - c ) ; excenter [ 3 ] [ 1 ] = ( ( a * y1 ) + ( b * y2 ) - ( c * y3 ) ) / ( a + b - c ) ; for ( var i = 1 ; i <= 3 ; i ++ ) { document . write ( excenter [ i ] [ 0 ] + " " + excenter [ i ] [ 1 ] + " " ) ; } } var x1 , x2 , x3 , y1 , y2 , y3 ; x1 = 0 ; x2 = 3 ; x3 = 0 ; y1 = 0 ; y2 = 0 ; y3 = 4 ; Excenters ( x1 , y1 , x2 , y2 , x3 , y3 ) ;
Icositetragonal Number | Function to find icositetragonal number ; Formula to calculate nth icositetragonal number ; Driver code
function Icositetragonal_num ( n ) { return ( 22 * n * n - 20 * n ) / 2 ; } let n = 3 ; document . write ( Icositetragonal_num ( n ) + " " ) ; n = 10 ; document . write ( Icositetragonal_num ( n ) ) ;
Area of circle inscribed in a Isosceles Trapezoid | Function to find area of circle inscribed in a trapezoid having non - parallel sides m , n ; Radius of circle by the formula i . e . root ( m * n ) / 2 area of circle = ( 3.141 ) * ( R * * 2 ) ; Driver Code
function area_of_circle ( m , n ) { var square_of_radius = ( m * n ) / 4 ; var area = ( 3.141 * square_of_radius ) ; return area ; } var n = 10 ; var m = 30 ; document . write ( area_of_circle ( m , n ) ) ;
Area of Equilateral triangle inscribed in a Circle of radius R | Function to find the area of equilateral triangle inscribed in a circle of radius R ; Base and Height of equilateral triangle ; Area using Base and Height ; Driver code
function area ( R ) { var base = 1.732 * R ; var height = ( 1.5 ) * R ; var area = 0.5 * base * height ; return area ; } var R = 7 ; document . write ( area ( R ) ) ;
Area of largest Circle that can be inscribed in a SemiCircle | Function to find the area of the circle ; Radius cannot be negative ; Area of the largest circle ; Driver code
function circlearea ( R ) { if ( R < 0 ) return - 1 ; var a = 3.14 * R * R / 4 ; return a ; } var R = 2 ; document . write ( circlearea ( R ) ) ;
Number of pairs of lines having integer intersection points | Count number of pairs of lines having integer intersection point ; Initialize arrays to store counts ; Count number of odd and even Pi ; Count number of odd and even Qi ; Return the count of pairs ; Driver code
function countPairs ( P , Q , N , M ) { var A = [ 0 , 0 ] , B = [ 0 , 0 ] ; for ( var i = 0 ; i < N ; i ++ ) A [ P [ i ] % 2 ] ++ ; for ( var i = 0 ; i < M ; i ++ ) B [ Q [ i ] % 2 ] ++ ; return ( A [ 0 ] * B [ 0 ] + A [ 1 ] * B [ 1 ] ) ; } var P = [ 1 , 3 , 2 ] , Q = [ 3 , 0 ] ; var N = P . length ; var M = Q . length ; document . write ( countPairs ( P , Q , N , M ) ) ;
Maximum number of line intersections formed through intersection of N planes | Function to count maximum number of intersections possible ; Driver Code
function countIntersections ( n ) { return n * ( n - 1 ) / 2 ; } var n = 3 ; document . write ( countIntersections ( n ) ) ;
Area of a Regular Pentagram | Javascript implementation of the approach ; Function to return the area of triangle BCD ; Using Golden ratio ; Calculate area of triangle BCD ; Return area of all 5 triangle are same ; Function to return the area of regular pentagon ; Calculate the area of regular pentagon using above formula ; Return area of regular pentagon ; Function to return the area of pentagram ; Area of a pentagram is equal to the area of regular pentagon and five times the area of Triangle ; Driver code
var PI = 3.14159 function areaOfTriangle ( d ) { var c = 1.618 * d ; var s = ( d + c + c ) / 2 ; var area = Math . sqrt ( s * ( s - c ) * ( s - c ) * ( s - d ) ) ; return 5 * area ; } function areaOfRegPentagon ( d ) { var cal = 4 * Math . tan ( PI / 5 ) ; var area = ( 5 * d * d ) / cal ; return area ; } function areaOfPentagram ( d ) { return areaOfRegPentagon ( d ) + areaOfTriangle ( d ) ; } var d = 5 ; document . write ( areaOfPentagram ( d ) . toFixed ( 3 ) ) ;
Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given | javascript program to find the angle subtended by the chord to the centre of the circle when the angle subtended by another equal chord of a congruent circle is given ; Driver code
function anglequichord ( z ) { document . write ( " " + z + " " ) ; } var z = 48 ; anglequichord ( z ) ;
Program to print ASCII Value of all digits of a given number | Function to convert digits of N to respective ASCII values ; Driver Code
function convertToASCII ( N ) { let num = N . toString ( ) ; for ( let ch = 0 ; ch < num . length ; ch ++ ) { document . write ( num [ ch ] + " " + num [ ch ] . charCodeAt ( 0 ) + " " ) ; } } let N = 36 ; convertToASCII ( N ) ;
A Product Array Puzzle | Set 3 | Function to form product array with O ( n ) time and O ( 1 ) space ; Stores the product of array ; Stores the count of zeros ; Traverse the array ; If arr [ i ] is not zero ; If arr [ i ] is zero then increment count of z by 1 ; Stores the absolute value of the product ; If Z is equal to 1 ; If arr [ i ] is not zero ; Else ; If count of 0 s at least 2 ; Assign arr [ i ] = 0 ; Store absolute value of arr [ i ] ; Find the value of a / b ; If arr [ i ] and product both are less than zero ; If arr [ i ] and product both are greater than zero ; Else ; Traverse the array arr [ ] ; Driver Code ; Function Call
function productExceptSelf ( arr , N ) { let product = 1 ; let z = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) product *= arr [ i ] ; if ( arr [ i ] == 0 ) z += 1 ; } let a = Math . abs ( product ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( z == 1 ) { if ( arr [ i ] != 0 ) arr [ i ] = 0 ; else arr [ i ] = product ; continue ; } else if ( z > 1 ) { arr [ i ] = 0 ; continue ; } let b = Math . abs ( arr [ i ] ) ; let curr = Math . round ( Math . exp ( Math . log ( a ) - Math . log ( b ) ) ) ; if ( arr [ i ] < 0 && product < 0 ) arr [ i ] = curr ; else if ( arr [ i ] > 0 && product > 0 ) arr [ i ] = curr ; else arr [ i ] = - 1 * curr ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 10 , 3 , 5 , 6 , 2 ] ; let N = arr . length ; productExceptSelf ( arr , N ) ;
Count subarrays made up of single | Function to count of subarrays made up of single digit integers only ; Stores count of subarrays ; Stores the count of consecutive single digit numbers in the array ; Traverse the array ; Increment size of block by 1 ; Increment res by count ; Assign count = 0 ; Given array ; Size of the array
function singleDigitSubarrayCount ( arr , N ) { let res = 0 ; let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] <= 9 ) { count ++ ; res += count ; } else { count = 0 ; } } document . write ( res ) ; } let arr = [ 0 , 1 , 14 , 2 , 5 ] ; let N = arr . length ; singleDigitSubarrayCount ( arr , N ) ;
Count integers up to N which can be represented as sum of two or more consecutive numbers | Function to check if the number N can be expressed as sum of 2 or more consecutive numbers or not ; Function to count integers in the range [ 1 , N ] that can be expressed as sum of 2 or more consecutive numbers ; Stores the required count ; Driver Code
function isPossible ( N ) { return ( ( ( N & ( N - 1 ) ) & N ) ) ; } function countElements ( N ) { var count = 0 ; for ( i = 1 ; i <= N ; i ++ ) { if ( isPossible ( i ) != 0 ) count ++ ; } document . write ( count ) ; } var N = 15 ; countElements ( N ) ;
Count integers up to N which can be represented as sum of two or more consecutive numbers | Function to count integers in the range [ 1 , N ] that can be expressed as sum of 2 or more consecutive numbers ; Count powers of 2 up to N ; Increment count ; Update current power of 2 ; Driver Code
function countElements ( N ) { var Cur_Ele = 1 ; var Count = 0 ; while ( Cur_Ele <= N ) { Count ++ ; Cur_Ele = Cur_Ele * 2 ; } document . write ( N - Count ) ; } var N = 15 ; countElements ( N ) ;
Maximum difference between a pair of adjacent elements by excluding every element once | Function to calculate maximum difference between adjacent elements excluding every array element once ; Compute maximum adjacent difference for whole array ; Store the maximum between arr_max and curr_max ; Append the result into a vector ; Print the result ; Driver Code
function maxAdjacent ( arr , N ) { let res = [ ] ; let arr_max = Number . MIN_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { arr_max = Math . max ( arr_max , Math . abs ( arr [ i - 1 ] - arr [ i ] ) ) ; } for ( let i = 1 ; i < N - 1 ; i ++ ) { let curr_max = Math . abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; let ans = Math . max ( curr_max , arr_max ) ; res . push ( ans ) ; } document . write ( res . join ( " " ) ) ; } let arr = [ 1 , 3 , 4 , 7 , 8 ] ; let N = arr . length ; maxAdjacent ( arr , N ) ;
Minimize increments required to make count of even and odd array elements equal | Function to find min operations to make even and odd count equal ; Odd size will never make odd and even counts equal ; Stores the count of even numbers in the array arr ; Stores count of odd numbers in the array arr ; Traverse the array arr ; If arr [ i ] is an even number ; Update cntEven ; Odd numbers in arr ; Return absolute difference divided by 2 ; Driver code ; Function call
function minimumIncrement ( arr , N ) { if ( N % 2 != 0 ) { document . write ( " " ) ; System . exit ( 0 ) ; } var cntEven = 0 ; var cntOdd = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { cntEven += 1 ; } } cntOdd = N - cntEven ; return Math . abs ( cntEven - cntOdd ) / 2 ; } var arr = [ 1 , 3 , 4 , 9 ] ; var N = arr . length ; document . write ( minimumIncrement ( arr , N ) ) ;
Count ways to construct array with even product from given array such that absolute difference of same indexed elements is at most 1 | Function to find count the ways to construct an array , B such that abs ( A [ i ] - B [ i ] ) <= 1 and product of elements of B is even ; Stores count of arrays B such that abs ( A [ i ] - B [ i ] ) <= 1 ; Stores count of arrays B whose product of elements is not even ; Traverse the array ; Update total ; If A [ i ] is an even number ; Update oddArray ; Print var 3 ^ N - 2 ^ X ; Driver Code
function cntWaysConsArray ( A , N ) { var total = 1 ; var oddArray = 1 ; for ( i = 0 ; i < N ; i ++ ) { total = total * 3 ; if ( A [ i ] % 2 == 0 ) { oddArray *= 2 ; } } document . write ( total - oddArray ) ; } var A = [ 2 , 4 ] ; var N = A . length ; cntWaysConsArray ( A , N ) ;
Count numbers up to N whose rightmost set bit is K | Function to count the numbers in the range [ 1 , N ] whose rightmost set bit is K ; Stores the number whose rightmost set bit is K ; Numbers whose rightmost set bit is i ; Subtracting the number whose rightmost set bit is i , from N ; Since i = k , then the number whose rightmost set bit is K is stored ; Driver Code
function countNumberHavingKthBitSet ( N , K ) { let numbers_rightmost_setbit_K = 0 ; for ( let i = 1 ; i <= K ; i ++ ) { let numbers_rightmost_bit_i = ( N + 1 ) / 2 ; N -= numbers_rightmost_bit_i ; if ( i == K ) { numbers_rightmost_setbit_K = numbers_rightmost_bit_i ; } } document . write ( numbers_rightmost_setbit_K ) ; } let N = 15 ; let K = 2 ; countNumberHavingKthBitSet ( N , K ) ;
Count odd and even Binomial Coefficients of N | Function to count set bits in binary representation of number N ; Count set bits in N ; Return the final count ; Driver Code ; Print odd Binomial coefficients ; Print even Binomial coefficients
function countSetBits ( N ) { let count = 0 ; while ( N != 0 ) { N = N & ( N - 1 ) ; count ++ ; } return count ; } let N = 4 ; let bits = countSetBits ( N ) ; document . write ( " " + " " + ( Math . pow ( 2 , bits ) ) + " " ) ; document . write ( " " + " " + ( N + 1 - ( Math . pow ( 2 , bits ) ) ) ) ;
Make all array elements even by replacing any pair of array elements with their sum | Function to find the minimum number of replacements required to make all array elements even ; Stores the count of odd elements ; Traverse the array ; Increase count of odd elements ; Store number of replacements required ; Two extra moves will be required to make the last odd element even ; Print the minimum replacements ; Driver Code ; Function call
function minMoves ( arr , N ) { var odd_element_cnt = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != 0 ) { odd_element_cnt ++ ; } } var moves = Math . floor ( ( odd_element_cnt ) / 2 ) ; if ( odd_element_cnt % 2 != 0 ) moves += 2 ; document . write ( moves ) ; } var arr = [ 5 , 6 , 3 , 7 , 20 ] ; N = arr . length ; minMoves ( arr , N ) ;
Split squares of first N natural numbers into two sets with minimum absolute difference of their sums | Function to partition squares of N natural number in two subset ; Store the count of blocks of size 8 ; Partition of block of 8 element ; Store the minimum subset difference ; Partition of N elements to minimize their subset sum difference ; Store elements of subset A and B ; If element is of type A ; If the element is of type B ; Print the minimum subset difference ; Print the first subset ; Print the second subset ; Driver code ; Function Call
function minimumSubsetDifference ( N ) { let blockOfSize8 = N / 8 ; let str = " " ; let subsetDifference = 0 ; let partition = " " ; while ( blockOfSize8 -- > 0 ) { partition += str ; } let A = [ ] ; let B = [ ] ; let x = 0 , y = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( partition [ i ] == ' ' ) { A [ x ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } else { B [ y ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } } document . write ( subsetDifference + " " ) ; for ( let i = 0 ; i < x ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) ; for ( let i = 0 ; i < y ; i ++ ) document . write ( B [ i ] + " " ) ; } let N = 8 ; minimumSubsetDifference ( N ) ;