codeparrot/xlcost-text-to-code huggingface dataset:
Make the list non | Javascript implementation of the approach ; Function to return the minimum element from the range [ prev , MAX ] such that it differs in at most 1 digit with cur ; To start with the value we have achieved in the last step ; Store the value with which the current will be compared ; Current value ; There are at most 4 digits ; If the current digit differs in both the numbers ; Eliminate last digits in both the numbers ; If the number of different digits is at most 1 ; If we can 't find any number for which  the number of change is less than or  equal to 1 then return -1 ; Function to get the non - decreasing list ; Creating a vector for the updated list ; Let 's assume that it is possible to  make the list non-decreasing ; Element of the original array ; Can 't make the list non-decreasing ; If possible then print the list ; Driver code
var DIGITS = 4 , MIN = 1000 , MAX = 9999 ; function getBest ( prev , cur ) { var maximum = Math . max ( MIN , prev ) ; for ( var i = maximum ; i <= MAX ; i ++ ) { var cnt = 0 ; var a = i ; var b = cur ; for ( var k = 0 ; k < DIGITS ; k ++ ) { if ( a % 10 != b % 10 ) cnt += 1 ; a = parseInt ( a / 10 ) ; b = parseInt ( b / 10 ) ; } if ( cnt <= 1 ) return i ; } return - 1 ; } function getList ( arr , n ) { var myList = [ ] ; var i , cur ; var possible = true ; myList . push ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { cur = arr [ i ] ; myList . push ( getBest ( myList [ myList . length - 1 ] , cur ) ) ; if ( myList [ myList . length - 1 ] == - 1 ) { possible = false ; break ; } } if ( possible ) { for ( i = 1 ; i < myList . length ; i ++ ) document . write ( myList [ i ] + " " ) ; } else document . write ( " " ) ; } var arr = [ 1095 , 1094 , 1095 ] ; var n = arr . length ; getList ( arr , n ) ;
Maximum items that can be bought with the given type of coins | Javascript implementation of the approach ; Function to find maximum fruits Can buy from given values of x , y , z . ; Items of type 1 that can be bought ; Update the coins ; Items of type 2 that can be bought ; Update the coins ; Items of type 3 that can be bought ; Update the coins ; Items of type 4 that can be bought To buy a type 4 item , a coin of each type is required ; Total items that can be bought ; Driver code
const COST = 3 ; function maxItems ( x , y , z ) { let type1 = parseInt ( x / COST ) ; x %= COST ; let type2 = parseInt ( y / COST ) ; y %= COST ; let type3 = parseInt ( z / COST ) ; z %= COST ; let type4 = Math . min ( x , Math . min ( y , z ) ) ; let maxItems = type1 + type2 + type3 + type4 ; return maxItems ; } let x = 4 , y = 5 , z = 6 ; document . write ( maxItems ( x , y , z ) ) ;
Count occurrences of a prime number in the prime factorization of every element from the given range | Function to return the highest power of p that divides n ; Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; For every element of the range ; Add the highest power of p that divides i ; Driver code
function countFactors ( n , p ) { let pwr = 0 ; while ( n > 0 && n % p == 0 ) { n /= p ; pwr ++ ; } return pwr ; } function getCount ( l , r , p ) { let cnt = 0 ; for ( let i = l ; i <= r ; i ++ ) { cnt += countFactors ( i , p ) ; } return cnt ; } let l = 2 , r = 8 , p = 2 ; document . write ( getCount ( l , r , p ) ) ;
Count occurrences of a prime number in the prime factorization of every element from the given range | Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; Number of values in the range [ 0 , r ] that are divisible by val ; Number of values in the range [ 0 , l - 1 ] that are divisible by val ; Increment the power of the val ; ( a - b ) is the count of numbers in the range [ l , r ] that are divisible by val ; No values that are divisible by val thus exiting from the loop ; Driver code
function getCount ( l , r , p ) { let cnt = 0 ; let val = p ; while ( 1 ) { let a = parseInt ( r / val ) ; let b = parseInt ( ( l - 1 ) / val ) ; val *= p ; if ( a - b ) { cnt += ( a - b ) ; } else break ; } return cnt ; } let l = 2 , r = 8 , p = 2 ; document . write ( getCount ( l , r , p ) ) ;
Check if the number is valid when flipped upside down | Function that returns true if str is Topsy Turvy ; For every character of the string ; If the current digit cannot form a valid digit when turned upside - down ; Driver code
function topsyTurvy ( str ) { for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' ) { return false ; } } return true ; } var str = " " ; if ( topsyTurvy ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the count of subsequences where each element is divisible by K | Function to return the count of all valid subsequences ; To store the count of elements which are divisible by k ; If current element is divisible by k then increment the count ; Total ( 2 ^ n - 1 ) non - empty subsequences are possible with n element ; Driver code
function countSubSeq ( arr , n , k ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { count ++ ; } } return ( Math . pow ( 2 , count ) - 1 ) ; } let arr = [ 1 , 2 , 3 , 6 ] ; let n = arr . length ; let k = 3 ; document . write ( countSubSeq ( arr , n , k ) ) ;
Count of numbers below N whose sum of prime divisors is K | Javascript implementation of the approach ; Function to return the count of numbers below N whose sum of prime factors is K ; To store the sum of prime factors for all the numbers ; If i is prime ; Add i to all the numbers which are divisible by i ; To store the count of required numbers ; Return the required count ; Driver code
const MAX = 1000001 ; function countNum ( N , K ) { let sumPF = new Array ( MAX ) . fill ( 0 ) ; for ( let i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == 0 ) { for ( let j = i ; j < N ; j += i ) { sumPF [ j ] += i ; } } } let count = 0 ; for ( let i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == K ) count ++ ; } return count ; } let N = 20 , K = 7 ; document . write ( countNum ( N , K ) ) ;
Queries for the smallest and the largest prime number of given digit | Javascript implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the smallest prime number with d digits ; check if prime ; Function to return the largest prime number with d digits ; check if prime ; Driver code ; Perform queries
const MAX = 100000 ; let prime = new Array ( MAX + 1 ) . fill ( true ) ; function SieveOfEratosthenes ( ) { for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function smallestPrime ( d ) { let l = Math . pow ( 10 , d - 1 ) ; let r = Math . pow ( 10 , d ) - 1 ; for ( let i = l ; i <= r ; i ++ ) { if ( prime [ i ] ) { return i ; } } return - 1 ; } function largestPrime ( d ) { let l = Math . pow ( 10 , d - 1 ) ; let r = Math . pow ( 10 , d ) - 1 ; for ( let i = r ; i >= l ; i -- ) { if ( prime [ i ] ) { return i ; } } return - 1 ; } SieveOfEratosthenes ( ) ; let queries = [ 2 , 5 ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) { document . write ( smallestPrime ( queries [ i ] ) + " " + largestPrime ( queries [ i ] ) + " " ) ; }
Find a Square Matrix such that sum of elements in every row and column is K | Function to print the required matrix ; Print k for the left diagonal elements ; Prvar 0 for the rest ; Driver code
function printMatrix ( n , k ) { for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( i == j ) document . write ( k + " " ) ; else document . write ( " " ) ; } document . write ( " " ) ; } } var n = 3 , k = 7 ; printMatrix ( n , k ) ;
Divide first N natural numbers into 3 equal sum subsets | Function that returns true if the subsets are possible ; If n <= 3 then it is not possible to divide the elements in three subsets satisfying the given conditions ; Sum of all the elements in the range [ 1 , n ] ; If the sum is divisible by 3 then it is possible ; Driver code
function possible ( n ) { if ( n > 3 ) { let sum = parseInt ( ( n * ( n + 1 ) ) / 2 ) ; if ( sum % 3 == 0 ) { return true ; } } return false ; } let n = 5 ; if ( possible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the Nth element of the modified Fibonacci series | Function to return the Nth number of the modified Fibonacci series where A and B are the first two terms ; To store the current element which is the sum of previous two elements of the series ; This loop will terminate when the Nth element is found ; Return the Nth element ; Driver code
function findNthNumber ( A , B , N ) { var sum = 0 ; for ( i = 2 ; i < N ; i ++ ) { sum = A + B ; A = B ; B = sum ; } return sum ; } var A = 5 , B = 7 , N = 10 ; document . write ( findNthNumber ( A , B , N ) ) ;
Check if an array is increasing or decreasing | Function to check the type of the array ; If the first two and the last two elements of the array are in increasing order ; If the first two and the last two elements of the array are in decreasing order ; If the first two elements of the array are in increasing order and the last two elements of the array are in decreasing order ; If the first two elements of the array are in decreasing order and the last two elements of the array are in increasing order ; Driver code
function checkType ( arr , n ) { if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] <= arr [ n - 1 ] ) document . write ( " " ) ; else if ( arr [ 0 ] >= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) document . write ( " " ) ; else if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) document . write ( " " ) ; else document . write ( " " ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; checkType ( arr , n ) ;
Calculate the IST : Indian Standard Time | Function to calculate Indian Standard Time ; Separate integer part ; Separate float part and return ceil value ; Number of hours ( 1 - 24 ) ; Rotations in degrees
function cal_IST ( h , r ) { let IST = ( h * r * 1.0 ) / 360 ; let int_IST = parseInt ( IST ) ; let float_IST = Math . ceil ( parseInt ( ( IST - int_IST ) * 60 ) ) ; document . write ( int_IST + " " + float_IST ) ; } let h = 20 ; let r = 150 ; cal_IST ( h , r ) ;
Check if it is possible to perform the given Grid Division | Function that returns true if it is possible to divide the grid satisfying the given conditions ; To store the sum of all the cells of the given parts ; If the sum is equal to the total number of cells in the given grid ; Driver code
function isPossible ( arr , p , n , m ) { var sum = 0 ; for ( var i = 0 ; i < p ; i ++ ) sum += arr [ i ] ; if ( sum == ( n * m ) ) return true ; return false ; } var n = 3 , m = 4 ; var arr = [ 6 , 3 , 2 , 1 ] ; var p = arr . length ; if ( isPossible ( arr , p , n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum steps to make the product of the array equal to 1 | Function to return the minimum steps required ; To store the count of 0 s , positive and negative numbers ; To store the ans ; If array element is equal to 0 ; If array element is a negative number ; Extra cost needed to make it - 1 ; If array element is a positive number ; Extra cost needed to make it 1 ; Now the array will have - 1 , 0 and 1 only ; As count of negative is even so we will change all 0 to 1 total cost here will be count of 0 s ; If there are zeroes present in the array ; Change one zero to - 1 and rest of them to 1 Total cost here will be count of '0' ; If there are no zeros in the array ; As no 0 s are available so we have to change one - 1 to 1 which will cost 2 to change - 1 to 1 ; Driver code
function MinStep ( a , n ) { let positive = 0 , negative = 0 , zero = 0 ; let step = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } else if ( a [ i ] < 0 ) { negative ++ ; step = step + ( - 1 - a [ i ] ) ; } else { positive ++ ; step = step + ( a [ i ] - 1 ) ; } } if ( negative % 2 == 0 ) { step = step + zero ; } else { if ( zero > 0 ) { step = step + zero ; } else { step = step + 2 ; } } return step ; } let a = [ 0 , - 2 , - 1 , - 3 , 4 ] ; let n = a . length ; document . write ( MinStep ( a , n ) ) ;
Find the first N integers such that the sum of their digits is equal to 10 | Function to return the sum of digits of n ; Add the last digit to the sum ; Remove last digit ; Return the sum of digits ; Function to prvar the first n numbers whose sum of digits is 10 ; First number of the series is 19 ; If the sum of digits of the current number is equal to 10 ; Print the number ; Add 9 to the previous number ; Driver code
function sum ( n ) { var sum = 0 ; while ( n > 0 ) { sum = sum + n % 10 ; n = parseInt ( n / 10 ) ; } return sum ; } function firstN ( n ) { var num = 19 , cnt = 1 ; while ( cnt != n ) { if ( sum ( num ) == 10 ) { document . write ( num + " " ) ; cnt ++ ; } num += 9 ; } } var n = 10 ; firstN ( n ) ;
Sum of last digit of all integers from 1 to N divisible by M | Function to return the required sum ; Number of element between 1 to n divisible by m ; Array to store the last digit of elements in a cycle ; Storing and adding last digit of cycle ; Number of elements present in last cycle ; Sum of k / 10 cycle ; Adding value of digits of last cycle to the answer ; input n and m
function sumOfLastDig ( n , m ) { let sum = 0 , k ; k = parseInt ( n / m ) ; let arr = new Array ( 10 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { arr [ i ] = m * ( i + 1 ) % 10 ; sum += arr [ i ] ; } let rem = k % 10 ; let ans = parseInt ( k / 10 ) * sum ; for ( let i = 0 ; i < rem ; i ++ ) { ans += arr [ i ] ; } return ans ; } let n = 100 , m = 3 ; document . write ( sumOfLastDig ( n , m ) ) ;
Number of Subsequences with Even and Odd Sum | Set 2 | Function to find number of Subsequences with Even and Odd Sum ; Counting number of odds ; Even count ; Total Subsequences is ( 2 ^ n - 1 ) For NumberOfEvenSubsequences subtract NumberOfOddSubsequences from total ; Driver code ; Calling the function
function countSum ( arr , n ) { let NumberOfOdds = 0 , NumberOfEvens = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) NumberOfOdds ++ ; NumberOfEvens = n - NumberOfOdds ; let NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; let NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; return [ NumberOfEvenSubsequences , NumberOfOddSubsequences ] ; } let arr = [ 1 , 2 , 2 , 3 ] ; let n = arr . length ; let ans = countSum ( arr , n ) ; document . write ( " " + ans [ 0 ] ) ; document . write ( " " + ans [ 1 ] ) ;
Program to find the next prime number | Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function nextPrime ( N ) { if ( N <= 1 ) return 2 ; let prime = N ; let found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } let N = 3 ; document . write ( nextPrime ( N ) ) ;
Check if a number is Flavius Number | Return the number is Flavious Number or not ; index i starts from 2 because at 1 st iteration every 2 nd element was remove and keep going for k - th iteration ; removing the elements which are already removed at kth iteration ; Driver Code
function Survives ( n ) { let i ; for ( let i = 2 ; ; i ++ ) { if ( i > n ) return true ; if ( n % i == 0 ) return false ; n -= parseInt ( n / i ) ; } } let n = 17 ; if ( Survives ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Nth XOR Fibonacci number | Function to return the nth XOR Fibonacci number ; Driver code
function nthXorFib ( n , a , b ) { if ( n == 0 ) return a ; if ( n == 1 ) return b ; if ( n == 2 ) return ( a ^ b ) ; return nthXorFib ( n % 3 , a , b ) ; } let a = 1 , b = 2 , n = 10 ; document . write ( nthXorFib ( n , a , b ) ) ;
Sand Timer Flip Counting Problem | Recursive function to return the gcd of a and b ; Everything divides 0 ; Function to print the number of flips for both the sand timers ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function flip ( a , b ) { let lcm = parseInt ( ( a * b ) / gcd ( a , b ) ) ; a = parseInt ( lcm / a ) ; b = parseInt ( lcm / b ) ; document . write ( ( a - 1 ) + " " + ( b - 1 ) ) ; } let a = 10 ; let b = 5 ; flip ( a , b ) ;
Sum of N terms in the expansion of Arcsin ( x ) | Function to find the arcsin ( x ) ; The power to which ' x ' is raised ; Numerator value ; Denominator value ; Driver code
function find_Solution ( x , n ) { let sum = x , e = 2 , o = 1 , p = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { p += 2 ; sum += ( o / e ) * ( Math . pow ( x , p ) / p ) ; o = o * ( o + 2 ) ; e = e * ( e + 2 ) ; } document . write ( sum . toFixed ( 10 ) ) ; } let x = - 0.5 ; if ( Math . abs ( x ) >= 1 ) { document . write ( " " ) ; } let n = 8 ; find_Solution ( x , n ) ;
Minimize the cost of buying the Objects | Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function
function totalPay ( totalItems , priceOfOneItem , N , M ) { var freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; var amount = actual * priceOfOneItem ; return amount ; } var T = 12 , P = 8 ; var N = 2 , M = 1 ; document . write ( " " + totalPay ( T , P , N , M ) ) ;
Right most non | Function to return the rightmost non - zero digit in the multiplication of the array elements ; To store the count of times 5 can divide the array elements ; Divide the array elements by 5 as much as possible ; increase count of 5 ; Divide the array elements by 2 as much as possible ; Decrease count of 5 , because a '2' and a '5' makes a number with last digit '0' ; If c5 is more than the multiplier should be taken as 5 ; Driver code
function rightmostNonZero ( a , n ) { var c5 = 0 ; for ( i = 0 ; i < n ; i ++ ) { while ( a [ i ] > 0 && a [ i ] % 5 == 0 ) { a [ i ] /= 5 ; c5 ++ ; } } for ( i = 0 ; i < n ; i ++ ) { while ( c5 != 0 && a [ i ] > 0 && ( a [ i ] & 1 ) == 0 ) { a [ i ] >>= 1 ; c5 -- ; } } var ans = 1 ; for ( i = 0 ; i < n ; i ++ ) { ans = ( ans * a [ i ] % 10 ) % 10 ; } if ( c5 != 0 ) ans = ( ans * 5 ) % 10 ; if ( ans != 0 ) return ans ; return - 1 ; } var a = [ 7 , 42 , 11 , 64 ] ; var n = a . length ; document . write ( rightmostNonZero ( a , n ) ) ;
Find the remainder when First digit of a number is divided by its Last digit | Function to find the remainder ; Get the last digit ; Get the first digit ; Compute the remainder ; Driver code
function findRemainder ( n ) { let l = n % 10 ; while ( n >= 10 ) n /= 10 ; let f = n ; let remainder = f % l ; document . write ( Math . floor ( remainder ) ) ; } let n = 5223 ; findRemainder ( n ) ;
Percentage increase in the volume of cuboid if length , breadth and height are increased by fixed percentages | Function to return the percentage increase in the volume of the cuboid ; Driver code
function increaseInVol ( l , b , h ) { let percentInc = ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ; percentInc -= 1 ; percentInc *= 100 ; return percentInc ; } let l = 50 , b = 20 , h = 10 ; document . write ( increaseInVol ( l , b , h ) + " " ) ;
Count the number of occurrences of a particular digit in a number | Function to count the occurrences of the digit D in N ; Loop to find the digits of N ; check if the digit is D ; return the count of the occurrences of D in N ; Driver code
function countOccurrances ( n , d ) { let count = 0 ; while ( n > 0 ) { count = ( n % 10 == d ) ? count + 1 : count ; n = parseInt ( n / 10 ) ; } return count ; } let d = 2 ; let n = 214215421 ; document . write ( countOccurrances ( n , d ) ) ;
Find number of factors of N when location of its two factors whose product is N is given | Function to find the number of factors ; print the number of factors ; initialize the factors position
function findFactors ( a , b ) { let c ; c = a + b - 1 ; document . write ( c ) ; } let a , b ; a = 13 ; b = 36 ; findFactors ( a , b ) ;
21 Matchsticks Problem | Function to return the optimal strategy ; Removing matchsticks in blocks of five ; Driver code
function TwentyoneMatchstick ( arr , N ) { for ( var i = 0 ; i < N ; i += 1 ) { document . write ( 5 - arr [ i ] + " " ) ; } document . write ( " " ) ; } var arr = [ 3 , 4 , 2 , 2 ] ; var N = arr . length ; TwentyoneMatchstick ( arr , N ) ;
Find the smallest positive number which can not be represented by given digits | Function to find the smallest positive number which can not be represented by given digits ; Storing the count of 0 digit or store the value at 0 th index ; Calculates the min value in the array starting from 1 st index and also store it index . ; If its value at 0 th index is less than min value than either 10 , 100 , 1000 ... can 't be expressed ; If it value is greater than min value than iterate the loop upto first min value index and simply prvar it index value . ; Driver code ; Value of N is always 10 as we take digit from 0 to 9 ; Calling function
function expressDigit ( arr , n ) { var min = 9 , index = 0 , temp = 0 ; temp = arr [ 0 ] ; for ( i = 1 ; i < 10 ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; index = i ; } } if ( temp < min ) { document . write ( 1 ) ; for ( i = 1 ; i <= temp + 1 ; i ++ ) document . write ( 0 ) ; } else { for ( i = 0 ; i < min ; i ++ ) document . write ( index ) ; document . write ( index ) ; } } var arr = [ 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 ] ; var N = 10 ; expressDigit ( arr , N ) ;
Find the average of k digits from the beginning and l digits from the end of the given number | Function to return the count of digits in num ; Function to return the sum of first n digits of num ; Remove the unnecessary digits ; Function to return the sum of the last n digits of num ; If the average can 't be calculated without  using the same digit more than once ; Sum of the last l digits of n ; Sum of the first k digits of n ( totalDigits - k ) must be removed from the end of the number to get the remaining k digits from the beginning ; Return the average ; Driver code
function countDigits ( num ) { var cnt = 0 ; while ( num > 0 ) { cnt ++ ; num = parseInt ( num / 10 ) ; } return cnt ; } function sumFromStart ( num , n , rem ) { num = ( parseInt ( num / Math . pow ( 10 , rem ) ) ) ; var sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num = parseInt ( num / 10 ) ; } return sum ; } function sumFromEnd ( num , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += ( num % 10 ) ; num = parseInt ( num / 10 ) ; } return sum ; } function getAverage ( n , k , l ) { var totalDigits = countDigits ( n ) ; if ( totalDigits < ( k + l ) ) return - 1 ; var sum1 = sumFromEnd ( n , l ) ; var sum2 = sumFromStart ( n , k , totalDigits - k ) ; return ( ( sum1 + sum2 ) / ( k + l ) ) ; } var n = 123456 , k = 2 , l = 3 ; document . write ( getAverage ( n , k , l ) ) ;
Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given | Function to return the sum of numbers which are in the ration a : b and after adding x to both the numbers the new ratio becomes c : d ; Driver code
function sum ( a , b , c , d , x ) { var ans = ( x * ( a + b ) * ( c - d ) ) / ( ( a * d ) - ( b * c ) ) ; return ans ; } var a = 1 , b = 2 , c = 9 , d = 13 , x = 5 ; document . write ( sum ( a , b , c , d , x ) ) ;