codeparrot/xlcost-text-to-code huggingface dataset:
Check if each element of the given array is the product of exactly K prime numbers | Javascript program to check if each element of the given array is a product of exactly K prime factors ; initialise the global sieve array ; Function to generate Sieve ; NOTE : k value is necessarily more than 1 hence , 0 , 1 and any prime number cannot be represented as product of two or more prime numbers ; Function to check if each number of array satisfies the given condition ; first construct the sieve
const MAX = 1000000 ; let Sieve = new Array ( MAX ) . fill ( 0 ) ; function constructSieve ( ) { for ( let i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( let j = 2 * i ; j <= MAX ; j += i ) { let temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = parseInt ( temp / i ) ; } } } } } function checkElements ( A , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } constructSieve ( ) ; let k = 3 ; let A = [ 12 , 36 , 42 , 72 ] ; let n = A . length ; checkElements ( A , n , k ) ;
Fibonacci Power |  ; Iterative function to compute modular power ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Helper function that multiplies 2 matrices F and M of size 2 * 2 , and puts the multiplication result back to F [ ] [ ] ; Helper function that calculates F [ ] [ ] raise to the power n and puts the result in F [ ] [ ] Note that this function is designed only for fib ( ) and won 't work as general power function ; Function that returns nth Fibonacci number ; Driver Code
let mod = 1000000007 ; function modularexpo ( x , y , p ) { let res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function multiply ( F , M , m ) { let x = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; let y = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; let z = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; let w = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } function power ( F , n , m ) { if ( n == 0 n == 1 ) return ; let M = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; power ( F , Math . floor ( n / 2 ) , m ) ; multiply ( F , F , m ) ; if ( n % 2 != 0 ) multiply ( F , M , m ) ; } function fib ( n , m ) { let F = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; if ( n == 0 ) return 0 ; power ( F , n - 1 , m ) ; return F [ 0 ] [ 0 ] ; } let n = 4 ; let base = fib ( n , mod ) % mod ; let expo = fib ( n , mod - 1 ) % ( mod - 1 ) ; let result = modularexpo ( base , expo , mod ) % mod ; document . write ( result ) ;
Count number of ways to divide an array into two halves with same sum | Function to count the number of ways to divide an array into two halves with same sum ; if length of array is 1 answer will be 0 as we have to split it into two non - empty halves ; variables to store total sum , current sum and count ; finding total sum ; checking if sum equals total_sum / 2 ; Driver Code
function cntWays ( arr , n ) { if ( n == 1 ) return 0 ; var tot_sum = 0 , sum = 0 , ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) tot_sum += arr [ i ] ; for ( var i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] ; if ( sum == tot_sum / 2 ) ans ++ ; } return ans ; } var arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; var n = arr . length ; document . write ( cntWays ( arr , n ) ) ;
Find N distinct numbers whose bitwise Or is equal to K | Javascript implementation of the approach ; Function to pre - calculate all the powers of 2 upto MAX ; Function to return the count of set bits in x ; To store the count of set bits ; Function to add num to the answer by setting all bit positions as 0 which are also 0 in K ; Bit i is 0 in K ; Function to find and print N distinct numbers whose bitwise OR is K ; Choosing K itself as one number ; Find the count of set bits in K ; Impossible to get N distinct integers ; Add i to the answer after setting all the bits as 0 which are 0 in K ; If N distinct numbers are generated ; Print the generated numbers ; Driver code ; Pre - calculate all the powers of 2
const MAX = 32 ; let pow2 = new Array ( MAX ) ; let visited = new Array ( MAX ) ; let ans = [ ] ; function power_2 ( ) { let ans = 1 ; for ( let i = 0 ; i < MAX ; i ++ ) { pow2 [ i ] = ans ; ans *= 2 ; } } function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } function add ( num ) { let point = 0 ; let value = 0 ; for ( let i = 0 ; i < MAX ; i ++ ) { if ( visited [ i ] ) continue ; else { if ( num & 1 ) { value += ( 1 << i ) ; } num = parseInt ( num / 2 ) ; } } ans . push ( value ) ; } function solve ( n , k ) { ans . push ( k ) ; let countk = countSetBits ( k ) ; if ( pow2 [ countk ] < n ) { document . write ( - 1 ) ; return ; } let count = 0 ; for ( let i = 0 ; i < pow2 [ countk ] - 1 ; i ++ ) { add ( i ) ; count ++ ; if ( count == n ) break ; } for ( let i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } let n = 3 , k = 5 ; power_2 ( ) ; solve ( n , k ) ;
Count of all possible values of X such that A % X = B | Function to return the count of all possible values for x such that ( A % x ) = B ; Case 1 ; Case 2 ; Case 3 ; Find the number of divisors of x which are greater than b ; Driver code
function countX ( a , b ) { if ( b > a ) return 0 ; else if ( a == b ) return - 1 ; else { let x = a - b , ans = 0 ; for ( let i = 1 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { let d1 = i , d2 = b - 1 ; if ( i * i != x ) d2 = parseInt ( x / i ) ; if ( d1 > b ) ans ++ ; if ( d2 > b ) ans ++ ; } } return ans ; } } let a = 21 , b = 5 ; document . write ( countX ( a , b ) ) ;
Find the sum of numbers from 1 to n excluding those which are powers of K | Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code
function sumPowersK ( n , k ) { var sum = 0 , num = 1 ; while ( num <= n ) { sum += num ; num *= k ; } return sum ; } function getSum ( n , k ) { var pwrK = sumPowersK ( n , k ) ; var sumAll = ( n * ( n + 1 ) ) / 2 ; return ( sumAll - pwrK ) ; } var n = 10 , k = 3 ; document . write ( getSum ( n , k ) ) ;
Maximum number of people that can be killed with strength P | Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code
function maxPeople ( p ) { var tmp = 0 , count = 0 ; for ( var i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } var p = 14 ; document . write ( maxPeople ( p ) ) ;
Maximum number of people that can be killed with strength P | Javascript implementation of the approach ; Function to return the maximum number of people that can be killed ; Storing the sum beforehand so that it can be used in each query ; lower_bound returns an iterator pointing to the first element greater than or equal to your val ; Previous value ; Returns the index in array upto which killing is possible with strength P ; Driver code
const kN = 1000000 ; function maxPeople ( p ) { let sums = new Array ( kN ) ; sums [ 0 ] = 0 ; for ( let i = 1 ; i < kN ; i ++ ) sums [ i ] = ( i * i ) + sums [ i - 1 ] ; let it = lower_bound ( sums , 0 , kN , p ) ; if ( it > p ) { -- it ; } return it ; } function lower_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + parseInt ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } let p = 14 ; document . write ( maxPeople ( p ) ) ;
Maximum number of people that can be killed with strength P | Helper function which returns the sum of series ( 1 ^ 2 + 2 ^ 2 + ... + n ^ 2 ) ; maxPeople function which returns appropriate value using Binary Search in O ( logn ) ; Set the lower and higher values ; Calculate the mid using low and high ; Compare value with n ; Return the ans ; Driver code
function squareSeries ( n ) { return Math . floor ( ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ) ; } function maxPeople ( n ) { let low = 0 ; let high = 1000000 ; let ans = 0 ; while ( low <= high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; let value = squareSeries ( mid ) ; if ( value <= n ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } let p = 14 ; document . write ( maxPeople ( p ) ) ;
Find the index which is the last to be reduced to zero after performing a given operation | Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code
function findIndex ( a , n , k ) { var index = - 1 , max_ceil = Number . MIN_VALUE ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + k - 1 ) / k ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= max_ceil ) { max_ceil = a [ i ] ; index = i ; } } return index ; } var arr = [ 31 , 12 , 25 , 27 , 32 , 19 ] ; var K = 5 ; var N = arr . length ; document . write ( findIndex ( arr , N , K ) ) ;
Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree | Javascript implementation of the approach ; Function to solve queries of the maximum xor value between the nodes in a given level L of a perfect binary tree and a given value X ; Initialize result ; Initialize array to store bits ; Initialize a copy of X and size of array ; Storing the bits of X in the array a [ ] ; Filling the array b [ ] ; Initializing variable which gives maximum xor ; Getting the maximum xor value ; Return the result ; Driver code ; Perform queries
var MAXN = 60 ; function solveQuery ( L , X ) { var res ; var a = Array ( MAXN ) , b = Array ( L ) ; var ref = X , size_a = 0 ; while ( ref > 0 ) { a [ size_a ] = ref % 2 ; ref = parseInt ( ref / 2 ) ; size_a ++ ; } for ( var i = 0 ; i < Math . min ( size_a , L ) ; i ++ ) { if ( a [ i ] == 1 ) b [ i ] = 0 ; else b [ i ] = 1 ; } for ( var i = Math . min ( size_a , L ) ; i < L ; i ++ ) b [ i ] = 1 ; b [ L - 1 ] = 1 ; var temp = 0 , p = 1 ; for ( var i = 0 ; i < L ; i ++ ) { temp += b [ i ] * p ; p *= 2 ; } res = temp ^ X ; return res ; } var queries = [ [ 2 , 5 ] , [ 3 , 15 ] ] ; var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) document . write ( solveQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) + " " ) ;
Count index pairs which satisfy the given condition | Function to return the count of required index pairs ; To store the required count ; Array to store the left elements upto which current element is maximum ; Iterating through the whole permutation except first and last element ; If current element can be maximum in a subsegment ; Current maximum ; Iterating for smaller values then current maximum on left of it ; Storing left borders of the current maximum ; Iterating for smaller values then current maximum on right of it ; Condition satisfies ; Return count of subsegments ; Driver Code
function Count_Segment ( p , n ) { var count = 0 ; var upto = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) upto [ i ] = 0 ; var j = 0 , curr = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( p [ i ] > p [ i - 1 ] && p [ i ] > p [ i + 1 ] ) { curr = p [ i ] ; j = i - 1 ; while ( j >= 0 && p [ j ] < curr ) { upto [ p [ j ] ] = curr ; j -= 1 ; } j = i + 1 ; while ( j < n && p [ j ] < curr ) { if ( upto [ curr - p [ j ] ] == curr ) count += 1 ; j += 1 ; } } } return count ; } var p = [ 3 , 4 , 1 , 5 , 2 ] ; var n = p . length ; document . write ( Count_Segment ( p , n ) ) ;
Check whether a number can be represented as sum of K distinct positive integers | Function that returns true if n can be represented as the sum of exactly k distinct positive integers ; If n can be represented as 1 + 2 + 3 + ... + ( k - 1 ) + ( k + x ) ; Driver code
function solve ( n , k ) { if ( n >= ( k * ( k + 1 ) ) / 2 ) { return true ; } return false ; } var n = 12 , k = 4 ; if ( solve ( n , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether product of integers from a to b is positive , negative or zero | Function to check whether the product of integers of the range [ a , b ] is positive , negative or zero ; If both a and b are positive then the product will be positive ; If a is negative and b is positive then the product will be zero ; If both a and b are negative then we have to find the count of integers in the range ; Total integers in the range ; If n is even then the resultant product is positive ; If n is odd then the resultant product is negative ; Driver code
function solve ( a , b ) { if ( a > 0 && b > 0 ) { document . write ( " " ) ; } else if ( a <= 0 && b >= 0 ) { document . write ( " " ) ; } else { let n = Math . abs ( a - b ) + 1 ; if ( n % 2 == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } let a = - 10 ; let b = - 2 ; solve ( a , b ) ;
Bitwise AND of sub | Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code
function closetAND ( arr , n , k ) { let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let X = arr [ i ] ; for ( let j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . min ( ans , Math . abs ( k - X ) ) ; } } return ans ; } let arr = [ 4 , 7 , 10 ] ; let n = arr . length ; let k = 2 ; document . write ( closetAND ( arr , n , k ) ) ;
Program to find the rate percentage from compound interest of consecutive years | Function to return the required rate percentage ; Driver code
function Rate ( N1 , N2 ) { var rate = ( N2 - N1 ) * 100 / N1 ; return parseInt ( rate ) ; } var N1 = 100 , N2 = 120 ; document . write ( Rate ( N1 , N2 ) + " " ) ;
Find prime number K in an array such that ( A [ i ] % K ) is maximum | Function to return the required prime number from the array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code
function getPrime ( arr , n ) { let max_val = Math . max ( ... arr ) ; let prime = new Array ( max_val + 1 ) . fill ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } let maximum = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) maximum = Math . max ( maximum , arr [ i ] ) ; } return maximum ; } let arr = [ 2 , 10 , 15 , 7 , 6 , 8 , 13 ] ; let n = arr . length ; document . write ( getPrime ( arr , n ) ) ;
Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code
function getMinNum ( N ) { var lcm = 1 ; for ( var i = 2 ; i <= N ; i ++ ) lcm = ( ( i * lcm ) / ( __gcd ( i , lcm ) ) ) ; return ( lcm + 1 ) ; } function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } var N = 5 ; document . write ( getMinNum ( N ) ) ;
Maximum number of edges in Bipartite graph | Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code
function maxEdges ( N ) { var edges = 0 ; edges = Math . floor ( ( N * N ) / 4 ) ; return edges ; } var N = 5 ; document . write ( maxEdges ( N ) ) ;
Check if the robot is within the bounds of the grid after given moves | Function that returns true if the robot is safe ; If current move is " L " then increase the counter of coll ; If value of coll is equal to column then break ; If current move is " R " then increase the counter of colr ; If value of colr is equal to column then break ; If current move is " U " then increase the counter of rowu ; If value of rowu is equal to row then break ; If current move is " D " then increase the counter of rowd ; If value of rowd is equal to row then break ; If robot is within the bounds of the grid ; Unsafe ; Driver code
function isSafe ( N , M , str ) { var coll = 0 , colr = 0 , rowu = 0 , rowd = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { coll ++ ; if ( colr > 0 ) { colr -- ; } if ( coll == M ) { break ; } } else if ( str [ i ] == ' ' ) { colr ++ ; if ( coll > 0 ) { coll -- ; } if ( colr == M ) { break ; } } else if ( str [ i ] == ' ' ) { - rowu ++ ; if ( rowd > 0 ) { rowd -- ; } if ( rowu == N ) { break ; } } else if ( str [ i ] == ' ' ) { rowd ++ ; if ( rowu > 0 ) { rowu -- ; } if ( rowd == N ) { break ; } } } if ( Math . abs ( rowd ) < N && Math . abs ( rowu ) < N && Math . abs ( coll ) < M && Math . abs ( colr ) < M ) { return true ; } return false ; } var N = 1 , M = 1 ; var str = " " ; if ( isSafe ( N , M , str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find permutation of first N natural numbers that satisfies the given condition | Function to find permutation ( p ) of first N natural numbers such that there are exactly K elements of permutation such that GCD ( p [ i ] , i ) > 1 ; First place all the numbers in their respective places ; Modify for first n - k integers ; In first index place n - k ; Print the permutation ; Driver code
function Permutation ( n , k ) { let p = new Array ( n + 1 ) ; for ( let i = 1 ; i <= n ; i ++ ) p [ i ] = i ; for ( let i = 1 ; i < n - k ; i ++ ) p [ i + 1 ] = i ; p [ 1 ] = n - k ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( p [ i ] + " " ) ; } let n = 5 , k = 2 ; Permutation ( n , k ) ;
Count number of 1 s in the array after N moves | Function to count number of 1 's in the array after performing N moves ; If index is multiple of move number ; Convert 0 to 1 ; Convert 1 to 0 ; Count number of 1 's ; count number of 1 's ; Initialize array size ; Initialize all elements to 0
function countOnes ( arr , N ) { for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = i ; j <= N ; j ++ ) { if ( j % i == 0 ) { if ( arr [ j - 1 ] == 0 ) arr [ j - 1 ] = 1 ; else arr [ j - 1 ] = 0 ; } } } let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) if ( arr [ i ] == 1 ) count ++ ; return count ; } let N = 10 ; let arr = new Uint8Array ( 10 ) ; let ans = countOnes ( arr , N ) ; document . write ( ans ) ;
Number of positions such that adding K to the element is greater than sum of all other elements | Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code
function validPosition ( arr , N , K ) { var count = 0 , sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( var i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } var arr = [ 2 , 1 , 6 , 7 ] , K = 4 ; var N = arr . length ; document . write ( validPosition ( arr , N , K ) ) ;
Count unique numbers that can be generated from N by adding one and removing trailing zeros | Function to count the unique numbers ; If the number has already been visited ; Insert the number to the set ; First step ; Second step remove trailing zeros ; Recur again for the new number ; Driver code
function count_unique ( s , n ) { if ( s . has ( n ) ) return ; s . add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = Math . floor ( n / 10 ) ; } count_unique ( s , n ) ; } let n = 10 ; let s = new Set ( ) ; count_unique ( s , n ) ; document . write ( s . size ) ;
Find element with the maximum set bits in an array | Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code
function maxBitElement ( arr , n ) { let num = 0 , max = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let cnt = BitCount ( arr [ i ] ) ; if ( cnt > max ) { max = cnt ; num = arr [ i ] ; } } return num ; } function BitCount ( n ) { let count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } let arr = [ 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 ] ; let n = arr . length ; document . write ( maxBitElement ( arr , n ) ) ;
Count pairs with average present in the same array | javascript implementation of the approach ; Function to return the count of valid pairs ; Frequency array Twice the original size to hold negative elements as well ; Update the frequency of each of the array element ; If say x = - 1000 then we will place the frequency of - 1000 at ( - 1000 + 1000 = 0 ) a [ 0 ] index ; To store the count of valid pairs ; Remember we will check only for ( even , even ) or ( odd , odd ) pairs of indexes as the average of two consecutive elements is a floating povar number ; Driver code
var N = 1000 ; function countPairs ( arr , n ) { var size = ( 2 * N ) + 1 ; var freq = Array ( size ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { var x = arr [ i ] ; freq [ x + N ] ++ ; } var ans = 0 ; for ( i = 0 ; i < size ; i ++ ) { if ( freq [ i ] > 0 ) { ans += ( ( freq [ i ] ) * ( freq [ i ] - 1 ) ) / 2 ; for ( j = i + 2 ; j < 2001 ; j += 2 ) { if ( freq [ j ] > 0 && ( freq [ ( i + j ) / 2 ] > 0 ) ) { ans += ( freq [ i ] * freq [ j ] ) ; } } } } return ans ; } var arr = [ 4 , 2 , 5 , 1 , 3 , 5 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Smallest and Largest sum of two n | Function to return the smallest sum of 2 n - digit numbers ; Function to return the largest sum of 2 n - digit numbers ; Driver code
function smallestSum ( n ) { if ( n == 1 ) return 0 ; return ( 2 * Math . pow ( 10 , n - 1 ) ) ; } function largestSum ( n ) { return ( 2 * ( Math . pow ( 10 , n ) - 1 ) ) ; } var n = 4 ; document . write ( " " + largestSum ( n ) + " " ) ; document . write ( " " + smallestSum ( n ) ) ;
Given two arrays count all pairs whose sum is an odd number | Function that returns the number of pairs ; Count of odd and even numbers ; Traverse in the first array and count the number of odd and even numbers in them ; Traverse in the second array and count the number of odd and even numbers in them ; Count the number of pairs ; Return the number of pairs ; Driver code
function count_pairs ( a , b , n , m ) { let odd1 = 0 , even1 = 0 ; let odd2 = 0 , even2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 ) odd1 ++ ; else even1 ++ ; } for ( let i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 ) odd2 ++ ; else even2 ++ ; } let pairs = Math . min ( odd1 , even2 ) + Math . min ( odd2 , even1 ) ; return pairs ; } let a = [ 9 , 14 , 6 , 2 , 11 ] ; let b = [ 8 , 4 , 7 , 20 ] ; let n = a . length ; let m = b . length ; document . write ( count_pairs ( a , b , n , m ) ) ;