codeparrot/xlcost-text-to-code huggingface dataset:
Check if a given circle lies completely inside the ring formed by two concentric circles | Function to check if circle lies in the ring ; distance between center of circle center of concentric circles ( origin ) using Pythagoras theorem ; Condition to check if circle is strictly inside the ring ; Both circle with radius ' r ' and ' R ' have center ( 0 , 0 )
function checkcircle ( r , R , r1 , x1 , y1 ) { let dis = Math . sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; } let r = 8 , R = 4 , r1 = 2 , x1 = 6 , y1 = 0 ; if ( checkcircle ( r , R , r1 , x1 , y1 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program for Surface Area of Octahedron | Utility Function ; Driver Code
function surface_area_octahedron ( side ) { return ( 2 * ( Math . sqrt ( 3 ) ) * ( side * side ) ) ; } let side = 7 ; document . write ( " " + surface_area_octahedron ( side ) ) ;
Count of different straight lines with total n points with m collinear | Returns value of binomial coefficient Code taken from https : goo . gl / vhy4jp ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; function to calculate number of straight lines can be formed ; Driver Code
function nCk ( n , k ) { let C = new Array ( k + 1 ) ; C . fill ( 0 ) ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } function count_Straightlines ( n , m ) { return ( nCk ( n , 2 ) - nCk ( m , 2 ) + 1 ) ; } let n = 4 , m = 3 ; document . write ( count_Straightlines ( n , m ) ) ;
Calculate Volume of Dodecahedron | utility Function ; Driver Function
function vol_of_dodecahedron ( side ) { return ( ( ( 15 + ( 7 * ( Math . sqrt ( 5 ) ) ) ) / 4 ) * ( Math . pow ( side , 3 ) ) ) ; } let side = 4 ; document . write ( " " + vol_of_dodecahedron ( side ) . toFixed ( 2 ) ) ;
Program to check if water tank overflows when n solid balls are dipped in the water tank | function to find if tak will overflow or not ; cylinder capacity ; volume of water in tank ; volume of n balls ; total volume of water and n dipped balls ; condition to check if tank is in overflow state or not ; giving dimensions ; calling function
function overflow ( H , r , h , N , R ) { let tank_cap = 3.14 * r * r * H ; let water_vol = 3.14 * r * r * h ; let balls_vol = N * ( 4 / 3 ) * 3.14 * R * R * R ; let vol = water_vol + balls_vol ; if ( vol > tank_cap ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let H = 10 , r = 5 , h = 5 , N = 2 , R = 2 ; overflow ( H , r , h , N , R ) ;
Program to check if tank will overflow , underflow or filled in given time | function to calculate the volume of tank ; function to print overflow / filled / underflow accordingly ; radius of the tank ; height of the tank ; rate of flow of water ; time given ; calculate the required time ; printing the result
function volume ( radius , height ) { return ( ( 22 / 7 ) * radius * radius * height ) ; } function check_and_print ( required_time , given_time ) { if ( required_time < given_time ) document . write ( " " ) ; else if ( required_time > given_time ) document . write ( " " ) ; else document . write ( " " ) ; } let radius = 5 , height = 10 , rate_of_flow = 10 ; let given_time = 70.0 ; let required_time = volume ( radius , height ) / rate_of_flow ; check_and_print ( required_time , given_time ) ;
Program to find third side of triangle using law of cosines | Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum alet the series ; Holds the actual value of sin ( n ) ; Function to find third side ; Driver code ; function call
function cal_cos ( n ) { let accuracy = 0.0001 , x1 ; let denominator , cosx , cosval ; n = n * ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = Math . cos ( n ) ; let i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= Math . abs ( cosval - cosx ) ) ; return cosx ; } function third_side ( a , b , c ) { let angle = cal_cos ( c ) ; return Math . sqrt ( ( a * a ) + ( b * b ) - 2 * a * b * angle ) ; } let c = 49 ; let a = 5 , b = 8 ; document . write ( Math . round ( third_side ( a , b , c ) * 100000.0 ) / 100000.0 ) ;
Check whether given circle resides in boundary maintained by two other circles | function to check if given circle fit in boundary or not ; Distance from the center ; Checking the corners of circle ; driver program Radius of outer circle and inner circle respectively ; Co - ordinates and radius of the circle to be checked
function fitOrNotFit ( R , r , x , y , rad ) { var val = Math . sqrt ( Math . pow ( x , 2 ) + Math . pow ( y , 2 ) ) ; if ( val + rad <= R && val - rad >= R - r ) document . write ( " " ) ; else document . write ( " " ) ; } var R = 8 , r = 4 ; var x = 5 , y = 3 , rad = 3 ; fitOrNotFit ( R , r , x , y , rad ) ;
Number of Triangles that can be formed given a set of lines in Euclidean Plane | JavaScript program to find the number of triangles that can be formed using a set of lines in Euclidean Plane ; Double variables can ' t ▁ be ▁ checked ▁ precisely ▁ ▁ using ▁ ' == ' this function returns true if  the double variables are equal ; This function returns the number of triangles for a given set of lines ; Slope array stores the slope of lines ; Slope array is sorted so that all lines with same slope come together ; After sorting slopes , count different slopes . k is index in count [ ] . ; Count of current slope ; Calculating sum1 ( Sum of all slopes ) sum1 = m1 + m2 + ... ; Calculating sum2 . sum2 = m1 * m2 + m2 * m3 + ... ; Needed for sum3 ; Calculating sum3 which gives the final answer m1 * m2 * m3 + m2 * m3 * m4 + ... ; lines are stored as arrays of a , b and c for ' ax + by = c ' ; n is the number of lines
const EPSILON = 1.0842e-19 ; function compareDoubles ( A , B ) { var diff = A - B ; return diff < EPSILON && - diff < EPSILON ; } function numberOfTringles ( a , b , c , n ) { var slope = [ ] ; for ( var i = 0 ; i < n ; i ++ ) slope . push ( parseFloat ( a [ i ] * 1.0 ) / b [ i ] ) ; slope . sort ( ) ; var count = new Array ( n ) . fill ( 0 ) ; var k = 0 ; var this_count = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( compareDoubles ( parseFloat ( slope [ i ] ) , parseFloat ( slope [ i - 1 ] ) ) ) this_count ++ ; else { count [ k ++ ] = this_count ; this_count = 1 ; } } count [ k ++ ] = this_count ; var sum1 = 0 ; for ( var i = 0 ; i < k ; i ++ ) sum1 += count [ i ] ; var sum2 = 0 ; var temp = new Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < k ; i ++ ) { temp [ i ] = count [ i ] * ( sum1 - count [ i ] ) ; sum2 += temp [ i ] ; } sum2 /= 2 ; var sum3 = 0 ; for ( var i = 0 ; i < k ; i ++ ) sum3 += count [ i ] * ( sum2 - temp [ i ] ) ; sum3 /= 3 ; return sum3 ; } var a = [ 1 , 2 , 3 , 4 ] ; var b = [ 2 , 4 , 5 , 5 ] ; var c = [ 5 , 7 , 8 , 6 ] ; var n = a . length ; document . write ( " " + " " + numberOfTringles ( a , b , c , n ) ) ;
Program to find line passing through 2 Points | Function to find the line given two points ; Driver code
function lineFromPoints ( P , Q ) { var a = Q [ 1 ] - P [ 1 ] var b = P [ 0 ] - Q [ 0 ] var c = a * ( P [ 0 ] ) + b * ( P [ 1 ] ) if ( b < 0 ) document . write ( " " + " " + a + " " + b + " " + c + " " ) else document . write ( " " + " " + a + " " + b + " " + c + " " ) } var P = [ 3 , 2 ] var Q = [ 2 , 6 ] lineFromPoints ( P , Q )
Regular polygon using only 1 s in a binary numbered circle | method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver code to test above methods
function checkPolygonWithMidpoints ( arr , N , midpoints ) { for ( let j = 0 ; j < midpoints ; j ++ ) { let val = 1 ; for ( let k = j ; k < N ; k += midpoints ) { val &= arr [ k ] ; } if ( val && parseInt ( N / midpoints ) > 2 ) { document . write ( " " + parseInt ( N / midpoints ) + " " ) ; return true ; } } return false ; } function isPolygonPossible ( arr , N ) { let limit = Math . sqrt ( N ) ; for ( let i = 1 ; i <= limit ; i ++ ) { if ( N % i == 0 ) { if ( checkPolygonWithMidpoints ( arr , N , i ) || checkPolygonWithMidpoints ( arr , N , parseInt ( N / i ) ) ) return ; } } document . write ( " " ) ; } let arr = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ] ; let N = arr . length ; isPolygonPossible ( arr , N ) ;
Minimum lines to cover all points | Utility method to get gcd of a and b ; method returns reduced form of dy / dx as a pair ; get sign of result ; method returns minimum number of lines to cover all points where all lines goes through ( xO , yO ) ; set to store slope as a pair ; loop over all points once ; get x and y co - ordinate of current point ; if this slope is not there in set , increase ans by 1 and insert in set ; Driver code to test above methods
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function getReducedForm ( dy , dx ) { let g = gcd ( Math . abs ( dy ) , Math . abs ( dx ) ) ; let sign = ( dy < 0 ) ^ ( dx < 0 ) ; if ( sign ) { return [ Math . floor ( - Math . abs ( dy ) / g ) , Math . floor ( Math . abs ( dx ) / g ) ] ; } else return [ Math . floor ( Math . abs ( dy ) / g ) , Math . floor ( Math . abs ( dx ) / g ) ] ; } function minLinesToCoverPoints ( points , N , x0 , y0 ) { let st = new Set ( ) ; let temp ; let minLines = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let curX = points [ i ] [ 0 ] ; let curY = points [ i ] [ 1 ] ; temp = getReducedForm ( curY - yO , curX - xO ) ; if ( ! st . has ( temp . join ( " " ) ) ) { st . add ( temp . join ( " " ) ) ; minLines ++ ; } } return minLines ; } let xO , yO ; xO = 1 ; yO = 0 ; let points = [ [ - 1 , 3 ] , [ 4 , 3 ] , [ 2 , 1 ] , [ - 1 , - 2 ] , [ 3 , - 3 ] ] ; let N = points . length ; document . write ( minLinesToCoverPoints ( points , N , xO , yO ) )
Maximum height when coins are arranged in a triangle | Returns the square root of n . Note that the function ; We are using n itself as initial approximation . This can definitely be improved ; e decides the accuracy level ; Method to find maximum height of arrangement of coins ; calculating portion inside the square root ; Driver Code
function squareRoot ( n ) { let x = n ; let y = 1 ; let e = 0.000001 ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; } function findMaximumHeight ( N ) { let n = 1 + 8 * N ; let maxH = ( - 1 + squareRoot ( n ) ) / 2 ; return Math . round ( maxH ) ; } let N = 12 ; document . write ( findMaximumHeight ( N ) ) ;
Number of Integral Points between Two Points | Class to represent an Integral point on XY plane . ; Utility function to find GCD of two numbers GCD of a and b ; Finds the no . of Integral points between two given points . ; If line joining p and q is parallel to x axis , then count is difference of y values ; If line joining p and q is parallel to y axis , then count is difference of x values ; Driver program to test above
class Point { constructor ( a , b ) { this . x = a ; this . y = b ; } } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function getCount ( p , q ) { if ( p . x == q . x ) return Math . abs ( p . y - q . y ) - 1 ; if ( p . y == q . y ) return Math . abs ( p . x - q . x ) - 1 ; return gcd ( Math . abs ( p . x - q . x ) , Math . abs ( p . y - q . y ) ) - 1 ; } p = new Point ( 1 , 9 ) ; q = new Point ( 8 , 16 ) ; document . write ( " " + " " + p . x + " " + p . y + " " + q . x + " " + q . y + " " + getCount ( p , q ) ) ;
How to check if given four points form a square | A utility function to find square of distance from point ' p ' to point ' q ' ; This function returns true if ( p1 , p2 , p3 , p4 ) form a square , otherwise false ; let d2 = distSq ( p1 , p2 ) ; from p1 to p2 let d3 = distSq ( p1 , p3 ) ; from p1 to p3 let d4 = distSq ( p1 , p4 ) ; from p1 to p4 ; If lengths if ( p1 , p2 ) and ( p1 , p3 ) are same , then following conditions must met to form a square . 1 ) Square of length of ( p1 , p4 ) is same as twice the square of ( p1 , p2 ) 2 ) Square of length of ( p2 , p3 ) is same as twice the square of ( p2 , p4 ) ; The below two cases are similar to above case ; Driver program to test above function
function distSq ( p , q ) { return ( p . x - q . x ) * ( p . x - q . x ) + ( p . y - q . y ) * ( p . y - q . y ) ; } function isSquare ( p1 , p2 , p3 , p4 ) { if ( d2 == 0 d3 == 0 d4 == 0 ) return false ; if ( d2 == d3 && 2 * d2 == d4 && 2 * distSq ( p2 , p4 ) == distSq ( p2 , p3 ) ) { return true ; } if ( d3 == d4 && 2 * d3 == d2 && 2 * distSq ( p3 , p2 ) == distSq ( p3 , p4 ) ) { return true ; } if ( d2 == d4 && 2 * d2 == d3 && 2 * distSq ( p2 , p3 ) == distSq ( p2 , p4 ) ) { return true ; } return false ; } let p1 = { x : 20 , y : 10 } let p2 = { x : 10 , y : 20 } let p3 = { x : 20 , y : 20 } let p4 = { x : 10 , y : 10 } isSquare ( p1 , p2 , p3 , p4 ) ? document . write ( " " ) : document . write ( " " ) ;
Count triplets such that product of two numbers added with third number is N | Function to find the divisors of the number ( N - i ) ; Stores the resultant count of divisors of ( N - i ) ; Iterate over range [ 1 , sqrt ( N ) ] ; Return the total divisors ; Function to find the number of triplets such that A * B - C = N ; Loop to fix the value of C ; Adding the number of divisors in count ; Return count of triplets ; Driver Code
function countDivisors ( n ) { var divisors = 0 ; var i ; for ( i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) { divisors ++ ; } } if ( i - ( n / i ) == 1 ) { i -- ; } for ( ; i >= 1 ; i -- ) { if ( n % i == 0 ) { divisors ++ ; } } return divisors ; } function possibleTriplets ( N ) { var count = 0 ; for ( var i = 1 ; i < N ; i ++ ) { count += countDivisors ( N - i ) ; } return count ; } var N = 10 ; document . write ( possibleTriplets ( N ) ) ;
Maximize count of planes that can be stopped per second with help of given initial position and speed | Function to find maximum number of planes that can be stopped from landing ; Stores the times needed for landing for each plane ; Iterate over the arrays ; Stores the time needed for landing of current plane ; Update the value of t ; Append the t in set St ; Return the answer ; Driver Code
function maxPlanes ( A , B ) { let St = new Set ( ) ; for ( let i = 0 ; i < A . length ; i ++ ) { let t = ( A [ i ] % B [ i ] > 0 ) ? 1 : 0 ; t += Math . floor ( A [ i ] / B [ i ] ) + t ; St . add ( t ) ; } return St . size ; } let A = [ 1 , 3 , 5 , 4 , 8 ] ; let B = [ 1 , 2 , 2 , 1 , 2 ] ; document . write ( maxPlanes ( A , B ) ) ;
Find the player who will win by choosing a number in range [ 1 , K ] with sum total N | Function to predict the winner ; Given Input ; Function call
function predictTheWinner ( K , N ) { if ( N % ( K + 1 ) == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var K = 7 , N = 50 ; predictTheWinner ( K , N ) ;
Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Initializing ans to store Maximum valued rightmost element ; Calculating maximum value of Rightmost element ; returning rightmost element ; Given Input ; Function Call
function maxRightmostElement ( N , k , p , arr ) { var ans = arr [ N - 1 ] ; for ( var i = N - 2 ; i >= 0 ; i -- ) { var d = Math . min ( arr [ i ] / p , k / ( N - 1 - i ) ) ; k -= d * ( N - 1 - i ) ; ans += d * p ; } return ans ; } var N = 4 , k = 3.5 , p = 2 ; var arr = [ 3 , 8 , 1 , 4 ] ; document . write ( maxRightmostElement ( N , k , p , arr ) ) ;
Minimize the maximum element in constructed Array with sum divisible by K | Function to find smallest maximum number in an array whose sum is divisible by K . ; Minimum possible sum possible for an array of size N such that its sum is divisible by K ; If sum is not divisible by N ; If sum is divisible by N ; Driver code .
function smallestMaximum ( N , K ) { let sum = Math . floor ( ( N + K - 1 ) / K ) * K ; if ( sum % N != 0 ) return Math . floor ( sum / N ) + 1 ; else return Math . floor ( sum / N ) ; } let N = 4 ; let K = 3 ; document . write ( smallestMaximum ( N , K ) ) ;
Check if it is possible to construct an Array of size N having sum as S and XOR value as X | Function to find if any sequence is possible or not . ; Since , S is greater than equal to X , and either both are odd or even There always exists a sequence ; Only one case possible is S == X or NOT ; ; Considering the above conditions true , check if XOR of S ^ ( S - X ) is X or not ; Driver Code
function findIfPossible ( N , S , X ) { if ( S >= X && S % 2 == X % 2 ) { if ( N >= 3 ) { return " " ; } if ( N == 1 ) { if ( S == X ) { return " " ; } else { return " " ; } } if ( N == 2 ) { let C = ( S - X ) / 2 ; let A = C ; let B = C ; A = A + X ; if ( ( ( A ^ B ) == X ) ) { return " " ; } else { return " " ; } } } else { return " " ; } } let N = 3 , S = 10 , X = 4 ; document . write ( findIfPossible ( N , S , X ) ) ;
Check whether each Array element can be reduced to minimum element by replacing it with remainder with some X | Function to check if every integer in the array can be reduced to the minimum array element ; Stores the minimum array element ; Find the minimum element ; Traverse the array arr [ ] ; Stores the maximum value in the range ; Check whether mini lies in the range or not ; Otherwise , return Yes ; Driver code
function isPossible ( arr , n ) { var mini = Number . MAX_VALUE ; for ( var i = 0 ; i < n ; i ++ ) mini = Math . min ( mini , arr [ i ] ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == mini ) continue ; var Max = ( arr [ i ] + 1 ) / 2 - 1 ; if ( mini < 0 mini > Max ) return " " ; } return " " ; } var arr = [ 1 , 1 , 3 , 4 ] ; var N = arr . length ; document . write ( isPossible ( arr , N ) ) ;
Maximum number which can divide all array element after one replacement | Function to return gcd of two numbers ; If one of numbers is 0 then gcd is other number ; If both are equal then that value is gcd ; One is greater ; Function to return minimum sum ; Initialize min_sum with large value ; Initialize variable gcd ; Storing value of arr [ i ] in c ; Update maxGcd if gcd is greater than maxGcd ; returning the maximum divisor of all elements ; Driver Code
function gcdOfTwoNos ( num1 , num2 ) { if ( num1 == 0 ) return num2 ; if ( num2 == 0 ) return num1 ; if ( num1 == num2 ) return num1 ; if ( num1 > num2 ) return gcdOfTwoNos ( num1 - num2 , num2 ) ; return gcdOfTwoNos ( num1 , num2 - num1 ) ; } function Min_sum ( arr , N ) { let min_sum = 1000000 , maxGcd = 1 ; for ( let i = 0 ; i < N ; i ++ ) { let gcd ; if ( i == 0 ) gcd = arr [ 1 ] ; else { gcd = arr [ i - 1 ] ; } for ( let j = 0 ; j < N ; j ++ ) { if ( j != i ) gcd = gcdOfTwoNos ( gcd , arr [ j ] ) ; } let c = arr [ i ] ; if ( gcd > maxGcd ) maxGcd = gcd ; } return maxGcd ; } let arr = [ 16 , 5 , 10 , 25 ] ; let N = arr . length ; document . write ( Min_sum ( arr , N ) ) ;
Count of distinct N | Function to find the count of distinct odd integers with N digits using the given digits in the array arr [ ] ; Stores the factorial of a number ; Calculate the factorial of all numbers from 1 to N ; Stores the frequency of each digit ; Stores the final answer ; Loop to iterate over all values of Nth digit i and 1 st digit j ; If digit i does not exist in the given array move to next i ; Fixing i as Nth digit ; Stores the answer of a specific value of i and j ; If digit j does not exist move to the next j ; Fixing j as 1 st digit ; Calculate number of ways to arrange remaining N - 2 digits ; Including j back into the set of digits ; Including i back into the set of the digits ; Return Answer ; Driver Code ; Function Call
function countOddIntegers ( arr , N ) { let Fact = new Array ( N ) ; Fact [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) { Fact [ i ] = i * Fact [ i - 1 ] ; } let freq = new Array ( 10 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } let ans = 0 ; for ( let i = 1 ; i <= 9 ; i += 2 ) { if ( ! freq [ i ] ) { continue ; } freq [ i ] -- ; for ( let j = 1 ; j <= 9 ; j ++ ) { let cur_ans = 0 ; if ( freq [ j ] == 0 ) { continue ; } freq [ j ] -- ; cur_ans = Fact [ N - 2 ] ; for ( let k = 0 ; k <= 9 ; k ++ ) { cur_ans = Math . floor ( cur_ans / Fact [ freq [ k ] ] ) ; } ans = ans + cur_ans ; freq [ j ] ++ ; } freq [ i ] ++ ; } return ans ; } let A = [ 2 , 3 , 4 , 1 , 2 , 3 ] ; let N = A . length ; document . write ( countOddIntegers ( A , N ) ) ;
Count pairs in an array having sum of elements with their respective sum of digits equal | Function to find the sum of digits of the number N ; Stores the sum of digits ; If the number N is greater than 0 ; Return the sum ; Function to find the count of pairs such that arr [ i ] + sumOfDigits ( arr [ i ] ) is equal to ( arr [ j ] + sumOfDigits ( arr [ j ] ) ; Stores the frequency of value of arr [ i ] + sumOfDigits ( arr [ i ] ) ; Traverse the given array ; Find the value ; Increment the frequency ; Stores the total count of pairs ; Traverse the map mp ; Update the count of pairs ; Return the total count of pairs ; Driver Code
function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum = sum + ( N % 10 ) ; N = Math . floor ( N / 10 ) ; } return sum ; } function CountPair ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let val = arr [ i ] + sumOfDigits ( arr [ i ] ) ; if ( mp . has ( val ) ) { mp . set ( val , mp . get ( val ) + 1 ) ; } else { mp . set ( val , 1 ) ; } } let count = 0 ; for ( let [ key , value ] of mp ) { count = count + ( value * ( value - 1 ) ) / 2 ; } return count ; } let arr = [ 105 , 96 , 20 , 2 , 87 , 96 ] ; let N = arr . length ; document . write ( CountPair ( arr , N ) )
Longest subarray with GCD greater than 1 | Javascript program of the above approach ; Function to build the Segment Tree from the given array to process range queries in log ( N ) time ; Termination Condition ; Find the mid value ; Left and Right Recursive Call ; Update the Segment Tree Node ; Function to return the GCD of the elements of the Array from index l to index r ; Base Case ; Find the middle range ; Find the GCD and return ; Function to print maximum length of the subarray having GCD > one ; Stores the Segment Tree ; Function call to build the Segment tree from array arr [ ] ; Store maximum length of subarray ; Starting and ending pointer of the current window ; Case where the GCD of the current window is 1 ; Update the maximum length ; Print answer ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function build_tree ( b , seg_tree , l , r , vertex ) { if ( l == r ) { seg_tree [ vertex ] = b [ l ] ; return ; } let mid = Math . floor ( ( l + r ) / 2 ) ; build_tree ( b , seg_tree , l , mid , 2 * vertex ) ; build_tree ( b , seg_tree , mid + 1 , r , 2 * vertex + 1 ) ; seg_tree [ vertex ] = __gcd ( seg_tree [ 2 * vertex ] , seg_tree [ 2 * vertex + 1 ] ) ; } function range_gcd ( seg_tree , v , tl , tr , l , r ) { if ( l > r ) return 0 ; if ( l == tl && r == tr ) return seg_tree [ v ] ; let tm = Math . floor ( ( tl + tr ) / 2 ) ; return __gcd ( range_gcd ( seg_tree , 2 * v , tl , tm , l , Math . min ( tm , r ) ) , range_gcd ( seg_tree , 2 * v + 1 , tm + 1 , tr , Math . max ( tm + 1 , l ) , r ) ) ; } function maxSubarrayLen ( arr , n ) { let seg_tree = new Array ( 4 * n + 1 ) . fill ( 0 ) ; build_tree ( arr , seg_tree , 0 , n - 1 , 1 ) ; let maxLen = 0 ; let l = 0 , r = 0 ; while ( r < n && l < n ) { if ( range_gcd ( seg_tree , 1 , 0 , n - 1 , l , r ) == 1 ) { l ++ ; } maxLen = Math . max ( maxLen , r - l + 1 ) ; r ++ ; } document . write ( maxLen ) ; } let arr = [ 410 , 52 , 51 , 180 , 222 , 33 , 33 ] ; let N = arr . length ; maxSubarrayLen ( arr , N ) ;