codeparrot/xlcost-text-to-code huggingface dataset:
Find frequency of each element in a limited range array in less than O ( n ) time | A recursive function to count number of occurrences for each element in the array without traversing the whole array ; If element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; Find mid and recurse for left and right subarray ; A wrapper over recursive function findFrequencyUtil ( ) . It print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. Size of vector is maximum value ( which is last value in sorted array ) plus 1. ; Fill the vector with frequency ; Print the frequencies ; Driver Code
function findFrequencyUtil ( arr , low , high , freq ) { if ( arr [ low ] == arr [ high ] ) { freq [ arr [ low ] ] += high - low + 1 ; } else { let mid = Math . floor ( ( low + high ) / 2 ) ; findFrequencyUtil ( arr , low , mid , freq ) ; findFrequencyUtil ( arr , mid + 1 , high , freq ) ; } } function findFrequency ( arr , n ) { let freq = new Array ( arr [ n - 1 ] + 1 ) ; for ( let i = 0 ; i < arr [ n - 1 ] + 1 ; i ++ ) { freq [ i ] = 0 ; } findFrequencyUtil ( arr , 0 , n - 1 , freq ) ; for ( let i = 0 ; i <= arr [ n - 1 ] ; i ++ ) if ( freq [ i ] != 0 ) document . write ( " " + i + " " + freq [ i ] + " " ) ; } let arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] ; let n = arr . length ; findFrequency ( arr , n ) ;
Square root of an integer | Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver Code
function floorSqrt ( x ) { if ( x == 0 x == 1 ) return x ; let i = 1 ; let result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } let x = 11 ; document . write ( floorSqrt ( x ) ) ;
Maximum number of overlapping rectangles with at least one common point | Function to find the maximum number of overlapping rectangles ; Stores the maximum count of overlapping rectangles ; Stores the X and Y coordinates ; Iterate over all pairs of Xs and Ys ; Store the count for the current X and Y ; Update the maximum count of rectangles ; Returns the total count ; Driver Code
function maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) { let max_rectangles = 0 ; let X = [ ] , Y = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { X . push ( x1 [ i ] ) ; X . push ( x2 [ i ] - 1 ) ; Y . push ( y1 [ i ] ) ; Y . push ( y2 [ i ] - 1 ) ; } for ( let i = 0 ; i < X . length ; i ++ ) { for ( let j = 0 ; j < Y . length ; j ++ ) { let cnt = 0 ; for ( let k = 0 ; k < N ; k ++ ) { if ( X [ i ] >= x1 [ k ] && X [ i ] + 1 <= x2 [ k ] && Y [ j ] >= y1 [ k ] && Y [ j ] + 1 <= y2 [ k ] ) { cnt ++ ; } } max_rectangles = Math . max ( max_rectangles , cnt ) ; } } document . write ( max_rectangles ) ; } let x1 = [ 0 , 50 ] ; let y1 = [ 0 , 50 ] ; let x2 = [ 100 , 60 ] ; let y2 = [ 100 , 60 ] ; let N = x1 . length ; maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) ;
Form a Rectangle from boundary elements of Matrix using Linked List | Javascript program for above approach Node Class ; Constructor to initialize the node object ; Linked List class ; Constructor to initialize head ; function to form square linked list of matrix . ; initialising A [ 0 ] [ 0 ] as head . ; head is assigned to head . ; i is row index , j is column index ; loop till temp . top become equal to head . ; iterating over first i . e 0 th row and connecting node . ; iterating over last i . e ( m - 1 ) th column and connecting Node . ; iterating over last i . e ( n - 1 ) th row and connecting Node . ; iterating over first i . e 0 th column and connecting Node . ; function to print Linked list . ; printing head of linked list ; loop till temp . top become equal to head ; printing the node ; Driver Code ; n is number of rows ; m is number of column ; creation of object ; Call Quad method to create Linked List . ; Call printList method to print list .
class Node { constructor ( val ) { this . data = val this . next = null this . prev = null this . top = null this . bottom = null } } class LinkedList { constructor ( ) { this . head = null } Quad ( grid , n , m ) { this . head = new Node ( grid [ 0 ] [ 0 ] ) let temp = this . head let i = 0 let j = 1 while ( temp . top != this . head ) { if ( j < m && i == 0 ) { temp . next = new Node ( grid [ i ] [ j ] ) temp = temp . next j += 1 } else if ( j == m && i < n - 1 ) { i = i + 1 temp . bottom = new Node ( grid [ i ] [ j - 1 ] ) temp = temp . bottom } else if ( i == n - 1 && j <= m && j >= 1 ) { if ( j == m ) j = j - 1 j = j - 1 temp . prev = new Node ( grid [ i ] [ j ] ) temp = temp . prev } else if ( i <= n - 1 && j == 0 ) { i = i - 1 temp . top = new Node ( grid [ i ] [ j ] ) temp = temp . top if ( i == 1 ) temp . top = this . head } } } printList ( root ) { let temp = root document . write ( temp . data + " " ) while ( temp . top != root ) { if ( temp . next ) { document . write ( temp . next . data + " " ) temp = temp . next } if ( temp . prev ) { document . write ( temp . prev . data + " " ) temp = temp . prev } if ( temp . bottom ) { document . write ( temp . bottom . data + " " ) temp = temp . bottom } if ( temp . top ) { document . write ( temp . top . data + " " ) temp = temp . top } } } } let grid = [ [ 13 , 42 , 93 , 88 ] , [ 26 , 38 , 66 , 42 ] , [ 75 , 63 , 78 , 12 ] ] let n = grid . length let m = grid [ 0 ] . length let l = new LinkedList ( ) l . Quad ( grid , n , m ) l . printList ( l . head )
Check if it is possible to reach the point ( X , Y ) using distances given in an array | Function to check if the point ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , print Yes ; Driver Code
function isPossibleToReach ( A , N , X , Y ) { let distance = Math . sqrt ( ( X * X + Y * Y ) ) ; let mx = 0 ; for ( let i = 0 ; i < N ; i ++ ) { mx += A [ i ] ; } if ( mx < distance ) { document . write ( " " ) ; return 0 ; } if ( ( mx - distance ) < 0.000001 ) { document . write ( " " ) ; return 0 ; } for ( let i = 0 ; i < N ; i ++ ) { if ( distance + mx < 2 * A [ i ] ) { document . write ( " " ) ; return 0 ; } } document . write ( " " ) ; return 0 ; } let A = [ 2 , 5 ] ; let X = 5 , Y = 4 ; let N = A . length ; isPossibleToReach ( A , N , X , Y ) ;
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code
function canReach ( X , Y , steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - Math . pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - Math . pow ( 3 , steps ) , steps + 1 ) ) ; } let X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { document . write ( " " ) ; } else document . write ( " " ) ;
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code
function canReach ( X , Y ) { var steps = 0 ; while ( X != 0 Y != 0 ) { var pos1 = X % 3 ; var pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } var X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to calculate Surface Area of Ellipsoid | Function to find the surface area of the given Ellipsoid ; Formula to find surface area of an Ellipsoid ; Print the area ; Driver Code
function findArea ( a , b , c ) { let area = 4 * 3.141592653 * Math . pow ( ( Math . pow ( a * b , 1.6 ) + Math . pow ( a * c , 1.6 ) + Math . pow ( b * c , 1.6 ) ) / 3 , 1 / 1.6 ) ; document . write ( area . toPrecision ( 6 ) ) ; } let A = 11 , B = 12 , C = 13 ; findArea ( A , B , C ) ;
Descartes ' Circle Theorem with implementation | Function to find the fourth circle 's when three radius are given ; Radius of three circles ; Calculation of r4 using formula given above
function findRadius ( r1 , r2 , r3 ) { let r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * Math . sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; return r4 . toFixed ( 6 ) ; } let r1 = 1 ; let r2 = 1 ; let r3 = 1 ; let r4 = findRadius ( r1 , r2 , r3 ) ; document . write ( " " + r4 ) ;
Make N pairs from Array as ( X , Y ) coordinate point that are enclosed inside a minimum area rectangle | Function to make N pairs of coordinates such that they are enclosed in a minimum area rectangle with sides parallel to the X and Y axes ; A variable to store the answer ; For the case where the maximum and minimum are in different partitions ; For the case where the maximum and minimum are in the same partition ; Return the answer ; Given Input ; Function call
function minimumRectangleArea ( A , N ) { let ans ; A . sort ( ) ; ans = ( A [ N - 1 ] - A [ 0 ] ) * ( A [ 2 * N - 1 ] - A [ N ] ) ; for ( let i = 1 ; i < N ; i ++ ) ans = Math . min ( ans , ( A [ 2 * N - 1 ] - A [ 0 ] ) * ( A [ i + N - 1 ] - A [ i ] ) ) ; return ans ; } let A = [ 2 , 4 , 1 , 5 , 3 , 6 , 7 , 8 ] ; let N = A . length N = Math . floor ( N / 2 ) ; document . write ( minimumRectangleArea ( A , N ) + " " ) ;
Program to find the length of Latus Rectum of a Hyperbola | Function to calculate the length of the latus rectum of a hyperbola ; Store the length of major axis ; Store the length of minor axis ; Store the length of the latus rectum ; Return the length of the latus rectum ; Driver Code
function lengthOfLatusRectum ( A , B ) { var major = 2.0 * A ; var minor = 2.0 * B ; var latus_rectum = ( minor * minor ) / major ; return latus_rectum ; } var A = 3.0 , B = 2.0 ; document . write ( lengthOfLatusRectum ( A , B ) ) ;
Length of intercept cut off from a line by a Circle | Function to find the radius of a circle ; g and f are the coordinates of the center ; Case of invalid circle ; Apply the radius formula ; Function to find the perpendicular distance between circle center and the line ; Store the coordinates of center ; Stores the perpendicular distance between the line and the point ; Invalid Case ; Return the distance ; Function to find the length of intercept cut off from a line by a circle ; Calculate the value of radius ; Calculate the perpendicular distance between line and center ; Invalid Case ; If line do not cut circle ; Print the intercept length ; Given Input ; Function Call
function radius ( a , b , c ) { let g = a / 2 ; let f = b / 2 ; if ( g * g + f * f - c < 0 ) return ( - 1 ) ; return ( Math . sqrt ( g * g + f * f - c ) ) ; } function centerDistanceFromLine ( a , b , i , j , k ) { let g = a / 2 ; let f = b / 2 ; let distance = Math . abs ( i * g + j * f + k ) / ( Math . sqrt ( i * i + j * j ) ) ; if ( distance < 0 ) return ( - 1 ) ; return distance ; } function interceptLength ( a , b , c , i , j , k ) { let rad = radius ( a , b , c ) ; let dist = centerDistanceFromLine ( a , b , i , j , k ) ; if ( rad < 0 dist < 0 ) { document . write ( " " ) ; return ; } if ( dist > rad ) { document . write ( " " ) ; } else document . write ( 2 * Math . sqrt ( rad * rad - dist * dist ) ) ; } let a = 0 , b = 0 , c = - 4 ; let i = 2 , j = - 1 , k = 1 ; interceptLength ( a , b , c , i , j , k ) ;
Determine position of two points with respect to a 3D plane | Function to check position of two points with respect to a plane in 3D ; Put coordinates in plane equation ; If both values have same sign ; If both values have different sign ; If both values are zero ; If either of the two values is zero ; Given Input ; Coordinates of points ; Function Call
function check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) { var value_1 = a * x1 + b * y1 + c * z1 + d ; var value_2 = a * x2 + b * y2 + c * z2 + d ; if ( ( value_1 > 0 && value_2 > 0 ) || ( value_1 < 0 && value_2 < 0 ) ) document . write ( " " ) ; if ( ( value_1 > 0 && value_2 < 0 ) || ( value_1 < 0 && value_2 > 0 ) ) document . write ( " " ) ; if ( value_1 == 0 && value_2 == 0 ) document . write ( " " ) ; if ( value_1 == 0 && value_2 != 0 ) document . write ( " " ) ; if ( value_1 != 0 && value_2 == 0 ) document . write ( " " ) ; } var a = 1 , b = 2 , c = 3 , d = 4 ; var x1 = - 2 , y1 = - 2 , z1 = 1 ; var x2 = - 4 , y2 = 11 , z2 = - 1 ; check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) ;
Find the angle between tangents drawn from a given external point to a Circle | Function to find the distance between center and the exterior point ; Find the difference between the x and y coordinates ; Using the distance formula ; Function to find the angle between the pair of tangents drawn from the point ( X2 , Y2 ) to the circle . ; Calculate the distance between the center and exterior point ; Invalid Case ; Find the angle using the formula ; Print the resultant angle ; Driver code
function point_distance ( x1 , y1 , x2 , y2 ) { var p = ( x2 - x1 ) ; var q = ( y2 - y1 ) ; var distance = Math . sqrt ( p * p + q * q ) ; return distance ; } function tangentAngle ( x1 , y1 , x2 , y2 , radius ) { var distance = point_distance ( x1 , y1 , x2 , y2 ) ; if ( radius / distance > 1 radius / distance < - 1 ) { document . write ( - 1 + " " ) ; } var result = 2 * Math . asin ( radius / distance ) * 180 / 3.1415 ; document . write ( result . toFixed ( 4 ) + " " ) ; } var radius = 4 ; var x1 = 7 , y1 = 12 ; var x2 = 3 , y2 = 4 ; tangentAngle ( x1 , y1 , x2 , y2 , radius ) ;
Ratio of area of two nested polygons formed by connecting midpoints of sides of a regular N | Function to calculate the ratio of area of N - th and ( N + 1 ) - th nested polygons formed by connecting midpoints ; Stores the value of PI ; Calculating area the factor ; Printing the ratio precise upto 6 decimal places ; Driver Code
function AreaFactor ( n ) { let pi = 3.14159265 ; let areaf = ( 1 / ( Math . cos ( pi / n ) * Math . cos ( pi / n ) ) ) ; document . write ( areaf . toFixed ( 6 ) ) ; } let n = 4 ; AreaFactor ( n ) ;
Find the length of Kth N | Javascript program for the above approach ; Function to calculate the interior angle of a N - sided regular polygon ; Function to find the K - th polygon formed inside the ( K - 1 ) th polygon ; Stores the interior angle ; Stores the side length of K - th regular polygon ; Return the length ; Driver Code
const PI = 3.14159265 function findInteriorAngle ( n ) { return ( n - 2 ) * PI / n ; } function calculateSideLength ( L , N , K ) { let angle = findInteriorAngle ( N ) ; let length = L * Math . pow ( Math . sin ( angle / 2 ) , ( K - 1 ) ) ; return length ; } let N = 5 let L = 21 let K = 7 ; document . write ( calculateSideLength ( L , N , K ) )
Angle between a Pair of Lines | JavaScript program for the above approach ; Function to find the angle between two lines ; Store the tan value of the angle ; Calculate tan inverse of the angle ; Convert the angle from radian to degree ; Print the result ; Driver Code
const PI = 3.14159265 ; function findAngle ( M1 , M2 ) { var angle = Math . abs ( ( M2 - M1 ) / ( 1 + M1 * M2 ) ) ; var ret = Math . atan ( angle ) ; var val = ( ret * 180 ) / PI ; document . write ( val . toFixed ( 4 ) ) ; } var M1 = 1.75 , M2 = 0.27 ; findAngle ( M1 , M2 ) ;
Angle of intersection of two circles having their centers D distance apart | Function to find the cosine of the angle of the intersection of two circles with radius R1 and R2 ; Return the cosine of the angle ; Driver Code
function angle ( R1 , R2 , D ) { var ans = ( R1 * R1 + R2 * R2 - D * D ) / ( 2 * R1 * R2 ) ; return ans ; } var R1 = 3 , R2 = 4 ; var D = 5 ; document . write ( angle ( R1 , R2 , D ) ) ;
Check if any point exists in a plane whose Manhattan distance is at most K from N given points | Function to check if there exists any point with at most K distance from N given points ; Traverse the given N points ; Stores the count of pairs of coordinates having Manhattan distance <= K ; For the same coordinate ; Calculate Manhattan distance ; If Manhattan distance <= K ; If all coordinates can meet ; If all coordinates can 't meet ; Driver Code
function find ( a , b , N , K ) { for ( let i = 0 ; i < N ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < N ; j ++ ) { if ( i == j ) { continue ; } let dis = Math . abs ( a [ i ] - a [ j ] ) + Math . abs ( b [ i ] - b [ j ] ) ; if ( dis <= K ) { count ++ ; } if ( count == N - 1 ) { return " " ; } } } return " " ; } let N = 5 ; let A = [ 1 , 0 , 2 , 1 , 1 ] ; let B = [ 1 , 1 , 1 , 0 , 2 ] ; let K = 1 ; document . write ( find ( A , B , N , K ) ) ;
Minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes | Function to find the minimum area of triangle formed by any tangent to ellipse with the coordinate axes ; Stores the minimum area ; Print the calculated area ; Driver Code
function minimumTriangleArea ( a , b ) { var area = a * b console . log ( area ) } var a = 1 var b = 2 minimumTriangleArea ( a , b )
Find interior angles for each side of a given Cyclic Quadrilateral | Function to find the interior angles of the cyclic quadrilateral ; Stores the numerator and the denominator to find angle A ; Stores the numerator and the denominator to find angle B ; Stores the numerator and the denominator to find angle C : ; Stores the numerator and the denominator to find angle D : ; Driver Code
function findAngles ( a , b , c , d ) { var numerator = a * a + d * d - b * b - c * c var denominator = 2 * ( a * b + c * d ) var x = numerator / denominator document . write ( " " , Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100 ) / 100.0 , " " ) ; document . write ( " " ) ; numerator = a * a + b * b - c * c - d * d x = numerator / denominator document . write ( " " , Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100 ) / 100.0 , " " ) ; document . write ( " " ) ; numerator = c * c + b * b - a * a - d * d x = numerator / denominator document . write ( " " , Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100 ) / 100.0 , " " ) ; document . write ( " " ) ; numerator = d * d + c * c - a * a - b * b x = numerator / denominator document . write ( " " , Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100 ) / 100.0 , " " ) ; } var A = 10 var B = 15 var C = 20 var D = 25 findAngles ( A , B , C , D )
Number of smaller circles that can be inscribed in a larger circle | Function to count number of smaller circles that can be inscribed in the larger circle touching its boundary ; If R2 is greater than R1 ; Stores the angle made by the smaller circle ; Stores the ratio of R2 / ( R1 - R2 ) ; Stores the count of smaller circles that can be inscribed ; Stores the ratio ; If the diameter of smaller circle is greater than the radius of the larger circle ; Otherwise ; Find the angle using formula ; Divide 360 with angle and take the floor value ; Return the final result ; Driver Code
function countInscribed ( R1 , R2 ) { if ( R2 > R1 ) return 0 ; let angle ; let ratio ; let number_of_circles = 0 ; ratio = R2 / ( R1 - R2 ) ; if ( R1 < 2 * R2 ) { number_of_circles = 1 ; } else { angle = Math . abs ( Math . asin ( ratio ) * 180 ) / 3.14159265 ; number_of_circles = 360 / ( 2 * Math . floor ( angle ) ) ; } return number_of_circles ; } let R1 = 3 ; let R2 = 1 ; document . write ( countInscribed ( R1 , R2 ) )
Count pairs of coordinates connected by a line with slope in the range [ | Function to find the number of pairs of points such that the line passing through them has a slope in the range [ - k , k ] ; Store the result ; Traverse through all the combination of points ; If pair satisfies the given condition ; Increment ans by 1 ; Print the result ; Driver code ; Function Call
function findPairs ( x , y , K ) { let n = x . length ; let ans = 0 ; for ( let i = 0 ; i < n ; ++ i ) { for ( let j = i + 1 ; j < n ; ++ j ) { if ( K * Math . abs ( x [ i ] - x [ j ] ) >= Math . abs ( y [ i ] - y [ j ] ) ) { ++ ans ; } } } document . write ( ans ) ; } let X = [ 2 , 1 , 0 ] , Y = [ 1 , 2 , 0 ] ; let K = 1 ; findPairs ( X , Y , K ) ;
Number of largest circles that can be inscribed in a rectangle | Function to count the number of largest circles in a rectangle ; If length exceeds breadth ; Swap to reduce length to smaller than breadth ; Return total count of circles inscribed ; Driver Code
function totalCircles ( L , B ) { if ( L > B ) { var temp = L ; L = B ; B = temp ; } return B / L ; } var L = 3 ; var B = 8 ; document . write ( totalCircles ( L , B ) . toString ( ) . split ( ' ' ) [ 0 ] ) ;
Program to calculate length of diagonal of a square | Function to find the length of the diagonal of a square of a given side ; Driver Code
function findDiagonal ( s ) { return Math . sqrt ( 2 ) * s ; } var S = 10 ; document . write ( findDiagonal ( S ) . toFixed ( 6 ) ) ;
Area of largest isosceles triangle that can be inscribed in an Ellipse whose vertex coincides with one extremity of the major axis | Function to calculate area of the isosceles triangle ; If a and b are negative ; Stores the area of the triangle ; Print the area ; Given value of a & b ; Function call to find the area of the isosceles triangle
function triangleArea ( a , b ) { if ( a < 0 b < 0 ) { document . write ( - 1 ) ; return ; } var area = ( 3 * Math . sqrt ( 3 ) * a * b ) / ( 4 ) ; document . write ( area . toFixed ( 5 ) ) ; } var a = 1 , b = 2 ; triangleArea ( a , b ) ;
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides | Javascript implementation of above approach ; Function to find answer of each query ; Stores the count of points with sum less than or equal to their indices ; Traverse the array ; If both x and y - coordinate < 0 ; Stores the sum of co - ordinates ; Increment count of sum by 1 ; Prefix array ; Perform queries ; Drivers Code
let MAX = ( 1e6 + 5 ) function query ( arr , Q ) { let pre = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) pre [ i ] = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] [ 0 ] < 0 arr [ i ] [ 1 ] < 0 ) continue ; let sum = Math . ceil ( ( arr [ i ] [ 0 ] + arr [ i ] [ 1 ] ) ) ; pre [ sum ] ++ ; } for ( let i = 1 ; i < MAX ; i ++ ) pre [ i ] += pre [ i - 1 ] ; for ( let i = 0 ; i < Q . length ; i ++ ) { document . write ( pre [ Q [ i ] ] + " " ) ; } document . write ( " " ) ; } let arr = [ [ 2.1 , 3.0 ] , [ 3.7 , 1.2 ] , [ 1.5 , 6.5 ] , [ 1.2 , 0.0 ] ] ; let Q = [ 2 , 8 , 5 ] ; let N = arr . length ; let M = Q . length ; query ( arr , Q ) ;
Perimeter of the Union of Two Rectangles | Function to check if two rectangles are intersecting or not ; If one rectangle is to the right of other 's right edge ; If one rectangle is on the top of other 's top edge ; Function to return the perimeter of the Union of Two Rectangles ; Stores the resultant perimeter ; If rectangles do not interesect ; Perimeter of Rectangle 1 ; Perimeter of Rectangle 2 ; If the rectangles intersect ; Get width of combined figure ; Get length of combined figure ; Return the perimeter ; Driver Code
function doIntersect ( X , Y ) { if ( X [ 0 ] > X [ 3 ] X [ 2 ] > X [ 1 ] ) return false ; if ( Y [ 0 ] > Y [ 3 ] Y [ 2 ] > Y [ 1 ] ) return false ; return true ; } function getUnionPerimeter ( X , Y ) { let perimeter = 0 ; if ( ! doIntersect ( X , Y ) ) { perimeter += 2 * ( Math . abs ( X [ 1 ] - X [ 0 ] ) + Math . abs ( Y [ 1 ] - Y [ 0 ] ) ) ; perimeter += 2 * ( Math . abs ( X [ 3 ] - X [ 2 ] ) + Math . abs ( Y [ 3 ] - Y [ 2 ] ) ) ; } else { let w = Math . max ( ... X ) - Math . min ( ... X ) ; let l = Math . max ( ... Y ) - Math . min ( ... Y ) ; perimeter = 2 * ( l + w ) ; } return perimeter ; } let X = [ - 1 , 2 , 4 , 6 ] ; let Y = [ 2 , 5 , 3 , 7 ] ; document . write ( getUnionPerimeter ( X , Y ) ) ;