codeparrot/xlcost-text-to-code huggingface dataset:
Find numbers which are multiples of first array and factors of second array | Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function lcm ( x , y ) { var temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } function findNumbers ( a , n , b , m ) { var lcmA = 1 , gcdB = 0 ; for ( var i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( var i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { document . write ( " " ) ; return ; } var num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) document . write ( num + " " ) ; num += lcmA ; } } var a = [ 1 , 2 , 2 , 4 ] ; var b = [ 16 , 32 , 64 ] ; var n = a . length ; var m = b . length ; findNumbers ( a , n , b , m ) ;
Probability such that two subset contains same number of elements | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2 ; Recursive function to return gcd of a and b ; Function to find probability ; Calculate total possible ways and favourable ways . ; Divide by gcd such that they become relatively coprime ; Driver code
function binomialCoeff ( n , k ) { let res = 1 ; if ( k > n - k ) k = n - k ; for ( let i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res = parseInt ( res / ( i + 1 ) ) ; } return res ; } function power ( x , y ) { let res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function FindProbability ( n ) { let up = binomialCoeff ( 2 * n , n ) ; let down = power ( 2 , 2 * n ) ; let g = gcd ( up , down ) ; up = parseInt ( up / g ) , down = parseInt ( down / g ) ; document . write ( up + " " + down + " " ) ; } let N = 8 ; FindProbability ( N ) ;
How to learn Pattern printing easily ? | JavaScript implementation of the approach
var N = 4 , i , j , min ; document . write ( " " + N + " " ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } document . write ( " " ) ; } for ( i = N - 1 ; i >= 1 ; i -- ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } document . write ( " " ) ; }
Largest perfect square number in an Array | Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code
function checkPerfectSquare ( n ) { let d = Math . sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } function largestPerfectSquareNumber ( a , n ) { let maxi = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( checkPerfectSquare ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } let a = [ 16 , 20 , 25 , 2 , 3 , 10 ] ; let n = a . length ; document . write ( largestPerfectSquareNumber ( a , n ) ) ;
Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver code
function convert ( n , a ) { let z = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] > z ) z = a [ i ] ; } z = z + 1 ; let ff = new Array ( z ) ; ff . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { ff [ a [ i ] ] += 1 ; } let cc = 0 ; for ( let i = 0 ; i < z ; i ++ ) { cc += parseInt ( ff [ i ] / 4 , 10 ) ; ff [ i ] = ff [ i ] % 4 ; } let vv = 0 ; for ( let i = 0 ; i < z ; i ++ ) { if ( ff [ i ] >= 2 ) vv += 1 ; } cc += parseInt ( vv / 2 , 10 ) ; document . write ( cc ) ; } let n = 4 ; let a = [ 1 , 2 , 1 , 2 ] ; convert ( n , a ) ;
Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | Javascript implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function find_LCM ( x , y ) { return Math . floor ( ( x * y ) / gcd ( x , y ) ) ; } function CountPairs ( n , m , A , B ) { let cnt = 0 ; let lcm = find_LCM ( A , B ) ; for ( let i = 1 ; i <= n ; i ++ ) cnt += Math . floor ( ( m + ( i % lcm ) ) / lcm ) ; return cnt ; } let n = 60 , m = 90 , A = 5 , B = 10 ; document . write ( CountPairs ( n , m , A , B ) ) ;
Sorting without comparison of elements | Represents node of a doubly linked list ; Count of elements in given range ; Count frequencies of all elements ; Traverse through range . For every element , print it its count times . ; Driver Code
function sortArr ( arr , n , min , max ) { let m = max - min + 1 ; let c = new Array ( m ) ; c . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { c [ arr [ i ] - min ] ++ ; } for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < c [ i ] ; j ++ ) { document . write ( ( i + min ) + " " ) ; } } } let arr = [ 10 , 10 , 1 , 4 , 4 , 100 , 0 ] ; let min = 0 , max = 100 ; let n = arr . length ; sortArr ( arr , n , min , max ) ;
Minimize the maximum distance between adjacent points after adding K points anywhere in between | Function to check if it is possible to add K points such that the maximum distance between adjacent points is D ; Stores the count of point used ; Iterate over all given points ; Add number of points required to be placed between ith and ( i + 1 ) th point ; Return answer ; Function to find the minimum value of maximum distance between adjacent points after adding K points any where between ; Stores the lower bound and upper bound of the given range ; Perform binary search ; Find the middle value ; Update the current range to lower half ; Update the current range to upper half ; Driver Code
function isPossible ( D , arr , N , K ) { let used = 0 ; for ( let i = 0 ; i < N - 1 ; ++ i ) { used += Math . floor ( ( arr [ i + 1 ] - arr [ i ] ) / D ) ; } return used <= K ; } function minMaxDist ( stations , N , K ) { let low = 0 , high = 1e8 ; while ( high - low > 1e-6 ) { let mid = ( low + high ) / 2 ; if ( isPossible ( mid , stations , N , K ) ) { high = mid ; } else { low = mid ; } } return low . toFixed ( 1 ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let K = 9 ; let N = arr . length ; document . write ( minMaxDist ( arr , N , K ) ) ;
Find Binary permutations of given size not present in the Array | Function to find a Binary String of same length other than the Strings present in the array ; Map all the strings present in the array ; Find all the substring that can be made ; If num already exists then increase counter ; If not found print ; If all the substrings are present then print - 1 ; Driver Code
function findMissingBinaryString ( nums , N ) { let s = new Set ( ) ; let counter = 0 ; for ( let str of nums ) { s . add ( str ) ; } let total = Math . pow ( 2 , N ) ; let ans = " " ; for ( let i = 0 ; i < total ; i ++ ) { let num = " " ; for ( let j = N - 1 ; j >= 0 ; j -- ) { if ( i & ( 1 << j ) ) { num += " " ; } else { num += " " ; } } if ( s . has ( num ) ) { continue ; counter ++ ; } else { document . write ( num + " " ) ; } } if ( counter == total ) { document . write ( " " ) ; } } let N = 3 ; let arr = [ " " , " " , " " , " " , " " , " " ] ; findMissingBinaryString ( arr , N ) ;
Minimum value of X such that sum of arr [ i ] | Function to check if there exists an X that satisfies the given conditions ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver Code
function check ( a , b , k , n , x ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + Math . pow ( Math . max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } function max_element ( a ) { let maxi = Number . MIN_VALUE ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] > maxi ) { maxi = a [ i ] ; } } return maxi ; } function findMin ( a , b , n , k ) { let l = 0 , u = max_element ( a ) ; while ( l < u ) { let m = Math . floor ( ( l + u ) / 2 ) ; if ( check ( a , b , k , n , m ) ) { u = m ; } else { l = m + 1 ; } } return l ; } let arr = [ 2 , 1 , 4 , 3 , 5 ] ; let brr = [ 4 , 3 , 2 , 3 , 1 ] ; let K = 12 ; let N = arr . length ; document . write ( findMin ( arr , brr , N , K ) ) ;
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant count ; Driver Code
function getPairsCount ( arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && arr [ i ] * arr [ j ] == Math . abs ( i - j ) ) { count ++ ; } } } return count ; } let arr = [ 1 , 1 , 2 , 4 ] ; let N = arr . length ; document . write ( getPairsCount ( arr , N ) ) ;
Find frequency of each character with positions in given Array of Strings | Function to print every occurence of every characters in every string ; Iterate over the vector arr [ ] ; Traverse the string arr [ i ] ; Push the pair of { i + 1 , j + 1 } in mp [ arr [ i ] [ j ] ] ; Print the occurences of every character ; Input ; Function call
function printOccurences ( arr , N ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < arr [ i ] . length ; j ++ ) { if ( mp . has ( arr [ i ] [ j ] ) ) { let temp = mp . get ( arr [ i ] [ j ] ) ; temp . push ( [ i + 1 , j + 1 ] ) ; mp . set ( arr [ i ] [ j ] , temp ) ; } else { mp . set ( arr [ i ] [ j ] , [ [ i + 1 , j + 1 ] ] ) ; } } } for ( let it of new Map ( [ ... mp . entries ( ) ] . sort ( ) ) ) { document . write ( " " + it [ 0 ] + " " ) ; for ( let j = 0 ; j < it [ 1 ] . length ; j ++ ) { document . write ( " " + it [ 1 ] [ j ] [ 0 ] + " " + it [ 1 ] [ j ] [ 1 ] + " " ) ; } document . write ( " " ) ; } } let arr = [ " " , " " ] ; let N = arr . length ; printOccurences ( arr , N ) ;
Count of subarrays with X as the most frequent element , for each value of X from 1 to N | Function to calculate the number of subarrays where X ( 1 <= X <= N ) is the most frequent element ; array to store the final answers ; Array to store current frequencies ; Initialise count ; Variable to store the current most frequent element ; Update frequency array ; Update answer ; Print answer ; Input ; Function call
function mostFrequent ( arr , N ) { let ans = new Array ( N ) . fill ( 0 ) for ( let i = 0 ; i < N ; i ++ ) { let count = new Array ( N ) ; count . fill ( 0 ) let best = 1 ; for ( let j = i ; j < N ; j ++ ) { count [ arr [ j ] - 1 ] ++ ; if ( count [ arr [ j ] - 1 ] > count [ best - 1 ] || ( count [ arr [ j ] - 1 ] == count [ best - 1 ] && arr [ j ] < best ) ) { best = arr [ j ] ; } ans [ best - 1 ] ++ ; } } console . log ( ans ) for ( let i = 0 ; i < N ; i ++ ) document . write ( ans [ i ] + " " ) ; } let arr = [ 2 , 1 , 2 , 3 ] ; let N = arr . length mostFrequent ( arr , N ) ;
Count array elements whose highest power of 2 less than or equal to that number is present in the given array | Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
function countElement ( arr , N ) { let count = 0 ; let m = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) } else { m . set ( arr [ i ] , 1 ) } } for ( let i = 0 ; i < N ; i ++ ) { let lg = Math . floor ( Math . log2 ( arr [ i ] ) ) ; let p = Math . pow ( 2 , lg ) ; if ( m . get ( p ) ) { count ++ ; } } return count ; } let arr = [ 3 , 4 , 6 , 9 ] ; let N = arr . length document . write ( countElement ( arr , N ) ) ;
Maximum number of intersections possible for any of the N given segments | Javascript program for the above approach ; Function to find the maximum number of intersections one segment has with all the other given segments ; Stores the resultant maximum count ; Stores the starting and the ending polets ; Sort arrays polets in the ascending order ; Traverse the array arr [ ] ; Find the count of segments on left of ith segment ; Find the count of segments on right of ith segment ; Find the total segments not letersecting with the current segment ; Store the count of segments that letersect with the ith segment ; Update the value of count ; Return the resultant count ; Driver Code
function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function maximumLetersections ( arr , N ) { let count = 0 ; let L = Array . from ( { length : N } , ( _ , i ) => 0 ) ; let R = Array . from ( { length : N } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { L [ i ] = arr [ i ] [ 0 ] ; R [ i ] = arr [ i ] [ 1 ] ; } L . sort ( ) ; R . sort ( ) ; for ( let i = 0 ; i < N ; i ++ ) { let l = arr [ i ] [ 0 ] ; let r = arr [ i ] [ 1 ] ; let x = lower_bound ( L , 0 , N , l ) ; let y = N - lower_bound ( R , 0 , N , r + 1 ) ; let cnt = x + y ; cnt = N - cnt - 1 ; count = Math . max ( count , cnt ) ; } return count ; } let arr = [ [ 1 , 6 ] , [ 5 , 5 ] , [ 2 , 3 ] ] ; let N = arr . length ; document . write ( maximumLetersections ( arr , N ) ) ;
Program to find the Nth Composite Number | JavaScript program for the above approach ; Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ; Initialize the array to true ; Iterate over the range [ 2 , MAX_SIZE ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , MAX_SIZE ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , MAX_SIZE ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
let MAX_SIZE = 1000005 ; function NthComposite ( N ) { let IsPrime = [ ] ; for ( let i = 0 ; i < MAX_SIZE ; i ++ ) IsPrime . push ( true ) ; for ( let p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( let i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } let Composites = [ ] ; for ( let p = 4 ; p < MAX_SIZE ; p ++ ) if ( ! IsPrime [ p ] ) Composites . push ( p ) ; return Composites [ N - 1 ] ; } let N = 4 ; document . write ( NthComposite ( N ) ) ;
Intersection point of two Linked Lists | Set 3 | JavaScript program for the above approach ; Structure of a node of a Linked List ; Constructor ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a -> data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code
let head1 = null ; let head2 = null ; class Node { constructor ( x ) { this . data = x ; this . next = null ; } } constructor ( x ) { this . data = x ; this . next = null ; } } function intersectingNode ( headA , headB ) { let a = headA ; while ( a != null ) { a . data *= - 1 ; a = a . next ; } let b = headB ; while ( b != null ) { if ( b . data < 0 ) break ; b = b . next ; } return b ; } function formLinkList ( ) { head1 = new Node ( 3 ) ; head1 . next = new Node ( 6 ) ; head1 . next . next = new Node ( 9 ) ; head1 . next . next . next = new Node ( 15 ) ; head1 . next . next . next . next = new Node ( 30 ) ; head2 = new Node ( 10 ) ; head2 . next = head1 . next . next . next ; return ; } formLinkList ( ) ; document . write ( Math . abs ( intersectingNode ( head1 , head2 ) . data ) ) ;
Left rotate digits of node values of all levels of a Binary Tree in increasing order | TreeNode class ; Function to check if the nodes are in increasing order or not ; Perform Level Order Traversal ; Current len of queue ; If queue is empty ; Level order traversal ; Pop element from front of the queue ; If previous value exceeds current value , return false ; Function to print the Tree after modification ; Performs level order traversal ; Calculate size of the queue ; Iterate until queue is empty ; Function to arrange node values of each level in increasing order ; Perform level order traversal ; Calculate len of queue ; If queue is empty ; Level order traversal ; Pop element from front of the queue ; Initialize the optimal element by the initial element ; Check for all left shift operations ; Left shift ; If the current shifting gives optimal solution ; Replacing initial element by the optimal element ; Push the LST ; Push the RST ; Print the result ; Driver code
class Node { constructor ( v ) { this . val = v ; this . left = this . right = null ; } } function isInc ( root ) { let que = [ ] ; que . push ( root ) ; while ( true ) { let len = que . length ; if ( len == 0 ) break ; let pre = que [ 0 ] ; while ( len > 0 ) { let temp = que [ 0 ] ; que . shift ( ) ; if ( pre . val > temp . val ) return false ; pre = temp ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; len -= 1 ; } } return true ; } function levelOrder ( root ) { let que = [ ] ; que . push ( root ) ; while ( true ) { let len = que . length ; if ( len == 0 ) break ; while ( len > 0 ) { let temp = que [ 0 ] ; que . shift ( ) ; document . write ( temp . val + " " ) ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; len -= 1 ; } document . write ( " " ) ; } document . write ( " " ) ; } function makeInc ( root ) { let que = [ ] ; que . push ( root ) ; while ( true ) { let len = que . length ; if ( len == 0 ) break ; let prev = - 1 ; while ( len > 0 ) { let temp = que [ 0 ] ; que . shift ( ) ; let optEle = temp . val ; let strEle = ( optEle ) . toString ( ) ; let flag = true ; let yy = strEle . length ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let s1 = strEle . substring ( idx , yy ) ; let s2 = strEle . substring ( 0 , idx ) ; let s = s1 + s2 ; let ls = parseInt ( s ) ; if ( ls >= prev && flag ) { optEle = ls ; flag = false ; } if ( ls >= prev ) optEle = Math . min ( optEle , ls ) ; } temp . val = optEle ; prev = temp . val ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; len -= 1 ; } } if ( isInc ( root ) == true ) levelOrder ( root ) ; else document . write ( - 1 ) ; } let root = new Node ( 341 ) ; root . left = new Node ( 241 ) ; root . right = new Node ( 123 ) ; root . left . left = new Node ( 324 ) ; root . left . right = new Node ( 235 ) ; root . right . right = new Node ( 161 ) ; makeInc ( root ) ;
Modify array of strings by replacing characters repeating in the same or remaining strings | Function to remove duplicate characters across the strings ; Stores distinct characters ; Size of the array ; Stores the list of modified strings ; Traverse the array ; Stores the modified string ; Iterate over the characters of the modified string ; If character is already present ; Insert character into the Set ; Print the list of modified strings ; Print each string ; Given array of strings ; Function Call to modify the given array of strings
function removeDuplicateCharacters ( arr ) { var cset = new Set ( ) ; var n = arr . length ; var out = [ ] ; arr . forEach ( str => { var out_curr = " " ; str . split ( ' ' ) . forEach ( ch => { if ( ! cset . has ( ch ) ) { out_curr += ch ; cset . add ( ch ) ; } } ) ; if ( out_curr . size != 0 ) out . push ( out_curr ) ; } ) ; for ( var i = 0 ; i < out . length ; i ++ ) { document . write ( out [ i ] + " " ) ; } } var arr = [ " " , " " , " " , " " ] ; removeDuplicateCharacters ( arr ) ;
Sum of array elements which are prime factors of a given number | Function to check if a number is prime or not ; Corner cases ; Check if n is a multiple of 2 or 3 ; Above condition allows to check only for every 6 th number , starting from 5 ; If n is a multiple of i and i + 2 ; Function to find the sum of array elements which are prime factors of K ; Stores the required sum ; Traverse the given array ; If current element is a prime factor of k , add it to the sum ; Print the result ; Given arr [ ] ; Store the size of the array
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; var i ; for ( i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function primeFactorSum ( arr , n , k ) { var sum = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 && isPrime ( arr [ i ] ) ) { sum = sum + arr [ i ] ; } } document . write ( sum ) ; } var arr = [ 1 , 2 , 3 , 5 , 6 , 7 , 15 ] var N = arr . length ; var K = 35 ; primeFactorSum ( arr , N , K ) ;
Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 | Function to count the minimum number of operations required to reduce x to 0 ; If sum of the array is less than x ; Stores the count of operations ; Two poleters to traverse the array ; Reduce x by the sum of the entire array ; Iterate until l reaches the front of the array ; If sum of elements from the front exceeds x ; Shift towards left ; If sum exceeds 0 ; Reduce x by elements from the right ; If x is reduced to 0 ; Update the minimum count of operations required ; Driver code
function minOperations ( nums , x ) { let sum = 0 ; for ( let i = 0 ; i < x ; i ++ ) sum += nums [ i ] ; if ( sum < x ) return - 1 ; let ans = Number . MAX_VALUE ; let l = nums . length - 1 , r = nums . length ; x -= sum ; while ( l >= 0 ) { if ( x <= 0 ) { x += nums [ l ] ; l -= 1 ; } if ( x > 0 ) { r -= 1 ; x -= nums [ r ] ; } if ( x == 0 ) { ans = Math . min ( ans , ( l + 1 ) + ( nums . length - r ) ) ; } } if ( ans < Number . MAX_VALUE ) return ans ; else return - 1 ; } let nums = [ 1 , 1 , 4 , 2 , 3 ] ; let x = 5 ; document . write ( minOperations ( nums , x ) ) ;
Number of subarrays having even product | Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Prvar count of subarrays with even product ; Input ; Length of an array ; Function call to count even product subarrays
function evenproduct ( arr , length ) { var total_subarray = length * ( length + 1 ) / 2 ; var total_odd = 0 ; var count_odd = 0 ; for ( i = 0 ; i < arr . length ; ++ i ) { if ( arr [ i ] % 2 == 0 ) { count_odd = 0 ; } else { ++ count_odd ; total_odd += count_odd ; } } document . write ( total_subarray - total_odd ) ; } var arr = [ 7 , 5 , 4 , 9 ] ; var length = arr . length ; evenproduct ( arr , length ) ;