codeparrot/xlcost-text-to-code huggingface dataset:
Smallest pair of integers with minimum difference whose Bitwise XOR is N | Function to find the numbers A and B whose Bitwise XOR is N and the difference between them is minimum ; Find the MSB of the N ; Find the value of B ; Find the value of A ; Print the result ; Driver Code
function findAandB ( N ) { let K = Math . log2 ( N ) ; let B = ( 1 << K ) ; let A = B ^ N ; document . write ( A + ' ' + B ) ; } let N = 26 ; findAandB ( N ) ;
Find all possible values of K such that the sum of first N numbers starting from K is G | Function to find the count the value of K such that sum of the first N numbers from K is G ; Stores the total count of K ; Iterate till square root of g ; If the number is factor of g ; If the second factor is not equal to first factor ; Check if two factors are odd or not ; If second factor is the same as the first factor then check if the first factor is odd or not ; Print the resultant count ; Driver code
function findValuesOfK ( g ) { var count = 0 ; for ( var i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { if ( i != g / i ) { if ( i % 2 == 1 ) { count ++ ; } if ( ( g / i ) % 2 == 1 ) { count ++ ; } } else if ( i % 2 == 1 ) { count ++ ; } } } document . write ( count ) ; } var G = 125 ; findValuesOfK ( G ) ;
Difference between maximum and minimum average of all K | Function to find the difference between the maximum and minimum subarrays of length K ; Stores the sum of subarray over the range [ 0 , K ] ; Iterate over the range [ 0 , K ] ; Store min and max sum ; Iterate over the range [ K , N - K ] ; Increment sum by arr [ i ] - arr [ i - K ] ; Update max and min moving sum ; Return difference between max and min average ; Given Input ; Function Call
function Avgdifference ( arr , N , K ) { let sum = 0 ; for ( let i = 0 ; i < K ; i ++ ) sum += arr [ i ] ; let min = sum ; let max = sum ; for ( let i = K ; i <= N - K + 1 ; i ++ ) { sum += arr [ i ] - arr [ i - K ] ; if ( min > sum ) min = sum ; if ( max < sum ) max = sum ; } return ( max - min ) / K ; } let arr = [ 3 , 8 , 9 , 15 ] ; let N = arr . length ; let K = 2 ; document . write ( Avgdifference ( arr , N , K ) ) ;
Count of distinct integers in range [ 1 , N ] that do not have any subset sum as K | Function to find maximum number of distinct integers in [ 1 , N ] having no subset with sum equal to K ; Declare a vector to store the required numbers ; Store all the numbers in [ 1 , N ] except K ; Store the maximum number of distinct numbers ; Reverse the array ; Print the required numbers ; Given Input ; Function Call
function findSet ( N , K ) { let a = [ ] ; for ( let i = 1 ; i <= N ; i ++ ) { if ( i != K ) a . push ( i ) ; } let MaxDistinct = ( N - K ) + parseInt ( K / 2 ) ; a . reverse ( ) ; for ( let i = 0 ; i < MaxDistinct ; i ++ ) document . write ( a [ i ] + " " ) ; } let N = 5 , K = 3 ; findSet ( N , K ) ;
Solve Linear Congruences Ax = B ( mod N ) for values of x in range [ 0 , N | Function to stores the values of x and y and find the value of gcd ( a , b ) ; Base Case ; Store the result of recursive call ; Update x and y using results of recursive call ; Function to give the distinct solutions of ax = b ( mod n ) ; Function Call to find the value of d and u ; No solution exists ; Else , initialize the value of x0 ; Print all the answers ; Input ; Function Call
function ExtendedEuclidAlgo ( a , b ) { if ( a == 0 ) { return [ b , 0 , 1 ] ; } else { let x1 = 1 , y1 = 1 ; let gcdy = ExtendedEuclidAlgo ( b % a , a ) ; let gcd = gcdy [ 0 ] ; x1 = gcdy [ 1 ] ; y1 = gcdy [ 2 ] ; let y = x1 ; let x = y1 - Math . floor ( b / a ) * x1 ; return [ gcd , x , y ] ; } } function linearCongruence ( A , B , N ) { A = A % N ; B = B % N ; let u = 0 , v = 0 ; let person = ExtendedEuclidAlgo ( A , N ) ; let d = person [ 0 ] ; u = person [ 1 ] ; v = person [ 2 ] ; if ( B % d != 0 ) { document . write ( - 1 ) ; return ; } let x0 = ( u * ( B / d ) ) % N ; if ( x0 < 0 ) x0 += N ; for ( let i = 0 ; i <= d - 1 ; i ++ ) { let an = ( x0 + i * ( N / d ) ) % N ; document . write ( an + " " ) ; } } let A = 15 ; let B = 9 ; let N = 18 ; linearCongruence ( A , B , N ) ;
Factorial of a number without using multiplication | Function to calculate factorial of the number without using multiplication operator ; variable to store the final factorial ; Outer loop ; Inner loop ; Input ; Function calling
function factorialWithoutMul ( N ) { let ans = N ; for ( let i = N - 1 ; i > 0 ; i -- ) { let sum = 0 ; for ( let j = 0 ; j < i ; j ++ ) sum += ans ; ans = sum ; } return ans ; } let N = 5 ; document . write ( factorialWithoutMul ( N ) ) ;
Sum of Bitwise AND of all unordered triplets of an array | Function to calculate sum of Bitwise AND of all unordered triplets from a given array such that ( i < j < k ) ; Stores the resultant sum of Bitwise AND of all triplets ; Generate all triplets of ( arr [ i ] , arr [ j ] , arr [ k ] ) ; Add Bitwise AND to ans ; Print the result ; Driver Code
function tripletAndSum ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) { ans += arr [ i ] & arr [ j ] & arr [ k ] ; } } } document . write ( ans ) ; } let arr = [ 3 , 5 , 4 , 7 ] ; let N = arr . length tripletAndSum ( arr , N ) ;
Sum of Bitwise AND of all unordered triplets of an array | Function to calculate sum of Bitwise AND of all unordered triplets from a given array such that ( i < j < k ) ; Stores the resultant sum of Bitwise AND of all triplets ; Traverse over all the bits ; Count number of elements with the current bit set ; There are ( cnt ) C ( 3 ) numbers with the current bit set and each triplet contributes 2 ^ bit to the result ; Return the resultant sum ; Driver Code
function tripletAndSum ( arr , n ) { let ans = 0 ; for ( let bit = 0 ; bit < 32 ; bit ++ ) { let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & ( 1 << bit ) ) cnt ++ ; } ans += ( 1 << bit ) * cnt * ( cnt - 1 ) * ( cnt - 2 ) / 6 ; } return ans ; } let arr = [ 3 , 5 , 4 , 7 ] ; let N = arr . length ; document . write ( tripletAndSum ( arr , N ) ) ;
Lexicographically smallest permutation of length 2 N that can be obtained from an N | Function to find the lexicographically smallest permutation of length 2 * N satisfying the given conditions ; Stores if i - th element is placed at odd position or not ; Traverse the array ; Mark arr [ i ] true ; Stores all the elements not placed at odd positions ; Iterate in the range [ 1 , 2 * N ] ; If w [ i ] is not marked ; Stores whether it is possible to obtain the required permutation or not ; Stores the permutation ; Traverse the array arr [ ] ; If it is S . end ( ) ; Mark found false ; Push arr [ i ] and * it into the array ; Erase the current element from the Set ; If found is not marked ; Otherwise , ; Print the permutation ; Given Input ; Function call
function smallestPermutation ( arr , N ) { let w = new Array ( 2 * N + 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { w [ arr [ i ] ] = true ; } let S = new Set ( ) ; for ( let i = 1 ; i <= 2 * N ; i ++ ) { if ( ! w [ i ] ) S . add ( i ) ; } let found = true ; let P = [ ] ; let p = [ 4 , 5 , 1 , 2 , 3 , 6 ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( S . has ( arr [ i ] ) ) { found = false ; break ; } P . push ( arr [ i ] ) ; P . push ( arr [ i ] ) ; S . delete ( arr [ i ] ) ; } if ( ! found ) { document . write ( " " ) ; } else { for ( let i = 0 ; i < 2 * N ; i ++ ) document . write ( p [ i ] + " " ) ; } } let arr = [ 4 , 1 , 3 ] ; let N = arr . length ; smallestPermutation ( arr , N ) ;
Maximum sum of a subsequence having difference between their indices equal to the difference between their values | Function to find the maximum sum of a subsequence having difference between indices equal to difference in their values ; Stores the maximum sum ; Stores the value for each A [ i ] - i ; Traverse the array ; Update the value in map ; Update the answer ; Finally , print the answer ; Given Input ; Function Call
function maximumSubsequenceSum ( A , N ) { var ans = 0 ; var mp = new Map ( ) ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( mp . has ( A [ i ] - i ) ) mp . set ( A [ i ] - i , mp . get ( A [ i ] - i ) + A [ i ] ) ; else mp . set ( A [ i ] - i , A [ i ] ) ; ans = Math . max ( ans , mp . get ( A [ i ] - i ) ) ; } document . write ( ans ) ; } var A = [ 10 , 7 , 1 , 9 , 10 , 1 ] ; var N = A . length ; maximumSubsequenceSum ( A , N ) ;
Find the nearest perfect square for each element of the array | Function to find the nearest perfect square for every element in the given array ; Traverse the array ; Calculate square root of current element ; Calculate perfect square ; Find the nearest ; Driver Code
function nearestPerfectSquare ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let sr = parseInt ( Math . sqrt ( arr [ i ] ) ) ; let a = sr * sr ; let b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) document . write ( a + " " ) ; else document . write ( b + " " ) ; } } let arr = [ 5 , 2 , 7 , 13 ] ; let N = arr . length ; nearestPerfectSquare ( arr , N ) ;
Count of sets possible using integers from a range [ 2 , N ] using given operations that are in Equivalence Relation | Javascript program for tha above approach ; Sieve of Eratosthenes to find primes less than or equal to N ; Function to find number of Sets ; Handle Base Case ; Set which contains less than or equal to N / 2 ; Number greater than N / 2 and are prime increment it by 1 ; If the number is prime Increment answer by 1 ; Input ; Function Call
let prime = new Array ( 100001 ) ; function SieveOfEratosthenes ( n ) { for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } function NumberofSets ( N ) { SieveOfEratosthenes ( N ) ; if ( N == 2 ) { document . write ( 1 ) ; } else if ( N == 3 ) { document . write ( 2 ) ; } else { let ans = 1 ; for ( let i = Math . floor ( N / 2 ) + 1 ; i <= N ; i ++ ) { if ( prime [ i ] ) { ans += 1 ; } } document . write ( ans ) ; } } let N = 9 ; NumberofSets ( N ) ;
Absolute difference between floor of Array sum divided by X and floor sum of every Array element when divided by X | Function to find absolute difference between the two sum values ; Variable to store total sum ; Variable to store sum of A [ i ] / X ; Traverse the array ; Update totalSum ; Update perElementSum ; Floor of total sum divided by X ; Return the absolute difference ; Input ; Size of Array ; Function call to find absolute difference between the two sum values
function floorDifference ( A , N , X ) { let totalSum = 0 ; let perElementSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { totalSum += A [ i ] ; perElementSum += Math . floor ( A [ i ] / X ) ; } let totalFloorSum = Math . floor ( totalSum / X ) ; return Math . abs ( totalFloorSum - perElementSum ) ; } let A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let X = 4 ; let N = A . length ; document . write ( floorDifference ( A , N , X ) ) ;
Convert a number from base A to base B | Function to return ASCII value of a character ; Function to convert a number from given base to decimal number ; Stores the length of the var ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Update num ; Update power ; Function to return equivalent character of a given valueString . fromCharCode ; Function to convert a given decimal number to a given base ; Store the result ; Repeatedly divide inputNum by base and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a base to another base ; Convert the number from base A to decimal ; Convert the number from decimal to base B ; Prvar the result ; Given input ; Function Call
function val ( c ) { if ( c >= ' ' && c <= ' ' ) return c . charCodeAt ( 0 ) - 48 ; else return c . charCodeAt ( 0 ) - 65 + 10 ; } function toDeci ( str , base ) { var len = str . length ; var power = 1 ; var num = 0 ; for ( var i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { document . write ( " " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } function reVal ( num ) { if ( num >= 0 && num <= 9 ) return String . fromCharCode ( num + 48 ) ; else return String . fromCharCode ( num - 10 + 65 ) ; } function fromDeci ( base , inputNum ) { var res = " " ; while ( inputNum > 0 ) { res += reVal ( inputNum % base ) ; inputNum = Math . floor ( inputNum / base ) ; } res = res . split ( " " ) . reverse ( ) . join ( " " ) ; return res ; } function convertBase ( s , a , b ) { var num = toDeci ( s , a ) ; var ans = fromDeci ( b , num ) ; document . write ( ans ) ; } var s = " " ; var a = 16 var b = 10 ; convertBase ( s , a , b ) ;
Prefix Factorials of a Prefix Sum Array | Function to find the factorial of a number N ; Base Case ; Find the factorial recursively ; Function to find the prefix factorial array ; Find the prefix sum array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
function fact ( N ) { if ( N == 1 N == 0 ) return 1 ; return N * fact ( N - 1 ) ; } function prefixFactorialArray ( arr , N ) { for ( let i = 1 ; i < N ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = fact ( arr [ i ] ) ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; prefixFactorialArray ( arr , N ) ;
Mean of fourth powers of first N natural numbers | Function to find the average of the fourth power of first N natural numbers ; Store the resultant average calculated using formula ; Return the average ; Driver Code
function findAverage ( N ) { let avg = ( ( 6 * N * N * N * N ) + ( 15 * N * N * N ) + ( 10 * N * N ) - 1 ) / 30.0 ; return avg ; } let N = 3 ; document . write ( findAverage ( N ) . toFixed ( 4 ) ) ;
Modify array by removing ( arr [ i ] + arr [ i + 1 ] ) th element exactly K times | Function to modify array by removing every K - th element from the array ; Check if current element is the k - th element ; Stores the elements after removing every kth element ; Append the current element if it is not k - th element ; Return the new array after removing every k - th element ; Function to print the array ; Traverse the array l [ ] ; Function to print the array after performing the given operations exactly k times ; Store first N natural numbers ; Iterate over the range [ 0 , k - 1 ] ; Store sums of the two consecutive terms ; Remove every p - th element from the array ; Increment x by 1 for the next iteration ; Print the resultant array ; Given arrays ; Function Call
function removeEveryKth ( l , k ) { for ( let i = 0 ; i < l . length ; i ++ ) { if ( i % k == 0 ) l [ i ] = 0 ; } let arr = [ 0 ] ; for ( let i = 1 ; i < l . length ; i ++ ) { if ( l [ i ] != 0 ) arr . push ( l [ i ] ) ; } return arr ; } function printArray ( l ) { for ( let i = 1 ; i < l . length ; i ++ ) document . write ( l [ i ] + " " ) ; } function printSequence ( n , k ) { let l = [ 0 ] ; for ( let i = 0 ; i < n + 1 ; i ++ ) l [ i ] = i ; let x = 1 ; for ( let i = 0 ; i < k ; i ++ ) { let p = l [ x ] + l [ x + 1 ] ; l = removeEveryKth ( l , p ) ; x += 1 ; } printArray ( l ) ; } let N = 8 ; let K = 2 ; printSequence ( N , K ) ;
Minimum number of bits of array elements required to be flipped to make all array elements equal | Function to count minimum number of bits required to be flipped to make all array elements equal ; Stores the count of unset bits ; Stores the count of set bits ; Traverse the array ; Traverse the bit of arr [ i ] ; If current bit is set ; Increment fre1 [ j ] ; Otherwise ; Increment fre0 [ j ] ; Right shift x by 1 ; Stores the count of total moves ; Traverse the range [ 0 , 32 ] ; Update the value of ans ; Return the minimum number of flips required ; Driver Code
function makeEqual ( arr , n ) { var fre0 = Array ( 33 ) . fill ( 0 ) ; var fre1 = Array ( 33 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { var x = arr [ i ] ; for ( j = 0 ; j < 33 ; j ++ ) { if ( ( x & 1 ) != 0 ) { fre1 [ j ] += 1 ; } else { fre0 [ j ] += 1 ; } x = x >> 1 ; } } var ans = 0 ; for ( i = 0 ; i < 33 ; i ++ ) { ans += Math . min ( fre0 [ i ] , fre1 [ i ] ) ; } return ans ; } var arr = [ 3 , 5 ] ; var N = arr . length ; document . write ( makeEqual ( arr , N ) ) ;
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Traverse the array arr [ ] ; Traverse the vector ; If v [ i ] is divisible by K ; Iterate over the range [ 0 , K ] ; Update v ; Otherwise ; Traverse the vector v ; Return the sum of the updated array ; Driver Code
function sum ( arr , N , K ) { var sum = 0 ; var v = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { v . push ( arr [ i ] ) ; } for ( var i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] % K == 0 ) { var x = v [ i ] / K ; for ( var j = 0 ; j < K ; j ++ ) { v . push ( x ) ; } } else break ; } for ( var i = 0 ; i < v . length ; i ++ ) sum = sum + v [ i ] ; return sum ; } var arr = [ 4 , 6 , 8 , 2 ] ; var K = 2 ; var N = arr . length ; document . write ( sum ( arr , N , K ) ) ;
Check if sum of arr [ i ] / j for all possible pairs ( i , j ) in an array is 0 or not | Function to check if sum of all values of ( arr [ i ] / j ) for all 0 < i <= j < ( N - 1 ) is 0 or not ; Stores the required sum ; Traverse the array ; If the sum is equal to 0 ; Otherwise ; Driver Code
function check ( arr , N ) { var sum = 0 ; for ( i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , - 1 , 3 , - 2 , - 1 ] ; var N = arr . length ; check ( arr , N ) ;
Program to calculate expected increase in price P after N consecutive days | Function to find the increased value of P after N days ; Expected value of the number P after N day ; Driver code
function expectedValue ( P , a , b , N ) { var expValue = P + ( N * 0.5 * ( a + b ) ) ; return expValue ; } var P = 3000 var a = 20 var b = 10 var N = 30 document . write ( expectedValue ( P , a , b , N ) ) ;
Find the index in a circular array from which prefix sum is always non | Function to find the starting index of the given circular array s . t . prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array arr ; Update the value of sum ; If sum is less than min ; Update the min as the value of prefix sum ; Update in ; Otherwise , no such index is possible ; Driver Code
function startingPoint ( A , N ) { var sum = 0 ; var it = 0 ; var min = Number . MAX_VALUE ; for ( i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( sum < min ) { min = sum ; it = i + 1 ; } } if ( sum < 0 ) { return - 1 ; } return it % N ; } var arr = [ 3 , - 6 , 7 , - 4 , - 4 , 6 , - 1 ] ; var N = arr . length ; document . write ( startingPoint ( arr , N ) ) ;
Modify Linked List by replacing each node by nearest multiple of K | Structure of node ; Function to replace the node N by the nearest multiple of K ; Traverse the Linked List ; If data is less than K ; If the data of current node is same as K ; Otherwise change the value ; Move to the next node ; Return the updated LL ; Function to print the nodes of the Linked List ; Traverse the LL ; Print the node 's data ; Given Linked List
class node { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function EvalNearestMult ( N , K ) { var temp = N ; var t ; while ( N != null ) { if ( N . data < K ) N . data = 0 ; else { if ( N . data == K ) N . data = K ; else { N . data = parseInt ( N . data / K ) * K ; } } N = N . next ; } return temp ; } function printList ( N ) { while ( N != null ) { document . write ( N . data + " " ) ; N = N . next ; } } var head = null ; var second = null ; var third = null ; head = new node ( ) ; second = new node ( ) ; third = new node ( ) ; head . data = 3 ; head . next = second ; second . data = 4 ; second . next = third ; third . data = 8 ; third . next = null ; var t = EvalNearestMult ( head , 3 ) ; printList ( t ) ;
Modify array by replacing elements with the nearest power of its previous or next element | Function to calculate the power of y which is nearest to x ; Base Case ; Stores the logarithmic value of x with base y ; Function to replace each array element by the nearest power of its previous or next element ; Stores the previous and next element ; Traverse the array ; Calculate nearest power for previous and next elements ; Replacing the array values ; Print the updated array ; Given array
function nearestPow ( x , y ) { if ( y == 1 ) return 1 var k = Math . floor ( Math . log ( x ) / Math . log ( y ) ) if ( Math . abs ( Math . pow ( y , k ) - x ) < Math . abs ( Math . pow ( y , ( k + 1 ) ) - x ) ) return Math . pow ( y , k ) return Math . pow ( y , ( k + 1 ) ) } function replacebyNearestPower ( arr ) { var prev = arr [ arr . length - 1 ] var lastNext = arr [ 0 ] for ( var i = 0 ; i < arr . length ; i ++ ) { var temp = arr [ i ] if ( i == arr . length - 1 ) var next = lastNext else var next = arr [ ( i + 1 ) % arr . length ] var prevPow = nearestPow ( arr [ i ] , prev ) var nextPow = nearestPow ( arr [ i ] , next ) if ( Math . abs ( arr [ i ] - prevPow ) < Math . abs ( arr [ i ] - nextPow ) ) arr [ i ] = prevPow else arr [ i ] = nextPow prev = temp } document . write ( arr ) } var arr = [ 2 , 3 , 4 , 1 , 2 ] replacebyNearestPower ( arr )
Check if sum of array can be made equal to X by removing either the first or last digits of every array element | Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver code ; Function Call
function makeSumX ( arr , X , S , i , N ) { if ( i === N ) { return S === X } let a = ( arr [ i ] ) . toString ( ) let l = parseInt ( ( a . substr ( 0 , a . length - 1 ) ) ) let r = parseInt ( ( a . substr ( 1 ) ) ) let x = makeSumX ( arr , X , S + l , i + 1 , N ) ; let y = makeSumX ( arr , X , S + r , i + 1 , N ) ; return ( x y ) ; } function Check ( arr , X , N ) { if ( makeSumX ( arr , X , 0 , 0 , N ) ) { document . write ( " " ) } else { document . write ( " " ) } } let arr = [ 545 , 433 , 654 , 23 ] ; let N = arr . length ; let X = 134 ; Check ( arr , X , N ) ;
Count subarrays having product equal to the power of a given Prime Number | Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Input
function isPower ( m , y ) { let res1 = parseInt ( Math . log ( y ) / Math . log ( m ) ) ; let res2 = Math . log ( y ) / Math . log ( m ) ; return ( res1 == res2 ) ; } function numSub ( arr , n , m ) { let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isPower ( m , arr [ i ] ) ) { cnt ++ ; ans += parseInt ( ( cnt * ( cnt - 1 ) ) / 2 ) ; } else { cnt = 0 ; } } return ans ; } let arr = [ 1 , 1 , 1 , 3 ] ; let m = 3 ; let n = arr . length ; document . write ( numSub ( arr , n , m ) ) ;