codeparrot/xlcost-text-to-code huggingface dataset:
Aronson 's Sequence | Returns the given number in words ; Get number of digits in given number ; Base cases ; The following arrays contain one digit ( both cardinal and ordinal forms ) , two digit ( < 20 , ordinal forms ) numbers , and multiples ( ordinal forms ) and powers of 10. ; If single digit number ; Here len can be 3 or 4 ; Last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; Check if character is alphabet or not ; Convert number to words in ordinal format and append ; Driver code
function convert_to_words ( num ) { let len = num . length ; if ( len == 0 len > 4 ) { return " " ; } let single_digits_temp = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let single_digits = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let two_digits = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let tens_multiple = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let tens_power = [ " " , " " ] ; let word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; return word ; } let i = 0 , ctr = 0 ; let s = " " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != ' ' ) { word += single_digits_temp [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] + " " ; word += tens_power [ len - 3 ] + " " ; ctr ++ ; } len -- ; num = num . slice ( 1 , num . length ) ; } else { if ( ctr != 0 ) { s = " " ; word = word . substring ( 0 , word . length - 1 ) ; } if ( num [ i + 1 ] == ' ' ) if ( num [ i ] == ' ' ) word = word + " " ; else word += s + tens_multiple [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; else if ( num [ i ] == ' ' ) word += s + two_digits [ num [ i + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ] ; else { if ( num [ i ] != ' ' ) word += s + tens_multiple [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] . substring ( 0 , tens_multiple [ num [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] . length - 4 ) + " " ; else word += s ; word += single_digits [ num [ i + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } i += 2 ; } if ( i == len ) { if ( word . charAt ( 0 ) == ' ' ) word = word . substring ( 1 , word . length ) ; } } return word ; } function Aronsons_sequence ( n ) { let str = " " ; let ind = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] . toLowerCase ( ) != str [ i ] . toUpperCase ( ) ) { ind += 1 ; } if ( str [ i ] == ' ' str [ i ] == ' ' ) { n -= 1 ; str += convert_to_words ( ( ind ) . toString ( ) . split ( " " ) ) + " " ; document . write ( ind + " " ) ; } if ( n == 0 ) break ; } } let n = 6 ; Aronsons_sequence ( n ) ;
Find all good indices in the given Array | Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code
function niceIndices ( A , n ) { var sum = 0 ; var m = new Map ( ) ; for ( var i = 0 ; i < n ; ++ i ) { if ( m . has ( A [ i ] ) ) m . set ( A [ i ] , m . get ( A [ i ] ) + 1 ) else m . set ( A [ i ] , 1 ) sum += A [ i ] ; } for ( var i = 0 ; i < n ; ++ i ) { var k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . has ( k ) ) { if ( ( A [ i ] == k && m . get ( k ) > 1 ) || ( A [ i ] != k ) ) document . write ( i + 1 + " " ) ; } } } } var A = [ 8 , 3 , 5 , 2 ] ; var n = A . length ; niceIndices ( A , n ) ;
Count pieces of circle after N cuts | Function to find number of pieces of circle after N cuts ; driver program
function countPieces ( N ) { return 2 * N ; } let N = 100 ; document . write ( countPieces ( N ) ) ;
Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code
function sumAP ( n , d ) { n = parseInt ( n / d ) ; return ( n ) * ( 1 + n ) * ( d / 2 ) ; } function sumMultiples ( n ) { n -- ; return sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ; } let n = 24 ; document . write ( sumMultiples ( n ) ) ;
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code
function productSumDivisible ( n , size ) { var sum = 0 , product = 1 ; while ( n > 0 ) { if ( size % 2 == 0 ) { product *= n % 10 ; } else { sum += n % 10 ; } n = parseInt ( n / 10 ) ; size -- ; } if ( product % sum == 0 ) return true ; return false ; } var n = 1234 ; var len = 4 ; if ( productSumDivisible ( n , len ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; Converting integer to string ; Traveersing the string ; Driver code
function productSumDivisible ( n ) { var sum = 0 var product = 1 var num = n . toString ( ) ; for ( i = 0 ; i < num . length ; i ++ ) { if ( i % 2 != 0 ) { product = product * Number ( num [ i ] ) } else { sum = sum + Number ( num [ i ] ) } } if ( product % sum == 0 ) { return true ; } else { return false ; } } var n = 1234 if ( productSumDivisible ( n ) ) { document . write ( " " ) } else { document . write ( " " ) }
GCD of a number raised to some power and another number | javascript program to find GCD of a ^ n and b . ; Returns GCD of a ^ n and b . ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function powGCD ( a , n , b ) { for ( i = 0 ; i < n ; i ++ ) a = a * a ; return gcd ( a , b ) ; } var a = 10 , b = 5 , n = 2 ; document . write ( powGCD ( a , n , b ) ) ;
Position after taking N steps to the right and left in an alternate manner | Function to return the last destination ; Driver Code
function lastCoordinate ( n , a , b ) { return ( parseInt ( n + 1 ) / 2 ) * a - parseInt ( n / 2 ) * b ; } let n = 3 , a = 5 , b = 2 ; document . write ( lastCoordinate ( n , a , b ) ) ;
Smallest number greater than or equal to N divisible by K | Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code
function findNum ( N , K ) { var rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } var N = 45 , K = 6 ; document . write ( " " + N + " " + K + " " + findNum ( N , K ) ) ;
Sum and Product of digits in a number that divide the number | Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code
function countDigit ( n ) { let temp = n ; let sum = 0 ; let product = 1 ; while ( temp != 0 ) { let d = temp % 10 ; temp = parseInt ( temp / 10 ) ; if ( d > 0 && n % d == 0 ) { sum += d ; product *= d ; } } document . write ( " " + sum ) ; document . write ( " " + product ) ; } let n = 1012 ; countDigit ( n ) ;
Largest number smaller than or equal to N divisible by K | Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code
function findNum ( N , K ) { var rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } var N = 45 , K = 6 ; document . write ( " " + N + " " + K + " " + findNum ( N , K ) ) ;
Check if any permutation of a number is divisible by 3 and is Palindromic | Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code
function isDivisiblePalindrome ( n ) { var hash = Array ( 10 ) . fill ( 0 ) ; var digitSum = 0 ; while ( n ) { digitSum += n % 10 ; hash [ n % 10 ] ++ ; n = parseInt ( n / 10 ) ; } if ( digitSum % 3 != 0 ) return false ; var oddCount = 0 ; for ( var i = 0 ; i < 10 ; i ++ ) { if ( hash [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount > 1 ) return false ; else return true ; } var n = 34734 ; isDivisiblePalindrome ( n ) ? document . write ( " " ) : document . write ( " " ) ;
Minimum time to return array to its original state after given modifications | Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code
function lcm ( a , b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function dfs ( src , adj , visited ) { visited [ src ] = true ; let count = 1 ; for ( let i = 0 ; i < adj [ src ] . length ; i ++ ) if ( ! visited [ adj [ src ] [ i ] ] ) count += dfs ( adj [ src ] [ i ] , adj , visited ) ; return count ; } function findMinTime ( arr , P , n ) { let adj = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new Array ( ) ; for ( let i = 0 ; i < n ; i ++ ) { adj [ arr [ i ] ] . push ( P [ i ] ) ; } let visited = new Array ( n + 1 ) ; let ans = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { ans = lcm ( ans , dfs ( i , adj , visited ) ) ; } } return ans ; } let arr = [ 1 , 2 , 3 ] ; let P = [ 3 , 2 , 1 ] ; let n = arr . length ; document . write ( findMinTime ( arr , P , n ) ) ;
Check whether product of digits at even places of a number is divisible by K | below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code
function productDivisible ( n , k ) { var product = 1 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 0 ) product *= n % 10 ; n = parseInt ( n / 10 ) ; position ++ ; } if ( product % k == 0 ) return true ; return false ; } var n = 321922 ; var k = 3 ; if ( productDivisible ( n , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether product of digits at even places of a number is divisible by K | Function checks whether product of digits at even places is divisible by K ; Converting integer to string ; Traversing the string ; Driver code
function productDivisible ( n , k ) { var product = 1 ; var num = n . toString ( ) for ( let i = 0 ; i < num . length ; i ++ ) { if ( i % 2 == 0 ) { product = product * Number ( num [ i ] ) } } if ( product % k == 0 ) { return true } else { return false ; } } var n = 321922 var k = 3 if ( productDivisible ( n , k ) ) { document . write ( " " ) } else { document . write ( " " ) }
Permutations of n things taken r at a time with k things together | Function to find factorial of a number ; Function to calculate p ( n , r ) ; Function to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code
function factorial ( n ) { let fact = 1 ; for ( let i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } function npr ( n , r ) { let pnr = Math . floor ( factorial ( n ) / factorial ( n - r ) ) ; return pnr ; } function countPermutations ( n , r , k ) { return factorial ( k ) * ( r - k + 1 ) * npr ( n - k , r - k ) ; } let n = 8 ; let r = 5 ; let k = 2 ; document . write ( countPermutations ( n , r , k ) ) ;
Greatest Integer Function | Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code
function GIF ( n ) { return Math . floor ( n ) ; } var n = 2.3 ; document . write ( GIF ( n ) ) ;
Number of triangles formed from a set of points on three lines | Returns factorial of a number ; calculate c ( n , r ) ; Driver code
function factorial ( n ) { var fact = 1 ; for ( i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } function ncr ( n , r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } var m = 3 , n = 4 , k = 5 ; var totalTriangles = ncr ( m + n + k , 3 ) - ncr ( m , 3 ) - ncr ( n , 3 ) - ncr ( k , 3 ) ; document . write ( totalTriangles ) ;
Check whether sum of digits at odd places of a number is divisible by K | function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; Driver code
function SumDivisible ( n , k ) { let sum = 0 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 1 ) sum += n % 10 ; n = Math . floor ( n / 10 ) ; position ++ ; } if ( sum % k == 0 ) return true ; return false ; } let n = 592452 ; let k = 3 ; if ( SumDivisible ( n , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether sum of digits at odd places of a number is divisible by K | javascript implementation of the above approach ; Converting integer to string ; Traversing the string ; Driver code
function sumDivisible ( n , k ) { var sum = 0 var num = n . toString ( ) for ( var i = 0 ; i < num . length ; i ++ ) { if ( i % 2 != 0 ) { sum = sum + Number ( num [ i ] ) } } if ( sum % k == 0 ) { return true ; } else { return false ; } } var n = 592452 var k = 3 if ( sumDivisible ( n , k ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if a triangle of positive area is possible with the given angles | javascript program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Driver Code
function isTriangleExists ( a , b , c ) { if ( a != 0 && b != 0 && c != 0 && ( a + b + c ) == 180 ) if ( ( a + b ) >= c || ( b + c ) >= a || ( a + c ) >= b ) return " " ; else return " " ; else return " " ; } var a = 50 , b = 60 , c = 70 ; document . write ( isTriangleExists ( a , b , c ) ) ;
Find maximum value of x such that n ! % ( k ^ x ) = 0 | Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
function findX ( n , k ) { let r = n ; let v = 0 ; let u = 0 ; let m = Math . floor ( Math . sqrt ( k ) + 1 ) ; for ( let i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( let u = v = 0 ; k % i == 0 ; v ++ ) { k = Math . floor ( k / i ) ; } if ( v > 0 ) { let t = n ; while ( t > 0 ) { t = Math . floor ( t / i ) ; u += t ; } r = Math . min ( r , Math . floor ( u / v ) ) ; } } return r ; } let n = 5 ; let k = 2 ; document . write ( findX ( n , k ) ) ;
Ways of selecting men and women from a group to make a team | Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code
function fact ( n ) { var fact = 1 ; for ( i = 2 ; i <= n ; i ++ ) fact *= i ; return fact ; } function ncr ( n , r ) { var ncr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; return parseInt ( ncr ) ; } function ways ( m , w , n , k ) { var ans = 0 ; while ( m >= k ) { ans += ncr ( m , k ) * ncr ( w , n - k ) ; k += 1 ; } return parseInt ( ans ) ; } var m , w , n , k ; m = 7 ; w = 6 ; n = 5 ; k = 3 ; document . write ( ways ( m , w , n , k ) ) ;
Product of every K ’ th prime number in an array | Javascript implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all the entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; product of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; create the sieve
let MAX = 1000000 ; let prime = new Array ( MAX + 1 ) ; function SieveOfEratosthenes ( ) { prime . fill ( true ) prime [ 1 ] = false ; prime [ 0 ] = false ; for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function productOfKthPrimes ( arr , n , k ) { let c = 0 ; let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { product *= arr [ i ] ; c = 0 ; } } } document . write ( product + " " ) ; } SieveOfEratosthenes ( ) ; let n = 5 , k = 2 ; let arr = [ 2 , 3 , 5 , 7 , 11 ] ; productOfKthPrimes ( arr , n , k ) ;
Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; Odd n ; Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
function square ( n ) { return n * n ; } function sum ( n ) { if ( n == 0 ) return 0 ; if ( n % 2 == 1 ) { return square ( ( n + 1 ) / 2 ) + sum ( n / 2 ) ; } else { return square ( n / 2 ) + sum ( n / 2 ) ; } } function oddDivSum ( a , b ) { return sum ( b ) - sum ( a - 1 ) ; } var a = 3 , b = 9 ; document . write ( parseInt ( oddDivSum ( a , b ) ) ) ;
Minimum numbers needed to express every integer below N as a sum | Function to count length of binary expression of n ; Driver code
function countBits ( n ) { var count = 0 ; while ( n ) { count ++ ; n >>= 1 ; } return count ; } var n = 32 ; document . write ( " " + countBits ( n ) ) ;
Check if a number is an Achilles number or not | Function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver code
function isPowerful ( n ) { while ( n % 2 == 0 ) { let power = 0 ; while ( n % 2 == 0 ) { n = parseInt ( n / 2 , 10 ) ; power ++ ; } if ( power == 1 ) return false ; } for ( let factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { let power = 0 ; while ( n % factor == 0 ) { n = parseInt ( n / factor , 10 ) ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } function isPower ( a ) { if ( a == 1 ) return true ; for ( let i = 2 ; i * i <= a ; i ++ ) { let val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - parseInt ( val , 10 ) ) < 0.00000001 ) return true ; } return false ; } function isAchillesNumber ( n ) { if ( isPowerful ( n ) && ! isPower ( n ) ) return true ; else return false ; } let n = 72 ; if ( isAchillesNumber ( n ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; n = 36 ; if ( isAchillesNumber ( n ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Program to find count of numbers having odd number of divisors in given range | Function to count numbers having odd number of divisors in range [ A , B ] ; Variable to odd divisor count ; Iterate from a to b and count their number of divisors ; Variable to divisor count ; If count of divisor is odd then increase res by 1 ; Driver code
function OddDivCount ( a , b ) { let res = 0 ; for ( let i = a ; i <= b ; ++ i ) { let divCount = 0 ; for ( let j = 1 ; j <= i ; ++ j ) { if ( i % j == 0 ) { ++ divCount ; } } if ( ( divCount % 2 ) != 0 ) { ++ res ; } } return res ; } let a = 1 , b = 10 ; document . write ( OddDivCount ( a , b ) ) ;
Largest factor of a given number which is a perfect square | Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code
function largestSquareFactor ( num ) { let answer = 1 ; for ( let i = 2 ; i < Math . sqrt ( num ) ; ++ i ) { let cnt = 0 ; let j = i ; while ( num % j == 0 ) { cnt ++ ; j *= i ; } if ( cnt & 1 ) { cnt -- ; answer *= Math . pow ( i , cnt ) ; } else { answer *= Math . pow ( i , cnt ) ; } } return answer ; } let N = 420 ; document . write ( largestSquareFactor ( N ) ) ;
Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | calculate Nth term of given series ; Driver code
function Nth_Term ( n ) { return ( 3 * Math . pow ( n , 2 ) - n + 2 ) / ( 2 ) ; } let N = 5 ; document . write ( Nth_Term ( N ) ) ;
All possible numbers of N digits and base B without leading zeros | function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code
function countPermutations ( N , B ) { var x = Math . pow ( B , N ) ; var y = Math . pow ( B , N - 1 ) ; document . write ( x - y ) ; } var N = 6 ; var B = 4 ; countPermutations ( N , B ) ;
Absolute difference between the Product of Non | Function to find the difference between the product of non - primes and the product of primes of an 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 ; Store the product of primes in P1 and the product of non primes in P2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
function calculateDifference ( arr , n ) { var max_val = Math . max . apply ( Math , arr ) ; var prime = Array ( max_val + 1 ) . fill ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } var P1 = 1 , P2 = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { P1 *= arr [ i ] ; } else if ( arr [ i ] != 1 ) { P2 *= arr [ i ] ; } } return Math . abs ( P2 - P1 ) ; } var arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] ; var n = arr . length ; document . write ( calculateDifference ( arr , n ) ) ;
Maximum count of equal numbers in an array after performing given operations | Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver Code ; size of an array
function EqualNumbers ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; if ( sum % n ) return n - 1 ; return n ; } let a = [ 1 , 4 , 1 ] ; let n = a . length ; document . write ( EqualNumbers ( a , n ) ) ;
Count number of ordered pairs with Even and Odd Sums | function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code
function count_odd_pair ( n , a ) { var odd = 0 , even = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } var ans = odd * even * 2 ; return ans ; } function count_even_pair ( odd_sum_pairs , n ) { var total_pairs = ( n * ( n - 1 ) ) ; var ans = total_pairs - odd_sum_pairs ; return ans ; } var n = 6 ; var a = [ 2 , 4 , 5 , 9 , 1 , 8 ] ; var odd_sum_pairs = count_odd_pair ( n , a ) ; var even_sum_pairs = count_even_pair ( odd_sum_pairs , n ) ; document . write ( " " + even_sum_pairs + " " ) ; document . write ( " " + odd_sum_pairs + " " ) ;
Steps required to visit M points in order on a circular ring of N points | Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code
function findSteps ( n , m , a ) { var cur = 1 ; var steps = 0 ; for ( var i = 0 ; i < m ; i ++ ) { if ( a [ i ] >= cur ) steps += ( a [ i ] - cur ) ; else steps += ( n - cur + a [ i ] ) ; cur = a [ i ] ; } return steps ; } var n = 3 , m = 3 ; var a = [ 2 , 1 , 2 ] ; document . write ( findSteps ( n , m , a ) ) ;