codeparrot/xlcost-text-to-code huggingface dataset:
Find the final sequence of the array after performing given operations | Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code
function solveEven ( n , arr , b ) { let left = n - 1 ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } let right = 0 ; for ( let i = parseInt ( n / 2 , 10 ) ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } function solveOdd ( n , arr , b ) { let left = n - 1 ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) + 1 ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } let right = 1 ; for ( let i = parseInt ( n / 2 , 10 ) + 1 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } function solve ( n , arr ) { let b = new Array ( n ) ; if ( n % 2 == 0 ) solveEven ( n , arr , b ) ; else solveOdd ( n , arr , b ) ; for ( let i = 0 ; i <= n - 1 ; ++ i ) { document . write ( b [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; solve ( n , arr ) ;
Find the sum of the first half and second half elements of an array | Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver code ; Function call
function sum_of_elements ( arr , n ) { var sumfirst = 0 , sumsecond = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( i < parseInt ( n / 2 ) ) { sumfirst += arr [ i ] ; } else { sumsecond += arr [ i ] ; } } document . write ( " " + sumfirst + " " ) ; document . write ( " " + sumsecond + " " ) ; } var arr = [ 20 , 30 , 60 , 10 , 25 , 15 , 40 ] ; var n = arr . length ; sum_of_elements ( arr , n ) ;
Sequence with sum K and minimum sum of absolute differences between consecutive elements | Function to return the minimized sum ; If k is divisible by n then the answer will be 0 ; Else the answer will be 1 ; Driver code
function minimum_sum ( n , k ) { if ( k % n == 0 ) return 0 ; return 1 ; } let n = 3 , k = 56 ; document . write ( minimum_sum ( n , k ) ) ;
Print first N terms of Lower Wythoff sequence | Function to print the first n terms of the lower Wythoff sequence ; Calculate value of phi ; Find the numbers ; a ( n ) = floor ( n * phi ) ; Print the nth numbers ; Driver code
function lowerWythoff ( n ) { var phi = ( 1 + Math . sqrt ( 5 ) ) / 2.0 ; for ( var i = 1 ; i <= n ; i ++ ) { var ans = Math . floor ( i * phi ) ; document . write ( ans ) ; if ( i != n ) document . write ( " " ) ; } } var n = 5 ; lowerWythoff ( n ) ;
Create new linked list from two given linked list with greater element at each node | Representation of node ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver code ; First linked list ; Second linked list
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function insert ( root , item ) { var ptr , temp ; temp = new Node ( ) ; temp . data = item ; temp . next = null ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } return root ; } function newList ( root1 , root2 ) { var ptr1 = root1 , ptr2 = root2 , ptr ; var root = null , temp ; while ( ptr1 != null ) { temp = new Node ( ) ; temp . next = null ; if ( ptr1 . data < ptr2 . data ) temp . data = ptr2 . data ; else temp . data = ptr1 . data ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } ptr1 = ptr1 . next ; ptr2 = ptr2 . next ; } return root ; } function display ( root ) { while ( root != null ) { document . write ( root . data + " " ) ; root = root . next ; } document . write ( " " ) ; } root1 = null , root2 = null , root = null ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 2 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 8 ) ; document . write ( " " ) ; display ( root1 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 7 ) ; root2 = insert ( root2 , 4 ) ; root2 = insert ( root2 , 5 ) ; document . write ( " " ) ; display ( root2 ) ; root = newList ( root1 , root2 ) ; document . write ( " " ) ; display ( root ) ;
Count of quadruplets from range [ L , R ] having GCD equal to K | Function to return the gcd of a and b ; Function to return the count of quadruplets having gcd = k ; To store the required count ; Check every quadruplet pair whether its gcd is k ; Return the required count ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function countQuadruplets ( l , r , k ) { var count = 0 ; for ( u = l ; u <= r ; u ++ ) { for ( v = l ; v <= r ; v ++ ) { for ( w = l ; w <= r ; w ++ ) { for ( x = l ; x <= r ; x ++ ) { if ( gcd ( gcd ( u , v ) , gcd ( w , x ) ) == k ) count ++ ; } } } } return count ; } var l = 1 , r = 10 , k = 2 ; document . write ( countQuadruplets ( l , r , k ) ) ;
Find an index such that difference between product of elements before and after it is minimum | Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code
function findIndex ( a , n ) { let res = 0 ; let min_diff = Number . MAX_VALUE ; let prod = new Array ( n ) ; prod [ 0 ] = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) prod [ i ] = prod [ i - 1 ] * a [ i ] ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let curr_diff = Math . abs ( parseInt ( prod [ n - 1 ] / prod [ i ] , 10 ) - prod [ i ] ) ; if ( curr_diff < min_diff ) { min_diff = curr_diff ; res = i ; } } return res ; } let arr = [ 3 , 2 , 5 , 7 , 2 , 9 ] ; let N = arr . length ; document . write ( findIndex ( arr , N ) ) ;
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Drivers code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function printNumbers ( a , n , x ) { let flag = false ; for ( let i = 0 ; i < n ; i ++ ) { let num = a [ i ] ; let g = __gcd ( num , x ) ; while ( g != 1 ) { num = parseInt ( num / g ) ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; document . write ( a [ i ] + " " ) ; } } if ( ! flag ) document . write ( " " ) ; } let x = 60 ; let a = [ 2 , 5 , 10 , 7 , 17 ] ; let n = a . length ; printNumbers ( a , n , x ) ;
Find the final radiations of each Radiated Stations | Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; 1 - based indexing
function print ( rStation , n ) { for ( var i = 1 ; i <= n ; i ++ ) document . write ( rStation [ i ] + " " ) ; document . write ( " " ) ; } function radiated_Station ( station , n ) { var rStation = [ ] ; rStation . length = 6 ; rStation . fill ( 0 ) for ( var i = 1 ; i <= n ; i ++ ) { var li = i - 1 , ri = i + 1 ; var lRad = station [ i ] - 1 , rRad = station [ i ] - 1 ; rStation [ i ] += station [ i ] ; while ( li >= 1 && lRad >= 1 ) { rStation [ li -- ] += lRad -- ; } while ( ri <= n && rRad >= 1 ) { rStation [ ri ++ ] += rRad -- ; } } print ( rStation , n ) ; } var station = [ 0 , 7 , 9 , 12 , 2 , 5 ] var n = station . length - 1 ; radiated_Station ( station , n ) ;
Program to find the maximum difference between the index of any two different numbers | Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
function findMaximumDiff ( a , n ) { var ind1 = 0 ; for ( i = n - 1 ; i > 0 ; i -- ) { if ( a [ 0 ] != a [ i ] ) { ind1 = i ; break ; } } var ind2 = 0 ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( a [ n - 1 ] != a [ i ] ) { ind2 = ( n - 1 - i ) ; break ; } } return Math . max ( ind1 , ind2 ) ; } var a = [ 1 , 2 , 3 , 2 , 3 ] ; var n = a . length ; document . write ( findMaximumDiff ( a , n ) ) ;
Find the Kth node in the DFS traversal of a given subtree in a Tree | Javascript program to find the Kth node in the DFS traversal of the subtree of given vertex V in a Tree ; To store nodes ; To store the current index of vertex in DFS ; To store the starting index and ending index of vertex in the DFS traversal array ; To store the DFS of vertex 1 ; Function to add edge between two nodes ; Initialize the vectors ; Function to perform DFS of a vertex 1. stores the DFS of the vertex 1 in vector p , 2. store the start index of DFS of every vertex 3. store the end index of DFS of every vertex ; store staring index of node ch ; store ending index ; Function to find the Kth node in DFS of vertex V ; Check if kth number exits or not ; Number of nodes ; Add edges ; Store DFS of 1 st node
let N = 100005 ; let n = 10 ; let tree = [ ] ; let currentIdx = 0 ; let startIdx ; let endIdx ; let p ; function Add_edge ( u , v ) { tree [ u ] . push ( v ) ; tree [ v ] . push ( u ) ; } function intisalise ( ) { startIdx = new Array ( n + 1 ) ; endIdx = new Array ( n + 1 ) ; p = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { startIdx [ i ] = 0 ; endIdx [ i ] = 0 ; p [ i ] = 0 ; } } function Dfs ( ch , par ) { p [ currentIdx ] = ch ; startIdx [ ch ] = currentIdx ++ ; for ( let c = 0 ; c < tree [ ch ] . length ; c ++ ) { if ( tree [ ch ] != par ) Dfs ( tree [ ch ] , ch ) ; } endIdx [ ch ] = currentIdx - 1 ; } function findNode ( v , k ) { k += startIdx [ v ] - 1 ; if ( k <= endIdx [ v ] ) return p [ k ] ; return - 1 ; } n = 9 ; for ( let i = 0 ; i <= n ; i ++ ) tree . push ( [ ] ) ; Add_edge ( 1 , 2 ) ; Add_edge ( 1 , 3 ) ; Add_edge ( 1 , 4 ) ; Add_edge ( 3 , 5 ) ; Add_edge ( 3 , 7 ) ; Add_edge ( 5 , 6 ) ; Add_edge ( 5 , 8 ) ; Add_edge ( 7 , 9 ) ; intisalise ( ) ; Dfs ( 1 , 0 ) ; let v = 3 , k = 4 ; document . write ( findNode ( v , k ) ) ;
Sum of the series Kn + ( K ( n | Function to return sum ; Driver code
function sum ( k , n ) { let sum = 0 ; for ( let i = 0 ; i <= n ; i ++ ) { let p = 1 ; for ( let j = 0 ; j < n - i ; j ++ ) { p = p * k ; } for ( let j = 0 ; j < i ; j ++ ) { p = p * ( k - 1 ) ; } sum = sum + p ; } return sum ; } let n = 3 ; let K = 3 ; document . write ( sum ( K , n ) ) ;
Check whether factorial of N is divisible by sum of first N natural numbers | Function to check whether a number is prime or not . ; Count variable to store the number of factors of ' num ' ; Counting the number of factors ; If number is prime return true ; Function to check for divisibility ; if ' n ' equals 1 then divisibility is possible ; Else check whether ' n + 1' is prime or not ; If ' n + 1' is prime then ' n ! ' is not divisible by ' n * ( n + 1 ) / 2' ; else divisibility occurs ; Driver Code ; Test for n = 3 ; Test for n = 4
function is_prime ( num ) { var count = 0 ; for ( i = 1 ; i * i <= ( num ) ; i ++ ) { if ( ( num ) % i == 0 ) { if ( i * i != ( num ) ) count += 2 ; else count ++ ; } } if ( count == 2 ) return true ; else return false ; } function is_divisible ( n ) { if ( n == 1 ) { return " " ; } else { if ( is_prime ( n + 1 ) ) return " " ; else return " " ; } } var n ; n = 3 ; document . write ( is_divisible ( n ) + " " ) ; n = 4 ; document . write ( is_divisible ( n ) ) ;
Number of subarrays having sum of the form k ^ m , m >= 0 | JavaScript implementation of the above approach ; partial_sum ; Function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; Driver code
let MAX = 100005 ; function partial_sum ( prefix_sum , arr , n ) { for ( let i = 1 ; i <= n ; i ++ ) { prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i - 1 ] ) ; } return prefix_sum ; } function countSubarrays ( arr , n , k ) { let prefix_sum = new Array ( MAX ) ; prefix_sum [ 0 ] = 0 ; prefix_sum = partial_sum ( prefix_sum , arr , n ) ; let sum ; if ( k == 1 ) { sum = 0 ; let m = new Map ( ) ; for ( let i = n ; i >= 0 ; i -- ) { if ( m . has ( prefix_sum [ i ] + 1 ) ) sum += m . get ( prefix_sum [ i ] + 1 ) ; if ( m . has ( prefix_sum [ i ] ) ) m . set ( prefix_sum [ i ] , m . get ( prefix_sum [ i ] ) + 1 ) ; else m . set ( prefix_sum [ i ] , 1 ) ; } return sum ; } if ( k == - 1 ) { sum = 0 ; let m = new Map ( ) ; for ( let i = n ; i >= 0 ; i -- ) { if ( m . has ( prefix_sum [ i ] + 1 ) ) sum += m . get ( prefix_sum [ i ] + 1 ) ; if ( m . has ( prefix_sum [ i ] - 1 ) ) sum += m . get ( prefix_sum [ i ] - 1 ) ; if ( m . has ( prefix_sum [ i ] ) ) m . set ( prefix_sum [ i ] , m . get ( prefix_sum [ i ] ) + 1 ) ; else m . set ( prefix_sum [ i ] , 1 ) ; } return sum ; } sum = 0 ; let b , l = 100000000000000 ; let m = new Map ( ) ; for ( let i = n ; i >= 0 ; i -- ) { b = 1 ; while ( true ) { if ( b > l ) break ; if ( m . has ( prefix_sum [ i ] + b ) ) sum += m . get ( prefix_sum [ i ] + b ) ; b *= k ; } if ( m . has ( prefix_sum [ i ] ) ) m . set ( ( prefix_sum [ i ] ) , m . get ( prefix_sum [ i ] ) + 1 ) ; else m . set ( ( prefix_sum [ i ] ) , 1 ) ; } return sum ; } let arr = [ 2 , 2 , 2 , 2 ] ; let n = arr . length ; let k = 2 ; document . write ( countSubarrays ( arr , n , k ) ) ;
Given two binary strings perform operation until B > 0 and print the result | JavaScript implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code
let mod = ( 1e9 + 7 ) ; function BitOperations ( a , n , b , m ) { let ch1 = a . split ( ' ' ) ; ch1 . reverse ( ) ; a = ch1 . join ( " " ) ; let ch2 = b . split ( ' ' ) ; ch2 . reverse ( ) ; b = ch2 . join ( " " ) ; let c = 0 ; for ( let i = 0 ; i < m ; i ++ ) if ( b [ i ] == ' ' ) c ++ ; let power = new Array ( n ) ; power [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) power [ i ] = ( power [ i - 1 ] * 2 ) % mod ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == ' ' ) { ans += c * power [ i ] ; if ( ans >= mod ) ans %= mod ; } if ( b [ i ] == ' ' ) c -- ; if ( c == 0 ) break ; } return ans ; } let a = " " , b = " " ; let n = a . length , m = b . length ; document . write ( BitOperations ( a , n , b , m ) ) ;
Print prime numbers with prime sum of digits in an array | Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
function sieve ( maxEle , prime ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( var i = 2 ; i * i <= maxEle ; i ++ ) { if ( ! prime [ i ] ) { for ( var j = 2 * i ; j <= maxEle ; j += i ) prime [ j ] = 1 ; } } } function digitSum ( n ) { var sum = 0 ; while ( n ) { sum += n % 10 ; n = parseInt ( n / 10 ) ; } return sum ; } function printAdditivePrime ( arr , n ) { var maxEle = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var prime = Array ( maxEle + 1 ) . fill ( 0 ) ; sieve ( maxEle , prime ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 0 ) { var sum = digitSum ( arr [ i ] ) ; if ( prime [ sum ] == 0 ) document . write ( arr [ i ] + " " ) ; } } } var a = [ 2 , 4 , 6 , 11 , 12 , 18 , 7 ] ; var n = a . length ; printAdditivePrime ( a , n ) ;
Program to find nth term of the series 1 4 15 24 45 60 92 | Javascript implementation of the above approach ; function to calculate nth term of the series ; variable nth will store the nth term of series ; if n is even ; if n is odd ; return nth term ; Driver code
using System ; class GFG { function nthTerm ( n ) { let nth ; if ( n % 2 == 0 ) nth = 2 * ( ( n * n ) - n ) ; else nth = ( 2 * n * n ) - n ; return nth ; } let n = 5 ; document . write ( nthTerm ( n ) + " " ) ; n = 25 ; document . write ( nthTerm ( n ) + " " ) ; n = 25000000 ; document . write ( nthTerm ( n ) + " " ) ; n = 250000007 ; document . write ( nthTerm ( n ) + " " ) ;
Find the Nth term of the series 9 , 45 , 243 , 1377 | Function to return the nth term of the given series ; nth term ; Driver code
function nthterm ( n ) { let An = ( Math . pow ( 1 , n ) + Math . pow ( 2 , n ) ) * Math . pow ( 3 , n ) ; return An ; } let n = 3 ; document . write ( nthterm ( n ) ) ;
Count the numbers < N which have equal number of divisors as K | Function to return the count of the divisors of a number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n ; This condition is to handle the case when n is a prime number > 2 ; Count the total elements that have divisors exactly equal to as that of k 's ; Exclude k from the result if it is smaller than n . ; Driver code
function countDivisors ( n ) { var x = 0 , ans = 1 ; while ( n % 2 == 0 ) { x ++ ; n = n / 2 ; } ans = ans * ( x + 1 ) ; for ( var i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { x = 0 ; while ( n % i == 0 ) { x ++ ; n = n / i ; } ans = ans * ( x + 1 ) ; } if ( n > 2 ) ans = ans * 2 ; return ans ; } function getTotalCount ( n , k ) { var k_count = countDivisors ( k ) ; var count = 0 ; for ( var i = 1 ; i < n ; i ++ ) if ( k_count == countDivisors ( i ) ) count ++ ; if ( k < n ) count = count - 1 ; return count ; } var n = 500 , k = 6 ; document . write ( getTotalCount ( n , k ) ) ;
Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | Function to return the nth term of the given series ; Common difference ; First term ; nth term ; nth term of the given series ; Driver code
function term ( n ) { let d = 2 ; let a1 = 0 ; An = a1 + ( n - 1 ) * d ; return Math . pow ( An , 3 ) ; } let n = 5 ; document . write ( term ( n ) ) ;
Count the number of intervals in which a given value lies | Javascript program to count the number of intervals in which a given value lies ; Function to count the number of intervals in which a given value lies ; Variables to store overall minimum and maximum of the intervals ; Variables to store start and end of an interval ; Frequency array to keep track of how many of the given intervals an element lies in ; Constructing the frequency array ; Driver code ; length of the array
let MAX_VAL = 200000 ; function countIntervals ( arr , V , N ) { let min = Number . MAX_VALUE , max = Number . MIN_VALUE ; let li , ri ; let freq = new Array ( MAX_VAL ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { li = arr [ i ] [ 0 ] ; freq [ li ] = freq [ li ] + 1 ; ri = arr [ i ] [ 1 ] ; freq [ ri + 1 ] = freq [ ri + 1 ] - 1 ; if ( li < min ) min = li ; if ( ri > max ) max = ri ; } for ( let i = min ; i <= max ; i ++ ) freq [ i ] = freq [ i ] + freq [ i - 1 ] ; return freq [ V ] ; } let arr = [ [ 1 , 10 ] , [ 5 , 10 ] , [ 15 , 25 ] , [ 7 , 12 ] , [ 20 , 25 ] ] ; let V = 7 ; let N = arr . length ; document . write ( countIntervals ( arr , V , N ) ) ;
Split N ^ 2 numbers into N groups of equal sum | Function to print N groups of equal sum ; No . of Groups ; n / 2 pairs ; driver program
function prletGroups ( n ) { let x = 1 ; let y = n * n ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= n / 2 ; j ++ ) { document . write ( " " + x + " " + y + " " ) ; x ++ ; y -- ; } document . write ( " " ) ; } } let n = 4 ; prletGroups ( n ) ;
Program to find the Break Even Point | Function to calculate Break Even Point ; Calculating number of articles to be sold ; Main Function
function breakEvenPoint ( exp , S , M ) { var earn = S - M ; var res = Math . ceil ( exp / earn ) ; return res ; } var exp = 3550 , S = 90 , M = 65 ; document . write ( breakEvenPoint ( exp , S , M ) ) ;
Minimize the value of N by applying the given operations | function to return the product of distinct prime factors of a number ; find distinct prime ; Driver code
function minimum ( n ) { let product = 1 ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { while ( n % i == 0 ) n = n / i ; product = product * i ; } } if ( n >= 2 ) product = product * n ; return product ; } let n = 20 ; document . write ( minimum ( n ) ) ;
N digit numbers divisible by 5 formed from the M digits | Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code
function numbers ( n , arr , m ) { let isZero = 0 , isFive = 0 ; let result = 0 ; if ( m < n ) { return - 1 ; } for ( let i = 0 ; i < m ; i ++ ) { if ( arr [ i ] == 0 ) { isZero = 1 ; } if ( arr [ i ] == 5 ) { isFive = 1 ; } } if ( isZero == 1 && isFive == 1 ) { result = 2 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else if ( isZero == 1 isFive == 1 ) { result = 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else { result = - 1 ; } return result ; } let n = 3 , m = 6 ; let arr = [ 2 , 3 , 5 , 6 , 7 , 9 ] ; document . write ( numbers ( n , arr , m ) ) ;
Program to find the smallest element among three elements | Javascript implementation to find the smallest of three elements
let a = 5 , b = 7 , c = 10 ; if ( a <= b && a <= c ) document . write ( a + " " ) ; else if ( b <= a && b <= c ) document . write ( b + " " ) ; else document . write ( c + " " ) ;
Find subsequences with maximum Bitwise AND and Bitwise OR | Function to find the maximum sum ; Maximum AND is maximum element ; Maximum OR is bitwise OR of all . ; Driver code
function maxSum ( a , n ) { var maxAnd = Math . max ( ... a ) ; var maxOR = 0 ; for ( var i = 0 ; i < n ; i ++ ) { maxOR |= a [ i ] ; } document . write ( ( maxAnd + maxOR ) ) ; } var n = 4 ; var a = [ 3 , 5 , 6 , 1 ] ; maxSum ( a , n ) ;