codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to reach ( M , N ) in a matrix starting from the origin without visiting ( X , Y ) | Function for computing nCr ; Function to find factorial of a number ; Function for counting the number of ways to reach ( m , n ) without visiting ( x , y ) ; Given Dimensions of Matrix ; Cell not to be visited ; Function Call
function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function countWays ( m , n , x , y ) { return nCr ( m + n , m ) - nCr ( x + y , x ) * nCr ( m + n - x - y , m - x ) ; } let m = 5 ; let n = 4 ; let x = 3 ; let y = 2 ; document . write ( countWays ( m , n , x , y ) ) ;
Find the Batting Average of a batsman | Function to find the average of a batsman ; Calculate number of dismissals ; Check for 0 times out ; Calculate batting average ; Driver code
function averageRuns ( runs , matches , notout ) { let out1 ; out1 = matches - notout ; if ( out1 == 0 ) return - 1 ; let avg = parseInt ( ( runs ) / out1 , 10 ) ; return avg ; } let runs = 10000 ; let matches = 250 ; let notout = 50 ; let avg = averageRuns ( runs , matches , notout ) ; if ( avg == - 1 ) document . write ( " " ) ; else document . write ( avg ) ;
Sum of series formed by difference between product and sum of N natural numbers | Recursive Function to calculate the sum upto Nth term ; If N - th term is calculated ; Update multi to store product upto K ; Update add to store sum upto K ; Update prevSum to store sum upto K ; Proceed to next K ; Function to calculate and return the Sum upto Nth term ; Recursive Function ; Driver code
function seriesSumUtil ( k , n , prevSum , multi , add ) { if ( k == n + 1 ) { return prevSum ; } multi = multi * k ; add = add + k ; prevSum = prevSum + multi - add ; return seriesSumUtil ( k + 1 , n , prevSum , multi , add ) ; } function seriesSum ( n ) { if ( n == 1 ) return 0 ; let prevSum = 0 ; let multi = 1 ; let add = 1 ; return seriesSumUtil ( 2 , n , prevSum , multi , add ) ; } let N = 5 ; document . write ( seriesSum ( N ) ) ;
Count of total bits toggled / flipped in binary representation of 0 to N | Function to count and print the required number of toggles ; Store the count of toggles ; Add the contribution of the current LSB ; Update N ; Print the result ; Driver code
function solve ( N ) { let ans = 0 ; while ( N != 0 ) { ans += N ; N = parseInt ( N / 2 , 10 ) ; } document . write ( ans ) ; } let N = 5 ; solve ( N ) ;
Maximum Bitwise AND pair ( X , Y ) from given range such that X and Y can be same | Function to return the maximum bitwise AND ; Driver Code
function maximumAND ( L , R ) { return R ; } let l = 3 ; let r = 7 ; document . write ( maximumAND ( l , r ) ) ;
Check if K distinct array elements form an odd sum | Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver code
function oddSum ( A , N , K ) { let Odd = new Set ( ) ; let Even = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . add ( A [ i ] ) ; } else { Odd . add ( A [ i ] ) ; } } if ( Odd . size >= K ) return true ; let flag = false ; for ( let i = 1 ; i < K ; i += 2 ) { let needed = K - i ; if ( needed <= Even . size ) { return true ; } } return flag ; } let K = 5 ; let A = [ 12 , 1 , 7 , 7 , 26 , 18 ] ; let N = 3 ; if ( oddSum ( A , N , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of elements not divisible by any other elements of Array | Function to count the number of elements of array which are not divisible by any other element of same array ; Length for boolean array ; Hash map for storing the element and it 's frequency ; Update the maximum element ; Boolean array of size of the max element + 1 ; Marking the multiples as false ; To store the final count ; Traverse boolean array ; Check if i is not divisible by any other array elements and appears in the array only once ; Return the final Count ; Given array ; Function Call
function countEle ( a , n ) { let len = 0 ; let hmap = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { len = Math . max ( len , a [ i ] ) ; if ( hmap . has ( a [ i ] ) ) { hmap . set ( a [ i ] , hmap . get ( a [ i ] ) + 1 ) ; } else { hmap . set ( a [ i ] , 1 ) ; } } let v = Array . from ( { length : len + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i <= len ; i ++ ) { v [ i ] = true ; } for ( let i = 0 ; i < n ; i ++ ) { if ( v [ a [ i ] ] == false ) continue ; for ( let j = 2 * a [ i ] ; j <= len ; j += a [ i ] ) { v [ j ] = false ; } } let count = 0 ; for ( let i = 1 ; i <= len ; i ++ ) { if ( v [ i ] == true && hmap . has ( i ) && hmap . get ( i ) == 1 && hmap . get ( i ) == 1 ) { count += 1 ; } } return count ; } let arr = [ 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 ] ; let n = arr . length ; document . write ( countEle ( arr , n ) ) ;
Smallest N digit number divisible by N | Function to find the smallest N - digit number divisible by N ; Find largest n digit number ; Find smallest n digit number ; If i is divisible by N , then print i and return ; ; Given Number ; Function Call
function smallestNumber ( N ) { let L = Math . pow ( 10 , N ) - 1 ; let S = Math . pow ( 10 , N - 1 ) ; for ( let i = S ; i <= L ; i ++ ) { if ( i % N == 0 ) { document . write ( i ) ; return ; } } } let N = 2 ; smallestNumber ( N ) ;
Number less than equals to N with maximum product of prime factors | Function to find the smallest number having a maximum product of prime factors ; Declare the array arr [ ] ; Iterate from [ 2 , N ] ; If value at index i is 1 , then i is prime and make update array at index for all multiples of i ; Initialise maxValue ; Find number having maximum product of prime factor <= N ; Find the maximum value ; Given Number ; Function call
function maxPrimefactorNum ( N ) { let arr = Array ( N + 1 ) . fill ( 1 ) ; for ( let i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] == 1 ) { for ( let j = i ; j <= N ; j += i ) { arr [ j ] *= i ; } } } let maxValue = 1 ; for ( let i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] > maxValue ) { maxValue = i ; } } return maxValue ; } let N = 20 ; document . write ( maxPrimefactorNum ( N ) ) ;
Count of elements to be multiplied with integers to make each pair of Array a perfect square | Function to find the smallest prime factor of the elements ; Initializing the first element of the array ; Loop to add the remaining elements to the array ; Marking the smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all the numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to find the minimum number of steps to modify the array such that the product of any two numbers in the array is a perfect square ; Map created to store the unique prime numbers ; Variable to store the minimum number of operations ; Loop to store every unique prime number ; Erasing 1 as a key because it is not a prime number ; Iterating through the hash ; Two variables used for counting the frequency of prime is even or odd ; First prime number ; Iterating the number D ; check if prime is a factor of the element in the array ; Loop for calculating the frequency of the element ; Check for frequency odd or even ; If it is not a factor of the element , then it is automatically even ; Storing the minimum of two variable even or odd ; Driver code ; Input array ; Creating shortest prime factorisation array
function spf_array ( spf ) { spf [ 1 ] = 1 ; for ( var i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( var i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( var i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( var j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function minimum_operation ( b , d , spf ) { var m = new Map ( ) ; var i = 0 ; var c = 0 ; for ( i = 0 ; i < d ; i ++ ) { var x = b [ i ] ; while ( x != 1 ) { x = parseInt ( x / spf [ x ] ) ; if ( ! m . has ( spf [ x ] ) ) { m . set ( spf [ x ] , 1 ) ; } } } m . delete ( 1 ) ; m . forEach ( ( value , key ) => { var e = 0 , o = 0 ; var j = key ; for ( i = 0 ; i < d ; i ++ ) { if ( b [ i ] % j == 0 ) { var h = 0 ; var g = b [ i ] ; while ( g != 0 ) { if ( g % j != 0 ) { break ; } g = parseInt ( g / j ) ; h = h + 1 ; } if ( h % 2 == 0 ) { e = e + 1 ; } else { o = o + 1 ; } } else { e = e + 1 ; } } c = c + Math . min ( o , e ) ; } ) ; return c ; } var spf = Array ( 1001 ) var b = [ 1 , 4 , 6 ] ; var d = b . length ; spf_array ( spf ) ; document . write ( minimum_operation ( b , d , spf ) ) ;
Zuckerman Numbers | Function to get product of digits ; Function to check if N is an Zuckerman number ; Driver code
function getProduct ( n ) { let product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = parseInt ( n / 10 ) ; } return product ; } function isZuckerman ( n ) { return n % getProduct ( n ) == 0 ; } let n = 115 ; if ( isZuckerman ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Additive Prime Number | Check if N is prime or not ; Corner Cases ; This is checked to skip middle five numbers ; Function to get sum of digits ; Return the sum of digits ; Function to check whether the given number is Additive Prime number or not ; If number is not prime ; Check if sum of digits is prime or not ; Given Number N ; Function Call
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 getSum ( n ) { let sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } function isAdditivePrime ( n ) { if ( ! isPrime ( n ) ) return false ; return isPrime ( getSum ( n ) ) ; } let n = 23 ; if ( isAdditivePrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Straight | Function to check if N is a Straight Line number or not ; N must be > 99 ; Difference between consecutive digits must be same ; Given Number N ; Function Call
function isStraighLineNum ( N ) { if ( N <= 99 ) return false ; let s = N . toString ( ) ; let d = s [ 1 ] - s [ 0 ] ; for ( let i = 2 ; i < s . length ; i ++ ) if ( s [ i ] - s [ i - 1 ] != d ) return false ; return true ; } let n = 135 ; if ( isStraighLineNum ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Second | Function to find N - th term in the series ; Driver Code
function findNthTerm ( n ) { document . write ( ( Math . pow ( 2 , n ) ) - ( 2 * n ) ) ; } N = 4 ; findNthTerm ( N ) ;
Alternating Numbers | Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Given number N ; Function call
function isEvenOddForm ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 0 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 1 ) return false ; } return true ; } function isOddEvenForm ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 1 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 0 ) return false ; } return true ; } function isAlternating ( n ) { let str = n . toString ( ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } let N = 129 ; if ( isAlternating ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Ormiston prime Pairs | Function to check if the number is a prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagrams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Returns true if n1 and n2 are Ormiston primes ; 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 ; } const TEN = 10 ; function updateFreq ( n , freq ) { while ( n > 0 ) { let digit = n % TEN ; freq [ digit ] ++ ; n = parseInt ( n / TEN ) ; } } function areAnagrams ( a , b ) { let freqA = Array ( TEN ) . fill ( 0 ) ; let freqB = Array ( TEN ) . fill ( 0 ) ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } function OrmistonPrime ( n1 , n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && areAnagrams ( n1 , n2 ) ) ; } let n1 = 1913 , n2 = 1931 ; if ( OrmistonPrime ( n1 , n2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Decakismyriagon Number | Function to find the N - th Decakismyriagon Number ; Given Number N ; Function Call
function DecakismyriagonNum ( N ) { return ( 99998 * N * N - 99996 * N ) / 2 ; } let N = 3 ; console . log ( DecakismyriagonNum ( N ) ) ;
Untouchable Number | Function to calculate sum of all proper divisors of num ; Final result of summation of divisors ; Find all divisors of num ; If ' i ' is divisor of ' num ' ; If both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Function to check if N is a Untouchable Number ; Given Number n ; Function Call
function divSum ( num ) { let result = 0 ; for ( let i = 2 ; i <= Math . sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result + 1 ) ; } function isUntouchable ( n ) { for ( let i = 1 ; i <= 2 * n ; i ++ ) { if ( divSum ( i ) == n ) return false ; } return true ; } let n = 52 ; if ( isUntouchable ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Zygodrome Number | Function to check if N is an zygodrome number ; convert N to string ; Adding a space at the beginning and end of the string ; Traverse the string ; If any letacter is not same as prev and next then return false ; Driver code
function iszygodromeNum ( N ) { let s = N . toString ( ) ; s = ' ' + s + ' ' ; for ( i = 1 ; i < s . length - 1 ; i ++ ) { if ( s [ i ] != s [ i - 1 ] && s [ i ] != s [ i + 1 ] ) { return false ; } } return true ; } let n = 1122 ; if ( iszygodromeNum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Loeschian Number | Function to check if N is a Loeschian Number ; Iterate [ 0 , sqrt ( N ) ] for x ; Iterate [ 0 , sqrt ( N ) ] for y ; Check the given criteria ; If no such pair found then return false ; Given Number n ; Function Call
function isLoeschian ( n ) { for ( let x = 1 ; x <= Math . sqrt ( n ) ; x ++ ) { for ( let y = 1 ; y <= Math . sqrt ( n ) ; y ++ ) { if ( x * x + x * y + y * y == n ) return true ; } } return false ; } let n = 19 ; if ( isLoeschian ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Admirable Numbers | Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add ; Add 1 and n to result as above loop considers proper divisors greater ; Function to check if there exists a proper divisor D ' ▁ of ▁ N ▁ such ▁ that ▁ sigma ( n ) -2D ' = 2 N ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Check 1 since 1 is also a divisor ; Function to check if N is an admirable number ; Driver code
function divSum ( n ) { let result = 0 ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } function check ( num ) { let sigmaN = divSum ( num ) ; for ( let i = 2 ; i <= Math . sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) { if ( sigmaN - 2 * i == 2 * num ) return true ; } else { if ( sigmaN - 2 * i == 2 * num ) return true ; if ( sigmaN - 2 * ( num / i ) == 2 * num ) return true ; } } } if ( sigmaN - 2 * 1 == 2 * num ) return true ; return false ; } function isAdmirableNum ( N ) { return check ( N ) ; } let n = 12 ; if ( isAdmirableNum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to print the series 2 , 15 , 41 , 80 , 132 , 197 … till N terms | Function to print the series ; Generate the ith term and ; Driver Code
function printSeries ( N ) { let ith_term = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { ith_term = ( 13 * i * ( i - 1 ) ) / 2 + 2 ; document . write ( ith_term + " " ) ; } } let N = 7 ; printSeries ( N ) ;
Possible pairs forming a Pythagorean Triple with a given value | Function to generate all possible pairs ; Vector to store all the possible pairs ; Checking all the possible pair in the range of [ 1 , c ) ; If the pair satisfies the condition push it in the vector ; Driver Program ; If no valid pair exist ; Print all valid pairs
function Pairs ( C ) { var ans = [ ] ; for ( var i = 1 ; i < C ; i ++ ) { for ( var j = i + 1 ; j < C ; j ++ ) { if ( ( i * i ) + ( j * j ) == ( C * C ) ) { ans . push ( [ i , j ] ) ; } } } return ans ; } var C = 13 ; var ans = Pairs ( C ) ; if ( ans . length == 0 ) { document . write ( " " ) ; } ans . forEach ( x => { document . write ( " " + x [ 0 ] + " " + x [ 1 ] + " " + " " ) ; } ) ;
How to calculate strike rate of a batsman | function to calculate strike rate of a batsman ; Driver code
function strikerate ( bowls , runs ) { let z ; z = ( runs / bowls ) * 100 ; return z . toFixed ( 3 ) ; } let A , B ; A = 264 ; B = 173 ; document . write ( strikerate ( B , A ) ) ;
Check if a number exists with X divisors out of which Y are composite | javascript program to check if a number exists having exactly X positive divisors out of which Y are composite divisors ; Count the number of times 2 divides N ; Check for all possible numbers that can divide it ; If N at the end is a prime number . ; Function to check if any such number exists ; Driver code
function factorize ( N ) { var count = 0 ; var cnt = 0 ; while ( ( N % 2 ) == 0 ) { N = N / 2 ; count ++ ; } cnt = cnt + count ; for ( i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } cnt = cnt + count ; } if ( N > 2 ) cnt = cnt + 1 ; return cnt ; } function ifNumberExists ( X , Y ) { var C , dsum ; C = X - Y - 1 ; dsum = factorize ( X ) ; if ( dsum >= C ) document . write ( " " ) ; else document . write ( " " ) ; } var X , Y ; X = 6 ; Y = 4 ; ifNumberExists ( X , Y ) ;
Nearest prime number in the array of every array element | Javascript program to find nearest prime number in the array for all array elements ; Create a boolean array and set all entries it as false . A value in prime [ i ] will be true if i is not a prime , else false ; Sieve of Eratosthenes function ; Update all multiples of i greater than or equal to the square of it numbers which are multiple of i and are less than i ^ 2 are already been marked . ; Function to find nearest prime number for all elements ; Compute and store all prime numbers up to maxm ; Store the indices of all primes ; If no primes are present in the array ; Store the current prime ; If the no further primes exist in the array ; For all indices less than that of the current prime ; If the current prime is nearer ; If the next prime is nearer ; Make the next prime as the current ; Driver code
let max = 10000000 ; let prime = new Array ( max ) ; function SieveOfEratosthenes ( maxm ) { prime [ 0 ] = prime [ 1 ] = true ; for ( let i = 2 ; i * i <= maxm ; i ++ ) { if ( ! prime [ i ] ) { for ( let j = i * i ; j <= maxm ; j += i ) { prime [ j ] = true ; } } } } function print_nearest_prime ( arr , N ) { let maxm = Math . max ( ... arr ) ; SieveOfEratosthenes ( maxm ) ; let primes = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! prime [ arr [ i ] ] ) primes . push ( i ) ; } if ( primes . length == 0 ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( - 1 + " " ) ; } return ; } let curr = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( curr == primes . length - 1 || i <= primes [ curr ] ) { document . write ( arr [ primes [ curr ] ] + " " ) ; continue ; } if ( Math . abs ( primes [ curr ] - i ) < Math . abs ( primes [ curr + 1 ] - i ) ) { document . write ( arr [ primes [ curr ] ] + " " ) ; } else { curr ++ ; document . write ( arr [ primes [ curr ] ] + " " ) ; } } } let N = 6 ; let arr = [ 8 , 7 , 12 , 15 , 3 , 11 ] ; print_nearest_prime ( arr , N ) ;
Find position of given term in a series formed with only digits 4 and 7 allowed | Function to find the position of the number N ; To store the position of N ; Iterate through all digit of N ; If current digit is 7 ; If current digit is 4 ; Prvar the final position ; Given number of the series ; Function Call
function findPosition ( n ) { var i = 0 ; var pos = 0 ; while ( n > 0 ) { if ( n % 10 == 7 ) { pos = pos + parseInt ( Math . pow ( 2 , i + 1 ) ) ; } else { pos = pos + parseInt ( Math . pow ( 2 , i ) ) ; } i ++ ; n = parseInt ( n / 10 ) ; } document . write ( pos ) ; } var N = 777 ; findPosition ( N ) ;
Average of Cubes of first N natural numbers | Function to find average of cubes ; Store sum of cubes of numbers in the sum ; Calculate sum of cubes ; Return average ; Given number ; Function Call
function findAverageOfCube ( n ) { let sum = 0 ; let i ; for ( i = 1 ; i <= n ; i ++ ) { sum += i * i * i ; } return sum / n ; } let n = 3 ; document . write ( findAverageOfCube ( n ) . toFixed ( 6 ) ) ;
Smallest and Largest N | Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting and ending with n , we just need to append n at start and end ; Return the largest number ; Function to find n digit smallest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit smallest number ; Find the number of digits in number n ; To make it smallest n digit number starting and ending with n , we just need to append n at start and end ; Return the smallest number ; Given Number ; Function Call
function findNumberL ( n ) { if ( n == 1 ) return " " ; let result = " " ; let length = Math . floor ( Math . log10 ( n ) + 1 ) ; for ( let i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += ' ' ; } result = n . toString ( ) + result + n . toString ( ) ; return result ; } function findNumberS ( n ) { if ( n == 1 ) return " " ; let result = " " ; let length = Math . floor ( Math . log10 ( n ) + 1 ) ; for ( let i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += ' ' ; } result = n . toString ( ) + result + n . toString ( ) ; return result ; } let N = 3 ; document . write ( " " + findNumberS ( N ) + " " ) ; document . write ( " " + findNumberL ( N ) ) ;
Maximum number of 0 s that can be flipped such that Array has no adjacent 1 s | Maximum number of 0 s that can be replaced by 1 ; Check for three consecutive 0 s ; Flip the bit ; Increase the count ; Driver 's Code
function canReplace ( array , n ) { var i = 0 , count = 0 ; while ( i < n ) { if ( array [ i ] == 0 && ( i == 0 array [ i - 1 ] == 0 ) && ( i == n - 1 array [ i + 1 ] == 0 ) ) { array [ i ] = 1 ; count ++ ; } i ++ ; } return count ; } array = [ 1 , 0 , 0 , 0 , 1 ] document . write ( canReplace ( array , 5 ) ) ;
Sum of first N Star Numbers | Function to find the sum of the first N star number ; Variable to store the sum ; Driver code
function sum_star_num ( n ) { let summ = 2 * n * ( n + 1 ) * ( n - 1 ) + n ; return summ ; } let n = 3 ; document . write ( sum_star_num ( n ) ) ;