codeparrot/xlcost-text-to-code huggingface dataset:
Kth smallest element in an array that contains A [ i ] exactly B [ i ] times | Function to find the Kth smallest element that contains A [ i ] exactly B [ i ] times ; Traverse the given array ; Stores the frequency of every elements ; Traverse the given array ; Initialize a variable to store the prefix sums ; Iterate over the range [ 0 , M ] ; Increment sum by freq [ i ] ; If sum is greater than or equal to K ; Return the current element as answer ; Return - 1 ; Given Input ; Function call
function KthSmallest ( A , B , N , K ) { let M = 0 ; for ( let i = 0 ; i < N ; i ++ ) { M = Math . max ( A [ i ] , M ) ; } let freq = Array . from ( { length : M + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ A [ i ] ] += B [ i ] ; } let sum = 0 ; for ( let i = 0 ; i <= M ; i ++ ) { sum += freq [ i ] ; if ( sum >= K ) { return i ; } } return - 1 ; } let A = [ 3 , 4 , 5 ] ; let B = [ 2 , 1 , 3 ] ; let N = A . length ; let K = 4 ; document . write ( KthSmallest ( A , B , N , K ) ) ;
Bitwise OR of Bitwise AND of all subarrays of an array | Function to find the Bitwise OR of Bitwise AND of all subarrays ; Stores the required result ; Generate all the subarrays ; Store the current element ; Find the Bitwise OR ; Update the result ; Print the result ; Driver Code
function findbitwiseOR ( a , n ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let curr_sub_array = a [ i ] ; res = res | curr_sub_array ; for ( let j = i ; j < n ; j ++ ) { curr_sub_array = curr_sub_array & a [ j ] ; res = res | curr_sub_array ; } } document . write ( res ) ; } let A = [ 1 , 2 , 3 ] ; let N = A . length ; findbitwiseOR ( A , N ) ;
Bitwise OR of Bitwise AND of all subarrays of an array | Function to find the Bitwise OR of Bitwise AND of all consecutive subsets of the array ; Stores the required result ; Traverse the given array ; Print the result ; Driver Code
function findbitwiseOR ( a , n ) { var res = 0 ; var i ; for ( i = 0 ; i < n ; i ++ ) res = res | a [ i ] ; document . write ( res ) ; } var A = [ 1 , 2 , 3 ] ; var N = A . length ; findbitwiseOR ( A , N ) ;
Check if sum of digits of a number exceeds the product of digits of that number | Function to check if the sum of the digits of N is strictly greater than the product of the digits of N or not ; Stores the sum and the product of the digits of N ; Stores the last digit if N ; Increment the value of sumOfDigits ; Update the prodOfDigit ; Divide N by 10 ; Prlet the result ; Driver Code
function check ( n ) { let sumOfDigit = 0 ; let prodOfDigit = 1 ; while ( n > 0 ) { let rem ; rem = n % 10 ; sumOfDigit += rem ; prodOfDigit *= rem ; n = Math . floor ( n / 10 ) ; } if ( sumOfDigit > prodOfDigit ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 1234 ; check ( N ) ;
Print all distinct even and odd prefix Bitwise XORs of first N natural numbers | Print all distinct even & odd prefix Bitwise XORs from 1 to N ; Print the even number ; Print the odd number ; Driver Code
function evenOddBitwiseXOR ( N ) { document . write ( " " + 0 + " " ) ; for ( let i = 4 ; i <= N ; i = i + 4 ) { document . write ( i + " " ) ; } document . write ( " " ) ; document . write ( " " + 1 + " " ) ; for ( let i = 4 ; i <= N ; i = i + 4 ) { document . write ( i - 1 + " " ) ; } if ( N % 4 == 2 ) document . write ( N + 1 ) ; else if ( N % 4 == 3 ) document . write ( N ) ; } let N = 6 ; evenOddBitwiseXOR ( N ) ;
Lexicographically largest permutation possible by a swap that is smaller than a given array | Function to lexicographic largest permutation possible by a swap that is smaller than given array ; Find the index of first element such that arr [ i ] > arr [ i + 1 ] ; If the array is sorted in increasing order ; Find the index of first element which is smaller than arr [ i ] ; If arr [ j ] = = arr [ j - 1 ] ; Decrement j ; Swap the element ; Print the array arr [ ] ; Driver Code
function findPermutation ( arr ) { let N = arr . length ; let i = N - 2 ; while ( i >= 0 && arr [ i ] <= arr [ i + 1 ] ) i -- ; if ( i == - 1 ) { document . write ( " " ) ; return ; } let j = N - 1 ; while ( j > i && arr [ j ] >= arr [ i ] ) j -- ; while ( j > i && arr [ j ] == arr [ j - 1 ] ) { j -- ; } let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; for ( let it in arr ) { document . write ( arr [ it ] + " " ) ; } } let arr = [ 1 , 2 , 5 , 3 , 4 , 6 ] ; findPermutation ( arr ) ;
Absolute difference between the count of odd and even factors of N | Function to find the smallest prime factor of all the numbers using Sieve Of Eratosthenes ; Stores whether any number is prime or not ; Initialize smallest factor as 2 for all the even numbers ; Iterate over the range [ 3 , N ] ; If i is prime ; Iterate all multiples of i ; i is the smallest prime factor of i * j ; Function to find the absolute difference between the count of odd and even factors of N ; Stores the smallest prime factor of i ; Fill values in s [ ] using sieve of eratosthenes ; Stores the total number of factors and the total number of odd and even factors ; Store the current prime factor of the number N ; Store the power of current prime factor ; Loop while N is greater than 1 ; If N also has smallest prime factor as curr , then increment cnt by 1 ; Update only total number of factors if curr is 2 ; Update total number of factors and total number of odd factors ; Update current prime factor as s [ N ] and count as 1 ; Calculate the number of even factors ; Prlet the difference ; Driver Code
function sieveOfEratosthenes ( N , s ) { let prime = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( let i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( let j = i ; j * i <= N ; j += 2 ) { if ( ! prime [ i * j ] ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } function findDifference ( N ) { let s = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; sieveOfEratosthenes ( N , s ) ; let total = 1 , odd = 1 , even = 0 ; let curr = s [ N ] ; let cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } if ( curr == 2 ) { total = total * ( cnt + 1 ) ; } else { total = total * ( cnt + 1 ) ; odd = odd * ( cnt + 1 ) ; } curr = s [ N ] ; cnt = 1 ; } even = total - odd ; document . write ( Math . abs ( even - odd ) ) ; } let N = 12 ; findDifference ( N ) ;
Calculate Median from given values of Mean and Mode | Function to find the median of a group of data with given mean and mode ; Calculate the median ; Print the median ; Driver Code
function findMedian ( Mean , Mode ) { var Median = ( 2 * Mean + Mode ) / 3.0 ; document . write ( Median ) ; } var mode = 6 , mean = 3 ; findMedian ( mean , mode ) ;
Program to find the Magnitude of a Vector | Function to calculate magnitude of a 3 dimensional vector ; Stores the sum of squares of coordinates of a vector ; Return the magnitude ; Driver Code
function vectorMagnitude ( x , y , z ) { var sum = x * x + y * y + z * z ; return Math . sqrt ( sum ) ; } var x = 1 ; var y = 2 ; var z = 3 ; document . write ( vectorMagnitude ( x , y , z ) ) ;
Program to find the product of a number with a Mersenne Number | Function to find prodcut of a Mersenne number with another number ; Stores the power of 2 of integer M + 1 ; Return the product ; Driver code
function multiplyByMersenne ( N , M ) { let x = ( Math . log ( M + 1 ) / Math . log ( 2 ) ) ; return ( ( N << x ) - N ) ; } let N = 4 ; let M = 15 ; document . write ( multiplyByMersenne ( N , M ) ) ;
Nearest power of 2 of nearest perfect squares of non | Function to find nearest perfect square of num ; Calculate square root of num ; Calculate perfect square ; Find the nearest perfect square ; Function to find the power of 2 nearest to the number num ; Calculate log base 2 of num ; Highest power of 2 which is <= num ; Function to find the nearest perfect square and the nearest power of 2 of every array element whose occurrence is 1 ; Stores frequency of array elements ; Traverse the array and update frequency of current array element ; Traverse the map freq ; If the frequency is 1 ; Find nearest perfect square ; Print the nearest power of 2 ; If the any does not contain any non - repeating elements ; Driver Code
function perfectSquare ( num ) { let sr = Math . floor ( Math . sqrt ( num ) ) ; let a = sr * sr ; let b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( num - a ) < ( b - num ) ) { return a ; } else { return b ; } } function powerOfTwo ( num ) { let lg = Math . floor ( Math . log2 ( num ) ) ; let p = Math . pow ( 2 , lg ) ; return p ; } function uniqueElement ( arr , N ) { let ans = true ; arr . reverse ( ) ; let freq = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) } else [ freq . set ( arr [ i ] , 1 ) ] } for ( let el of freq ) { if ( el [ 1 ] == 1 ) { ans = false ; let ps = perfectSquare ( el [ 0 ] ) ; document . write ( powerOfTwo ( ps ) + ' ' ) ; } } if ( ans ) document . write ( " " ) ; } let arr = [ 4 , 11 , 4 , 3 , 4 ] ; let N = arr . length ; uniqueElement ( arr , N ) ;
Partition array into two subarrays with every element in the right subarray strictly greater than every element in left subarray | Function to partition the array into two non - empty subarrays which satisfies the given condition ; Stores the suffix min array ; Stores the minimum of a suffix ; Traverse the array in reverse ; Update minimum ; Store the minimum ; Stores the maximum value of a prefix ; Stores the index of the partition ; Update max ; If max is less than min [ i + 1 ] ; Store the index of partition ; break ; If ind is not - 1 ; Print the first subarray ; Print the second subarray ; Otherwise ; Driver Code
function partitionArray ( a , n ) { var min = Array ( n ) . fill ( 0 ) ; var mini = Number . MAX_VALUE ; for ( i = n - 1 ; i >= 0 ; i -- ) { mini = Math . min ( mini , a [ i ] ) ; min [ i ] = mini ; } var maxi = Number . MIN_VALUE ; var ind = - 1 ; for ( i = 0 ; i < n - 1 ; i ++ ) { maxi = Math . max ( maxi , a [ i ] ) ; if ( maxi < min [ i + 1 ] ) { ind = i ; break ; } } if ( ind != - 1 ) { for ( i = 0 ; i <= ind ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; for ( i = ind + 1 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; } else document . write ( " " ) ; } var arr = [ 5 , 3 , 2 , 7 , 9 ] ; var N = arr . length ; partitionArray ( arr , N ) ;
Check if a number can be represented as sum of K positive integers out of which at least K | Function to count all prime factors of a given number ; Count the number of 2 s that divides n ; Since n is odd at this point , skip one element ; While i divides n , count i and divide n ; If n is a prime number greater than 2 ; Function to find the sum of first n nearly prime numbers ; Store the required sum ; Add this number if it is satisfies the condition ; Increment count of nearly prime numbers ; Function to check if N can be represented as sum of K different positive integers out of which at least K - 1 of them are nearly prime ; Store the sum of first K - 1 nearly prime numbers ; If sum is greater than or equal to n ; Otherwise , prvar Yes ; Driver Code
function countPrimeFactors ( n ) { var count = 0 ; while ( n % 2 == 0 ) { n = parseInt ( n / 2 ) ; count ++ ; } for ( i = 3 ; i <= parseInt ( Math . sqrt ( n ) ) ; i = i + 2 ) { while ( n % i == 0 ) { n = parseInt ( n / i ) ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } function findSum ( n ) { var sum = 0 ; for ( i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == 2 ) { sum += num ; i ++ ; } } return sum ; } function check ( n , k ) { var s = findSum ( k - 1 ) ; if ( s >= n ) document . write ( " " ) ; else document . write ( " " ) ; } var n = 100 , k = 6 ; check ( n , k ) ;
Count ways to represent an integer as an exponent | Function to calculate GCD of a and b using Euclidean Algorithm ; Iterate until b is non - zero ; Return the GCD ; Function to count the number of ways N can be expressed as x ^ y ; Base Case ; Stores the gcd of powers ; Calculate the degree of 2 in N ; Calculate the degree of prime numbers in N ; Calculate the degree of prime ' i ' in N ; If N is a prime , g becomes 1. ; Stores the number of ways to represent N as x ^ y ; Find the number of Factors of g ; Update the count of ways ; Iterate to find rest of the prime numbers ; Find the power of i ; Update the count of ways ; If g is prime ; Return the total number of ways ; Driver Code
function gcd ( a , b ) { while ( b > 0 ) { let rem = a % b ; a = b ; b = rem ; } return a ; } function countNumberOfWays ( n ) { if ( n == 1 ) return - 1 ; let g = 0 ; let power = 0 ; while ( n % 2 == 0 ) { power ++ ; n /= 2 ; } g = gcd ( g , power ) ; for ( let i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { power = 0 ; while ( n % i == 0 ) { power ++ ; n /= i ; } g = gcd ( g , power ) ; } if ( n > 2 ) g = gcd ( g , 1 ) ; let ways = 1 ; power = 0 ; while ( g % 2 == 0 ) { g /= 2 ; power ++ ; } ways *= ( power + 1 ) ; for ( let i = 3 ; i <= Math . sqrt ( g ) ; i += 2 ) { power = 0 ; while ( g % i == 0 ) { power ++ ; g /= i ; } ways *= ( power + 1 ) ; } if ( g > 2 ) ways *= 2 ; return ways ; } let N = 64 ; document . write ( countNumberOfWays ( N ) ) ;
Highest power of 2 less than or equal to given Integer | Function to return the lowest power of 2 close to given positive number ; Floor function is used to determine the value close to the number ; Function to return the lowest power of 2 close to given negative number ; Ceil function is used for negative numbers as - 1 > - 4. It would be opposite to positive numbers where 1 < 4 ; Function to find the highest power of 2 ; To check if the given number is positive or negative ; If the number is negative , then the ceil of the positive number is calculated and negative sign is added ; Driver code
function powOfPositive ( n ) { let pos = Math . floor ( Math . log2 ( n ) ) ; return Math . pow ( 2 , pos ) ; } function powOfNegative ( n ) { let pos = Math . ceil ( Math . log2 ( n ) ) ; return ( - 1 * Math . pow ( 2 , pos ) ) ; } function highestPowerOf2 ( n ) { if ( n > 0 ) { document . write ( powOfPositive ( n ) ) ; } else { n = - n ; document . write ( powOfNegative ( n ) ) ; } } let n = - 24 ; highestPowerOf2 ( n ) ;
Number of cards needed build a House of Cards of a given level N | Function to find number of cards needed ; Driver Code
function noOfCards ( n ) { return parseInt ( n * ( 3 * n + 1 ) / 2 ) ; } var n = 3 ; document . write ( noOfCards ( n ) ) ;
Find smallest possible Number from a given large Number with same count of digits | Function for finding the smallest possible number after swapping the digits any number of times ; Variable to store the final answer ; Array to store the count of occurrence of each digit ; Loop to calculate the number of occurrences of every digit ; Loop to get smallest number ; Returning the answer ; Driver code
function smallestPoss ( s , n ) { var ans = " " ; var arr = Array ( 10 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ s [ i ] . charCodeAt ( 0 ) - 48 ] ++ ; } for ( var i = 0 ; i < 10 ; i ++ ) { for ( var j = 0 ; j < arr [ i ] ; j ++ ) ans = ans + i . toString ( ) ; } return ans ; } var N = 15 ; var K = " " ; document . write ( smallestPoss ( K , N ) ) ;
Count the subarray with sum strictly greater than the sum of remaining elements | Function to count the number of sub - arrays with sum strictly greater than the remaining elements of array ; For loop for beginning point of a subarray ; For loop for ending point of the subarray ; Initialise subarray_sum and remaining_sum to 0 ; For loop to calculate the sum of generated subarray ; For loop to calculate the sum remaining array element ; Checking for condition when subarray sum is strictly greater than remaining sum of array element ; Driver code
function Count_subarray ( arr , n ) { var subarray_sum , remaining_sum , count = 0 ; var i , j , k , l ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i ; j < n ; j ++ ) { subarray_sum = 0 ; remaining_sum = 0 ; for ( k = i ; k <= j ; k ++ ) { subarray_sum += arr [ k ] ; } for ( l = 0 ; l < i ; l ++ ) { remaining_sum += arr [ l ] ; } for ( l = j + 1 ; l < n ; l ++ ) { remaining_sum += arr [ l ] ; } if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } var arr = [ 10 , 9 , 12 , 6 ] ; var n = arr . length ; document . write ( Count_subarray ( arr , n ) ) ;
Count the subarray with sum strictly greater than the sum of remaining elements | javascript implementation of the above approach ; Calculating total sum of given array ; For loop for beginning povar of a subarray ; initialise subarray_sum to 0 ; For loop for calculating subarray_sum and remaining_sum ; Calculating subarray_sum and corresponding remaining_sum ; Checking for the condition when subarray sum is strictly greater than the remaining sum of the array element ; Driver code
function Count_subarray ( arr , n ) { var total_sum = 0 , subarray_sum , remaining_sum , count = 0 ; for ( i = 0 ; i < n ; i ++ ) { total_sum += arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { subarray_sum = 0 ; for ( j = i ; j < n ; j ++ ) { subarray_sum += arr [ j ] ; remaining_sum = total_sum - subarray_sum ; if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } var arr = [ 10 , 9 , 12 , 6 ] ; var n = arr . length ; document . write ( Count_subarray ( arr , n ) ) ;
Remove one element to get maximum XOR | Function to return the maximized XOR after removing an element from the array ; Find XOR of the complete array ; To store the final answer ; Iterating through the array to find the final answer ; Return the final answer ; Driver code
function maxXOR ( arr , n ) { let xorArr = 0 ; for ( let i = 0 ; i < n ; i ++ ) xorArr ^= arr [ i ] ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , ( xorArr ^ arr [ i ] ) ) ; return ans ; } let arr = [ 1 , 1 , 3 ] ; let n = arr . length ; document . write ( maxXOR ( arr , n ) ) ;
Count of numbers from the range [ L , R ] which contains at least one digit that divides K | Function that returns true if num contains at least one digit that divides k ; Get the last digit ; If the digit is non - zero and it divides k ; Remove the last digit ; There is no digit in num that divides k ; Function to return the required count of elements from the given range which contain at least one digit that divides k ; To store the result ; For every number from the range ; If any digit of the current number divides k ; Driver code
function digitDividesK ( num , k ) { while ( num ) { let d = num % 10 ; if ( d != 0 && k % d == 0 ) return true ; num = parseInt ( num / 10 ) ; } return false ; } function findCount ( l , r , k ) { let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { if ( digitDividesK ( i , k ) ) count ++ ; } return count ; } let l = 20 , r = 35 ; let k = 45 ; document . write ( findCount ( l , r , k ) ) ;
Check if a given number is factorial of any number | Function to check if the given number is a factorial of any number ; Driver Code
function isFactorial ( n ) { for ( var i = 1 ; ; i ++ ) { if ( n % i == 0 ) { n = parseInt ( n / i ) ; } else { break ; } } if ( n == 1 ) { return true ; } else { return false ; } } var n = 24 ; var ans = isFactorial ( n ) ; if ( ans == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Remove an element to minimize the LCM of the given array | Function to return the LCM of two numbers ; Function to return the minimum LCM after removing a single element from the given array ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing LCM of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing LCM of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be removed ; If any other element is replaced ; Return the minimum LCM ; Driver code
function lcm ( a , b ) { let GCD = __gcd ( a , b ) ; return Math . floor ( ( a * b ) / GCD ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function MinLCM ( a , n ) { let Prefix = new Array ( n + 2 ) ; let Suffix = new Array ( n + 2 ) ; Prefix [ 1 ] = a [ 0 ] ; for ( let i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = lcm ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( let i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = lcm ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } let ans = Math . min ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( let i = 2 ; i < n ; i += 1 ) { ans = Math . min ( ans , lcm ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } let a = [ 5 , 15 , 9 , 36 ] ; let n = a . length ; document . write ( MinLCM ( a , n ) ) ;
Number of coloured 0 's in an N | Function to return the count of coloured 0 s in an n - level hexagon ; Driver code
function count ( n ) { return parseInt ( n * ( 3 * n - 1 ) / 2 ) ; } var n = 3 ; document . write ( count ( n ) ) ;
Minimum value to be assigned to the elements so that sum becomes greater than initial sum | Function to return the minimum required value ; Find the sum of the array elements ; Return the required value ; Driver code
function findMinValue ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return ( parseInt ( sum / n ) + 1 ) ; } let arr = [ 4 , 2 , 1 , 10 , 6 ] ; let n = arr . length ; document . write ( findMinValue ( arr , n ) ) ;
Color all boxes in line such that every M consecutive boxes are unique | Javascript implementation of the approach ; Function to return ( m ! % MOD ) ; Driver code
const MOD = 1000000007 ; function modFact ( n , m ) { let result = 1 ; for ( let i = 1 ; i <= m ; i ++ ) result = ( result * i ) % MOD ; return result ; } let n = 3 , m = 2 ; document . write ( modFact ( n , m ) ) ;
Sum of squares of all Subsets of given Array | Javascript implementation of the approach ; Function to return ( 2 ^ P % mod ) ; Function to return the sum of squares of subsets ; Sqauaring the elements and adding it to ans ; Driver code
const mod = 1000000000 + 7 ; function power ( p ) { let res = 1 ; for ( let i = 1 ; i <= p ; ++ i ) { res *= 2 ; res %= mod ; } return res % mod ; } function subset_square_sum ( A ) { let n = A . length ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ans += ( A [ i ] * A [ i ] ) % mod ; ans %= mod ; } return ( ans * power ( n - 1 ) ) % mod ; } let A = [ 3 , 7 ] ; document . write ( subset_square_sum ( A ) ) ;
Find the number of pairs such that their gcd is equals to 1 | Javascript program to find the number of pairs such that gcd equals to 1 ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Function to find the number of pairs such that gcd equals to 1 ; To store maximum number ; To store frequency of each number ; Find frequency and maximum number ; To store number of pairs with gcd equals to 1 ; Traverse through the all possible elements ; Return the number of pairs ; Driver code ; Function call
var N = 100050 ; var lpf = Array ( N ) . fill ( 0 ) ; var mobius = Array ( N ) . fill ( 0 ) ; function least_prime_factor ( ) { for ( i = 2 ; i < N ; i ++ ) if ( lpf [ i ] == 0 ) for ( j = i ; j < N ; j += i ) if ( lpf [ j ] == 0 ) lpf [ j ] = i ; } function Mobius ( ) { for ( i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } } function gcd_pairs ( a , n ) { var maxi = 0 ; var fre = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { fre [ a [ i ] ] ++ ; maxi = Math . max ( a [ i ] , maxi ) ; } least_prime_factor ( ) ; Mobius ( ) ; var ans = 0 ; for ( i = 1 ; i <= maxi ; i ++ ) { if ( mobius [ i ] == 0 ) continue ; var temp = 0 ; for ( j = i ; j <= maxi ; j += i ) temp = parseInt ( temp + fre [ j ] ) ; ans += parseInt ( temp * ( temp - 1 ) / 2 * mobius [ i ] ) ; } return ans ; } var a = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = a . length ; document . write ( gcd_pairs ( a , n ) ) ;