codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of Factorials whose sum is equal to N | Array to calculate all factorials less than or equal to N Since there can be only 14 factorials till 10 ^ 10 Hence the maximum size of fact [ ] is 14 ; Store the actual size of fact [ ] ; Function to precompute factorials till N ; Precomputing factorials ; Function to find the minimum number of factorials whose sum represents N ; Precompute factorials ; Initialize result ; Traverse through all factorials ; Find factorials ; Print min count ; Print result ; Driver program
var fact = Array ( 14 ) . fill ( 0 ) ; var size = 1 ; function preCompute ( N ) { fact [ 0 ] = 1 ; for ( var i = 1 ; fact [ i - 1 ] <= N ; i ++ ) { fact [ i ] = ( fact [ i - 1 ] * i ) ; size ++ ; } } function findMin ( N ) { preCompute ( N ) ; var originalN = N ; ans = [ ] ; for ( var i = size - 1 ; i >= 0 ; i -- ) { while ( N >= fact [ i ] ) { N -= fact [ i ] ; ans . push ( fact [ i ] ) ; } } document . write ( ans . length + " " ) ; for ( var i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ; } var n = 27 ; findMin ( n ) ;
Longest increasing sub | Function to find the LCS ; Loop to create frequency array ; Driver code
function findLCS ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) != 0 ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } return mp . size ; } let n = 3 ; let arr = [ 3 , 2 , 1 ] ; document . write ( findLCS ( arr , n ) ) ;
Vertical and Horizontal retrieval ( MRT ) on Tapes | Javascript program to print Vertical filling ; 2D matrix for vertical insertion on tapes ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; It is used for calculating mean retrieval time ; vertical insertion on tape ; initialize variables to 0 for each iteration ; Used for getting ' sum ' sizes of records in tape to determine whether tape is full or not ; if tape is not full ; check for ability of tapes to hold value ; initialize variables to 0 for each iteration ; display elements of tape ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . size ( ) is function of vector is used to get size of vector ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
function vertical_Fill ( records , tape , m , n ) { let v = new Array ( m ) ; for ( let i = 0 ; i < m ; i ++ ) { v [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { v [ i ] [ j ] = 0 ; } } let sum = 0 ; let Retrieval_Time = 0 ; let Mrt ; let z = 0 , j = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { sum = 0 ; for ( let k = 0 ; k < i ; k ++ ) { sum += v [ j ] [ k ] ; } if ( sum + records [ z ] <= tape [ j ] ) { v [ j ] [ i ] = records [ z ] ; z ++ ; } } if ( v [ 2 ] [ i ] == 0 ) { break ; } } for ( let i = 0 ; i < m ; i ++ ) { Retrieval_Time = 0 ; document . write ( " " + ( i + 1 ) + " " ) ; for ( j = 0 ; j < n ; j ++ ) { if ( v [ i ] [ j ] != 0 ) { document . write ( v [ i ] [ j ] + " " ) ; } else { break ; } } document . write ( " " ) ; for ( let k = 0 ; v [ i ] [ k ] != 0 ; k ++ ) { Retrieval_Time += v [ i ] [ k ] * ( j - k ) ; } Mrt = Retrieval_Time / j ; if ( i != m - 1 ) { document . write ( " " + Mrt + " " ) ; } else { document . write ( " " + Mrt . toFixed ( 4 ) + " " ) ; } } } let records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ] ; let tape = [ 25 , 80 , 160 ] ; let n = records . length ; let m = tape . length ; records . sort ( function ( a , b ) { return a - b } ) ; vertical_Fill ( records , tape , m , n ) ;
Find two equal subsequences of maximum length with at least one different index | Javascript implementation of the approach ; Function to return the required length of the subsequences ; To store the result ; To store the last visited position of lowercase letters ; Initialisation of frequency array to - 1 to indicate no character has previously occured ; For every character of the string ; Get the index of the current character ; If the current character has appeared before in the string ; Update the result ; Update the last position of the current character ; Driver code
var MAX = 26 ; function maxLength ( str , len ) { var res = 0 ; var lastPos = Array ( MAX ) ; for ( var i = 0 ; i < MAX ; i ++ ) { lastPos [ i ] = - 1 ; } for ( var i = 0 ; i < len ; i ++ ) { var C = str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( lastPos [ C ] != - 1 ) { res = Math . max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; } lastPos [ C ] = i ; } return res ; } var str = " " ; var len = str . length ; document . write ( maxLength ( str , len ) ) ;
Partitions possible such that the minimum element divides all the other elements of the partition | Javascript implementation of the approach ; Function to return the count partitions possible from the given array such that the minimum element of any partition divides all the other elements of that partition ; Initialize the count variable ; Find the minimum element ; Break if no minimum element present ; Increment the count if minimum element present ; Replace all the element divisible by min_elem ; Driver code
var INT_MAX = 1000000000 ; function min_element ( A , N ) { var min = A [ 0 ] ; var i ; for ( i = 1 ; i < N ; i ++ ) { if ( min > A [ i ] ) { min = A [ i ] ; } } return min ; } function countPartitions ( A , N ) { var count = 0 ; var i , j ; for ( i = 0 ; i < N ; i ++ ) { var min_elem = min_element ( A , N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( j = 0 ; j < N ; j ++ ) { if ( A [ j ] % min_elem == 0 ) A [ j ] = INT_MAX ; } } return count ; } var arr = [ 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 ] ; var N = arr . length ; document . write ( countPartitions ( arr , N ) ) ;
Minimum changes required to make all element in an array equal | Function to count of minimum changes required to make all elements equal ; Store the count of each element as key value pair in unordered map ; Find the count of maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
function minChanges ( arr , n ) { var umap = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( umap . has ( arr [ i ] ) ) { umap . set ( arr [ i ] , umap . get ( arr [ i ] ) + 1 ) ; } else { umap . set ( arr [ i ] , 1 ) ; } } var maxFreq = 0 ; umap . forEach ( ( values , keys ) => { maxFreq = Math . max ( maxFreq , values ) ; } ) ; return n - maxFreq ; } var arr = [ 2 , 3 , 3 , 4 ] ; var n = arr . length ; document . write ( minChanges ( arr , n ) + ' ' ) ;
Minimum operations to make two numbers equal | Function to find the minimum no of operations ; find the maximum of two and store it in p ; increase it until it is achievable from given n and m ; Here value added to n and m will be S ( n ) = p - n + p - m ; check whether integer value of n exist by the formula n = ( - 1 + Math . sqrt ( 1 + 8 * S ( n ) ) ) / 2 ; Driver code ; Function calling
function minOperations ( n , m ) { var a = 0 , k = 1 ; var p = Math . max ( n , m ) ; while ( n != m ) { var s = ( p - n + p - m ) ; var q = ( ( - 1 + Math . sqrt ( 8 * s + 1 ) ) / 2 ) ; if ( q - Math . floor ( q ) == 0 ) { a = parseInt ( q ) ; n = m ; } p = p + 1 ; } return a ; } var n = 1 , m = 3 ; document . write ( minOperations ( n , m ) ) ;
Count of subarrays with sum at least K | Function to return the number of subarrays with sum atleast k ; To store the right index and the current sum ; To store the number of sub - arrays ; For all left indexes ; Get elements till current sum is less than k ; No such subarray is possible ; Add all possible subarrays ; Remove the left most element ; Return the required answer ; Driver code
function k_sum ( a , n , k ) { let r = 0 , sum = 0 ; let ans = 0 ; for ( let l = 0 ; l < n ; l ++ ) { while ( sum < k ) { if ( r == n ) break ; else { sum += a [ r ] ; r ++ ; } } if ( sum < k ) break ; ans += n - r + 1 ; sum -= a [ l ] ; } return ans ; } let a = [ 6 , 1 , 2 , 7 ] , k = 10 ; let n = a . length ; document . write ( k_sum ( a , n , k ) ) ;
Find if a crest is present in the index range [ L , R ] of the given array | Function that returns true if the array contains a crest in the index range [ L , R ] ; To keep track of elements which satisfy the Property ; Property is satisfied for the current element ; Cumulative Sum ; If a crest is present in the given index range ; Driver code
function hasCrest ( arr , n , L , R ) { let present = new Uint8Array ( n ) ; for ( let i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( let i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } let arr = [ 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 ] ; let N = arr . length ; let L = 2 ; let R = 6 ; if ( hasCrest ( arr , N , L , R ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum Sum of Products of two arrays by toggling adjacent bits | Function to return Max Sum ; intialParity and finalParity are 0 if total no . of 1 's is even else 1 ; minPositive and maxNegative will store smallest positive and smallest negative integer respectively . ; Count of Initial Parity ; if arr1 [ i ] is positive then add 1 in finalParity to get 1 at arr2 [ i ] ; if both parity are odd or even then return sum ; else add one more 1 or remove 1 ; if minPositive > maxNegative , put 1 at maxNegative and add it to our sum ; else remove minPositive no . ; Driver code
function maxSum ( arr1 , arr2 , n ) { let initialParity = 0 , finalParity = 0 ; let sum = 0 , minPositive = Number . MAX_SAFE_INTEGER , maxNegative = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { initialParity += arr2 [ i ] ; if ( arr1 [ i ] >= 0 ) { finalParity += 1 ; sum += arr1 [ i ] ; minPositive = Math . min ( minPositive , arr1 [ i ] ) ; } else { maxNegative = Math . max ( maxNegative , arr1 [ i ] ) ; } } if ( initialParity % 2 == finalParity % 2 ) { return sum ; } else { if ( minPositive + maxNegative >= 0 ) { return sum + maxNegative ; } else { return sum - minPositive ; } } } let arr1 = [ 2 , - 4 , 5 , 3 ] ; let arr2 = [ 0 , 1 , 0 , 1 ] ; let n = arr1 . length ; document . write ( maxSum ( arr1 , arr2 , n ) ) ;
Minimum number to be added to all digits of X to make X > Y | Function to check if X is lexicographically larger Y ; It is lexicographically larger ; Utility function to check minimum value of d ; If X is already larger do not need to add anything ; Adding d to all elements of X ; If X is larger now print d ; else print d + 1 ; Taking the numbers as sequences
function IsLarger ( X , Y , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } function solve ( X , Y , n ) { let ans = 0 ; if ( IsLarger ( X , Y , n ) ) { ans = 0 ; } else { let d = Y [ 0 ] - X [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { X [ i ] += d ; } if ( IsLarger ( X , Y , n ) ) { ans = d ; } else { ans = d + 1 ; } } return ans ; } let X = [ 2 , 3 , 6 , 9 ] ; let Y = [ 3 , 4 , 8 , 1 ] ; let n = X . length ; document . write ( solve ( X , Y , n ) ) ;
Maximum possible Bitwise OR of the two numbers from the range [ L , R ] | Function to return the maximum bitwise OR of any pair from the given range ; Converting L to its binary representation ; Converting R to its binary representation ; In order to make the number of bits of L and R same ; Push 0 to the MSB ; When ith bit of R is 1 and ith bit of L is 0 ; From MSB side set all bits of L to be 1 ; From ( i + 1 ) th bit , all bits of L changed to be 1 ; Driver code
function max_bitwise_or ( L , R ) { let v1 = [ ] , v2 = [ ] , v3 = [ ] ; let z = 0 , i , ans = 0 , cnt = 1 ; while ( L > 0 ) { v1 . push ( L % 2 ) ; L = parseInt ( L / 2 ) ; } while ( R > 0 ) { v2 . push ( R % 2 ) ; R = parseInt ( R / 2 ) ; } while ( v1 . length != v2 . length ) { v1 . push ( 0 ) ; } for ( i = v2 . length - 1 ; i >= 0 ; i -- ) { if ( v2 [ i ] == 1 && v1 [ i ] == 0 && z == 0 ) { z = 1 ; continue ; } if ( z == 1 ) { v1 [ i ] = 1 ; } } for ( i = 0 ; i < v2 . length ; i ++ ) { v3 . push ( v2 [ i ] v1 [ i ] ) ; } for ( i = 0 ; i < v2 . length ; i ++ ) { if ( v3 [ i ] == 1 ) { ans += cnt ; } cnt *= 2 ; } return ans ; } let L = 10 , R = 20 ; document . write ( max_bitwise_or ( L , R ) ) ;
Find the minimum value of X for an expression | Function to calculate value of X ; Check for both possibilities ; Driver Code
function valueofX ( ar , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + ar [ i ] ; } if ( sum % n == 0 ) { return Math . floor ( sum / n ) ; } else { let A = Math . floor ( sum / n ) , B = Math . floor ( sum / n + 1 ) ; let ValueA = 0 , ValueB = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) ; ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) ; } if ( ValueA < ValueB ) { return A ; } else { return B ; } } } let n = 7 ; let arr = [ 6 , 9 , 1 , 6 , 1 , 3 , 7 ] ; document . write ( valueofX ( arr , n ) + " " ) ;
Minimum length String with Sum of the alphabetical values of the characters equal to N | Function to find the minimum length ; Function to find the minimum length String ; Driver code
function minLength ( n ) { var ans = parseInt ( n / 26 ) ; if ( n % 26 != 0 ) ans ++ ; return ans ; } function minString ( n ) { var ans = parseInt ( n / 26 ) ; var res = " " ; while ( ans -- ) { res = res + " " ; } if ( n % 26 != 0 ) { res = res + String . fromCharCode ( ( n % 26 ) + 96 ) ; } return res ; } var n = 50 ; document . write ( minLength ( n ) + " " + minString ( n ) ) ;
Minimum halls required for class scheduling | JavaScript implementation of the approach ; Function to return the minimum number of halls required ; Array to store the number of lectures ongoing at time t ; For every lecture increment start point s decrement ( end point + 1 ) ; Perform prefix sum and update the ans to maximum ; Driver code
const MAX = 100001 ; function minHalls ( lectures , n ) { let prefix_sum = new Uint8Array ( MAX ) ; for ( let i = 0 ; i < n ; i ++ ) { prefix_sum [ lectures [ i ] [ 0 ] ] ++ ; prefix_sum [ lectures [ i ] [ 1 ] + 1 ] -- ; } let ans = prefix_sum [ 0 ] ; for ( let i = 1 ; i < MAX ; i ++ ) { prefix_sum [ i ] += prefix_sum [ i - 1 ] ; ans = Math . max ( ans , prefix_sum [ i ] ) ; } return ans ; } let lectures = [ [ 0 , 5 ] , [ 1 , 2 ] , [ 1 , 10 ] ] ; let n = lectures . length ; document . write ( minHalls ( lectures , n ) ) ;
Find the minimum capacity of the train required to hold the passengers | Function to return the minimum capacity required ; To store the minimum capacity ; To store the current capacity of the train ; For every station ; Add the number of people entering the train and subtract the number of people exiting the train to get the current capacity of the train ; Update the minimum capacity ; Driver code
function minCapacity ( enter , exit , n ) { let minCap = 0 ; let currCap = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currCap = currCap + enter [ i ] - exit [ i ] ; minCap = Math . max ( minCap , currCap ) ; } return minCap ; } let enter = [ 3 , 5 , 2 , 0 ] ; let exit = [ 0 , 2 , 4 , 4 ] ; let n = enter . length ; document . write ( minCapacity ( enter , exit , n ) ) ;
Count of numbers in the range [ L , R ] which satisfy the given conditions | Maximum possible valid number ; To store all the required number from the range [ 1 , MAX ] ; Function that returns true if x satisfies the given conditions ; To store the digits of x ; If current digit appears more than once ; If current digit is greater than 5 ; Put the digit in the map ; Function to generate all the required numbers in the range [ 1 , MAX ] ; Insert first 5 valid numbers ; Inserting 0 externally because 0 cannot be the leading digit in any number ; If x satisfies the given conditions ; Cannot append anymore digit as adding a digit will repeat one of the already present digits ; Append all the valid digits one by one and push the new generated number to the queue ; Append the digit ; Push the newly generated number to the queue ; Function to copmpare two Strings which represent a numerical value ; Function to return the count of valid numbers in the range [ l , r ] ; Generate all the valid numbers in the range [ 1 , MAX ] ; To store the count of numbers in the range [ l , r ] ; For every valid number in the range [ 1 , MAX ] ; If current number is within the required range ; If number is equal to either l or r ; Driver code
let MAX = 543210 ; let ans = [ ] ; function isValidNum ( x ) { let mp = new Map ( ) ; for ( let i = 0 ; i < x . length ; i ++ ) { if ( mp . has ( x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) { return false ; } else if ( x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) > 5 ) { return false ; } else { mp . set ( x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) , 1 ) ; } } return true ; } function generate ( ) { let q = [ ] ; q . push ( " " ) ; q . push ( " " ) ; q . push ( " " ) ; q . push ( " " ) ; q . push ( " " ) ; let flag = true ; ans . push ( " " ) ; while ( q . length != 0 ) { let x = q . shift ( ) ; if ( isValidNum ( x ) ) { ans . push ( x ) ; } if ( x . length == 6 ) continue ; for ( let i = 0 ; i <= 5 ; i ++ ) { let z = ( i ) . toString ( ) ; let temp = x + z ; q . push ( temp ) ; } } } function comp ( a , b ) { if ( a . length == b . length ) { return a < b ? true : false ; } else return a . length < b . length ; } function findcount ( l , r ) { generate ( ) ; let count = 0 ; for ( let i = 0 ; i < ans . length ; i ++ ) { let a = ans [ i ] ; if ( comp ( l , a ) && comp ( a , r ) ) { count ++ ; } else if ( a == l a == r ) { count ++ ; } } return count ; } let l = " " , r = " " ; document . write ( findcount ( l , r ) ) ;
Find permutation with maximum remainder Sum | Function to find the permutation ; Put n at the first index 1 ; Put all the numbers from 2 to n sequentially ; Driver code ; Display the permutation
function Findpermutation ( n ) { let a = new Array ( n + 1 ) ; a [ 1 ] = n ; for ( let i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } let n = 8 ; let v = Findpermutation ( n ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( v [ i ] + ' ' ) ;
Lexicographically smallest string of length N and sum K | Function to return the lexicographically smallest string of length n that satisfies the given condition ; Iteration from the last position in the array ; If k is a positive integer ; ' z ' needs to be inserted ; Add the required character ; Driver code
function lexo_small ( n , k ) { var arr = Array . from ( { length : n } , ( _ , i ) => ' ' ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { k -= i ; if ( k >= 0 ) { if ( k >= 26 ) { arr [ i ] = ' ' ; k -= 26 ; } else { arr [ i ] = String . fromCharCode ( k + 97 - 1 ) ; k -= arr [ i ] - ' ' + 1 ; } } else break ; k += i ; } return arr ; } var n = 5 , k = 42 ; var arr = lexo_small ( n , k ) ; document . write ( arr . join ( ' ' ) ) ;
Minimum element left from the array after performing given operations | Function to return the minimum possible value of the last element left after performing the given operations ; Driver code
function getMin ( arr , n ) { var minVal = Math . min . apply ( Math , arr ) ; return minVal ; } var arr = [ 5 , 3 , 1 , 6 , 9 ] ; var n = arr . length ; document . write ( getMin ( arr , n ) ) ;
Largest substring with same Characters | Function to find largest sub string with same characters ; Traverse the string ; If character is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
function Substring ( s ) { var ans = 1 , temp = 1 ; for ( var i = 1 ; i < s . length ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { ++ temp ; } else { ans = Math . max ( ans , temp ) ; temp = 1 ; } } ans = Math . max ( ans , temp ) ; return ans ; } var s = " " ; document . write ( Substring ( s ) ) ;
Number of balanced parenthesis substrings | Function to find number of balanced parenthesis sub strings ; To store required answer ; Vector to stores the number of balanced brackets at each depth . ; d stores checks the depth of our sequence For example level of ( ) is 1 and that of ( ( ) ) is 2. ; If open bracket increase depth ; If closing bracket ; Return the required answer ; Driver code ; Function call
function Balanced_Substring ( str , n ) { let ans = 0 ; let arr = new Array ( n / 2 + 1 ) . fill ( 0 ) ; let d = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) d ++ ; else { if ( d == 1 ) { for ( let j = 2 ; j <= parseInt ( n / 2 ) + 1 && arr [ j ] != 0 ; j ++ ) arr [ j ] = 0 ; } ++ ans ; ans += arr [ d ] ; arr [ d ] ++ ; d -- ; } } return ans ; } let str = " " ; let n = str . length ; document . write ( Balanced_Substring ( str , n ) ) ;
Count ways to divide circle using N non | Function to calculate x ^ y % mod efficiently ; Initialize the answer ; If power is odd ; Update the answer ; Square the base and half the exponent ; Return the value ; Function to calculate ncr % mod efficiently ; Initialize the answer ; Calculate ncr in O ( r ) ; Multiply with the numerator factor ; Calculate the inverse of factor of denominator ; Multiply with inverse value ; Return answer value ; Function to return the number of non intersecting chords ; define mod value ; Value of C ( 2 n , n ) ; Modulo inverse of ( n + 1 ) ; Multiply with modulo inverse ; Return the answer ; Driver code ; Function call
function power ( x , y , mod ) { var res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; x = ( x * x ) % mod ; y = ( y >> 1 ) ; } return parseInt ( res % mod ) ; } function ncr ( n , r , mod ) { var res = 1 ; for ( var i = 1 ; i <= r ; i += 1 ) { res = ( res * ( n - i + 1 ) ) % mod ; var inv = power ( i , mod - 2 , mod ) ; res = ( res * inv ) % mod ; } return parseInt ( res % mod ) ; } function NoOfChords ( A ) { var mod = parseInt ( 7 ) ; var ans = ncr ( 2 * A , A , mod ) ; var inv = power ( A + 1 , mod - 2 , mod ) ; ans = ( ans * inv ) % mod ; return parseInt ( ans % mod ) ; } var N = 2 ; document . write ( NoOfChords ( N ) ) ;
Longest Subarray having strictly positive XOR | Function to return the length of the longest sub - array having positive XOR ; To store the XOR of all the elements ; To check if all the elements of the array are 0 s ; Take XOR of all the elements ; If any positive value is found the make the checkallzero false ; If complete array is the answer ; If all elements are equal to zero ; Initialize l and r ; First positive value of the array ; Last positive value of the array ; Maximum length among these two subarrays ; Driver code
function StrictlyPositiveXor ( A , N ) { let allxor = 0 ; let checkallzero = true ; for ( let i = 0 ; i < N ; i += 1 ) { allxor ^= A [ i ] ; if ( A [ i ] > 0 ) checkallzero = false ; } if ( allxor != 0 ) return N ; if ( checkallzero ) return - 1 ; let l = N , r = - 1 ; for ( let i = 0 ; i < N ; i += 1 ) { if ( A [ i ] > 0 ) { l = i + 1 ; break ; } } for ( let i = N - 1 ; i >= 0 ; i -= 1 ) { if ( A [ i ] > 0 ) { r = i + 1 ; break ; } } return Math . max ( N - l , r - 1 ) ; } let A = [ 1 , 0 , 0 , 1 ] ; let N = A . length ; document . write ( StrictlyPositiveXor ( A , N ) ) ;