codeparrot/xlcost-text-to-code huggingface dataset:
LCA for general or n | Maximum number of nodes is 100000 and nodes are numbered from 1 to 100000 ; storing the path from root to node ; pushing current node into the path ; node found ; terminating the path ; This Function compares the path from root to ' a ' & root to ' b ' and returns LCA of a and b . Time Complexity : O ( n ) ; trivial case ; setting root to be first element in path ; calculating path from root to a ; calculating path from root to b ; runs till path 1 & path 2 mathches ; returns the last matching node in the paths ; Driver code ; Number of nodes
let MAXN = 100001 ; let tree = new Array ( MAXN ) ; let path = new Array ( 3 ) ; for ( let i = 0 ; i < 3 ; i ++ ) { path [ i ] = new Array ( MAXN ) ; for ( let j = 0 ; j < MAXN ; j ++ ) { path [ i ] [ j ] = 0 ; } } let flag ; / function dfs ( cur , prev , pathNumber , ptr , node ) { for ( let i = 0 ; i < tree [ cur ] . length ; i ++ ) { if ( tree [ cur ] [ i ] != prev && ! flag ) { path [ pathNumber ] [ ptr ] = tree [ cur ] [ i ] ; if ( tree [ cur ] [ i ] == node ) { flag = true ; path [ pathNumber ] [ ptr + 1 ] = - 1 ; return ; } dfs ( tree [ cur ] [ i ] , cur , pathNumber , ptr + 1 , node ) ; } } } function LCA ( a , b ) { if ( a == b ) return a ; path [ 1 ] [ 0 ] = path [ 2 ] [ 0 ] = 1 ; flag = false ; dfs ( 1 , 0 , 1 , 1 , a ) ; flag = false ; dfs ( 1 , 0 , 2 , 1 , b ) ; let i = 0 ; while ( i < MAXN && path [ 1 ] [ i ] == path [ 2 ] [ i ] ) i ++ ; return path [ 1 ] [ i - 1 ] ; } function addEdge ( a , b ) { tree [ a ] . push ( b ) ; tree [ b ] . push ( a ) ; } for ( let i = 0 ; i < MAXN ; i ++ ) tree [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; document . write ( " " + LCA ( 4 , 7 ) + " " ) ; document . write ( " " + LCA ( 4 , 6 ) + " " ) ;
LCA for general or n | Sparse Matrix DP approach to find LCA of two nodes ; pre - compute the depth for each node and their first parent ( 2 ^ 0 th parent ) time complexity : O ( n ) ; Dynamic Programming Sparse Matrix Approach populating 2 ^ i parent for each node Time complexity : O ( nlogn ) ; Returning the LCA of u and v Time complexity : O ( log n ) ; Step 1 of the pseudocode ; now depth [ u ] = = depth [ v ] ; Step 2 of the pseudocode ; Driver function ; running dfs and precalculating depth of each node . ; Precomputing the 2 ^ i th ancestor for evey node ; calling the LCA function
var MAXN = 100000 ; var level = 18 ; var tree = Array . from ( Array ( MAXN ) , ( ) => Array ( ) ) ; var depth = Array ( MAXN ) . fill ( 0 ) ; var parent = Array . from ( Array ( MAXN ) , ( ) => Array ( level ) . fill ( - 1 ) ) ; function dfs ( cur , prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur ] [ 0 ] = prev ; for ( var i = 0 ; i < tree [ cur ] . length ; i ++ ) { if ( tree [ cur ] [ i ] != prev ) dfs ( tree [ cur ] [ i ] , cur ) ; } } function precomputeSparseMatrix ( n ) { for ( var i = 1 ; i < level ; i ++ ) { for ( var node = 1 ; node <= n ; node ++ ) { if ( parent [ node ] [ i - 1 ] != - 1 ) parent [ node ] [ i ] = parent [ parent [ node ] [ i - 1 ] ] [ i - 1 ] ; } } } function lca ( u , v ) { if ( depth [ v ] < depth [ u ] ) { u = u + v ; v = u - v ; u = u - v ; } var diff = depth [ v ] - depth [ u ] ; for ( var i = 0 ; i < level ; i ++ ) if ( ( ( diff >> i ) & 1 ) == 1 ) v = parent [ v ] [ i ] ; if ( u == v ) return u ; for ( var i = level - 1 ; i >= 0 ; i -- ) if ( parent [ u ] [ i ] != parent [ v ] [ i ] ) { u = parent [ u ] [ i ] ; v = parent [ v ] [ i ] ; } return parent [ u ] [ 0 ] ; } function addEdge ( u , v ) { tree [ u ] . push ( v ) ; tree [ v ] . push ( u ) ; } function memset ( value ) { for ( var i = 0 ; i < MAXN ; i ++ ) { for ( var j = 0 ; j < level ; j ++ ) { parent [ i ] [ j ] = - 1 ; } } } memset ( - 1 ) ; var n = 8 ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; depth [ 0 ] = 0 ; dfs ( 1 , 0 ) ; precomputeSparseMatrix ( n ) ; document . write ( " " + lca ( 4 , 7 ) + " " ) ; document . write ( " " + lca ( 4 , 6 ) + " " ) ;
Longest Common Extension / LCE | Set 1 ( Introduction and Naive Method ) | Structure to represent a query of form ( L , R ) ; A utility function to find longest common extension from index - L and index - R ; A function to answer queries of longest common extension ; Driver code ; LCA Queries to answer
class Query { constructor ( L , R ) { this . L = L ; this . R = R ; } } function LCE ( str , n , L , R ) { let length = 0 ; while ( R + length < n && str [ L + length ] == str [ R + length ] ) length ++ ; return ( length ) ; } function LCEQueries ( str , n , q , m ) { for ( let i = 0 ; i < m ; i ++ ) { let L = q [ i ] . L ; let R = q [ i ] . R ; document . write ( " " + L + " " + R + " " + LCE ( str , n , L , R ) + " " ) ; } return ; } let str = " " ; let n = str . length ; let q = new Array ( 3 ) ; q [ 0 ] = new Query ( 1 , 2 ) ; q [ 1 ] = new Query ( 1 , 6 ) ; q [ 2 ] = new Query ( 0 , 5 ) ; let m = q . length ; LCEQueries ( str , n , q , m ) ;
Time Complexity and Space Complexity | Function to find a pair in the given array whose sum is equal to z ; Iterate through all the pairs ; Check if the sum of the pair ( a [ i ] , a [ j ] ) is equal to z ; Given Input ; Function Call
function findPair ( a , n , z ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( i != j && a [ i ] + a [ j ] == z ) return true ; return false ; } let a = [ 1 , - 2 , 1 , 0 , 5 ] ; let z = 0 ; let n = a . length ; if ( findPair ( a , n , z ) ) document . write ( " " ) ; else document . write ( " " ) ;
Analysis of Algorithms | Big | Function to print all possible pairs ; Given array ; Store the size of the array ; Function Call
function print ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i != j ) document . write ( a [ i ] + " " + a [ j ] + " " ) ; } } } let a = [ 1 , 2 , 3 ] ; let n = a . length ; print ( a , n ) ;
Maximum sum obtained by dividing Array into several subarrays as per given conditions | Function to find the required answer ; Stores maximum sum ; Adding the difference of elements at ends of increasing subarray to the answer ; Input ; Function calling
function maximumSum ( arr , N ) { let Sum = 0 ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } let arr = [ 1 , 5 , 3 ] ; let N = arr . length ; document . write ( maximumSum ( arr , N ) ) ;
The Slowest Sorting Algorithms | Function to implement stooge sort ; Base Case ; If first element is smaller than last element , swap them ; If there are more than 2 elements in the array ; Recursively sort the first 2 / 3 elements ; Recursively sort the last 2 / 3 elements ; Recursively sort the first 2 / 3 elements again ; Driver Code ; Function Call ; Display the sorted array
function stoogesort ( arr , l , h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { let temp = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = temp ; } if ( h - l + 1 > 2 ) { let t = Math . floor ( ( h - l + 1 ) / 3 ) ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } let arr = [ 2 , 4 , 5 , 3 , 1 ] ; let N = arr . length ; stoogesort ( arr , 0 , N - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Determine winner of the Game by arranging balls in a row | Function to find the winner of the Game by arranging the balls in a row ; Check if small balls are greater or equal to the large ones ; X can place balls therefore scores n - 1 ; Condition if large balls are greater than small ; X can have m - 1 as a score since greater number of balls can only be adjacent ; Compare the score ; Given number of small balls ( N ) and number of large balls ( M ) ; Function call
function findWinner ( n , m ) { let X = 0 ; let Y = 0 ; if ( n >= m ) { X = n - 1 ; Y = m ; } else { X = m - 1 ; Y = n ; } if ( X > Y ) document . write ( " " ) ; else if ( Y > X ) document . write ( " " ) ; else document . write ( " " ) ; } let n = 3 , m = 1 ; findWinner ( n , m ) ;
Check if Pascal 's Triangle is possible with a complete layer by using numbers upto N | Function to check if Pascaltriangle can be made by N integers ; Find X ; If x is integer ; Given number N ; Function Call
function checkPascaltriangle ( N ) { var x = ( Math . sqrt ( 8 * N + 1 ) - 1 ) / 2 ; if ( Math . ceil ( x ) - x == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var N = 10 ; checkPascaltriangle ( N ) ;
Count subarrays having sum of elements at even and odd positions equal | Function to count subarrays in which sum of elements at even and odd positions are equal ; Initialize variables ; Iterate over the array ; Check if position is even then add to sum then add it to sum ; Else subtract it to sum ; Increment the count if the sum equals 0 ; Print the count of subarrays ; Given array arr [ ] ; Size of the array ; Function call
function countSubarrays ( arr , n ) { var count = 0 ; var i , j ; for ( i = 0 ; i < n ; i ++ ) { var sum = 0 ; for ( j = i ; j < n ; j ++ ) { if ( ( j - i ) % 2 == 0 ) sum += arr [ j ] ; else sum -= arr [ j ] ; if ( sum == 0 ) count ++ ; } } document . write ( count ) ; } var arr = [ 2 , 4 , 6 , 4 , 2 ] ; var n = arr . length ; countSubarrays ( arr , n ) ;
Find position of non | Function to find the count of placing non - attacking rooks on the N x N chessboard ; Count of the Non - attacking rooks ; Printing lexographically smallest configuration ; Driver Code ; Function call
function findCountRooks ( row , col , n , k ) { let res = n - k ; document . write ( res + " " ) ; let ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( ri < k && row [ ri ] == 1 ) { ri ++ ; } while ( ci < k && col [ ci ] == 1 ) { ci ++ ; } document . write ( ( ri + 1 ) + " " + ( ci + 1 ) + " " ) ; ri ++ ; ci ++ ; } } let n = 4 ; let k = 2 ; let row = [ 1 , 2 ] ; let col = [ 4 , 2 ] ; findCountRooks ( row , col , n , k ) ;
Check if a large number is divisible by a number which is a power of 2 | Function to check divisibility ; Calculate the number of digits in num ; Check if the length of the string is less than the powerOf2 then return false ; Check if the powerOf2 is 0 that means the given number is 1 and as every number is divisible by 1 so return true ; Find the number which is formed by the last n digits of the string where n = powerOf2 ; Check if the number formed is divisible by input num or not ; Given number ; Function call
function checkIfDivisible ( str , num ) { let powerOf2 = ( Math . log ( num ) / Math . log ( 2 ) ) ; if ( str . length < powerOf2 ) return false ; if ( powerOf2 == 0 ) return true ; let i , number = 0 ; let len = str . length ; for ( i = len - powerOf2 ; i < len ; i ++ ) { number += ( str [ i ] - ' ' ) * Math . pow ( 10 , powerOf2 - 1 ) ; powerOf2 -- ; } if ( number % num != 0 ) return false ; else return true ; } let str = " " ; let num = 4 ; if ( checkIfDivisible ( str , num ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | Javascript program to check clockwise or counterclockwise order in an array ; Comparing the first and last value of array ; If the Count is greater than 1 then it can 't be  represented in required order ; Driver code
function check_order ( arr ) { var cnt = 0 ; for ( i = 0 ; i < arr . length - 1 ; i ++ ) { if ( Math . abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . abs ( arr [ 0 ] - arr [ arr . length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } var arr = [ 2 , 3 , 4 , 5 , 1 ] ; if ( check_order ( arr ) ) document . write ( " " ) ; else document . write ( " " ) ;
Largest number M less than N such that XOR of M and N is even | Function to find the maximum possible value of M ; Edge case ; M = N - 2 is maximum possible value ; Driver code
function getM ( n ) { if ( n == 1 ) return - 1 ; else return n - 2 ; } var n = 10 ; document . write ( getM ( n ) ) ;
Maximize profit in buying and selling stocks with Rest condition | JavaScript program for the above problem ; If there is only one day for buying and selling no profit can be made ; Array to store Maxprofit by resting on given day ; Array to store Maxprofit by buying or resting on the given day ; Array to store Maxprofit by selling on given day ; Initially there will 0 profit ; Buying on 1 st day results in negative profit ; zero profit since selling before buying isn 't possible ; max of profit on ( i - 1 ) th day by resting and profit on ( i - 1 ) th day by selling . ; max of profit by resting on ith day and buying on ith day . ; max of profit by selling on ith day ; maxprofit ; Driver Code
function maxProfit ( prices , n ) { if ( n <= 1 ) return 0 ; let rest = [ ] ; for ( let k = 0 ; k < n ; k ++ ) rest . push ( 0 ) ; let hold = [ ] ; for ( let k = 0 ; k < n ; k ++ ) hold . push ( 0 ) ; let sold = [ ] ; for ( let k = 0 ; k < n ; k ++ ) sold . push ( 0 ) ; rest [ 0 ] = 0 ; hold [ 0 ] = - prices [ 0 ] ; sold [ 0 ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) { rest [ i ] = Math . max ( rest [ i - 1 ] , sold [ i - 1 ] ) ; hold [ i ] = Math . max ( hold [ i - 1 ] , rest [ i - 1 ] - prices [ i ] ) ; sold [ i ] = hold [ i - 1 ] + prices [ i ] ; } return Math . max ( rest [ n - 1 ] , sold [ n - 1 ] ) ; } let price = [ 2 , 4 , 5 , 0 , 2 ] ; let n = price . length ; document . write ( maxProfit ( price , n ) , ' ' ) ;
Count of subarrays of size K having at least one pair with absolute difference divisible by K | Function to return the required number of subarrays ; Return number of possible subarrays of length K ; Driver Code
function findSubarrays ( arr , N , K ) { return N - K + 1 ; } let arr = [ 1 , 5 , 3 , 2 , 17 , 18 ] ; let K = 4 ; let N = arr . length ; document . write ( findSubarrays ( arr , N , K ) ) ;
Maximum length of subarray such that all elements are equal in the subarray | Function to find the longest subarray with same element ; Check if the elements are same then we can increment the length ; Reinitialize j ; Compare the maximum length e with j ; Return max length ; Given array arr ; Function Call
function longest_subarray ( arr , d ) { var i = 0 , j = 1 , e = 0 ; for ( i = 0 ; i < d - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { j = j + 1 ; } else { j = 1 ; } if ( e < j ) { e = j ; } } return e ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( longest_subarray ( arr , N ) ) ;
Print all Strong numbers less than or equal to N | Store the factorial of all the digits from [ 0 , 9 ] ; Function to return true if number is strong or not ; Converting N to String so that can easily access all it 's digit ; sum will store summation of factorial of all digits of a number N ; Returns true of N is strong number ; Function to print all strong number till N ; Iterating from 1 to N ; Checking if a number is strong then print it ; Given number ; Function call
let factorial = [ 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 ] ; function isStrong ( N ) { let num = N . toString ( ) ; let sum = 0 ; for ( let i = 0 ; i < num . length ; i ++ ) { sum += factorial [ num [ i ] - ' ' ] ; } return sum == N ; } function printStrongNumbers ( N ) { for ( let i = 1 ; i <= N ; i ++ ) { if ( isStrong ( i ) ) { document . write ( i + " " ) ; } } } let N = 1000 ; printStrongNumbers ( N ) ;
Count of pairs in a given range with sum of their product and sum equal to their concatenated number | Function for counting pairs ; Count possible values of Y ; Driver Code
function countPairs ( A , B ) { var countY = 0 , countX = ( B - A ) + 1 , next_val = 9 ; while ( next_val <= B ) { if ( next_val >= A ) { countY += 1 ; } next_val = next_val * 10 + 9 ; } return ( countX * countY ) ; } var A = 1 ; var B = 16 ; document . write ( countPairs ( A , B ) ) ;
Minimum sprinklers required to water a rectangular park | Function to find the minimum number sprinklers required to water the park . ; General requirements of sprinklers ; if M is odd then add one additional sprinklers ; Driver code
function solve ( N , M ) { var ans = ( N ) * parseInt ( ( M / 2 ) ) ; if ( M % 2 == 1 ) { ans += parseInt ( ( N + 1 ) / 2 ) ; } document . write ( ans ) ; } var N , M ; N = 5 ; M = 3 ; solve ( N , M ) ;
Queries to find frequencies of a string within specified substrings | Javascript program to find frequency of a string K in a substring [ L , R ] in S ; Store the frequency of string for each index ; Compute and store frequencies for every index ; Driver Code
var max_len = 100005 ; var cnt = Array ( max_len ) . fill ( 0 ) ; function precompute ( s , K ) { var n = s . length ; for ( var i = 0 ; i < n - 1 ; i ++ ) { cnt [ i + 1 ] = cnt [ i ] + ( s . substring ( i , i + K . length ) == K ) ; } } var s = " " ; var K = " " ; precompute ( s , K ) ; var Q = [ [ 1 , 6 ] , [ 5 , 11 ] ] ; Q . forEach ( ( it ) => { document . write ( cnt [ it [ 1 ] - 1 ] - cnt [ it [ 0 ] - 1 ] + " " ) ; } ) ;
XOR of elements in a given range with updates using Fenwick Tree | Returns XOR of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial XORs of array elements are stored in BITree [ ] . ; Traverse ancestors of BITree [ index ] ; XOR current element of BIT to ans ; Update index to that of the parent node in getXor ( ) view by subtracting LSB ( Least Significant Bit ) ; Updates the Binary Index Tree by replacing all ancestors of index by their respective XOR with val ; Traverse all ancestors and XOR with ' val ' . ; XOR ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Constructs and returns a Binary Indexed Tree for the given array ; Create and initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Driver Code ; Create the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx
function getXOR ( BITree , index ) { let ans = 0 ; index += 1 ; while ( index > 0 ) { ans ^= BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } function updateBIT ( BITree , n , index , val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] ^= val ; index += index & ( - index ) ; } } function constructBITree ( arr , n ) { let BITree = new Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } function rangeXor ( BITree , l , r ) { return getXOR ( BITree , r ) ^ getXOR ( BITree , l - 1 ) ; } let A = [ 2 , 1 , 1 , 3 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; let n = A . length ; let q = [ [ 1 , 0 , 9 ] , [ 2 , 3 , 6 ] , [ 2 , 5 , 5 ] , [ 2 , 8 , 1 ] , [ 1 , 0 , 9 ] ] ; let BITree = constructBITree ( A , n ) ; for ( let i = 0 ; i < q . length ; i ++ ) { let id = q [ i ] [ 0 ] ; if ( id == 1 ) { let L = q [ i ] [ 1 ] ; let R = q [ i ] [ 2 ] ; document . write ( " " + " " + rangeXor ( BITree , L , R ) + " " ) ; } else { let idx = q [ i ] [ 1 ] ; let val = q [ i ] [ 2 ] ; A [ idx ] ^= val ; updateBIT ( BITree , n , idx , val ) ; } }
Count of Double Prime numbers in a given range L to R | Array to make Sieve where arr [ i ] = 0 indicates non prime and arr [ i ] = 1 indicates prime ; Array to find double prime ; Function to find the number double prime numbers in range ; Assume all numbers as prime ; Check if the number is prime ; check for multiples of i ; Make all multiples of ith prime as non - prime ; Check if number at ith position is prime then increment count ; Indicates count of numbers from 1 to i that are also prime and hence double prime ; If number is not a double prime ; finding cumulative sum ; Driver code
let arr = [ ] ; let dp = [ ] ; function count ( ) { let maxN = 1000000 , i , j ; for ( i = 0 ; i < maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( j = 2 * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } let cnt = 0 ; for ( i = 0 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) cnt ++ ; if ( arr [ cnt ] == 1 ) dp [ i ] = 1 ; else dp [ i ] = 0 ; } for ( i = 1 ; i <= maxN ; i ++ ) dp [ i ] += dp [ i - 1 ] ; } let L = 4 , R = 12 ; count ( ) ; document . write ( dp [ R ] - dp [ L - 1 ] ) ;
Count of ungrouped characters after dividing a string into K groups of distinct characters | Javascript implementation of the above approach ; Create array where index represents alphabets ; Fill count of every alphabet to corresponding array index ; Count for every element how much is exceeding from no . of groups then sum them ; Print answer ; Driver code
function findUngroupedElement ( s , k ) { let n = s . length ; let b = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < 26 ; i ++ ) b [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let p = s [ i ] ; b [ p . charCodeAt ( ) - ' ' . charCodeAt ( ) ] += 1 ; } let sum = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( b [ i ] > k ) sum += b [ i ] - k ; } document . write ( sum ) ; } let s = " " ; let k = 1 ; findUngroupedElement ( s , k ) ;
Generate an array of given size with equal count and sum of odd and even numbers | Function to find the array such that the array contains the same count of even and odd elements with equal sum of even and odd elements ; Length of array which is not divisible by 4 is unable to form such array ; Loop to find the resulted array containing the same count of even and odd elements ; Find the total sum of even elements ; Find the total sum of odd elements ; Find the difference between the total sum of even and odd elements ; The difference will be added in the last odd element ; Driver Code
function findSolution ( N ) { if ( N % 4 != 0 ) document . write ( - 1 + " " ) ; else { let temp = 0 , sum_odd = 0 , sum_even = 0 ; let result = new Uint8Array ( N ) ; for ( let i = 0 ; i < N ; i += 2 ) { temp += 2 ; result [ i + 1 ] = temp ; sum_even += result [ i + 1 ] ; result [ i ] = temp - 1 ; sum_odd += result [ i ] ; } let diff = sum_even - sum_odd ; result [ N - 2 ] += diff ; for ( let i = 0 ; i < N ; i ++ ) document . write ( result [ i ] + " " ) ; document . write ( " " ) ; } } let N = 8 ; findSolution ( N ) ;
Number of ways to place two queens on a N * N chess | Function to find number of valid positions for two queens in the N * N chess board ; Driver Code ; Function Call
function possiblePositions ( n ) { let term1 = Math . pow ( n , 4 ) ; let term2 = Math . pow ( n , 3 ) ; let term3 = Math . pow ( n , 2 ) ; let term4 = n / 3 ; let ans = ( Math . ceil ( term1 / 2 ) ) - ( Math . ceil ( 5 * term2 ) / 3 ) + ( Math . ceil ( 3 * term3 ) / 2 ) - term4 ; return ans ; } let n ; n = 3 ; let ans = possiblePositions ( n ) ; document . write ( Math . floor ( ans ) ) ;
Group all co | Function to group the mutually co - prime numbers into one group ; Loop for the numbers less than the 4 ; Integers 1 , 2 and 3 can be grouped into one group ; Consecutive even and odd numbers ; Driver Code ; Function Call
function mutually_coprime ( n ) { if ( n <= 3 ) { for ( let j = 1 ; j < n + 1 ; j ++ ) document . write ( j + " " + " " ) ; document . write ( " " ) ; } else { document . write ( " " + " " ) ; for ( let j = 4 ; j < n ; j += 2 ) { document . write ( j + " " + ( j + 1 ) + " " ) ; if ( n % 2 == 0 ) document . write ( n + " " ) ; } } } let n = 9 ; mutually_coprime ( n ) ;
Maximum sum subset having equal number of positive and negative elements | Function to find maximum sum subset with equal number of positive and negative elements ; Loop to store the positive and negative elements in two different array ; Sort both the array ; Pointers starting from the highest elements ; Find pairs having sum greater than zero ; Driver code
function findMaxSum ( arr , n ) { var a = [ ] ; var b = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { a . push ( arr [ i ] ) ; } else if ( arr [ i ] < 0 ) { b . push ( arr [ i ] ) ; } } a . sort ( ( a , b ) => a - b ) b . sort ( ( a , b ) => a - b ) var p = a . length - 1 ; var q = b . length - 1 ; var s = 0 ; while ( p >= 0 && q >= 0 ) { if ( a [ p ] + b [ q ] > 0 ) { s = s + a [ p ] + b [ q ] ; } else { break ; } p = p - 1 ; q = q - 1 ; } return s ; } var arr1 = [ 1 , - 2 , 3 , 4 , - 5 , 8 ] ; var n1 = arr1 . length ; document . write ( findMaxSum ( arr1 , n1 ) ) ;