codeparrot/xlcost-text-to-code huggingface dataset:
Maximize product of subarray sum with its maximum element | Function to find the maximum product of the sum of the subarray with its maximum element ; Traverse the array arr [ ] ; Increment currSum by a [ i ] ; Maximize the value of currMax ; Maximize the value of largestSum ; If currSum goes less than 0 then update currSum = 0 ; Return the resultant value ; Function to maximize the product of the sum of the subarray with its maximum element ; Find the largest sum of the subarray ; Multiply each array element with - 1 ; Find the largest sum of the subarray with negation of all array element ; Return the resultant maximum value ; Driver Code
function Kadane ( arr , n ) { let largestSum = 0 , currMax = 0 ; let currSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currSum += arr [ i ] ; currMax = Math . max ( currMax , arr [ i ] ) ; largestSum = Math . max ( largestSum , currMax * currSum ) ; if ( currSum < 0 ) { currMax = 0 ; currSum = 0 ; } } return largestSum ; } function maximumWeight ( arr , n ) { let largestSum = Kadane ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = - arr [ i ] ; } largestSum = Math . max ( largestSum , Kadane ( arr , n ) ) ; return largestSum ; } let arr = [ 2 , - 3 , 8 , - 2 , 5 ] ; let N = arr . length ; document . write ( maximumWeight ( arr , N ) ) ;
Partition an array into two subsets with equal count of unique elements | Function to partition the array into two subsets such that count of unique elements in both subsets is the same ; Stores the subset number for each array elements ; Stores the count of unique array elements ; Stores the frequency of elements ; Traverse the array ; Count of elements having a frequency of 1 ; Check if there exists any element with frequency > 2 ; Count of elements needed to have frequency exactly 1 in each subset ; Initialize all values in the / array ans [ ] as 1 ; Traverse the array ans [ ] ; This array element is a part of first subset ; Half array elements with frequency 1 are part of the second subset ; If count of elements is exactly 1 are odd and has no element with frequency > 2 ; If count of elements that occurs exactly once are even ; Print the result ; If the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; Print the result ; Driver Codea
function arrayPartition ( a , n ) { let ans = new Array ( n ) ; let cnt = 0 ; let ind , flag = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( a [ i ] ) ) { mp . set ( a [ i ] , mp . get ( a [ i ] ) + 1 ) } else { mp . set ( a [ i ] , 1 ) } } for ( let i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == 1 ) cnt ++ ; if ( mp . get ( a [ i ] ) > 2 && flag == 0 ) { flag = 1 ; ind = i ; } } let p = Math . floor ( ( cnt + 1 ) / 2 ) ; let ans1 = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == 1 && ans1 < p ) { ans [ i ] = 1 ; ans1 ++ ; } else if ( mp . get ( a [ i ] ) == 1 ) { ans [ i ] = 2 ; } } if ( cnt % 2 == 1 && flag == 0 ) { document . write ( - 1 + " " ) ; return ; } if ( cnt % 2 == 0 ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } else { for ( let i = 0 ; i < n ; i ++ ) { if ( ind == i ) document . write ( 2 + " " ) ; else document . write ( ans [ i ] << " " ) ; } } } let arr = [ 1 , 1 , 2 , 3 , 4 , 4 ] ; let N = arr . length arrayPartition ( arr , N ) ;
Modify a given array by replacing each element with the sum or product of their digits based on a given condition | Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
function evenOdd ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let even_digits = 0 ; let odd_digits = 0 ; let temp = arr [ i ] ; while ( temp ) { if ( ( temp % 10 ) & 1 ) odd_digits ++ ; else even_digits ++ ; temp = parseInt ( temp / 10 ) } if ( even_digits > odd_digits ) { let res = 0 ; while ( arr [ i ] ) { res += arr [ i ] % 10 ; arr [ i ] = parseInt ( arr [ i ] / 10 ) ; } document . write ( res + " " ) ; } else if ( odd_digits > even_digits ) { let res = 1 ; while ( arr [ i ] ) { res *= arr [ i ] % 10 ; arr [ i ] = parseInt ( arr [ i ] / 10 ) } document . write ( res + " " ) ; } else document . write ( arr [ i ] + " " ) ; } } let arr = [ 113 , 141 , 214 , 3186 ] ; let N = arr . length ; evenOdd ( arr , N ) ;
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers | Function to print the prime factorization of the product of all numbers <= N that are even and can be expressed as a product of two distinct prime numbers ; Sieve of Eratosthenese ; Store prime numbers in the range [ 3 , N / 2 ] ; Print the coefficient of 2 in the prime factorization ; Print the coefficients of other primes ; Input ; Function calling
function primeFactorization ( N ) { let sieve = new Array ( parseInt ( N / 2 ) + 1 ) . fill ( 0 ) ; for ( let i = 2 ; i <= N / 2 ; i ++ ) { if ( sieve [ i ] == 0 ) { for ( let j = i * i ; j <= N / 2 ; j += i ) { sieve [ j ] = 1 ; } } } let prime = [ ] ; for ( let i = 3 ; i <= N / 2 ; i ++ ) if ( sieve [ i ] == 0 ) prime . push ( i ) ; let x = prime . length ; document . write ( " " + x ) ; document . write ( " " ) for ( let i of prime ) { document . write ( i + " " ) ; document . write ( " " ) ; } } let N = 18 ; primeFactorization ( N ) ;
Sum of the first M elements of Array formed by infinitely concatenating given array | Function to find the sum of first M numbers formed by the infinite concatenation of the array A [ ] ; Stores the resultant sum ; Iterate over the range [ 0 , M - 1 ] ; Add the value A [ i % N ] to sum ; Return the resultant sum ; Driver Code
function sumOfFirstM ( A , N , M ) { let sum = 0 ; for ( let i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } let arr = [ 1 , 2 , 3 ] ; let M = 5 ; let N = arr . length ; document . write ( sumOfFirstM ( arr , N , M ) ) ;
Check if it is possible to reach M from 0 by given paths | Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code
function canReach0toM ( a , n , m ) { let rightMost = new Array ( m + 1 ) ; let dp = new Array ( m + 1 ) ; for ( let i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let a1 = a [ i ] [ 0 ] ; let b1 = a [ i ] [ 1 ] ; rightMost [ a1 ] = Math . max ( rightMost [ a1 ] , b1 ) ; } for ( let i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( let j = Math . min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let arr = [ [ 0 , 2 ] , [ 2 , 2 ] , [ 2 , 5 ] , [ 4 , 5 ] ] ; let M = 5 ; let N = arr . length ; canReach0toM ( arr , N , M ) ;
Count of non co | Recursive function to return gcd of two numbers ; Function to count the number of non co - prime pairs for each query ; Traverse the array arr [ ] ; Stores the count of non co - prime pairs ; Iterate over the range [ 1 , x ] ; Iterate over the range [ x , y ] ; If gcd of current pair is greater than 1 ; Given Input ; Function Call
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function countPairs ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) { var count = 0 ; for ( var x = 1 ; x <= arr [ i ] ; x ++ ) { for ( var y = x ; y <= arr [ i ] ; y ++ ) { if ( gcd ( x , y ) > 1 ) count ++ ; } } document . write ( count + " " ) ; } } var arr = [ 5 , 10 , 20 ] ; var N = 3 ; countPairs ( arr , N ) ;
Count of non co | JavaScript program for the above approach ; Auxiliary function to pre - compute the answer for each array ; Iterate over the range [ 1 , MAX ] ; Iterate over the range [ 1 , MAX ] ; If the number is prime ; Subtract the number of pairs which has i as one of their factors ; Iterate over the range [ 1 , MAX ] ; Function to count the number of non co - prime pairs for each query ; The i - th element stores the count of element that are co - prime with i ; Stores the resulting array ; Function Call ; Traverse the array arr [ ] ; Given Input ; Function Call
const MAX = 1005 ; function preCalculate ( phi , ans ) { phi [ 0 ] = 0 ; phi [ 1 ] = 1 ; for ( let i = 2 ; i <= MAX ; i ++ ) phi [ i ] = i ; for ( let i = 2 ; i <= MAX ; i ++ ) { if ( phi [ i ] == i ) { for ( let j = i ; j <= MAX ; j += i ) phi [ j ] -= Math . floor ( phi [ j ] / i ) ; } } for ( let i = 1 ; i <= MAX ; i ++ ) ans [ i ] = ans [ i - 1 ] + ( i - phi [ i ] ) ; } function countPairs ( arr , N ) { let phi = new Array ( 1e5 ) . fill ( 0 ) ; let ans = new Array ( 1e5 ) . fill ( 0 ) ; preCalculate ( phi , ans ) ; for ( let i = 0 ; i < N ; ++ i ) { document . write ( ans [ arr [ i ] ] + " " ) ; } } let arr = [ 5 , 10 , 20 ] ; let N = 3 ; countPairs ( arr , N ) ;
Maximum number of multiplication by 3 or division by 2 operations possible on an array | Function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; Stores the maximum number of operations possible ; Traverse the array arr [ ] ; Iterate until arr [ i ] is even ; Increment count by 1 ; Update arr [ i ] ; Return the value of Count as the answer ; Given Input ; Function Call
function maximumTurns ( arr , N ) { let Count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { while ( arr [ i ] % 2 == 0 ) { Count ++ ; arr [ i ] = Math . floor ( arr [ i ] / 2 ) ; } } return Count ; } let arr = [ 5 , 2 , 4 ] ; let M = 3 , K = 2 ; let N = arr . length ; document . write ( maximumTurns ( arr , N ) ) ;
Distribute the white and black objects into maximum groups under certain constraints | Function to check if it is possible to distribute W and B into maximum groups possible ; If W is greater than B , swap them ; Distribution is not possible ; Distribution is possible ; Input ; Function call
function isPossible ( W , B , D ) { if ( W > B ) { let temp = W ; W = B ; B = temp ; } if ( B > W * ( D + 1 ) ) document . write ( " " ) ; else document . write ( " " ) ; } let W = 2 ; let B = 5 ; let D = 2 ; isPossible ( W , B , D ) ;
Find the maximum GCD possible for some pair in a given range [ L , R ] | Function to calculate GCD ; Function to calculate maximum GCD in a range ; Variable to store the answer ; If Z has two multiples in [ L , R ] ; Update ans ; Return the value ; Input ; Function Call
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function maxGCDInRange ( L , R ) { let ans = 1 ; for ( let Z = R ; Z >= 1 ; Z -- ) { if ( parseInt ( ( R / Z ) ) - parseInt ( ( L - 1 ) / Z ) > 1 ) { ans = Z ; break ; } } return ans ; } let L = 102 ; let R = 139 ; document . write ( maxGCDInRange ( L , R ) ) ;
Number of ways to form a number with maximum Ks in it | Function to calculate number of ways a number can be formed that has the maximum number of Ks ; Convert to string ; Calculate length of subarrays that can contribute to the answer ; Count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; Return the answer ; Input ; Function call
function noOfWays ( N , K ) { let S = N . toString ( ) ; let ans = 1 ; for ( let i = 1 ; i < S . length ; i ++ ) { let count = 1 ; while ( i < S . length && S [ i ] . charCodeAt ( ) - 48 + S [ i - 1 ] . charCodeAt ( ) - 48 == K ) { count ++ ; i ++ ; } if ( count % 2 == 1 ) ans *= Math . floor ( ( count + 1 ) / 2 ) ; } return ans ; } let N = 1454781 ; let K = 9 ; document . write ( noOfWays ( N , K ) ) ;
Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND | Function for counting number of set bit ; Function to return the count of minimum operations required ; cnt to count the number of bits set in A and in B ; if odd numbers of total set bits ; one_zero = 1 in A and 0 in B at ith bit similarly for zero_one ; When bitpos is set in B , unset in B ; When bitpos is set in A , unset in B ; number of moves is half of number pairs of each group ; odd number pairs ; Input ; Function call to compute the result
function countSetBits ( n ) { let count = 0 ; while ( n ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } function minOperations ( A , B ) { let cnt1 = 0 , cnt2 = 0 ; cnt1 += countSetBits ( A ) ; cnt2 += countSetBits ( B ) ; if ( ( cnt1 + cnt2 ) % 2 != 0 ) return - 1 ; let oneZero = 0 , zeroOne = 0 ; let ans = 0 ; for ( let i = 0 ; i < Math . max ( cnt1 , cnt2 ) ; i ++ ) { let bitpos = 1 << i ; if ( ( ! ( bitpos & A ) ) && ( bitpos & B ) ) zeroOne ++ ; if ( ( bitpos & A ) && ( ! ( bitpos & B ) ) ) oneZero ++ ; } ans = parseInt ( zeroOne / 2 ) + parseInt ( oneZero / 2 ) ; if ( zeroOne % 2 != 0 ) ans += 2 ; return ans ; } let A = 27 , B = 5 ; document . write ( minOperations ( A , B ) ) ;
Largest sum subarray of size K containing consecutive elements | Function to find the largest sum subarray such that it contains K consecutive elements ; Stores sum of subarray having K consecutive elements ; Stores the maximum sum among all subarrays of size K having consecutive elements ; Traverse the array ; Store K elements of one subarray at a time ; Sort the duplicate array in ascending order ; Checks if elements in subarray are consecutive or not ; Traverse the k elements ; If not consecutive , break ; If flag is true update the maximum sum ; Stores the sum of elements of the current subarray ; Update the max_sum ; Reset curr_sum ; Return the result ; Driver Code
function maximumSum ( A , N , K ) { let curr_sum = 0 ; let max_sum = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < N - K + 1 ; i ++ ) { let dupl_arr = [ ... A . slice ( i , i + K ) ] ; dupl_arr . sort ( ( a , b ) => a - b ) let flag = true ; for ( let j = 1 ; j < K ; j ++ ) { if ( dupl_arr [ j ] - dupl_arr [ j - 1 ] != 1 ) { flag = false ; break ; } } if ( flag ) { let temp = 0 ; curr_sum = dupl_arr . reduce ( ( acc , cur ) => acc + cur , 0 ) max_sum = Math . max ( max_sum , curr_sum ) ; curr_sum = 0 ; } } return max_sum ; } let arr = [ 10 , 12 , 9 , 8 , 10 , 15 , 1 , 3 , 2 ] ; let K = 3 ; let N = arr . length ; document . write ( maximumSum ( arr , N , K ) ) ;
Find the count of Smith Brothers Pairs in a given Array | JavaScript program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Iterate through all pairs ; Increment count if there is a smith brother pair ; Preprocessing ; Input ; Function call
let MAX = 10000 ; let primes = new Array ( ) ; function sieveSundaram ( ) { let marked = Array . from ( { length : MAX / 2 + 100 } , ( _ , i ) => 0 ) ; for ( let i = 1 ; i <= ( Math . floor ( Math . sqrt ( MAX ) - 1 ) ) / 2 ; i ++ ) for ( let j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push ( 2 ) ; for ( let i = 1 ; i <= Math . floor ( MAX / 2 ) ; i ++ ) if ( marked [ i ] == false ) primes . push ( 2 * i + 1 ) ; } function isSmith ( n ) { let original_no = n ; let pDigitSum = 0 ; for ( let i = 0 ; primes [ i ] <= Math . floor ( n / 2 ) ; i ++ ) { while ( n % primes [ i ] == 0 ) { let p = primes [ i ] ; n = Math . floor ( n / p ) ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = Math . floor ( p / 10 ) ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = Math . floor ( n / 10 ) ; } } let sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = Math . floor ( original_no / 10 ) ; } return ( pDigitSum == sumDigits ) ; } function isSmithBrotherPair ( X , Y ) { return isSmith ( X ) && isSmith ( Y ) && Math . abs ( X - Y ) == 1 ; } function countSmithBrotherPairs ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = i + 1 ; j < N ; j ++ ) { if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) count ++ ; } return count ; } sieveSundaram ( ) ; let A = [ 728 , 729 , 28 , 2964 , 2965 ] ; let N = A . length ; document . write ( countSmithBrotherPairs ( A , N ) ) ;
Find the count of Smith Brothers Pairs in a given Array | JavaScript Program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Variable to store number of Smith Brothers Pairs ; sort A ; check for consecutive numbers only ; Preprocessing sieve of sundaram ; Input ; Function call
const MAX = 10000 ; var primes = [ ] ; function sieveSundaram ( ) { let marked = new Array ( MAX / 2 + 100 ) . fill ( false ) ; for ( let i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( let j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push ( 2 ) ; for ( let i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push ( 2 * i + 1 ) ; } function isSmith ( n ) { let original_no = n ; let pDigitSum = 0 ; for ( let i = 0 ; primes [ i ] <= Math . floor ( n / 2 ) ; i ++ ) { while ( n % primes [ i ] == 0 ) { let p = primes [ i ] ; n = Math . floor ( n / p ) ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = Math . floor ( p / 10 ) ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = Math . floor ( n / 10 ) ; } } let sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = Math . floor ( original_no / 10 ) ; } return ( pDigitSum == sumDigits ) ; } function isSmithBrotherPair ( X , Y ) { return isSmith ( X ) && isSmith ( Y ) ; } function countSmithBrotherPairs ( A , N ) { let count = 0 ; A . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < N - 2 ; i ++ ) if ( isSmithBrotherPair ( A [ i ] , A [ i + 1 ] ) ) count ++ ; return count ; } sieveSundaram ( ) ; let A = [ 728 , 729 , 28 , 2964 , 2965 ] ; let N = A . length ; document . write ( countSmithBrotherPairs ( A , N ) ) ;
Count pair sums that are factors of the sum of the array | Function to find the number of pairs whose sums divides the sum of array ; Initialize the totalSum and count as 0 ; Calculate the total sum of array ; Generate all possible pairs ; If the sum is a factor of totalSum or not ; Increment count by 1 ; Print the total count obtained ; Driver Code
function countPairs ( arr , N ) { let count = 0 , totalSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) { count += 1 ; } } } document . write ( count ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; countPairs ( arr , N ) ;
Maximize average of the ratios of N pairs by M increments | Function to find the change in the ratio in pair after applying operation ; Stores the current ratio ; Stores the new ratio ; Stores the increase in ratio ; Returns the change ; Function to find the maximum average of the ratio of the pairs by applying M increments ; Stores the required result ; Declare a priority queue for storing the increments ; Store the increase in the ratio after applying one operation ; Push the increased value and index value in priority queue ; Store the ratio ; Update the value of sum ; Iterate while M > 0 ; Add the maximum change to the sum ; Remove the element from the priority queue ; Increase the pairs elements by 1 on which operation is applied ; Push the updated change of the pair in priority queue ; Decrease the operation count ; Update the value of the sum by dividing it by N ; Return the result ; Driver Code
function change ( pass , total ) { let currentPassRatio , newPassRatio ; let increase ; currentPassRatio = ( pass ) / total ; newPassRatio = ( ( pass + 1 ) ) / ( total + 1 ) ; increase = newPassRatio - currentPassRatio ; return increase ; } function maximumAverage ( v , M , N ) { let sum = 0 ; let increase , average ; let pq = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { increase = change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) ; pq . push ( [ increase , i ] ) ; average = v [ i ] [ 0 ] * 1.0 / v [ i ] [ 1 ] ; sum += average ; } pq . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; while ( M > 0 ) { sum += pq [ pq . length - 1 ] [ 0 ] ; let i = pq [ pq . length - 1 ] [ 1 ] ; pq . pop ( ) ; v [ i ] [ 0 ] += 1 ; v [ i ] [ 1 ] += 1 ; pq . push ( [ change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) , i ] ) ; M -- ; pq . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; } let ans = sum / N ; return ans ; } let V = [ [ 1 , 2 ] , [ 3 , 5 ] , [ 2 , 2 ] ] ; let M = 2 ; let N = V . length ; document . write ( maximumAverage ( V , M , N ) . toFixed ( 6 ) ) ;
Print all numbers that are divisors of N and are co | JavaScript program for tha above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function printUnitaryDivisors ( n ) { for ( var i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && gcd ( i , n / i ) == 1 ) { document . write ( i + " " ) ; } else { if ( gcd ( i , n / i ) == 1 ) { document . write ( i + " " + n / i + " " ) ; } } } } } var N = 12 ; printUnitaryDivisors ( N ) ;
Maximize K to make given array Palindrome when each element is replaced by its remainder with K | utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; Check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; Variable to store the largest K that makes A palindromic ; Return the required answer ; Input ; Function call
function gcd ( a , b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } function largestK ( A , N ) { let l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return - 1 ; let K = Math . abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( let i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , Math . abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } let A = [ 1 , 2 , 3 , 2 , 1 ] ; let N = A . length ; document . write ( largestK ( A , N ) + " " ) ;