codeparrot/xlcost-text-to-code huggingface dataset:
Check if a line passes through the origin | JavaScript program to find if line passing through two coordinates also passes through origin or not ; Driver Code
function checkOrigin ( x1 , y1 , x2 , y2 ) { return ( x1 * ( y2 - y1 ) == y1 * ( x2 - x1 ) ) ; } if ( checkOrigin ( 1 , 28 , 2 , 56 ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Number of horizontal or vertical line segments to connect 3 points | Function to check if the third point forms a rectangle with other two points at corners ; Returns true if point k can be used as a joining point to connect using two line segments ; Check for the valid polyline with two segments ; Check whether the X - coordinates or Y - cocordinates are same . ; Iterate over all pairs to check for two line segments ; Otherwise answer is three . ; Driver code
function isBetween ( a , b , c ) { return ( Math . min ( a , b ) <= c && c <= Math . max ( a , b ) ) ; } function canJoin ( x , y , i , j , k ) { return ( x [ k ] == x [ i ] x [ k ] == x [ j ] ) && isBetween ( y [ i ] , y [ j ] , y [ k ] ) || ( y [ k ] == y [ i ] y [ k ] == y [ j ] ) && isBetween ( x [ i ] , x [ j ] , x [ k ] ) ; } function countLineSegments ( x , y ) { if ( ( x [ 0 ] == x [ 1 ] && x [ 1 ] == x [ 2 ] ) || ( y [ 0 ] == y [ 1 ] && y [ 1 ] == y [ 2 ] ) ) return 1 ; else if ( canJoin ( x , y , 0 , 1 , 2 ) || canJoin ( x , y , 0 , 2 , 1 ) || canJoin ( x , y , 1 , 2 , 0 ) ) return 2 ; else return 3 ; } let x = [ ] , y = [ ] ; x [ 0 ] = - 1 ; y [ 0 ] = - 1 ; x [ 1 ] = - 1 ; y [ 1 ] = 3 ; x [ 2 ] = 4 ; y [ 2 ] = 3 ; document . write ( countLineSegments ( x , y ) ) ;
Pythagorean Quadruple | function for checking ; Driver code
function pythagorean_quadruple ( a , b , c , d ) { let sum = a * a + b * b + c * c ; if ( d * d == sum ) return true ; else return false ; } let a = 1 , b = 2 , c = 2 , d = 3 ; if ( pythagorean_quadruple ( a , b , c , d ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find Perimeter / Circumference of Square and Rectangle | Javascript program to find Circumference of a square ; Driver Code
function Circumference ( a ) { return 4 * a ; } let a = 5 ; document . write ( " " + " " + Circumference ( a ) ) ;
Maximum area of quadrilateral | JavaScript program to find maximum are of a quadrilateral ; Calculating the semi - perimeter of the given quadrilateral ; Applying Brahmagupta 's formula to  get maximum area of quadrilateral ; Driver code
function maxArea ( a , b , c , d ) { let semiperimeter = ( a + b + c + d ) / 2 ; return Math . sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ; } let a = 1 , b = 2 , c = 1 , d = 2 ; document . write ( maxArea ( a , b , c , d ) ) ;
Direction of a Point from a Line Segment | structure for point in cartesian plane . ; constant integers for directions ; subtracting co - ordinates of point A from B and P , to make A as origin ; Determining cross Product ; return RIGHT if cross product is positive ; return LEFT if cross product is negative ; return ZERO if cross product is zero . ; Driver code ; A . y = 10 ; A ( - 30 , 10 ) ; B . y = - 15 ; B ( 29 , - 15 ) ; P . y = 28 ; P ( 15 , 28 )
class point { constructor ( ) { this . x = 0 ; this . y = 0 ; } } let RIGHT = 1 , LEFT = - 1 , ZERO = 0 ; function directionOfPoint ( A , B , P ) { B . x -= A . x ; B . y -= A . y ; P . x -= A . x ; P . y -= A . y ; let cross_product = B . x * P . y - B . y * P . x ; if ( cross_product > 0 ) return RIGHT ; if ( cross_product < 0 ) return LEFT ; return ZERO ; } let A = new point ( ) , B = new point ( ) , P = new point ( ) ; A . x = - 30 ; B . x = 29 ; P . x = 15 ; let direction = directionOfPoint ( A , B , P ) ; if ( direction == 1 ) document . write ( " " ) ; else if ( direction == - 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Find minimum radius such that atleast k point lie inside the circle | Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; driver function
function minRadius ( k , x , y , n ) { let dis = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; dis . sort ( ) ; return dis [ k - 1 ] ; } let k = 3 ; let x = [ 1 , - 1 , 1 ] ; let y = [ 1 , - 1 , - 1 ] ; let n = x . length ; document . write ( minRadius ( k , x , y , n ) ) ;
Program for Area And Perimeter Of Rectangle | Utility function ; Driver program
function areaRectangle ( a , b ) { let area = a * b ; return area ; } function perimeterRectangle ( a , b ) { let perimeter = 2 * ( a + b ) ; return perimeter ; } let a = 5 ; let b = 6 ; document . write ( " " + areaRectangle ( a , b ) + " " ) ; document . write ( " " + perimeterRectangle ( a , b ) ) ;
Program for Area Of Square | Javascript program to find the area of the square ; Driver Code
function areaSquare ( side ) { let area = side * side ; return area ; } let side = 4 ; document . write ( areaSquare ( side ) ) ;
Minimum Perimeter of n blocks | JavaScript program for the above approach ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver Code
function minPerimeter ( n ) { let l = Math . sqrt ( n ) ; let sq = l * l ; if ( sq == n ) return l * 4 ; else { let row = n / l ; let perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } let n = 10 ; document . write ( Math . floor ( minPerimeter ( n ) ) )
Find if it 's possible to rotate the page by an angle or not. | function to find if it 's possible  to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Points a , b , and c
function possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) { var dis1 = Math . pow ( b1 - a1 , 2 ) + Math . pow ( b2 - a2 , 2 ) ; var dis2 = Math . pow ( c1 - b1 , 2 ) + Math . pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) document . write ( " " ) ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b2 == ( ( a2 + c2 ) / 2.0 ) ) document . write ( " " ) ; else document . write ( " " ) ; } var a1 = 1 , a2 = 0 , b1 = 2 , b2 = 0 , c1 = 3 , c2 = 0 ; possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) ;
Check if two given circles touch or intersect each other | JavaScript program to check if two circles touch each other or not . ; Driver Code
function circle ( x1 , y1 , x2 , y2 , r1 , r2 ) { let distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; let radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; } let x1 = - 10 , y1 = 8 ; let x2 = 14 , y2 = - 24 ; let r1 = 30 , r2 = 10 ; let t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( t == 1 ) document . write ( " " + " " ) ; else if ( t < 0 ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Count of obtuse angles in a circle with ' k ' equidistant points between 2 given points | Javascript program to count number of obtuse angles for given two points ; There are two arcs connecting a and b . Let us count points on both arcs . ; Both arcs have same number of points ; Points on smaller arc is answer ; Driver Program to test above function
function countObtuseAngles ( a , b , k ) { var c1 = ( b - a ) - 1 ; var c2 = ( k - b ) + ( a - 1 ) ; if ( c1 == c2 ) return 0 ; return Math . min ( c1 , c2 ) ; } var k = 6 , a = 1 , b = 3 ; document . write ( countObtuseAngles ( a , b , k ) ) ;
Count of acute , obtuse and right triangles with given sides | Find the number of acute , right , obtuse triangle that can be formed from given array . ; Finding the square of each element of array . ; Sort the sides of array and their squares . ; x for acute triangles y for right triangles z for obtuse triangles ; Finding the farthest point p where a ^ 2 + b ^ 2 >= c ^ 2. ; Finding the farthest point q where a + b > c . ; If point p make right triangle . ; All triangle between j and p are acute triangles . So add p - j - 1 in x . ; Increment y by 1. ; All triangle between q and p are acute triangles . So add q - p in z . ; If no right triangle ; All triangle between j and p are acute triangles . So add p - j in x . ; All triangle between q and p are acute triangles . So add q - p in z . ; Driver Code
function findTriangle ( a , n ) { let b = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { b [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) b [ i ] = a [ i ] * a [ i ] ; a . sort ( function ( i , j ) { return i - j ; } ) ; b . sort ( function ( i , j ) { return i - j ; } ) ; let x = 0 , y = 0 , z = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let p = i + 1 ; let q = i + 1 ; for ( let j = i + 1 ; j < n ; j ++ ) { while ( p < n - 1 && b [ i ] + b [ j ] >= b [ p + 1 ] ) p ++ ; q = Math . max ( q , p ) ; while ( q < n - 1 && a [ i ] + a [ j ] > a [ q + 1 ] ) q ++ ; if ( b [ i ] + b [ j ] == b [ p ] ) { x += Math . max ( p - j - 1 , 0 ) ; y ++ ; z += q - p ; } else { x += Math . max ( p - j , 0 ) ; z += q - p ; } } } document . write ( " " + x + " " ) ; document . write ( " " + y + " " ) ; document . write ( " " + z + " " ) ; } let arr = [ 2 , 3 , 9 , 10 , 12 , 15 ] ; let n = arr . length ; findTriangle ( arr , n ) ;
Check whether a point exists in circle sector or not . | Javascript program to check if a point lies inside a circle sector . ; Calculate endAngle ; Calculate polar co - ordinates ; Check whether polarradius is less then radius of circle or not and Angle is between startAngle and endAngle or not ; Driver code
function checkPoint ( radius , x , y , percent , startAngle ) { let endAngle = 360 / percent + startAngle ; let polarradius = Math . sqrt ( x * x + y * y ) ; let Angle = Math . atan ( y / x ) ; if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) document . write ( " " + " " + x + " " + y + " " + " " ) ; else document . write ( " " + " " + x + " " + y + " " + " " ) ; } let radius = 8 , x = 3 , y = 4 ; let percent = 12 , startAngle = 0 ; checkPoint ( radius , x , y , percent , startAngle ) ;
Program to find the Type of Triangle from the given Coordinates | Javascript program to classify a given triangle ; Utility method to return square of x ; Utility method to sort a , b , c ; after this method a <= b <= c ; Utility method to return Square of distance between two points ; Method to classify side ; if all sides are equal ; if any two sides are equal ; Method to classify angle ; If addition of sum of square of two side is less , then acute ; by pythagoras theorem ; Method to classify the triangle by sides and angles ; Find squares of distances between points ; Sort all squares of distances in increasing order ; Driver code
class point { constructor ( x , y ) { this . x = x ; this . y = y ; } } function square ( x ) { return x * x ; } let a , b , c ; function order ( ) { let copy = new Array ( 3 ) ; copy [ 0 ] = a ; copy [ 1 ] = b ; copy [ 2 ] = c ; ( copy ) . sort ( function ( a , b ) { return a - b ; } ) ; a = copy [ 0 ] ; b = copy [ 1 ] ; c = copy [ 2 ] ; } function euclidDistSquare ( p1 , p2 ) { return square ( p1 . x - p2 . x ) + square ( p1 . y - p2 . y ) ; } function getSideClassification ( a , b , c ) { if ( a == b && b == c ) return " " ; else if ( a == b b == c ) return " " ; else return " " ; } function getAngleClassification ( a , b , c ) { if ( a + b > c ) return " " ; else if ( a + b == c ) return " " ; else return " " ; } function classifyTriangle ( p1 , p2 , p3 ) { a = euclidDistSquare ( p1 , p2 ) ; b = euclidDistSquare ( p1 , p3 ) ; c = euclidDistSquare ( p2 , p3 ) ; order ( ) ; document . write ( " " + getAngleClassification ( a , b , c ) + " " + getSideClassification ( a , b , c ) + " " ) ; } let p1 , p2 , p3 ; p1 = new point ( 3 , 0 ) ; p2 = new point ( 0 , 4 ) ; p3 = new point ( 4 , 7 ) ; classifyTriangle ( p1 , p2 , p3 ) ; p1 = new point ( 0 , 0 ) ; p2 = new point ( 1 , 1 ) ; p3 = new point ( 1 , 2 ) ; classifyTriangle ( p1 , p2 , p3 ) ;
Area of a polygon with given n ordered vertices | ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j is previous vertex to i ; Return absolute value ; Driver Code
function polygonArea ( X , Y , n ) { let area = 0.0 ; let j = n - 1 ; for ( let i = 0 ; i < n ; i ++ ) { area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ; j = i ; } return Math . abs ( area / 2.0 ) ; } let X = [ 0 , 2 , 4 ] ; let Y = [ 1 , 3 , 7 ] ; let n = 3 ; document . write ( polygonArea ( X , Y , n ) ) ;
Difference of count of distinct elements present to left and right for each array element | Function to find the difference of count of distince elements to the left and right for each array elements ; Stores distinct array element in the left and right ; Traverse the array ; Insert all element to the left in the set S1 ; Insert all element to the right in the set S2 ; Print the difference ; Driver Code
function findDifferenceArray ( arr , N ) { let S1 = new Set ( ) ; let S2 = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { S1 . add ( arr [ j ] ) ; } for ( let j = i + 1 ; j < N ; j ++ ) { S2 . add ( arr [ j ] ) ; } document . write ( Math . abs ( S1 . size - S2 . size ) + ' ' ) ; S1 . clear ( ) ; S2 . clear ( ) ; } } let arr = [ 7 , 7 , 3 , 2 , 3 ] ; let N = arr . length ; findDifferenceArray ( arr , N ) ;
Maximum subarray sum with same first and last element formed by removing elements | Function to find the maximum sum of sub - array ; Initialize the variables ; Traverse over the range ; Add the current value to the variable currsum for prefix sum ; Calculate the result ; Return the answer ; Driver Code
function maximumSubarraySum ( arr ) { let N = arr . length ; let memo = new Map ( ) ; let res = - 99999999 ; let currsum = 0 , currval = 0 ; for ( let i = 0 ; i < N ; ++ i ) { currval = arr [ i ] ; currsum = currsum + currval ; if ( memo . has ( currval ) ) { if ( currval > 0 ) res = Math . max ( res , currsum - memo . get ( currval ) + currval ) ; else res = Math . max ( res , currsum - memo . get ( currval ) + 2 * currval ) ; } else memo . set ( currval , currsum ) ; if ( currval < 0 ) currsum = currsum - currval ; } return res ; } let arr = [ - 1 , - 3 , 4 , 0 , - 1 , - 2 ] ; document . write ( maximumSubarraySum ( arr ) ) ;
Find Nth root of a number using Bisection method | Function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; The function that returns the root of given number ; Defining range on which answer can be found ; finding mid value ; Driver code
function f ( x , p , num ) { return Math . pow ( x , p ) - num ; } function f_prime ( x , p ) { return p * Math . pow ( x , p - 1 ) ; } function root ( num , p ) { let left = - num ; let right = num ; let x ; while ( true ) { x = ( left + right ) / 2.0 ; let value = f ( x , p , num ) ; let prime = f_prime ( x , p ) ; if ( value * prime <= 0 ) left = x ; else right = x ; if ( value < 0.000001 && value >= 0 ) { return x ; } } } let P = 1234321 ; let N = 2 ; let ans = Math . floor ( root ( P , N ) ) ; document . write ( ans ) ;
Count of permutations of an Array having maximum MEXs sum of prefix arrays | To calculate the factorial ; To return the number of permutations of an array with maximum MEXs sum of prefix array ; Map to store the frequency of each element ; Running a loop from i = 0 to i < n ; If continuity breaks , then break the loop ; Considering choices available to be filled at this position , i . e . mp [ i ] ; Decrement the count of remaining right elements ; Adding all permutations of the elements present to the right of the point where continuity breaks . ; Driver Code
function factorial ( n ) { let res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } function countPermutations ( ar , n ) { let mp = new Map ( ) ; let ans = 1 , cnt = n ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( ar [ i ] ) ) { mp . set ( ar [ i ] , mp . get ( ar [ i ] ) + 1 ) } else { mp . set ( ar [ i ] , 1 ) } } for ( let i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( i ) ) { break ; } ans = ( ans * mp . get ( i ) ) ; cnt -- ; } ans = ans * factorial ( cnt ) ; return ans ; } let arr = [ 1 , 0 , 1 ] ; let N = arr . length document . write ( countPermutations ( arr , N ) ) ;
Convert 0 to N by adding 1 or multiplying by 2 in minimum steps | Function to count number of set bits in N ; Stores the count of set bits ; If N is odd , then it a set bit ; Return the result ; Driver Code
function minimumAdditionOperation ( N ) { let count = 0 ; while ( N ) { if ( N & ( 1 == 1 ) ) { count ++ ; } N = N >> 1 ; } return count ; } let N = 6 ; document . write ( minimumAdditionOperation ( N ) ) ;
Count of unordered pairs of semi | JavaScript Program to implement the above approach ; Stores the count of distinct prime number in factor of current number ; Function to return the vector of semi prime numbers in range [ 1 , N ] ; Count of distinct prime number in the factor of current number using Sieve of Eratosthenes ; If current number is prime ; Stores the semi prime numbers ; If p has 2 distinct prime factors ; Return vector ; Function to count unordered pairs of semi prime numbers with prime sum ; Stores the final count ; Loop to iterate over al the l unordered pairs ; If sum of current semi prime numbers is a prime number ; Return answer ; Driver Code
const maxn = 100000 ; let prime = new Array ( maxn ) . fill ( 0 ) ; function semiPrimes ( N ) { for ( let p = 2 ; p <= maxn ; p ++ ) { if ( prime [ p ] == 0 ) { for ( let i = 2 * p ; i <= maxn ; i += p ) prime [ i ] ++ ; } } let sPrimes = [ ] ; for ( let p = 2 ; p <= N ; p ++ ) if ( prime [ p ] == 2 ) sPrimes . push ( p ) ; return sPrimes ; } function countPairs ( semiPrimes ) { let cnt = 0 ; for ( let i = 0 ; i < semiPrimes . length ; i ++ ) { for ( let j = i + 1 ; j < semiPrimes . length ; j ++ ) { if ( prime [ semiPrimes [ i ] + semiPrimes [ j ] ] == 0 ) { cnt ++ ; } } } return cnt ; } let N = 100 ; document . write ( countPairs ( semiPrimes ( N ) ) ) ;
Minimum divide by 2 operations required to make GCD odd for given Array | Function to find the minimum number of operations to make the GCD of the array odd ; Stores the minimum operations required ; Stores the powers of two for the current array element ; Dividing by 2 ; Increment the count ; Update the minimum operation required ; Return the result required ; Driver Code
function minimumOperations ( arr , N ) { let mini = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < N ; i ++ ) { let count = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = Math . floor ( arr [ i ] / 2 ) ; count ++ ; } if ( mini > count ) { mini = count ; } } return mini ; } let arr = [ 4 , 6 ] ; let N = arr . length ; document . write ( minimumOperations ( arr , N ) ) ;
Minimum size of the array with MEX as A and XOR of the array elements as B | Function to find the minimum size of array with given MEX and XOR ; Find the XOR of values from 0 to A - 1 ; If A is a multiple of 4 ; If A % 4 gives remainder 1 ; If A % 4 gives remainder 2 ; Initializing array size by A ; If XOR of all values of array is equal to B ; If the required integer to be added is equal to A ; Else any integer can be added ; Driver Code
function minimumSizeArr ( A , B ) { let currXor = 0 ; let reminder = ( A - 1 ) % 4 ; if ( reminder == 0 ) currXor = A - 1 ; else if ( reminder == 1 ) currXor = 1 ; else if ( reminder == 2 ) currXor = A ; let minSize = A ; if ( currXor == B ) return minSize ; else if ( currXor ^ ( B == A ) ) return minSize + 2 ; else return minSize + 1 ; } let A = 1 ; let B = 999 ; document . write ( minimumSizeArr ( A , B ) ) ;
Count of triplets till N whose product is at most N | Function to find number of triplets ( A , B , C ) having A * B * C <= N ; Stores the count of triplets ; Iterate a loop fixing the value of A ; Iterate a loop fixing the value of A ; Find the total count of triplets and add it to cnt ; Return the total triplets formed ; Driver Code
function countTriplets ( N ) { let cnt = 0 ; for ( let A = 1 ; A <= N ; ++ A ) { for ( let B = 1 ; B <= N / A ; ++ B ) { cnt += N / ( A * B ) ; } } return cnt ; } let N = 2 ; document . write ( countTriplets ( N ) ) ;
Largest integer upto N having greatest prime factor greater than its square root | Javascript program for the above approach ; Stores the Greatest Prime Factor ; Modified Sieve to find the Greatest Prime Factor of all integers in the range [ 1 , maxn ] ; Initialize the array with 0 ; Iterate through all values of i ; If i is not a prime number ; Update the multiples of i ; Function to find integer in the range [ 1 , N ] such that its Greatest Prime factor is greater than its square root ; Iterate through all values of i in the range [ N , 1 ] ; If greatest prime factor of i is greater than its sqare root ; Return answer ; If no valid integer exist ; Driver Code
const maxn = 100001 ; let gpf = new Array ( maxn ) ; function modifiedSieve ( ) { gpf . fill ( 0 ) ; gpf [ 0 ] = 0 ; gpf [ 1 ] = 1 ; for ( let i = 2 ; i < maxn ; i ++ ) { if ( gpf [ i ] > 0 ) continue ; for ( let j = i ; j < maxn ; j += i ) { gpf [ j ] = Math . max ( i , gpf [ j ] ) ; } } } function greatestValidInt ( N ) { modifiedSieve ( ) ; for ( let i = N ; i > 0 ; i -- ) { if ( gpf [ i ] > Math . sqrt ( i ) ) { return i ; } } return - 1 ; } let N = 25 ; document . write ( greatestValidInt ( N ) ) ;
Count of pass required to visit same index again by moving arr [ i ] to index arr [ i ] | Function to find the number of moves required to visit the same index again for every array element ; Make given array 0 index based ; Stores the number of moves ; Store index value ; Update the value of cnt ; Make a pass ; Print the value of cnt ; Driver Code
const numberOfPasses = ( arr , N ) => { for ( let i = 0 ; i < N ; ++ i ) { -- arr [ i ] ; } for ( let i = 0 ; i < N ; ++ i ) { let cnt = 0 ; let k = i ; do { ++ cnt ; k = arr [ k ] ; } while ( k != i ) ; document . write ( ` ${ cnt } ` ) ; } } let arr = [ 4 , 6 , 2 , 1 , 5 , 3 ] ; let N = arr . length ; numberOfPasses ( arr , N ) ;
Find subfactorial of a number | Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code
function subfactorial ( N ) { let res = 0 , fact = 1 ; let count = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { fact = fact * i ; if ( count % 2 == 0 ) res = res - 1 / fact ; else res = res + 1 / fact ; count ++ ; } return fact * ( 1 + res ) ; } let N = 4 ; document . write ( subfactorial ( N ) ) ;
Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B | Function to find the minimum number of operations to delete all elements of permutation A in order described by B ; Stores the count of operations ; Stores the index of current integer in B to be deleted ; Loop to iterate over all values of B ; Stores the current index in A ; Iterate over all values A ; If current integer of B and A equal , increment the index of the current integer of B ; As the permutation A has been traversed completely , increment the count of operations by 1 ; Return Answer ; Driver Code
function minOperations ( A , B , N ) { let cnt = 0 ; let i = 0 ; while ( i < N ) { let j = 0 ; while ( j < N && i < N ) { if ( B [ i ] == A [ j ] ) { i ++ ; } j ++ ; } cnt ++ ; } return cnt ; } let A = [ 2 , 4 , 6 , 1 , 5 , 3 ] ; let B = [ 6 , 5 , 4 , 2 , 3 , 1 ] ; let N = A . length ; document . write ( minOperations ( A , B , N ) ) ;
Count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | Function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; Stores the count of total pairs ; Iterate q value 1 to sqrt ( N ) ; Maximum possible value of p is min ( 2 * N - q , N / q ) ; P must be greater than or equal to q ; Total number of pairs are ; Adding all valid pairs to res ; Return total no of pairs ( x , y ) ; Driver Code
function countPairs ( N ) { let res = 0 ; for ( let q = 1 ; q * q <= N ; q ++ ) { let maxP = Math . min ( 2 * N - q , N / q ) ; if ( maxP < q ) continue ; let cnt = maxP - q + 1 ; res += Math . floor ( cnt / 2 + ( cnt & 1 ) ) ; } return res ; } let N = 3 ; document . write ( countPairs ( N ) ) ;