codeparrot/xlcost-text-to-code huggingface dataset:
Count array elements having modular inverse under given prime number P equal to itself | Function to get the count of elements that satisfy the given condition . ; Stores count of elements that satisfy the condition ; Traverse the given array . ; If square of current element is equal to 1 ; Driver Code
function equvInverse ( arr , N , P ) { let cntElem = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] * arr [ i ] ) % P == 1 ) { cntElem ++ ; } } return cntElem ; } let arr = [ 1 , 6 , 4 , 5 ] ; let N = arr . length ; let P = 7 ; document . write ( equvInverse ( arr , N , P ) ) ;
Count ways to split array into K non | Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver Code
function power ( K , M ) { var res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } function cntWays ( arr , N , K ) { var cntways = 0 ; var M = 0 ; var st = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } M = st . size ; cntways = power ( K , M ) ; return cntways ; } var arr = [ 2 , 3 ] ; var N = arr . length ; var K = 2 ; document . write ( cntWays ( arr , N , K ) ) ;
Minimize product of first N | JavaScript program for the above approach ; Function to find the minimum product of 1 to N - 1 after performing the given operations ; Initialize ans with 1 ; Multiply ans with N - 2 ( ( N - 4 ) / 2 ) times ; Multiply ans with N - 1 and N - 2 once ; Print ans ; Given Number N ; Function Call
let mod = 1e9 + 7 ; function minProduct ( n ) { let ans = 1 ; for ( let i = 1 ; i <= Math . floor ( ( n - 4 ) / 2 ) ; i ++ ) { ans = ( 1 * ans * ( n - 2 ) ) % mod ; } ans = ( 1 * ans * ( n - 2 ) * ( n - 1 ) ) % mod ; document . write ( ans + " " ) ; } let N = 8 ; minProduct ( N ) ;
Bitwise XOR of all unordered pairs from a given array | Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver Code
function TotalXorPair ( arr , N ) { let totalXOR = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { totalXOR ^= arr [ i ] ^ arr [ j ] ; } } return totalXOR ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; document . write ( TotalXorPair ( arr , N ) ) ;
Count permutations of all integers upto N that can form an acyclic graph based on given conditions | Find the count of possible graphs ; Driver Code
function possibleAcyclicGraph ( N ) { document . write ( Math . pow ( 2 , N - 1 ) ) ; return ; } let N = 4 ; possibleAcyclicGraph ( N ) ;
Minimize positive product of two given numbers by at most N decrements | Function to minimize the product of two numbers ; Reducing X , N times , minimizes the product ; Reduce X to 1 and reduce remaining N from Y ; Reducing Y , N times , minimizes the product ; Reduce Y to 1 and reduce remaining N from X ; Driver Code
function minProd ( X , Y , N ) { if ( X <= Y ) { if ( N < X ) return ( X - N ) * Y ; else { return Math . max ( Y - ( N - X + 1 ) , 1 ) ; } } if ( Y >= N ) return ( Y - N ) * X ; return Math . max ( X - ( N - Y + 1 ) , 1 ) ; } let X = 47 , Y = 42 , N = 167 ; document . write ( minProd ( X , Y , N ) ) ;
Count pairs from an array having product of their sum and difference equal to 1 | Function to count the desired number of pairs ; Initialize oneCount ; Initialize the desiredPair ; Traverse the given array ; If 1 is encountered ; If 0 is encountered ; Update count of pairs ; Return the final count ; Given array arr [ ] ; Function call
function countPairs ( arr , n ) { let oneCount = 0 ; let desiredPair = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } let arr = [ 3 , 1 , 1 , 0 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;
Smallest missing non | Function to prlet the smallest missing non - negative integer up to every array indices ; Stores the smallest missing non - negative integers between start index to current index ; Store the boolean value to check smNonNeg present between start index to each index of the array ; Traverse the array ; Since output always lies in the range [ 0 , N - 1 ] ; Check if smNonNeg is present between start index and current index or not ; Print smallest missing non - negative integer ; Driver Code
function smlstNonNeg ( arr , N ) { let smNonNeg = 0 ; let hash = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { hash [ i ] = false ; } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i ] < N ) { hash [ arr [ i ] ] = true ; } while ( hash [ smNonNeg ] ) { smNonNeg ++ ; } document . write ( smNonNeg + " " ) ; } } let arr = [ 0 , 1 , 2 , 3 , 5 ] ; let N = arr . length ; smlstNonNeg ( arr , N ) ;
Split array into two subsequences having minimum count of pairs with sum equal to X | Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
function solve ( arr , N , X ) { var A = [ ] , B = [ ] ; var c = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . push ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . push ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . push ( arr [ i ] ) ; } else { B . push ( arr [ i ] ) ; } c ++ ; } } document . write ( " " ) ; for ( var i = 0 ; i < A . length ; i ++ ) { document . write ( A [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( var i = 0 ; i < B . length ; i ++ ) { document . write ( B [ i ] + " " ) ; } } var arr = [ 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 ] ; var X = 7 ; var N = arr . length ; solve ( arr , N , X ) ;
Split array into minimum number of subsets having maximum pair sum at most K | Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code
function cntMinSub ( arr , N , K ) { var res = 0 ; var start = 0 ; var end = N - 1 ; arr . sort ( ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } var arr = [ 2 , 6 , 8 , 10 , 20 , 25 ] ; var N = arr . length ; var K = 26 ; document . write ( cntMinSub ( arr , N , K ) ) ;
Count possible values of K such that X | Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code
function condition ( a , b ) { let d = Math . abs ( a - b ) , count = 0 ; for ( let i = 1 ; i <= Math . sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } let x = 2 , y = 6 ; document . write ( condition ( x , y ) ) ;
Count pairs in an array whose product is composite number | Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code
function isComposite ( N ) { var i ; for ( i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return true ; } } return false ; } function compositePair ( arr , N ) { var res = 0 ; var i , j ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { var prod = arr [ i ] * arr [ j ] ; if ( isComposite ( prod ) ) { res ++ ; } } } return res ; } var arr = [ 1 , 1 , 2 , 2 , 8 ] ; var N = arr . length ; document . write ( compositePair ( arr , N ) ) ;
Count N | Javascript Program for the above approach ; Function for calculate ( x ^ y ) % mod in O ( log y ) ; Base Condition ; Transition state of power Function ; Function for counting total numbers that can be formed such that digits X , Y are present in each number ; Calculate the given expression ; Return the final answer ; Driver Code
var mod = 1000000007 ; function power ( x , y ) { if ( y == 0 ) return 1 ; var p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( y & 1 ) { p = ( x * p ) % mod ; } return p ; } function TotalNumber ( N ) { var ans = ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod ; return ans ; } var N = 10 , X = 3 , Y = 4 ; document . write ( TotalNumber ( N ) ) ;
Array obtained by repeatedly reversing array after every insertion from given array | Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
function generateArray ( arr , n ) { var ans = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( i & 1 ) ans . splice ( 0 , 0 , arr [ i ] ) ; else ans . push ( arr [ i ] ) ; } if ( n & 1 ) { ans . reverse ( ) ; } ans . forEach ( x => { document . write ( x + " " ) ; } ) ; } var n = 4 ; var arr = [ 1 , 2 , 3 , 4 ] ; generateArray ( arr , n ) ;
Find the greater number closest to N having at most one non | Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant string ; Append n - 1 times '0' to the end of resultant string ; Driver code
function closestgtNum ( str ) { var res = " " ; var n = str . length ; if ( str [ 0 ] < ' ' ) { res = res + String . fromCharCode ( str [ 0 ] . charCodeAt ( 0 ) + 1 ) ; } else { res = res + ' ' ; res = res + ' ' ; } for ( var i = 0 ; i < n - 1 ; i ++ ) { res = res + ' ' ; } return res ; } var str = " " ; document . write ( closestgtNum ( str ) ) ;
Count maximum non | Function to count maximum number of non - overlapping subarrays with sum equals to the target ; Stores the final count ; Next subarray should start from index >= availIdx ; Tracks the prefix sum ; Map to store the prefix sum for respective indices ; Check if cur_sum - target is present in the array or not ; Update the index of current prefix sum ; Return the count of subarrays ; Given array arr [ ] ; Given sum target ; Function Call
function maximumSubarrays ( arr , N , target ) { var ans = 0 ; var availIdx = - 1 ; var cur_sum = 0 ; var mp = new Map ( ) ; mp . set ( 0 , 1 ) ; for ( var i = 0 ; i < N ; i ++ ) { cur_sum += arr [ i ] ; if ( mp . has ( cur_sum - target ) && mp . get ( cur_sum - target ) >= availIdx ) { ans ++ ; availIdx = i ; } mp . set ( cur_sum , i ) ; } return ans ; } var arr = [ 2 , - 1 , 4 , 3 , 6 , 4 , 5 , 1 ] ; var N = arr . length ; var target = 6 ; document . write ( maximumSubarrays ( arr , N , target ) ) ;
Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver Code ; Function Call
function power ( x , y ) { var temp ; if ( y == 0 ) return 1 ; temp = power ( x , parseInt ( y / 2 ) ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } function maximumPossibleSum ( N , X ) { document . write ( X * ( power ( 2 , N ) - 1 ) ) ; } var N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ;
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the final count ; Given X and Y ; Function Call
function countOfPairs ( x , y ) { let counter = 1 ; while ( x > 0 y > 0 ) { let bit1 = x % 2 ; let bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 && bit2 == 0 ) { counter *= 2 ; continue ; } if ( ( bit1 & bit2 ) > 0 ) { counter = 0 ; break ; } } return counter ; } let X = 2 , Y = 5 ; document . write ( countOfPairs ( X , Y ) ) ;
Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | Function to return gcd of a and b ; Base Case ; Recursive Call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Given array arr [ ] ; Function Call
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findGCD ( arr , N ) { let result = 0 ; for ( let element in arr ) { result = gcd ( result , element ) ; if ( result == 1 ) { return 1 ; } } return result ; } let arr = [ 2 , 3 , 4 ] ; let N = arr . length ; document . write ( findGCD ( arr , N ) ) ;
Check if any permutation of array contains sum of every adjacent pair not divisible by 3 | Function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; Count remainder 0 ; Count remainder 1 ; Count remainder 2 ; Condition for valid arrangements ; Given array arr [ ] ; Function Call
function factorsOf3 ( arr , N ) { let a = 0 , b = 0 , c = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 3 == 0 ) a ++ ; else if ( arr [ i ] % 3 == 1 ) b ++ ; else if ( arr [ i ] % 3 == 2 ) c ++ ; } if ( a >= 1 && a <= b + c + 1 ) document . write ( " " + " " ) ; else if ( a == 0 && b == 0 && c > 0 ) document . write ( " " + " " ) ; else if ( a == 0 && c == 0 && b > 0 ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let arr = [ 1 , 2 , 3 , 3 ] ; let N = arr . length ; factorsOf3 ( arr , N ) ;
Check if a number is a perfect square having all its digits as a perfect square | Function to check if digits of N is a perfect square or not ; Iterate over the digits ; Extract the digit ; Check if digit is a perfect square or not ; Divide N by 10 ; Return true ; Function to check if N is a perfect square or not ; If floor and ceil of n is not same ; Function to check if N satisfies the required conditions or not ; If both the conditions are satisfied ; Driver Code ; Function Call
function check_digits ( N ) { while ( N > 0 ) { let n = N % 10 ; if ( ( n != 0 ) && ( n != 1 ) && ( n != 4 ) && ( n != 9 ) ) { return 0 ; } N = Math . floor ( N / 10 ) ; } return 1 ; } function is_perfect ( N ) { let n = Math . sqrt ( N ) ; if ( Math . floor ( n ) != Math . ceil ( n ) ) { return 0 ; } return 1 ; } function isFullSquare ( N ) { if ( is_perfect ( N ) && check_digits ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let N = 144 ; isFullSquare ( N ) ;
Count substrings with different first and last characters | Function to count the substrings having different first & last character ; Stores frequency of each char ; Loop to store frequency of the characters in a Map ; To store final result ; Traversal of string ; Store answer for every iteration ; Map traversal ; Compare current char ; Print the final count ; Given string ; Length of the string ; Function Call
function countSubstring ( s , n ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . has ( s [ i ] ) ) m . set ( s [ i ] , m . get ( s [ i ] ) + 1 ) else m . set ( s [ i ] , 1 ) } var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var cnt = 0 ; if ( m . has ( s [ i ] ) ) m . set ( s [ i ] , m . get ( s [ i ] ) - 1 ) m . forEach ( ( value , key ) => { if ( key != s [ i ] ) { cnt += value ; } } ) ; ans += cnt ; } document . write ( ans ) ; } var S = " " ; var N = 5 ; countSubstring ( S , N ) ;
Maximize count of empty water bottles from N filled bottles | Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call
function maxBottles ( n , e ) { var s = 0 , b = 0 ; var a = n ; while ( a != 0 ) { s = s + a ; a = ( a + b ) / e ; b = n - ( a * e ) ; n = a + b ; } return s ; } var n = 9 , e = 3 ; var s = maxBottles ( n , e ) ; document . write ( parseInt ( s ) + " " ) ;
Count all N digit numbers whose digits are multiple of X | Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count all N - digit numbers whose digits are multiples of x ; Count all digits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Given N and X ; Function Call
function power ( x , n ) { let temp ; if ( n == 0 ) return 1 ; temp = power ( x , parseInt ( n / 2 ) ) ; if ( n % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } function count_Total_Numbers ( n , x ) { let total , multiples = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) { if ( i % x == 0 ) multiples ++ ; } if ( n == 1 ) return multiples ; total = ( multiples - 1 ) * power ( multiples , n - 1 ) ; return total ; } let N = 1 , X = 3 ; document . write ( count_Total_Numbers ( N , X ) ) ;
Find the vertex diagonally opposite to the vertex M from an N | Function to return the required vertex ; Case 1 : ; Case 2 : ; Driver Code
function getPosition ( N , M ) { if ( M > parseInt ( N / 2 ) ) { return ( M - parseInt ( N / 2 ) ) ; } return ( M + parseInt ( N / 2 ) ) ; } var N = 8 , M = 5 ; document . write ( getPosition ( N , M ) ) ;
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | Function that counts the number of strings of length three that can be made with given m 0 s and n 1 s ; Iterate until N & M are positive ; Case 1 : ; Case 2 : ; Print the count of strings ; Given count of 1 s and 0 s ; Function call
function number_of_strings ( N , M ) { var ans = 0 ; while ( N > 0 && M > 0 ) { if ( N > M ) { if ( N >= 2 ) { N -= 2 ; -- M ; ++ ans ; } else { break ; } } else { if ( M >= 2 ) { M -= 2 ; -- N ; ++ ans ; } else { break ; } } } document . write ( ans ) ; } var N = 4 , M = 19 ; number_of_strings ( N , M ) ;
Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum | Function that returns the minimum length of the array after merging unequal adjacent elements ; Stores the first element and its frequency ; Traverse the array ; If all elements are equal ; No merge - pair operations can be performed ; Otherwise ; Given array ; Length of the array ; Function Call
function minLength ( A , N ) { var elem = A [ 0 ] , count = 1 ; for ( var i = 1 ; i < N ; i ++ ) { if ( A [ i ] == elem ) { count ++ ; } else { break ; } } if ( count == N ) return N ; else return 1 ; } var arr = [ 2 , 1 , 3 , 1 ] ; var N = arr . length ; document . write ( minLength ( arr , N ) + " " ) ;
Reduce N to 0 or less by given X and Y operations | Function to check if N can be reduced to <= 0 or not ; Update N to N / 2 + 10 at most X times ; Update N to N - 10 Y times ; Driver code
function NegEqu ( N , X , Y ) { while ( X > 0 && ( N > N / 2 + 10 ) ) { N = N / 2 + 10 ; X -= 1 ; } while ( Y > 0 ) { N = N - 10 ; Y -= 1 ; } if ( N <= 0 ) return true ; return false ; } let N = 100 ; let X = 3 ; let Y = 4 ; if ( NegEqu ( N , X , Y ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 | Function to find the minimum number of subarrays ; Right pointer ; Left pointer ; Count of subarrays ; Find GCD ( left , right ) ; Found a valid large subarray between arr [ left , right ] ; Searched the arr [ 0. . right ] and found no subarray having size > 1 and having gcd ( left , right ) > 1 ; Driver Code ; Function call
function minSubarrays ( arr , n ) { var right = n - 1 ; var left = 0 ; var subarrays = 0 ; while ( right >= 0 ) { for ( left = 0 ; left <= right ; left += 1 ) { if ( __gcd ( arr [ left ] , arr [ right ] ) > 1 ) { subarrays += 1 ; right = left - 1 ; break ; } if ( left == right && __gcd ( arr [ left ] , arr [ right ] ) == 1 ) { return 0 ; } } } return subarrays ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var N = 6 ; var arr = [ 2 , 3 , 4 , 4 , 4 , 3 ] ; document . write ( minSubarrays ( arr , N ) ) ;
Find the largest possible value of K such that K modulo X is Y | Function to find the largest positive integer K such that K % x = y ; Stores the minimum solution ; Return the maximum possible value ; Driver Code
function findMaxSoln ( n , x , y ) { var ans = - 1000000000 ; for ( var k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = Math . max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : - 1 ) ; } var n = 15 , x = 10 , y = 5 ; document . write ( findMaxSoln ( n , x , y ) ) ;
Count all numbers up to N having M as the last digit | Function to count the numbers ending with M ; Stores the count of numbers required ; Calculate count upto nearest power of 10 ; Computing the value of x ; Adding the count of numbers ending at M from x to N ; Driver Code ; Function call
function getCount ( N , M ) { let total_count = 0 total_count += Math . floor ( N / 10 ) let x = Math . floor ( N / 10 ) * 10 if ( ( N - x ) >= M ) { total_count = total_count + 1 } return total_count } let N = 100 let M = 1 document . write ( getCount ( N , M ) )
Count of Right | Function to find the number of right angled triangle that are formed from given N points whose perpendicular or base is parallel to X or Y axis ; To store the number of points has same x or y coordinates ; Store the total count of triangle ; Iterate to check for total number of possible triangle ; Add the count of triangles formed ; Total possible triangle ; Driver Code ; Given N points ; Function Call
function RightAngled ( a , n ) { var xpoints = { } ; var ypoints = { } ; for ( var i = 0 ; i < n ; i ++ ) { if ( xpoints . hasOwnProperty ( a [ i ] [ 0 ] ) ) { xpoints [ a [ i ] [ 0 ] ] = xpoints [ a [ i ] [ 0 ] ] + 1 ; } else { xpoints [ a [ i ] [ 0 ] ] = 1 ; } if ( ypoints . hasOwnProperty ( a [ i ] [ 1 ] ) ) { ypoints [ a [ i ] [ 1 ] ] = ypoints [ a [ i ] [ 1 ] ] + 1 ; } else { ypoints [ a [ i ] [ 1 ] ] = 1 ; } } var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( xpoints [ a [ i ] [ 0 ] ] >= 1 && ypoints [ a [ i ] [ 1 ] ] >= 1 ) { count += ( xpoints [ a [ i ] [ 0 ] ] - 1 ) * ( ypoints [ a [ i ] [ 1 ] ] - 1 ) ; } } return count ; } var N = 5 ; var arr = [ [ 1 , 2 ] , [ 2 , 1 ] , [ 2 , 2 ] , [ 2 , 3 ] , [ 3 , 2 ] , ] ; document . write ( RightAngled ( arr , N ) ) ;