codeparrot/xlcost-text-to-code huggingface dataset:
Split N powers of 2 into two subsets such that their difference of sum is minimum | javascript program to find the minimum difference possible by splitting all powers of 2 up to N into two sets of equal size ; Store the largest ; Form two separate groups ; Initialize the sum for two groups as 0 ; Insert 2 ^ n in the first group ; Calculate the sum of least n / 2 - 1 elements added to the first set ; Sum of remaining n / 2 - 1 elements ; Min difference between the two groups ; Driver Code
function MinDiff ( n ) { var val = parseInt ( Math . pow ( 2 , n ) ) ; var sep = n / 2 ; var grp1 = 0 ; var grp2 = 0 ; grp1 = grp1 + val ; for ( i = 1 ; i < sep ; i ++ ) grp1 = grp1 + parseInt ( Math . pow ( 2 , i ) ) ; for ( i = sep ; i < n ; i ++ ) grp2 = grp2 + parseInt ( Math . pow ( 2 , i ) ) ; document . write ( Math . abs ( grp1 - grp2 ) ) ; } var n = 4 ; MinDiff ( n ) ;
Count elements in first Array with absolute difference greater than K with an element in second Array | Function to count the such elements ; Store count of required elements in arr1 ; Initialise the smallest and the largest value from the second array arr2 ; Find the smallest and the largest element in arr2 ; Check if absolute difference of smallest and arr1 [ i ] or largest and arr1 [ i ] is > K then arr [ i ] is a required element ; Prvar the final result ; Driver code
function countDist ( arr1 , n , arr2 , m , k ) { var count = 0 ; var smallest = arr2 [ 0 ] ; var largest = arr2 [ 0 ] ; for ( i = 0 ; i < m ; i ++ ) { smallest = Math . max ( smallest , arr2 [ i ] ) ; largest = Math . min ( largest , arr1 [ i ] ) ; } for ( i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr1 [ i ] - smallest ) > k || Math . abs ( arr1 [ i ] - largest ) > k ) count ++ ; } document . write ( count ) ; } var arr1 = [ 3 , 1 , 4 ] ; var n = arr1 . length ; var arr2 = [ 5 , 1 , 2 ] ; var m = arr2 . length ; var k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ;
Make sum of all subarrays of length K equal by only inserting elements | Function that prints another array whose all subarray of length k have an equal sum ; Store all distinct elements in the unordered map ; Condition if the number of distinct elements is greater than k ; Push all distinct elements in a vector ; Push 1 while the size of vector not equal to k ; Print the vector 2 times ; Driver Code
function MakeArray ( a , n , k ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( a [ i ] ) ) mp . set ( a [ i ] , 1 ) ; } if ( mp . count > k ) { document . write ( " " ) ; return ; } var ans = [ ] ; mp . forEach ( ( value , key ) => { ans . push ( key ) ; } ) ; while ( ans . length < k ) { ans . push ( 1 ) ; } for ( var i = 0 ; i < 2 ; i ++ ) { for ( var j = 0 ; j < k ; j ++ ) document . write ( ans [ j ] + " " ) ; } } var arr = [ 1 , 2 , 2 , 1 ] ; var K = 2 ; var size = arr . length ; MakeArray ( arr , size , K ) ;
Find the minimum value of the given expression over all pairs of the array | Function to find the minimum value of the expression ; Iterate over all the pairs and find the minimum value ; Driver code
function MinimumValue ( a , n ) { let answer = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { answer = Math . min ( answer , ( ( a [ i ] & a [ j ] ) ^ ( a [ i ] a [ j ] ) ) ) ; } } return answer ; } let N = 6 ; let A = [ 12 , 3 , 14 , 5 , 9 , 8 ] ; document . write ( MinimumValue ( A , N ) ) ;
Number of substrings with length divisible by the number of 1 's in it | Function return count of such substring ; Mark 1 at those indices where '1' appears ; Take prefix sum ; Iterate through all the subStrings ; Driver Code
function countOfSubStrings ( s ) { let n = s . length ; let prefix_sum = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) prefix_sum [ i ] = 1 ; } for ( let i = 1 ; i < n ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; let answer = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { let countOfOnes = prefix_sum [ j ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) ; let length = j - i + 1 ; if ( countOfOnes > 0 && length % countOfOnes == 0 ) answer ++ ; } } return answer ; } let S = " " ; document . write ( countOfSubStrings ( S ) ) ;
Find the number of elements X such that X + K also exists in the array | Function to return the count of element x such that x + k also lies in this array ; Key in map will store elements and value will store the frequency of the elements ; Find if i . first + K is present in this map or not ; If we find i . first or key + K in this map then we have to increase in answer the frequency of this element ; array initialisation ; size of array ; initialise k
function count_element ( N , K , arr ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; ++ i ) if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) } else { mp . set ( arr [ i ] , 1 ) } let answer = 0 ; for ( let i of mp ) { if ( mp . has ( i [ 0 ] + K ) ) answer += i [ 1 ] ; } return answer ; } let arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ; let N = arr . length ; let K = 2 ; document . write ( count_element ( N , K , arr ) ) ;
Find the size of largest group where groups are according to the xor of digits | Function to find out xor of digit ; calculate xor digitwise ; return xor ; Function to find the size of largest group ; hash map for counting frequency ; Counting freq of each element ; Find the maximum ; initialise N
function digit_xor ( x ) { let xorr = 0 ; while ( x ) { xorr ^= x % 10 ; x = x / 10 ; } return xorr ; } function find_count ( n ) { let mpp = new Map ( ) ; for ( let i = 1 ; i <= n ; i ++ ) { let t = digit_xor ( i ) ; if ( mpp . has ( t ) ) { mpp . set ( t , mpp . get ( t ) + 1 ) } else { mpp . set ( t , 1 ) } } let maxm = 0 ; for ( let x of mpp ) { if ( x [ 1 ] > maxm ) maxm = x [ 1 ] ; } return maxm ; } let N = 13 ; document . write ( find_count ( N ) ) ;
Count of largest sized groups while grouping according to product of digits | Function to find out product of digit ; calculate product ; return the product of digits ; Function to find the count ; hash map for counting frequency ; counting freq of each element ; find the maximum ; count the number of groups having size of equal to largest group . ; initialise N
function digit_prod ( x ) { let prod = 1 ; while ( x != 0 ) { prod = prod * ( x % 10 ) ; x = Math . floor ( x / 10 ) ; } return prod ; } function find_count ( n ) { let mpp = new Map ( ) ; for ( let i = 1 ; i <= n ; i ++ ) { let t = digit_prod ( i ) if ( mpp . has ( t ) ) { mpp . set ( t , mpp . get ( t ) + 1 ) } else { mpp . set ( t , 1 ) } } let ans = 1 ; let maxm = 0 ; for ( let x of mpp ) { if ( x [ 1 ] > maxm ) { maxm = x [ 1 ] ; ans = 1 ; } else if ( x [ 1 ] == maxm ) { ans ++ ; } } return ans ; } let N = 13 ; document . write ( find_count ( N ) ) ;
Count of subarrays having exactly K prime numbers | A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i & i + 2 then it is not prime ; Function to find number of subarrays with sum exactly equal to k ; STL map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the final result ; Function to count the subarray with K primes ; Update the array element ; If current element is prime then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function Call ; Driver Code ; Function Call
function isPrime ( n ) { let i ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function findSubarraySum ( arr , n , K ) { let prevSum = new Map ( ) ; let res = 0 ; let currsum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . has ( currsum - K ) ) res += ( prevSum . get ( currsum - K ) ) ; if ( prevSum . has ( currsum ) ) { prevSum . set ( currsum , prevSum . get ( currsum ) + 1 ) } else { prevSum . set ( currsum , 1 ) } } return res ; } function countSubarray ( arr , n , K ) { for ( let i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } document . write ( findSubarraySum ( arr , n , K ) ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let K = 2 ; let N = arr . length ; countSubarray ( arr , N , K ) ;
Count the elements having frequency equals to its value | Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Check if value equals to frequency and increment the count ; Driver code ; Function call
function find_maxm ( arr , n ) { let mpp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mpp . has ( arr [ i ] ) ) { mpp . set ( arr [ i ] , mpp . get ( arr [ i ] ) + 1 ) } else { mpp . set ( arr [ i ] , 1 ) } } let ans = 0 ; for ( let x of mpp ) { let value = x [ 0 ] ; let freq = x [ 1 ] ; if ( value == freq ) { ans ++ ; } } return ans ; } let arr = [ 3 , 2 , 2 , 3 , 4 , 3 ] ; let n = arr . length ; document . write ( find_maxm ( arr , n ) ) ;
Find the Number of Permutations that satisfy the given condition in an array | Function to calculate x ^ y recursively ; Function to return the number of permutations that satisfy the given condition in an array ; If there is only one element then only one permutation is available ; Sort the array for calculating the number of elements occurring twice ; If the maximum element is occurring twice , then the number of permutations satisfying the condition is 0 ; This variable will store the number of element occurring twice ; Loop to check the number of elements occurring twice ; Check if this element is occurring twice ; If this element is occurring twice then check if this number is occurring more than twice ; If element occurring thrice then no permutation will satisfy the given condition ; Since we have checked the next element as well , then we can increment the loop variable ; Driver code
function pow ( x , y ) { if ( y == 1 ) return x ; if ( y == 0 ) return 1 ; let temp = pow ( x , y / 2 ) ; temp *= temp ; if ( y % 2 == 1 ) temp *= x ; return temp ; } function noOfPermutations ( a , n ) { if ( n == 1 ) { return 1 ; } a . sort ( ) ; if ( a [ n - 1 ] == a [ n - 2 ] ) { return 0 ; } let x = 0 ; for ( let i = 0 ; i < n - 2 ; ++ i ) { if ( a [ i ] == a [ i + 1 ] ) { if ( a [ i ] == a [ i + 2 ] ) { return 0 ; } x ++ ; i ++ ; } } return pow ( 2 , n - 2 * x - 1 ) ; } let a = [ 1 , 2 , 2 , 3 , 4 ] ; let n = a . length ; let num = noOfPermutations ( a , n ) ; document . write ( num ) ;
Length of the Smallest Subarray that must be removed in order to Maximise the GCD | Function to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Store the maximum possible GCD of the resulting subarray ; Two pointers initially pointing to the first and last element respectively ; Moving the left pointer to the right if the elements are divisible by the maximum GCD ; Moving the right pointer to the left if the elements are divisible by the maximum GCD ; Return the length of the subarray ; Driver code
function GetMinSubarrayLength ( a , n ) { var ans = Math . max ( a [ 0 ] , a [ n - 1 ] ) ; var lo = 0 , hi = n - 1 ; while ( lo < n && a [ lo ] % ans == 0 ) lo ++ ; while ( hi > lo && a [ hi ] % ans == 0 ) hi -- ; return ( hi - lo + 1 ) ; } var arr = [ 4 , 8 , 2 , 1 , 4 ] ; var N = arr . length ; var length = GetMinSubarrayLength ( arr , N ) ; document . write ( length ) ;
Maximize the number of indices such that element is greater than element to its left | Function to find the maximum pairs such that arr [ i + 1 ] > arr [ i ] ; To store the frequency of the element in arr [ ] ; Store the frequency in map M ; To find the maximum frequency store in map M ; Print the maximum number of possible pairs ; Driver Code
function countPairs ( arr , N ) { let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M [ arr [ i ] ] ) M [ arr [ i ] ] ++ ; else M [ arr [ i ] ] = 1 } let maxFreq = 0 ; for ( let it in M ) { maxFreq = Math . max ( maxFreq , M [ it ] ) ; } document . write ( N - maxFreq , ' ' ) ; } let a = [ 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 ] ; let N = a . length ; countPairs ( a , N ) ;
Find minimum value of the expression by choosing K elements from given array | Function to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Sorting the array for least k element selection ; Select first k elements from sorted array ; Return value of solved expression ; Driver code ; Function call
function minimumValue ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) ; let answer = 0 ; for ( let i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } let arr = [ 4 , 21 , 5 , 3 , 8 ] , k = 3 ; let n = arr . length ; document . write ( minimumValue ( arr , n , k ) ) ;
Transform N to Minimum possible value | Let initialising the answer ; Function to find the digitsum ; Iterate over all digits and add them ; Return the digit su , ; Function to transform N to the minimum value ; If the final value is lesser than least value ; If final value is equal to least value then check for lesser number of steps to reach this value ; The value will be obtained in less than 15 steps as proved so applying normal recursive operations ; Driver Code ; Function call ; Prlet the answers
let min_val = Number . MAX_VALUE ; let min_steps = 0 ; function sumOfDigits ( n ) { let s = n . toString ( ) ; let sum = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { sum += ( s [ i ] - ' ' ) ; } return sum ; } function Transform ( n , d , steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = Math . min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } let N = 9 , D = 3 ; Transform ( N , D , 0 ) ; document . write ( min_val + " " + min_steps ) ;
Remove all 1 s from the adjacent left of 0 s in a Binary Array | Function to find the maximum number of 1 's before 0 ; Traverse the array ; If value is 1 ; If consecutive 1 followed by 0 , then update the maxCnt ; Print the maximum consecutive 1 's  followed by 0 ; Driver Code ; Function Call ; Function Call
function noOfMoves ( arr , n ) { let cnt = 0 ; let maxCnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = Math . max ( maxCnt , cnt ) ; cnt = 0 ; } } } document . write ( maxCnt + " " ) ; } let arr = [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 ] ; let N = arr . length ; noOfMoves ( arr , N ) ; let arr1 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 ] ; N = arr . length ; noOfMoves ( arr1 , N ) ;
Check if given coins can be used to pay a value of S | Function to check if it is possible to pay a value ; Loop to add the value of coin A ; Condition to check if it is possible to pay a value of S ; Driver Code
function knowPair ( a , b , n , s , m ) { var i = 0 , rem = 0 ; var count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = parseInt ( rem / m ) ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var A = 1 ; var B = 2 ; var n = 3 ; var S = 4 ; var m = 2 ; knowPair ( A , B , n , S , m ) ;
Sum of all numbers in the given range which are divisible by M | Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; Running a loop from A to B and check if a number is divisible by i . ; If the number is divisible , then add it to sum ; Return the sum ; A and B define the range M is the dividend ; Printing the result
function sumDivisibles ( A , B , M ) { var sum = 0 ; for ( var i = A ; i <= B ; i ++ ) if ( i % M == 0 ) sum += i ; return sum ; } var A = 6 , B = 15 , M = 3 ; document . write ( sumDivisibles ( A , B , M ) ) ;
Sum of all numbers in the given range which are divisible by M | Function to find the largest number smaller than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the smallest number greater than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; To bring the smallest and largest numbers in the range [ A , B ] ; To count the number of terms in the AP ; Sum of n terms of an AP ; A and B define the range , M is the dividend ; Printing the result
function findSmallNum ( N , K ) { var rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } function findLargeNum ( N , K ) { var rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } function sumDivisibles ( A , B , M ) { var sum = 0 ; var first = findSmallNum ( A , M ) ; var last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; var n = ( parseInt ( B / M ) - parseInt ( ( A - 1 ) / M ) ) ; return n * ( first + last ) / 2 ; } var A = 6 , B = 15 , M = 3 ; document . write ( sumDivisibles ( A , B , M ) ) ;
Count of all unique substrings with non | Function to count all unique distinct character substrings ; Hashmap to store all substrings ; Iterate over all the substrings ; Boolean array to maintain all characters encountered so far ; Variable to maintain the substring till current position ; Get the position of the character in the string ; Check if the character is encountred ; Add the current character to the substring ; Insert substring in Hashmap ; Driver code
function distinctSubstring ( P , N ) { var S = new Set ( ) ; for ( var i = 0 ; i < N ; ++ i ) { var freq = Array ( 26 ) . fill ( false ) ; var s = " " ; for ( var j = i ; j < N ; ++ j ) { var pos = P [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( freq [ pos ] == true ) break ; freq [ pos ] = true ; s += P [ j ] ; S . add ( s ) ; } } return S . size ; } var S = " " ; var N = S . length ; document . write ( distinctSubstring ( S , N ) ) ;
Sum of all subarrays of size K | Function to find the sum of all subarrays of size K ; Loop to consider every subarray of size K ; Initialize sum = 0 ; Calculate sum of all elements of current subarray ; Print sum of each subarray ; Driver Code ; Function Call
function calcSum ( arr , n , k ) { for ( var i = 0 ; i <= n - k ; i ++ ) { var sum = 0 ; for ( var j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; document . write ( sum + " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 3 ; calcSum ( arr , n , k ) ;
Sum of all subarrays of size K | Function to find the sum of all subarrays of size K ; Initialize sum = 0 ; Consider first subarray of size k Store the sum of elements ; Print the current sum ; Consider every subarray of size k Remove first element and add current element to the window ; Add the element which enters into the window and subtract the element which pops out from the window of the size K ; Print the sum of subarray ; Drivers Code ; Function Call
function calcSum ( arr , n , k ) { var sum = 0 ; for ( var i = 0 ; i < k ; i ++ ) sum += arr [ i ] ; document . write ( sum + " " ) ; for ( var i = k ; i < n ; i ++ ) { sum = ( sum - arr [ i - k ] ) + arr [ i ] ; document . write ( sum + " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n = arr . length ; var k = 3 ; calcSum ( arr , n , k ) ;
Number of indices pair such that element pair sum from first Array is greater than second Array | Function to get the number of pairs of indices { i , j } in the given two arrays A and B such that A [ i ] + A [ j ] > B [ i ] + B [ j ] ; Intitializing the difference array D ; Computing the difference between the elements at every index and storing it in the array D ; Sort the array D ; Variable to store the total number of pairs that satisfy the given condition ; Loop to iterate through the difference array D and find the total number of pairs of indices that follow the given condition ; If the value at the index i is positive , then it remains positive for any pairs with j such that j > i . ; If the value at that index is negative then we need to find the index of the value just greater than - D [ i ] ; Driver code
function getPairs ( A , B , n ) { let D = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { D [ i ] = A [ i ] - B [ i ] ; } D . sort ( ( a , b ) => a - b ) ; let total = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( D [ i ] > 0 ) { total += n - i - 1 ; } else { let k = upper_bound ( D , 0 , D . length , - D [ i ] ) total += n - k ; } } return total ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let n = 5 ; let A = new Array ( ) ; let B = new Array ( ) ; A . push ( 4 ) ; A . push ( 8 ) ; A . push ( 2 ) ; A . push ( 6 ) ; A . push ( 2 ) ; B . push ( 4 ) ; B . push ( 5 ) ; B . push ( 4 ) ; B . push ( 1 ) ; B . push ( 3 ) ; document . write ( getPairs ( A , B , n ) )
Number of pairs such that their HCF and LCM is equal | Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function lcm ( a , b ) { return ( a * b ) / gcd ( a , b ) ; } function countPairs ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } var arr = [ 1 , 1 , 1 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Number of pairs such that their HCF and LCM is equal | Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function
function countPairs ( arr , n ) { let frequency = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( frequency . has ( arr [ i ] ) ) { frequency . set ( arr [ i ] , frequency . get ( arr [ i ] ) + 1 ) ; } else { frequency . set ( arr [ i ] , 1 ) ; } } let count = 0 ; for ( let [ key , value ] of frequency . entries ( ) ) { let f = value ; count += f * ( f - 1 ) / 2 ; } return count ; } let arr = [ 1 , 1 , 1 ] ; let n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Find the maximum possible value of last element of the Array | Function to find the maximum possible value of last element of the array ; Traverse for all element ; Find the distance ; If moves less than distance then we can not move this number to end ; How many number we can move to end ; Take the minimum of both of them ; Increment in the end ; Remove taken moves ; Return the last element ; Driver code ; Function call
function maxValue ( arr , n , moves ) { for ( var i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { var distance = n - 1 - i ; if ( moves < distance ) break ; var can_take = parseInt ( moves / distance ) ; var take = Math . min ( arr [ i ] , can_take ) ; arr [ n - 1 ] += take ; moves -= take * distance ; } } return arr [ n - 1 ] ; } var arr = [ 2 , 3 , 0 , 1 ] ; var M = 5 ; var N = arr . length ; document . write ( maxValue ( arr , N , M ) ) ;