codeparrot/xlcost-text-to-code huggingface dataset:
Minimum length paths between 1 to N including each node | Function to calculate the distances from node 1 to N ; Vector to store our edges ; Storing the edgees in the Vector ; Initialize queue ; BFS from first node using queue ; Pop from queue ; Traversing its adjacency list ; Initialize queue ; BFS from last node using queue ; Pop from queue ; Traversing its adjacency list ; Printing the minimum distance including node i ; If not reachable ; Path exists ; Given Input ; Function Call
function minDisIncludingNode ( n , m , edges ) { let g = new Array ( 10005 ) . fill ( 0 ) . map ( ( ) => [ ] ) ; for ( let i = 0 ; i < m ; i ++ ) { let a = edges [ i ] [ 0 ] - 1 ; let b = edges [ i ] [ 1 ] - 1 ; g [ a ] . push ( b ) ; g [ b ] . push ( a ) ; } let q = [ ] ; q . push ( [ 0 , 0 ] ) ; dist = new Array ( n ) . fill ( 1e9 ) ; dist [ 0 ] = 0 ; while ( q . length > 0 ) { let up = q [ 0 ] ; q . pop ( ) ; let x = up [ 0 ] ; let lev = up [ 1 ] ; if ( lev > dist [ x ] ) continue ; if ( x == n - 1 ) continue ; for ( let y of g [ x ] ) { if ( dist [ y ] > lev + 1 ) { dist [ y ] = lev + 1 ; q . push ( [ y , lev + 1 ] ) ; } } } let q1 = [ ] ; q1 . push ( [ n - 1 , 0 ] ) ; let dist1 = new Array ( n ) . fill ( 1e9 ) ; dist1 [ n - 1 ] = 0 ; while ( q1 . length > 0 ) { let up = q1 [ 0 ] ; q1 . pop ( ) ; let x = up [ 0 ] ; let lev = up [ 1 ] ; if ( lev > dist1 [ x ] ) continue ; if ( x == 0 ) continue ; for ( let y of g [ x ] ) { if ( dist1 [ y ] > lev + 1 ) { dist1 [ y ] = lev + 1 ; q1 . push ( [ y , lev + 1 ] ) ; } } } for ( let i = 0 ; i < n ; i ++ ) { if ( dist [ i ] + dist1 [ i ] > 1e9 ) document . write ( - 1 + " " ) ; else document . write ( dist [ i ] + dist1 [ i ] + " " ) ; } } let n = 5 ; let m = 7 ; let edges = [ [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 2 , 5 ] , [ 4 , 3 ] , [ 4 , 5 ] , [ 1 , 5 ] , ] ; minDisIncludingNode ( n , m , edges ) ;
Check if possible to make Array sum equal to Array product by replacing exactly one element | Function to check if it is possible to form an array whose sum and the product is the same or not ; Find the sum of the array initialize sum ; Iterate through all elements and add them to sum ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code
function canPossibleReplacement ( N , arr ) { var S = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) S += arr [ i ] ; var P = 1 ; for ( i = 0 ; i < N ; i ++ ) { P *= i ; } for ( i = 0 ; i < N ; i ++ ) { var x = arr [ i ] ; var y = ( S - x ) / ( P / x - 1 ) ; if ( ( S - x + y ) == ( P * y ) / x ) return 1 ; } return 0 ; } var N = 3 ; var arr = [ 1 , 3 , 4 ] if ( canPossibleReplacement ( N , arr ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Check if all the digits of the given number are same | JavaScript program for the above approach Function to check if all the digits in the number N is the same or not ; Get the length of N ; Form the number M of the type K * 111. . . where K is the rightmost digit of N ; Check if the numbers are equal ; Otherwise ; Driver Code
function checkSameDigits ( N ) { var length = ( Math . log10 ( N ) ) + 1 ; var M = ( Math . pow ( 10 , length ) - 1 ) / ( 10 - 1 ) ; M *= N % 10 ; if ( M = N ) return " " ; return " " ; } var N = 222 ; document . write ( checkSameDigits ( N ) ) ;
Maximize sum of path from the Root to a Leaf node in N | Stores the maximum sum of a path ; Structure of a node in the tree ; Utility function to create a new node in the tree ; Recursive function to calculate the maximum sum in a path using DFS ; If current node is a leaf node ; Traversing all children of the current node ; Recursive call for all the children nodes ; Given Generic Tree ; Function Call
let maxSumPath = 0 ; class Node { constructor ( key ) { this . child = [ ] ; this . val = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return temp ; } function DFS ( root , sum ) { if ( root . child . length == 0 ) { maxSumPath = Math . max ( maxSumPath , sum ) ; return ; } for ( let i = 0 ; i < root . child . length ; i ++ ) { DFS ( root . child [ i ] , sum + root . child [ i ] . val ) ; } } let root = newNode ( 1 ) ; ( root . child ) . push ( newNode ( 2 ) ) ; ( root . child ) . push ( newNode ( 3 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 4 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 6 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 5 ) ) ; ( root . child [ 1 ] ) . child . push ( newNode ( 7 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 8 ) ) ; DFS ( root , root . val ) ; document . write ( maxSumPath ) ;
Count of even sum triplets in the array for Q range queries | Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Prleting the ans ; Given Input ; Function Call
function countTriplets ( size , queries , arr , Q ) { let arr_even = new Array ( size + 1 ) ; let arr_odd = new Array ( size + 1 ) ; let even = 0 ; let odd = 0 ; arr_even [ 0 ] = 0 ; arr_odd [ 0 ] = 0 ; for ( let i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } arr_even [ i + 1 ] = even ; arr_odd [ i + 1 ] = odd ; } for ( let i = 0 ; i < queries ; i ++ ) { let l = Q [ i ] [ 0 ] , r = Q [ i ] [ 1 ] ; odd = arr_odd [ r ] - arr_odd [ l - 1 ] ; even = arr_even [ r ] - arr_even [ l - 1 ] ; let ans = ( even * ( even - 1 ) * ( even - 2 ) ) / 6 + ( odd * ( odd - 1 ) / 2 ) * even ; document . write ( ans + " " ) ; } } let N = 6 , Q = 2 ; let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let queries = [ [ 1 , 3 ] , [ 2 , 5 ] ] ; countTriplets ( N , Q , arr , queries ) ;
Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; Printing rightmost element ; Given Input ; Function Call
function maxRightmostElement ( N , k , p , arr ) { while ( k ) { for ( let i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= p ) { arr [ i ] = arr [ i ] - p ; arr [ i + 1 ] = arr [ i + 1 ] + p ; break ; } } k -- ; } document . write ( arr [ N - 1 ] + " " ) ; } let N = 4 , p = 2 , k = 5 , arr = [ 3 , 8 , 1 , 4 ] ; maxRightmostElement ( N , k , p , arr ) ;
Mean of minimum of all possible K | Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code
function nCr ( n , r , f ) { if ( n < r ) { return 0 ; } return f [ n ] / ( f [ r ] * f [ n - r ] ) ; } function findMean ( N , X ) { let f = new Array ( N + 1 ) . fill ( 0 ) ; f [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) { f [ i ] = f [ i - 1 ] * i ; } let total = nCr ( N , X , f ) ; let count = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { count += nCr ( N - i , X - 1 , f ) * i ; } let E_X = count / total ; document . write ( parseFloat ( E_X ) . toFixed ( 9 ) ) ; return 0 ; } let N = 3 , X = 2 ; findMean ( N , X ) ;
Sum of product of all unordered pairs in given range with update queries | Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
function pairwiseProductSum ( a , l , r ) { let sum = 0 ; for ( let j = l - 1 ; j <= r - 1 ; j ++ ) { for ( let k = j + 1 ; k <= r - 1 ; k ++ ) { sum += a [ j ] * a [ k ] ; } } document . write ( sum + " " ) ; } function updateArray ( a , p , x ) { a [ p - 1 ] = x ; } function solveQueries ( a , n , Q , query ) { for ( let i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } let A = [ 5 , 7 , 2 , 3 , 1 ] ; let N = A . length ; let Q = 3 ; let query = [ [ 1 , 1 , 3 ] , [ 2 , 2 , 5 ] , [ 1 , 2 , 5 ] , ] ; solveQueries ( A , N , Q , query ) ;
Check if final remainder is present in original Array by reducing it based on given conditions | Javascript program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO
function Reduced ( a , n ) { let original_array = [ ] ; original_array = a ; while ( a . length != 2 ) { let mid = Math . floor ( a . length / 2 ) ; let mid_ele = a [ mid ] ; let start = a [ 0 ] ; a . shift ( ) ; let end = a [ a . length - 1 ] ; a . pop ( ) ; let rmd = ( start * end ) % mid_ele ; a . push ( rmd ) ; let remainder = ( a [ 0 ] * a [ 1 ] ) % n ; for ( let i = 0 ; i < original_array . length ; i ++ ) { if ( original_array [ i ] == remainder ) { return 1 ; } } } return 0 ; } let x = Reduced ( Arr , N ) ; if ( x ) document . write ( " " ) ; else document . write ( " " ) ;
Find two numbers from their sum and OR | Javascript program for the above approach ; Function to find the two integers from the given sum and Bitwise OR value ; Check if Z is non negative ; Iterate through all the bits ; Find the kth bit of A & B ; Find the kth bit of A | B ; If bit1 = 1 and bit2 = 0 , then there will be no possible pairs ; Print the possible pairs ; Driver Code
let MaxBit = 32 ; function possiblePair ( X , Y ) { let Z = Y - X ; if ( Z < 0 ) { document . write ( " " ) ; return 0 ; } for ( let k = 0 ; k < MaxBit ; k ++ ) { let bit1 = ( Z >> k ) & 1 ; let bit2 = ( Z >> k ) & 1 ; if ( bit1 && ! bit2 ) { document . write ( " " ) ; return 0 ; } } document . write ( Z + " " + X ) ; return 0 ; } let X = 7 , Y = 11 ; possiblePair ( X , Y ) ;
Maximum frequencies in each M | Function to find the frequency of the most common element in each M length subarrays ; Stores frequency of array element ; Stores the maximum frequency ; Iterate for the first sub - array and store the maximum ; Print the maximum frequency for the first subarray ; Iterate over the range [ M , N ] ; Subtract the A [ i - M ] and add the A [ i ] in the map ; Find the maximum frequency ; Print the maximum frequency for the current subarray ; Driver Code
function maxFrequencySubarrayUtil ( A , N , M ) { let m = new Map ( ) ; let val = 0 ; for ( let i = 0 ; i < M ; i ++ ) { if ( m . has ( A [ i ] ) ) { m . set ( m . get ( A [ i ] ) , m . get ( A [ i ] ) + 1 ) ; } else { m . set ( A [ i ] , 1 ) ; } val = Math . max ( val , m . get ( A [ i ] ) ) ; } document . write ( val + " " ) ; for ( i = M ; i < N ; i ++ ) { if ( m . has ( A [ i - m ] ) ) m . set ( m . get ( A [ i - M ] ) , m . get ( A [ i - M ] ) - 1 ) ; if ( m . has ( A [ i ] ) ) m . set ( m . get ( A [ i ] ) , m . get ( A [ i ] ) + 1 ) ; val = 0 ; for ( let [ key , value ] of m ) { val = Math . max ( val , value ) ; } document . write ( val + " " ) ; } } let A = [ 1 , 1 , 2 , 2 , 3 , 5 ] ; let N = A . length ; let M = 4 ; maxFrequencySubarrayUtil ( A , N , M ) ;
Maximum number of pairs of distinct array elements possible by including each element in only one pair | Function to count the maximum number of pairs having different element from the given array ; Stores the frequency of array element ; Stores maximum count of pairs ; Increasing the frequency of every element ; Stores the frequencies of array element from highest to lowest ; Iterate until size of PQ > 1 ; Stores the top two element ; Form the pair between the top two pairs ; Decrement the frequencies ; Insert updated frequencies if it is greater than 0 ; Return the total count of resultant pairs ; Driver Code
function maximumPairs ( a , n ) { var freq = new Map ( ) ; var count = 0 ; for ( var i = 0 ; i < n ; ++ i ) { if ( freq . has ( a [ i ] ) ) freq . set ( a [ i ] , freq . get ( a [ i ] ) + 1 ) else freq . set ( a [ i ] , 1 ) } var pq = [ ... freq . values ( ) ] ; pq . sort ( ( a , b ) => a - b ) ; while ( pq . length > 1 ) { var freq1 = pq [ pq . length - 1 ] ; pq . pop ( ) ; var freq2 = pq [ pq . length - 1 ] ; pq . pop ( ) ; count ++ ; freq1 -- ; freq2 -- ; if ( freq1 > 0 ) pq . push ( freq1 ) ; if ( freq2 > 0 ) pq . push ( freq2 ) ; pq . sort ( ( a , b ) => a - b ) ; } return count ; } var arr = [ 4 , 2 , 4 , 1 , 4 , 3 ] ; var N = arr . length ; document . write ( maximumPairs ( arr , N ) ) ;
Generate an N | Function to print target array ; Sort the given array ; Seeking for index of elements with minimum diff . ; Seeking for index ; To store target array ; Copying element ; Copying remaining element ; Printing target array ; Given Input ; Function Call
function printArr ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; let minDifference = Number . MAX_SAFE_INTEGER ; let minIndex = - 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( minDifference > Math . abs ( arr [ i ] - arr [ i - 1 ] ) ) { minDifference = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; minIndex = i - 1 ; } } let Arr = new Array ( n ) ; Arr [ 0 ] = arr [ minIndex ] ; Arr [ n - 1 ] = arr [ minIndex + 1 ] ; let pos = 1 ; for ( let i = minIndex + 2 ; i < n ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( let i = 0 ; i < minIndex ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { document . write ( Arr [ i ] + " " ) ; } } let N = 8 ; let arr = [ 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 ] ; printArr ( arr , N ) ;
Construct a Perfect Binary Tree from Preorder Traversal | Structure of the tree ; Function to create a new node with the value val ; Return the newly created node ; Function to create the Perfect Binary Tree ; If preStart > preEnd return NULL ; Initialize root as pre [ preStart ] ; If the only node is left , then return node ; Parameters for further recursion ; Recursive Call to build the subtree of root node ; Return the created root ; Function to build Perfect Binary Tree ; Function to print the Inorder of the given Tree ; Base Case ; Left Recursive Call ; Print the data ; Right Recursive Call ; Driver Code ; Function Call ; Print Inorder Traversal
class Node { constructor ( val ) { this . left = null ; this . right = null ; this . data = val ; } } function getNewNode ( val ) { let newNode = new Node ( val ) ; return newNode ; } function buildPerfectBT_helper ( preStart , preEnd , pre ) { if ( preStart > preEnd ) return null ; let root = getNewNode ( pre [ preStart ] ) ; if ( preStart == preEnd ) return root ; let leftPreStart = preStart + 1 ; let rightPreStart = leftPreStart + parseInt ( ( preEnd - leftPreStart + 1 ) / 2 ) ; let leftPreEnd = rightPreStart - 1 ; let rightPreEnd = preEnd ; root . left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre ) ; root . right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre ) ; return root ; } function buildPerfectBT ( pre , size ) { return buildPerfectBT_helper ( 0 , size - 1 , pre ) ; } function printInorder ( root ) { if ( root == null ) return ; printInorder ( root . left ) ; document . write ( root . data + " " ) ; printInorder ( root . right ) ; } let pre = [ 1 , 2 , 4 , 5 , 3 , 6 , 7 ] ; let N = pre . length ; let root = buildPerfectBT ( pre , N ) ; document . write ( " " ) ; printInorder ( root ) ;
Minimize operations to convert each node of N | Create adjacency list ; Function to add an edges in graph ; Function to perform the DFS of graph recursively from a given vertex u ; Check for the condition for the flipping of node 's initial value ; Traverse all the children of the current source node u ; Swap foo and foo1 signifies there is change of level ; Function to perform the DFSUtil ( ) for all the unvisited vertices ; Traverse the given set of nodes ; If the current node is unvisited ; Print the number of operations ; Function to count the number of flips required to change initial node values to final node value ; Add the given edges ; DFS Traversal ; Driver code
let N = 3 ; let adj = [ ] ; for ( let i = 0 ; i < N + 1 ; i ++ ) { adj . push ( [ ] ) ; } let visited ; let ans ; function addEdges ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function DFSUtil ( u , foo , foo1 , initial , finall ) { visited [ u ] = true ; if ( ( initial [ u - 1 ] ^ foo ) ^ finall [ u - 1 ] == true ) { ans += 1 ; foo ^= true ; } for ( let i = 0 ; i < adj [ u ] . length ; i ++ ) { if ( visited [ adj [ u ] [ i ] ] == false ) DFSUtil ( adj [ u ] [ i ] , foo1 , foo , initial , finall ) ; } } function DFS ( V , initial , finall ) { ans = 0 ; visited = new Array ( V ) ; visited . fill ( false ) ; for ( let u = 1 ; u <= 1 ; u ++ ) { if ( visited [ u ] == false ) DFSUtil ( u , 0 , 0 , initial , finall ) ; } document . write ( ans ) ; } function countOperations ( N , initial , finall , Edges ) { for ( let i = 0 ; i < N - 1 ; i ++ ) { addEdges ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } DFS ( N + 1 , initial , finall ) ; } let Edges = [ [ 1 , 2 ] , [ 1 , 3 ] ] ; let initial = [ true , true , false ] ; let finall = [ false , true , true ] ; countOperations ( N , initial , finall , Edges ) ;
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | Function to find the minimum number of moves to make N and M equal . ; Array to maintain the numbers included . ; pair of vertex , count ; run bfs from N ; if we reached goal ; Iterate in the range ; If i is a factor of aux ; If i is less than M - aux and is not included earlier . ; If aux / i is less than M - aux and is not included earlier . ; Not possible ; Driver Code
function countOperations ( N , M ) { let visited = new Array ( 100001 ) ; let Q = [ ] ; Q . push ( [ N , 0 ] ) ; visited [ N ] = true ; while ( Q . length > 0 ) { let aux = Q [ 0 ] [ 0 ] ; let cont = Q [ 0 ] [ 1 ] ; Q . shift ( ) ; if ( aux == M ) return cont ; for ( let i = 2 ; i * i <= aux ; i ++ ) if ( aux % i == 0 ) { if ( aux + i <= M && ! visited [ aux + i ] ) { Q . push ( [ aux + i , cont + 1 ] ) ; visited [ aux + i ] = true ; } if ( aux + parseInt ( aux / i , 10 ) <= M && ! visited [ aux + parseInt ( aux / i , 10 ) ] ) { Q . push ( [ aux + parseInt ( aux / i , 10 ) , cont + 1 ] ) ; visited [ aux + parseInt ( aux / i , 10 ) ] = true ; } } } return - 1 ; } let N = 4 , M = 24 ; document . write ( countOperations ( N , M ) ) ;
Finding Astronauts from different countries | Function to perform the DFS Traversal to find the count of connected components ; Marking vertex visited ; DFS call to neighbour vertices ; If the current node is not visited , then recursively call DFS ; Function to find the number of ways to choose two astronauts from the different countries ; Stores the Adjacency list ; Constructing the graph ; Stores the visited vertices ; Stores the size of every connected components ; DFS call to the graph ; Store size of every connected component ; Stores the total number of ways to count the pairs ; Traverse the array ; Print the value of ans ; Driver Code
let adj ; let visited ; let num ; function dfs ( v ) { visited [ v ] = true ; num ++ ; for ( let i = 0 ; i < adj [ v ] . length ; i ++ ) { if ( ! visited [ adj [ v ] [ i ] ] ) { dfs ( adj [ v ] [ i ] ) ; } } } function numberOfPairs ( N , arr ) { adj = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { adj . push ( [ ] ) ; } for ( let i = 0 ; i < arr . length ; i ++ ) { adj [ arr [ i ] [ 0 ] ] . push ( arr [ i ] [ 1 ] ) ; adj [ arr [ i ] [ 1 ] ] . push ( arr [ i ] [ 0 ] ) ; } visited = new Array ( N ) ; visited . fill ( false ) ; let v = [ ] ; num = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { dfs ( i ) ; v . push ( num ) ; num = 0 ; } } let ans = N * ( N - 1 ) / 2 ; for ( let i = 0 ; i < v . length ; i ++ ) { ans -= ( v [ i ] * ( v [ i ] - 1 ) / 2 ) ; } document . write ( ans ) ; } let N = 6 ; let arr = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 2 , 5 ] ] ; numberOfPairs ( N , arr ) ;
Count of Perfect Numbers in given range for Q queries | JavaScript program for the above approach ; Function to check whether a number is perfect Number ; Stores sum of divisors ; Itearate over the range [ 2 , sqrt ( N ) ] ; If sum of divisors is equal to N , then N is a perfect number ; Function to find count of perfect numbers in a given range ; Stores the count of perfect Numbers upto a every number less than MAX ; Iterate over the range [ 1 , MAX ] ; Traverse the array arr [ ] ; Print the count of perfect numbers in the range [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ; Driver Code
const MAX = 100005 ; function isPerfect ( N ) { let sum = 1 ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( i * i != N ) sum = sum + i + Math . floor ( N / i ) ; else sum = sum + i ; } } if ( sum == N && N != 1 ) return true ; return false ; } function Query ( arr , N ) { let prefix = new Array ( MAX + 1 ) . fill ( 0 ) ; for ( let i = 2 ; i <= MAX ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + isPerfect ( i ) ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( prefix [ arr [ i ] [ 1 ] ] - prefix [ arr [ i ] [ 0 ] - 1 ] + " " ) ; } } let arr = [ [ 1 , 1000 ] , [ 1000 , 2000 ] , [ 2000 , 3000 ] ] ; let N = arr . length ; Query ( arr , N ) ;
2 Keys Keyboard Problem | Function to find the minimum number of steps required to form N number of A 's ; Stores the count of steps needed ; Traverse over the range [ 2 , N ] ; Iterate while N is divisible by d ; Increment the value of ans by d ; Divide N by d ; If N is not 1 ; Return the ans ; Driver Code
function minSteps ( N ) { var ans = 0 ; for ( var d = 2 ; d * d <= N ; d ++ ) { while ( N % d == 0 ) { ans += d ; N /= d ; } } if ( N != 1 ) { ans += N ; } return ans ; } var N = 3 ; minSteps ( N ) ; document . write ( minSteps ( N ) ) ;
Find any possible two coordinates of Rectangle whose two coordinates are given | Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Given Input ; Function Call
function Create_Rectangle ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] . split ( " " ) } let p1 = [ - 1 , - 1 ] ; let p2 = [ - 1 , - 1 ] ; let p3 = [ ] ; let p4 = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ i ] [ j ] == ' ' ) if ( p1 [ 0 ] == - 1 ) p1 = [ i , j ] ; else p2 = [ i , j ] ; } } p3 = p1 ; p4 = p2 ; if ( p1 [ 0 ] == p2 [ 0 ] ) { p3 [ 0 ] = ( p1 [ 0 ] + 1 ) % n ; p4 [ 0 ] = ( p2 [ 0 ] + 1 ) % n ; } else if ( p1 [ 1 ] == p2 [ 1 ] ) { p3 [ 1 ] = ( p1 [ 1 ] + 1 ) % n ; p4 [ 1 ] = ( p2 [ 1 ] + 1 ) % n ; } else { let temp = p3 [ 0 ] ; p3 [ 0 ] = p4 [ 0 ] ; p4 [ 0 ] = temp ; } arr [ p3 [ 0 ] ] [ p3 [ 1 ] ] = ' ' ; arr [ p4 [ 0 ] ] [ p4 [ 1 ] ] = ' ' ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] . join ( " " ) + " " ) ; } } let n = 4 ; let arr = [ " " , " " , " " , " " ] ; Create_Rectangle ( arr , n ) ;
Sum of all the prime divisors of a number | Set 2 | Function to find sum of prime divisors of the given number N ; Add the number 2 if it divides N ; Traverse the loop from [ 3 , sqrt ( N ) ] ; If i divides N , add i and divide N ; This condition is to handle the case when N is a prime number greater than 2 ; Given Input ; Function Call
function SumOfPrimeDivisors ( n ) { let sum = 0 ; if ( n % 2 == 0 ) { sum = sum + 2 ; } while ( n % 2 == 0 ) { n = n / 2 ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { sum = sum + i ; } while ( n % i == 0 ) { n = n / i ; } } if ( n > 2 ) { sum = sum + n ; } return sum ; } let n = 10 ; document . write ( SumOfPrimeDivisors ( n ) ) ;