codeparrot/xlcost-text-to-code huggingface dataset:
Find the side of the squares which are inclined diagonally and lined in a row | javascript program to find side of the squares inclined and touch each other externally at vertices and are lined in a row and distance between the centers of first and last squares is given ; Driver code
function radius ( n , d ) { document . write ( " " + ( d / ( ( n - 1 ) * Math . sqrt ( 2 ) ) ) . toFixed ( 5 ) ) ; } var d = 42 , n = 4 ; radius ( n , d ) ;
Program to calculate area of inner circle which passes through center of outer circle and touches its circumference | Function calculate the area of the inner circle ; the radius cannot be negative ; area of the circle ; Driver Code
function innerCirclearea ( radius ) { if ( radius < 0 ) { return - 1 ; } let r = radius / 2 ; let Area = ( 3.14 * Math . pow ( r , 2 ) ) ; return Area ; } let radius = 4 ; document . write ( " " + innerCirclearea ( radius ) ) ;
Angle subtended by the chord when the angle subtended by another chord of same length is given | JavaScript program to find the angle subtended at the center by the chord when the angle subtended at center by another chord of equal length is given ; Driver code
function angleequichord ( z ) { document . write ( " " + z + " " + " " ) ; } let z = 48 ; angleequichord ( z ) ;
Distance of chord from center when distance between center and another equal length chord is given | JavaScript program to find the distance of chord from center when distance between center and another equal length chord is given ; Driver code
function lengequichord ( z ) { document . write ( " " + " " + z + " " ) ; } let z = 48 ; lengequichord ( z ) ;
Length of the perpendicular bisector of the line joining the centers of two circles | javascript program to find the Length of the perpendicular bisector of the line joining the centers of two circles in which one lies completely inside touching the bigger circle at one point ; Driver code
function lengperpbisect ( r1 , r2 ) { var z = 2 * Math . sqrt ( ( r1 * r1 ) - ( ( r1 - r2 ) * ( r1 - r2 ) / 4 ) ) ; document . write ( " " + " " + z . toFixed ( 5 ) ) ; } var r1 = 5 , r2 = 3 ; lengperpbisect ( r1 , r2 ) ;
Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given | javascript program to find the length of the chord the circle if length of the another chord which is equally inclined through the diameter is given ; Driver code
function lengchord ( z ) { document . write ( " " + z ) ; } var z = 48 ; lengchord ( z ) ;
Exterior angle of a cyclic quadrilateral when the opposite interior angle is given | javascript program to find the exterior angle of a cyclic quadrilateral when the opposite interior angle is given ; Driver code
function angleextcycquad ( z ) { document . write ( " " + " " + z + " " ) ; } var z = 48 ; angleextcycquad ( z ) ;
Angle between a chord and a tangent when angle in the alternate segment is given | javascript program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code
function anglechordtang ( z ) { document . write ( " " + " " + z + " " ) ; } var z = 48 ; anglechordtang ( z ) ;
Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number | Function to check if a number is perfect number or not ; Stores sum of proper divisors ; If sum of digits is equal to N , then it 's a perfect number ; Otherwise , not a perfect number ; Function to find the sum of digits of a number ; Stores sum of digits ; Return sum of digits ; Function to count perfect numbers from an array whose sum of digits is also perfect ; Traverse the array ; If number is perfect ; Stores sum of digits of the number ; If that is also perfect number ; Print that number ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
function isPerfect ( N ) { let sumOfDivisors = 1 ; for ( let i = 2 ; i <= Math . floor ( N / 2 ) ; ++ i ) { if ( N % i === 0 ) { sumOfDivisors += i ; } } if ( sumOfDivisors === N ) { return 1 ; } else return 0 ; } function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum += ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } return sum ; } function countPerfectNumbers ( arr , N ) { for ( let i = 0 ; i < N ; ++ i ) { if ( isPerfect ( arr [ i ] ) ) { let sum = sumOfDigits ( arr [ i ] ) ; if ( isPerfect ( sum ) ) { document . write ( arr [ i ] + " " ) ; } } } } let arr = [ 3 , 8 , 12 , 28 , 6 ] ; let N = arr . length ; countPerfectNumbers ( arr , N ) ;
Sum of Bitwise AND of the sum of all leaf and non | Structure of a Binary tree node ; Helper function to allocate a new node with the given data and left and right pointers as None ; Function to calculate the sum of bitwise AND of the sum of all leaf nodes and non - leaf nodes for each level ; Initialize a queue and append root to it ; Store the required answer ; Stores the sum of leaf nodes at the current level ; Stores the sum of non - leaf nodes at the current level ; Get the size of the queue ; Iterate for all the nodes in the queue currently ; Dequeue a node from queue ; Check if the node is a leaf node ; If true , update the leaf node sum ; Otherwise , update the non - leaf node sum ; Enqueue left and right children of removed node ; Update the answer ; Return the answer ; Given Tree ; Function Call
class TreeNode { constructor ( x ) { this . val = x ; this . left = null ; this . right = null ; } } function findSum ( root ) { let que = [ ] ; que . push ( root ) ; let ans = 0 ; while ( que . length > 0 ) { let leaf = 0 ; let nonleaf = 0 ; let length = que . length ; while ( length > 0 ) { let temp = que . shift ( ) ; if ( temp . left == null && temp . right == null ) leaf += temp . val ; else nonleaf += temp . val ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; length -= 1 ; } ans += leaf & nonleaf ; } return ans ; } let root = new TreeNode ( 5 ) ; root . left = new TreeNode ( 3 ) ; root . right = new TreeNode ( 9 ) ; root . left . left = new TreeNode ( 6 ) ; root . left . right = new TreeNode ( 4 ) ; root . left . left . right = new TreeNode ( 7 ) ; document . write ( findSum ( root ) ) ;
Minimize replacements to make every element in an array exceed every element in another given array | Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code
function Mletime ( a , b , n , m ) { let ans = 2147483647 ; let prefix_a = [ ] ; for ( let i = 0 ; i < 10 ; i ++ ) { prefix_a [ i ] = 0 ; } let prefix_b = [ ] ; for ( let i = 0 ; i < 10 ; i ++ ) { prefix_b [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( let i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( let i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( let i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( let i = 0 ; i <= 9 ; i ++ ) { ans = Math . min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = Math . min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } document . write ( ans ) ; } let A = [ 0 , 0 , 1 , 3 , 3 ] ; let B = [ 2 , 0 , 3 ] ; let N = A . length ; let M = B . length ; Mletime ( A , B , N , M ) ;
Count remaining array elements after reversing binary representation of each array element | Function to reverse the binary representation of a number ; Traverse bits of N from the right ; Bitwise left shift ' rev ' by 1 ; If current bit is '1' ; Bitwise right shift N by 1 ; Required number ; Function to count elements from the original array that are also present in the array formed by reversing the binary representation of each element ; Stores the reversed num ; Iterate from [ 0 , N ] ; Stores the presence of integer ; Stores count of elements present in original array ; Traverse the array ; If current number is present ; Print the answer ; Driver Code
function findReverse ( N ) { let rev = 0 ; while ( N > 0 ) { rev <<= 1 ; if ( N & 1 == 1 ) rev ^= 1 ; N >>= 1 ; } return rev ; } function countElements ( arr , N ) { let ans = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { ans . push ( findReverse ( arr [ i ] ) ) ; } let cnt = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { cnt [ arr [ i ] ] = 1 ; } let count = 0 ; for ( let i = 0 ; i < ans . length ; i ++ ) { if ( cnt [ ans [ i ] ] ) count ++ ; } document . write ( count , ' ' ) ; } let arr = [ 1 , 30 , 3 , 8 , 12 ] ; let N = arr . length ; countElements ( arr , N ) ;
Fizz Buzz Implementation | Set 2 | Function to generate FizzBuzz sequence ; Stores count of multiples of 3 and 5 respectively ; Iterate from 1 to N ; Increment count3 by 1 ; Increment count5 by 1 ; Initialize a boolean variable to check if none of the condition matches ; Check if the value of count3 is equal to 3 ; Reset count3 to 0 , and set flag as True ; Check if the value of count5 is equal to 5 ; Reset count5 to 0 , and set flag as True ; If none of the condition matches ; Driver Code
function fizzBuzz ( N ) { let count3 = 0 ; let count5 = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { count3 ++ ; count5 ++ ; let flag = false ; if ( count3 == 3 ) { document . write ( " " ) ; count3 = 0 ; flag = true ; } if ( count5 == 5 ) { document . write ( " " ) ; count5 = 0 ; flag = true ; } if ( ! flag ) { document . write ( i ) ; } document . write ( " " ) ; } } let N = 15 ; fizzBuzz ( N ) ;
Check if diagonal elements of a Matrix are Prime or not | Stores if a number is prime or not ; Function to generate and store primes using Sieve Of Eratosthenes ; Set all numbers as prime ; If p is a prime ; Set all its multiples as non - prime ; Function to check if all diagonal elements are prime or not ; Stores if all diagonal elements are prime or not ; Precompute primes ; Traverse the range [ 0 , N - 1 ] ; Check if numbers on the cross diagonal and main diagonal are primes or not ; If true , then print " Yes " ; Otherwise , print " No " ; Driver Code ; Function Call
var prime = Array ( 1000005 ) . fill ( true ) ; function SieveOfEratosthenes ( N ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= N ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i <= N ; i += p ) prime [ i ] = false ; } } } function checkElementsOnDiagonal ( M , N ) { var flag = 1 ; SieveOfEratosthenes ( 1000000 ) ; for ( var i = 0 ; i < N ; i ++ ) { flag &= ( prime [ M [ i ] [ i ] ] && prime [ M [ i ] [ N - 1 - i ] ] ) ; } if ( flag ) document . write ( " " ) ; else document . write ( " " ) ; } var M = [ [ 1 , 2 , 3 , 13 ] , [ 5 , 3 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 7 ] ] ; var N = M . length ; checkElementsOnDiagonal ( M , N ) ;
Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | Function to find the permutation of integers from a given range such that number of mismatching bits between pairs of adjacent elements is 1 ; Initialize an arrayList to store the resultant permutation ; Store the index of rotation ; Iterate over the range [ 0 , N - 1 ] ; Traverse all the array elements up to ( 2 ^ k ) - th index in reverse ; If current element is S ; Check if S is zero ; Rotate the array by index value to the left ; Driver Code
function circularPermutation ( n , start ) { var res = [ 0 ] ; var ret = [ ] ; var index = - 1 ; for ( var k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( var i = res . length - 1 ; i >= 0 ; i -- ) { if ( res [ i ] + add == start ) index = res . length ; res . push ( res [ i ] + add ) ; } } if ( start == 0 ) return res ; while ( ret . length < res . length ) { ret . push ( res [ index ] ) ; index = ( index + 1 ) % res . length ; } return ret ; } var N = 2 , S = 3 ; var print = circularPermutation ( N , S ) ; document . write ( " " ) ; for ( var i = 0 ; i < print . length - 1 ; i ++ ) { document . write ( print [ i ] + " " ) ; } document . write ( print [ print . length - 1 ] + " " ) ;
Count pairs from an array having equal sum and quotient | Function to count all pairs ( i , j ) such that a [ i ] + [ j ] = a [ i ] / a [ j ] ; Stores total count of pairs ; Generate all possible pairs ; If a valid pair is found ; Increment count ; Return the final count ; Driver Code
function countPairs ( a , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] != 0 && a [ i ] % a [ j ] == 0 ) { if ( ( a [ i ] + a [ j ] ) == ( a [ i ] / a [ j ] ) ) count ++ ; } } } return count ; } var arr = [ - 4 , - 3 , 0 , 2 , 1 ] ; var N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Count pairs from an array having equal sum and quotient | Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Prlet the final count ; Driver Code ; Function Call
function countPairs ( a , n ) { let count = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let y = a [ i ] ; if ( y != 0 && y != 1 ) { let x = ( ( y * 1.0 ) / ( 1 - y ) ) * y ; if ( mp . has ( x ) ) count += mp . get ( x ) ; } if ( mp . has ( y ) ) { mp . set ( y , mp . get ( y ) + 1 ) ; } else { mp . set ( y , 1 ) ; } } return count ; } let arr = [ - 4 , - 3 , 0 , 2 , 1 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not ; Stores if the digit exists or not ; Iterate till nums is non - zero ; Find the remainder ; If the remainder is K ; Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal and octal representation ; Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation ; Traverse the range [ 0 , 1e6 + 5 ] ; Check if i contains the digit ' K ' in its decimal or octal representation ; Update pref [ i ] ; Print the answer of queries ; Driver Code ; Function Call
function contains ( num , K , base ) { var isThere = 0 ; while ( num ) { var remainder = num % base ; if ( remainder == K ) { isThere = 1 ; } num /= base ; } return isThere ; } function count ( n , k , v ) { var pref = Array ( 100005 ) . fill ( 0 ) ; for ( var i = 1 ; i < 100005 ; i ++ ) { var present = contains ( i , k , 10 ) || contains ( i , k , 8 ) ; pref [ i ] += pref [ i - 1 ] + present ; } for ( var i = 0 ; i < n ; ++ i ) { document . write ( v [ i ] [ 1 ] - v [ i ] [ 0 ] + 1 - ( pref [ v [ i ] [ 1 ] ] - pref [ v [ i ] [ 0 ] - 1 ] ) + ' ' ) ; } } var K = 7 ; var Q = [ [ 2 , 5 ] , [ 1 , 15 ] ] ; var N = Q . length ; count ( N , K , Q ) ;
Count tiles of dimensions 2 * 1 that can be placed in an M * N rectangular board that satisfies the given conditions | Function to count tiles of dimensions 2 x 1 that can be placed in a grid of dimensions M * N as per given conditions ; Number of tiles required ; Driver Code
function numberOfTiles ( n , m ) { if ( n % 2 == 1 ) { return - 1 ; } return ( m * n ) / 2 ; } var n = 2 , m = 4 ; document . write ( numberOfTiles ( n , m ) ) ;
Check if an array can be converted to another given array by swapping pairs of unequal elements | Function to check if arr1 [ ] can be converted to arr2 [ ] by swapping pair ( i , j ) such that i < j and arr [ i ] is 1 and arr [ j ] is 0 ; Stores the differences of prefix sum of arr1 and arr2 ; Stores the count of 1 and zero of arr1 ; Stores the count of 1 and zero of arr2 ; Iterate in the range [ 0 , N - 1 ] ; If arr1 [ i ] is 1 , then increment arr1_one by one ; Otherwise increment arr1_zero by one ; If arr2 [ i ] is 1 , then increment arr2_one by one ; Otherwise increment arr2_zero by one ; Check if number of 1 s and 0 s of arr1 is equal to number of 1 s and 0 s of arr2 respectievly ; Iterate over the range [ 0 , N - 1 ] ; Increment count by differences arr1 [ i ] and arr2 [ i ] ; Check if number of 1 's in  arr2 are more than arr1 and  then print "No" ; Finally , print " Yes " ; Given input arrays ; Size of the array ; Function Call
function canMakeEqual ( arr1 , arr2 , N ) { var count = 0 ; var arr1_one = 0 , arr1_zero = 0 ; var arr2_one = 0 , arr2_zero = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] == 1 ) { arr1_one ++ ; } else if ( arr1 [ i ] == 0 ) { arr1_zero ++ ; } if ( arr2 [ i ] == 1 ) { arr2_one ++ ; } else if ( arr2 [ i ] == 0 ) { arr2_zero ++ ; } } if ( arr1_one != arr2_one arr1_zero != arr2_zero ) { document . write ( " " ) ; return ; } for ( var i = 0 ; i < N ; i ++ ) { count = count + ( arr1 [ i ] - arr2 [ i ] ) ; if ( count < 0 ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var arr1 = [ 0 , 1 , 1 , 0 ] ; var arr2 = [ 0 , 0 , 1 , 1 ] ; var N = arr1 . length ; canMakeEqual ( arr1 , arr2 , N ) ;
Check for each subarray whether it consists of all natural numbers up to its length or not | Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i leto st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver Code
function checksubarrayExist1_N ( arr , N ) { let pos = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { pos . set ( arr [ i ] , i ) ; } let st = new Set ( ) ; for ( let i = 1 ; i <= N ; i ++ ) { st . add ( pos . get ( i ) ) ; let Min = Math . min ( ... st ) ; let Max = Math . max ( ... st ) ; if ( Max - Min + 1 == i ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } let arr = [ 1 , 4 , 3 , 2 ] ; let N = arr . length ; checksubarrayExist1_N ( arr , N ) ;
Check if a pair of integers A and B can coincide by shifting them by distances arr [ ( A % N + N ) % N ] and arr [ ( B % N + N ) % N ] | Function to check if two integers coincide at a point ; Store the final position of integers A and B ; Iterate over the range [ 0 , n ] ; Store the final position of the integer i ; If temp is present in the Map ; Print Yes and return ; Mark its final position as visited ; If every integer stored in the Map is unique ; Driver Code
function checkSamePosition ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { var temp = ( ( i + arr [ i ] ) % n + n ) % n ; if ( mp . has ( temp ) ) { document . write ( " " ) ; return ; } if ( mp . has ( temp ) ) { mp . set ( temp , mp . get ( temp ) + 1 ) } else mp . set ( temp , 1 ) } document . write ( " " ) ; } var arr = [ 5 , 4 , 3 ] ; var N = arr . length ; checkSamePosition ( arr , N ) ;
Sum of Fibonacci Numbers | Set 2 | Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code
function sumFib ( N ) { var num = Math . round ( Math . pow ( ( Math . sqrt ( 5 ) + 1 ) / 2.0 , N + 2 ) / Math . sqrt ( 5 ) ) ; document . write ( num - 1 ) ; } var N = 3 ; sumFib ( N ) ;
Sum of Fibonacci Numbers | Set 2 | Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Prvar the result ; Driver Code ; Function Call
function sumFib ( N ) { var num = ( ( 1 - Math . sqrt ( 5 ) ) / 2 ) ; var val = Math . round ( Math . abs ( 1 / ( Math . pow ( num , N + 2 ) + Math . pow ( num , N + 1 ) + Math . pow ( num , N ) + Math . pow ( num , N - 1 ) ) ) - 1 ) ; document . write ( val ) ; } var N = 3 ; sumFib ( N ) ;
Make all array elements equal to K by repeatedly incrementing subsequences | Function to find the minimum number of operations required to make all elements equal to k ; Initialize a hashmap ; Store frequency of array elements ; Store the minimum number of operations required ; Iterate until all array elements becomes equal to K ; Iterate through range [ 1 , k - 1 ] since only one element can be increased from each group ; Check if the current number has frequency > 0 , i . e . , it is a part of a group ; If true , decrease the frequency of current group of element by 1 ; Increase the frequency of the next group of elements by 1 ; If the next element is not the part of any group , then skip it ; Increment count of operations ; Print the count of operations ; Driver Code ; Function Call
function minOperations ( arr , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } let ans = 0 ; while ( mp . has ( k ) == false || mp . get ( k ) < n ) { for ( let i = 1 ; i <= k - 1 ; i ++ ) { if ( mp . has ( i ) && mp . get ( i ) > 0 ) { mp . set ( i , mp . get ( i ) - 1 ) ; if ( mp . has ( i + 1 ) ) mp . set ( i + 1 , mp . get ( i + 1 ) + 1 ) ; else mp . set ( i + 1 , 1 ) ; if ( mp . has ( i + 1 ) && mp . get ( i + 1 ) == 1 ) { i ++ ; } } } ans ++ ; } document . write ( ans ) ; } let arr = [ 2 , 3 , 3 , 4 ] ; let K = 5 ; let N = arr . length ; minOperations ( arr , N , K ) ;
Minimum number of pigs required to find the poisonous bucket | Function to find the minimum number of pigs required to find the poisonous bucket ; Print the result ; Driver Code
function poorPigs ( buckets , minutesToDie , minutesToTest ) { document . write ( Math . ceil ( Math . log ( buckets ) / Math . log ( ( minutesToTest / minutesToDie ) + 1 ) ) ) ; } let N = 1000 , M = 15 , P = 60 ; poorPigs ( N , M , P ) ;
Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum | Function to find the maximum product possible after repeatedly replacing pairs of adjacent array elements with their sum ; Store the maximum product ; Store the prefix sum ; Store the total sum of array ; Traverse the array to find the total sum ; Iterate in the range [ 0 , N - 2 ] ; Add arr [ i ] to prefix_sum ; Store the value of prefix_sum ; Store the value of ( total sum - prefix sum ) ; Update the maximum product ; Print the answer ; Driver Code
function maxProduct ( arr , N ) { var max_product = Number . MIN_VALUE ; var prefix_sum = 0 ; var sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( i = 0 ; i < N - 1 ; i ++ ) { prefix_sum += arr [ i ] ; var X = prefix_sum ; var Y = sum - prefix_sum ; max_product = Math . max ( max_product , X * Y ) ; } document . write ( max_product ) ; } var arr = [ 2 , 3 , 5 , 6 , 7 ] ; var N = arr . length ; maxProduct ( arr , N ) ;
Find array elements with rightmost set bit at the position of the rightmost set bit in K | Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Input ; Function call to find the elements having same rightmost set bit as of K
function findMask ( K ) { let mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } function sameRightSetBitPos ( arr , N , K ) { let mask = findMask ( K ) ; let pos = ( K & mask ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 3 , 4 , 6 , 7 , 9 , 12 , 15 ] ; let N = arr . length ; let K = 7 ; sameRightSetBitPos ( arr , N , K ) ;