codeparrot/xlcost-text-to-code huggingface dataset:
Length of the longest path ending at vertex V in a Graph | Javascript program for the above approach ; Function to perform DFS Traversal from source node to the deepest node and update maximum distance to the deepest node ; Mark source as visited ; Update the maximum distance ; Traverse the adjacency list of the current source node ; Recursively call for the child node ; Backtracking step ; Function to calculate maximum length of the path ending at vertex v from any source node ; Stores the maximum length of the path ending at vertex v ; Stores the size of the matrix ; Stores the adjacency list of the given graph ; Traverse the matrix to create adjacency list ; Perform DFS Traversal to update the maximum distance ; Driver code
var distance = 0 ; function dfs ( src , Adj , visited , level ) { visited [ src ] = true ; distance = Math . max ( distance , level ) ; for ( var child of Adj [ src ] ) { if ( ( child != src ) && ( visited [ child ] == false ) ) { dfs ( child , Adj , visited , level + 1 ) ; } } visited [ src ] = false ; } function maximumLength ( mat , v ) { distance = 0 ; var N = mat [ 0 ] . length ; var visited = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { visited . push ( false ) ; } var Adj = Array . from ( Array ( N ) , ( ) => Array ( ) ) ; var i , j ; for ( i = 0 ; i < mat [ 0 ] . length ; i ++ ) { for ( j = 0 ; j < mat . length ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { Adj [ i ] . push ( j ) ; } } } dfs ( v , Adj , visited , 0 ) ; return distance ; } var mat = [ [ 0 , 1 , 0 , 0 ] , [ 1 , 0 , 1 , 1 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] ] ; var v = 2 ; document . write ( maximumLength ( mat , v ) ) ;
Minimum removals required such that given string consists only of a pair of alternating characters | Function to find the maximum length of alternating occurrences of a pair of characters in a string s ; Stores the next character for alternating sequence ; Stores the length of alternating occurrences of a pair of characters ; Traverse the given string ; If current character is same as the required character ; Increase length by 1 ; Reset required character ; Return the length ; Function to find minimum characters required to be deleted from S to obtain an alternating sequence ; Stores maximum length of alternating sequence of two characters ; Stores length of the string ; Generate every pair of English alphabets ; Function call to find length of alternating sequence for current pair of characters ; Update len to store the maximum of len and newLen in len ; Return n - len as the final result ; Given Input ; Function call to find minimum characters required to be removed from S to make it an alternating sequence of a pair of characters
function findLength ( s , i , j ) { var required = i ; let length = 0 ; for ( let k = 0 ; k < s . length ; k ++ ) { var curr = s . charAt ( k ) ; if ( curr == required ) { length += 1 ; if ( required == i ) required = j ; else required = i ; } } return length ; } function minimumDeletions ( S ) { let len = 0 ; let n = S . length ; for ( let i = 0 ; i < 26 ; i ++ ) { for ( let j = i + 1 ; j < 26 ; j ++ ) { let newLen = findLength ( S , String . fromCharCode ( i + 97 ) , String . fromCharCode ( j + 97 ) ) ; len = Math . max ( len , newLen ) ; } } return n - len ; } let S = " " ; document . write ( minimumDeletions ( S ) ) ;
Smallest value of X satisfying the condition X % A [ i ] = B [ i ] for two given arrays | Function to calculate modulo inverse of a w . r . t m using Extended Euclid Algorithm ; Base Case ; Perform extended euclid algorithm ; q is quotient ; m is remainder now , process same as euclid 's algorithm ; If x1 is negative ; Make x1 positive ; Function to implement Chinese Remainder Theorem to find X ; Stores the product of array elements ; Traverse the array ; Update product ; Initialize the result ; Apply the above formula ; Function to calculate the product of all elements of the array a [ ] ; Stores product of all array elements ; Traverse the array ; Return the product ; Function to find the value of X that satisfies the given condition ; Stores the required smallest value using Chinese Remainder Theorem ; Stores the product of all array elements ; The equation is Y + K * M >= P Therefore , calculate K = ceil ( ( P - Y ) / M ) ; So , X = Y + K * M ; Print the resultant value of X ; Driver Code
function inv ( a , m ) { var m0 = m , t , q ; var x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = parseInt ( a / m ) ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } function findMinX ( A , B , N ) { var prod = 1 ; for ( var i = 0 ; i < N ; i ++ ) prod *= A [ i ] ; var result = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var pp = parseInt ( prod / A [ i ] ) ; result += B [ i ] * inv ( pp , A [ i ] ) * pp ; } return result % prod ; } function product ( a , n ) { var ans = 1 ; for ( var i = 0 ; i < n ; i ++ ) { ans *= a [ i ] ; } return ans ; } function findSmallestInteger ( A , B , P , n ) { var Y = findMinX ( A , B , n ) ; var M = product ( A , n ) ; var K = Math . ceil ( ( P - Y ) / M ) ; var X = Y + K * M ; document . write ( X ) ; } var A = [ 3 , 4 , 5 ] ; var B = [ 2 , 3 , 1 ] ; var n = A . length ; var P = 72 ; findSmallestInteger ( A , B , P , n ) ;
Minimum number of basic logic gates required to realize given Boolean expression | Function to count the total number of gates required to realize the boolean expression S ; Length of the string ; Stores the count of total gates ; Traverse the string ; AND , OR and NOT Gate ; Print the count of gates required ; Input ; Function call to count the total number of gates required
function numberOfGates ( s ) { let N = s . length ; let ans = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { ans ++ ; } } document . write ( ans ) ; } let S = " " ; numberOfGates ( S ) ;
Count pairs ( i , j ) from an array such that | arr [ i ] | and | arr [ j ] | both lies between | arr [ i ] | Function to find pairs ( i , j ) such that | arr [ i ] | and | arr [ j ] | lies in between | arr [ i ] - arr [ j ] | and | arr [ i ] + arr [ j ] | ; Calculate absolute value of all array elements ; Sort the array ; Stores the count of pairs ; Traverse the array ; Increment left ; Add to the current count of pairs ; Print the answer ; Driver Code
function findPairs ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) arr [ i ] = Math . abs ( arr [ i ] ) ; arr . sort ( ( a , b ) => a - b ) ; let left = 0 ; let ans = 0 ; for ( let right = 0 ; right < N ; right ++ ) { while ( 2 * arr [ left ] < arr [ right ] ) left ++ ; ans += ( right - left ) ; } document . write ( ans ) ; } let arr = [ 1 , 3 , 5 , 7 ] ; let N = arr . length ; findPairs ( arr , N ) ;
Calculate absolute difference between minimum and maximum sum of pairs in an array | Function to find the difference between the maximum and minimum sum of a pair ( arr [ i ] , arr [ j ] ) from the array such that i < j and arr [ i ] < arr [ j ] ; Stores the maximum from the suffix of the array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Stores the maximum sum of any pair ; Calculate the maximum sum ; Stores the maximum sum of any pair ; Stores the minimum of suffixes from the given array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Calculate the minimum sum ; Return the resultant difference ; Driver Code ; Function Call
function GetDiff ( A , N ) { let SuffMaxArr = Array ( N ) . fill ( 0 ) ; SuffMaxArr [ N - 1 ] = A [ N - 1 ] ; for ( let i = N - 2 ; i >= 0 ; -- i ) { SuffMaxArr [ i ] = Math . max ( SuffMaxArr [ i + 1 ] , A [ i + 1 ] ) ; } let MaximumSum = Number . MIN_VALUE ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMaxArr [ i ] ) MaximumSum = Math . max ( MaximumSum , A [ i ] + SuffMaxArr [ i ] ) ; } let MinimumSum = Number . MAX_VALUE ; let SuffMinArr = Array ( N ) . fill ( 0 ) ; SuffMinArr [ N - 1 ] = Number . MAX_VALUE ; for ( let i = N - 2 ; i >= 0 ; -- i ) { SuffMinArr [ i ] = Math . min ( SuffMinArr [ i + 1 ] , A [ i + 1 ] ) ; } for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMinArr [ i ] ) { MinimumSum = Math . min ( MinimumSum , A [ i ] + SuffMinArr [ i ] ) ; } } return Math . abs ( MaximumSum - MinimumSum ) ; } let arr = [ 2 , 4 , 1 , 3 , 7 , 5 , 6 ] ; let N = arr . length ; document . write ( GetDiff ( arr , N ) ) ;
Minimum number of swaps required to make parity of array elements same as their indices | Function to count the minimum number of swaps required to make the parity of array elements same as their indices ; Stores count of even and odd array elements ; Traverse the array ; Check if indices and array elements are not of the same parity ; If index is even ; Update even ; Update odd ; Condition for not possible ; Otherwise ; Driver code
function minimumSwaps ( arr , N ) { let even = 0 , odd = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != i % 2 ) { if ( i % 2 == 0 ) { even ++ ; } else { odd ++ ; } } } if ( even != odd ) { document . write ( - 1 ) ; } else { document . write ( even ) ; } } let arr = [ 3 , 2 , 7 , 6 ] ; let N = arr . length ; minimumSwaps ( arr , N ) ;
Smallest substring occurring only once in a given string | Function to find the smallest substring occurring only once ; Stores all occurences ; Generate all the substrings ; Avoid multiple occurences ; Append all substrings ; Take into account all the substrings ; Iterate over all unique substrings ; If frequency is 1 ; Append into fresh list ; Initialize a dictionary ; Append the keys ; Traverse the dictionary ; Return the minimum of dictionary ; Driver Code
function smallestSubstring ( a ) { let a1 = [ ] ; for ( let i = 0 ; i < a . length ; i ++ ) { for ( let j = i + 1 ; j <= a . length ; j ++ ) { if ( i != j ) a1 . push ( a . substring ( i , j ) ) ; } } let a2 = new Map ( ) ; for ( let s = 0 ; s < a1 . length ; s ++ ) { if ( a2 . has ( a1 [ s ] ) ) a2 . set ( a1 [ s ] , a2 . get ( a1 [ s ] ) + 1 ) ; else a2 . set ( a1 [ s ] , 1 ) ; } let freshlist = [ ] ; for ( let s of a2 . keys ( ) ) { if ( a2 . get ( s ) == 1 ) freshlist . push ( s ) ; } let dictionary = new Map ( ) ; for ( let s = 0 ; s < freshlist . length ; s ++ ) { dictionary . set ( freshlist [ s ] , freshlist [ s ] . length ) ; } let newlist = [ ] ; for ( let s of dictionary . keys ( ) ) newlist . push ( dictionary . get ( s ) ) ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < newlist . length ; i ++ ) ans = Math . min ( ans , newlist [ i ] ) ; return ans == Number . MAX_VALUE ? 0 : ans ; } let S = " " ; document . write ( smallestSubstring ( S ) ) ;
Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | Function to count the minimum number of insertions required ; Stores the number of insertions required ; Traverse the array ; Store the minimum array element ; Store the maximum array element ; Checking condition ; Increase current count ; Return the count of insertions ; Driver Code
function mininsert ( arr , K , N ) { var ans = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { var a = Math . min ( arr [ i ] , arr [ i + 1 ] ) ; var b = Math . max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } var arr = [ 2 , 10 , 25 , 21 ] ; var K = 2 ; var N = arr . length ; document . write ( mininsert ( arr , K , N ) ) ;
Sum of numbers formed by consecutive digits present in a given string | Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver Code
function sumOfDigits ( s ) { var curr = 0 ; var ret = 0 ; s . split ( ' ' ) . forEach ( ch => { if ( parseInt ( ch ) ) { curr = curr * 10 + ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; } else { ret += curr ; curr = 0 ; } } ) ; ret += curr ; return ret ; } var S = " " ; document . write ( sumOfDigits ( S ) ) ;
Find a triplet ( i , j , k ) from an array such that i < j < k and arr [ i ] < arr [ j ] > arr [ k ] | Function to find a triplet such that i < j < k and arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ; Traverse the array ; Condition to satisfy for the resultant triplet ; Otherwise , triplet doesn 't exist ; Driver Code
function print_triplet ( arr , n ) { for ( var i = 1 ; i <= n - 2 ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { document . write ( i - 1 + " " + i + " " + ( i + 1 ) ) ; return ; } } document . write ( - 1 ) ; } var arr = [ 4 , 3 , 5 , 2 , 1 , 6 ] ; var N = arr . length ; print_triplet ( arr , N ) ;
Check if an array element is concatenation of two elements from another array | Function to find elements present in the array b [ ] which are concatenation of any pair of elements in the array a [ ] ; Stores if there doesn 't any such  element in the array brr[] ; Stored the size of both the arrays ; Store the presence of an element of array a [ ] ; Traverse the array a [ ] ; Traverse the array b [ ] ; Traverse over all possible concatenations of b [ i ] ; Update right and left parts ; Check if both left and right parts are present in a [ ] ; Driver Code
function findConcatenatedNumbers ( a , b ) { var ans = true ; var n1 = a . length ; var n2 = b . length ; var cnt = new Map ( ) ; for ( var i = 0 ; i < n1 ; i ++ ) { cnt . set ( a [ i ] , 1 ) ; } for ( var i = 0 ; i < n2 ; i ++ ) { var left = b [ i ] ; var right = 0 ; var mul = 1 ; while ( left > 9 ) { right += ( left % 10 ) * mul ; left = parseInt ( left / 10 ) ; mul *= 10 ; if ( cnt . has ( left ) && cnt . has ( right ) ) { ans = false ; document . write ( b [ i ] + " " ) ; } } } if ( ans ) document . write ( " " ) ; } var a = [ 2 , 34 , 4 , 5 ] ; var b = [ 26 , 24 , 345 , 4 , 22 ] ; findConcatenatedNumbers ( a , b ) ;
Number of subarrays having even product | Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Input ; Length of an array ; Function call to count subarrays with even product
function evenproduct ( arr , length ) { var count = 0 ; var i , j ; for ( i = 0 ; i < length + 1 ; i ++ ) { var product = 1 ; for ( j = i ; j < length + 1 ; j ++ ) { product *= arr [ j ] ; if ( product % 2 == 0 ) ++ count ; } } document . write ( count ) ; } var arr = [ 7 , 5 , 4 , 9 ] ; var length = arr . length ; evenproduct ( arr , length ) ;
Count pairs of nodes having minimum distance between them equal to the difference of their distances from root | Stores the count of pairs ; Store the adjacency list of the connecting vertex ; Function for theto perform DFS traversal of the given tree ; Traverse the adjacency list of the current node u ; If the current node is the parent node ; Add number of ancestors , which is same as depth of the node ; Function for DFS traversal of the given tree ; Print the result ; Function to find the count of pairs such that the minimum distance between them is equal to the difference between distance of the nodes from root node ; Add edges to adj [ ] ; Driver Code
var ans = 0 ; var adj = Array . from ( Array ( 100001 ) , ( ) => Array ( ) ) ; function dfsUtil ( u , par , depth ) { adj [ u ] . forEach ( it => { if ( it != par ) { dfsUtil ( it , u , depth + 1 ) ; } } ) ; ans += depth ; } function dfs ( u , par , depth ) { dfsUtil ( u , par , depth ) ; document . write ( ans + " " ) ; } function countPairs ( edges ) { for ( var i = 0 ; i < edges . length ; i ++ ) { var u = edges [ i ] [ 0 ] ; var v = edges [ i ] [ 1 ] ; adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } dfs ( 1 , 1 , 1 ) ; } var edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 2 , 4 ] ] ; countPairs ( edges ) ;
Sum of decimal equivalents of binary node values in each level of a Binary Tree | Structure of a Tree Node ; Function to convert binary number to its equivalent decimal value ; Function to calculate sum of decimal equivalent of binary numbers of node values present at each level ; Push root node into queue ; Connect nodes at the same level to form a binary number ; Append the value of the current node to eachLvl ; Insert the Left child to queue , if its not NULL ; Insert the Right child to queue , if its not NULL ; Decrement length by one ; Stores the front element of the queue ; Add decimal equivalent of the binary number formed on the current level to ans ; Finally print ans ; Given Tree ; Function Call
class TreeNode { constructor ( ) { this . val = 0 ; this . left = null ; this . right = null ; } } ; function newNode ( key ) { var temp = new TreeNode ( ) ; temp . val = key ; temp . left = temp . right = null ; return temp ; } function convertBinaryToDecimal ( arr ) { var ans = 0 ; for ( var i of arr ) ans = ( ans << 1 ) | i ; return ans ; } function decimalEquilvalentAtEachLevel ( root ) { var ans = 0 ; var que = [ ] ; que . push ( root ) ; while ( true ) { var length = que . length ; if ( length == 0 ) break ; var eachLvl = [ ] ; while ( length > 0 ) { var temp = que [ 0 ] ; que . shift ( ) ; eachLvl . push ( temp . val ) ; if ( temp . left != null ) que . push ( temp . left ) ; if ( temp . right != null ) que . push ( temp . right ) ; length -= 1 ; } ans += convertBinaryToDecimal ( eachLvl ) ; } document . write ( ans ) ; } var root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 0 ) ; root . left . left = newNode ( 0 ) ; root . left . right = newNode ( 1 ) ; root . right . left = newNode ( 1 ) ; root . right . right = newNode ( 1 ) ; decimalEquilvalentAtEachLevel ( root ) ;
Minimize increments required to make differences between all pairs of array elements even | Function to find the minimum increments required to difference between all pairs of array elements even ; Store the count of odd and even numbers ; Traverse the array ; Increment evenCnt by 1 ; Increment oddCnt by 1 ; Print the minimum of oddCnt and eveCnt ; Driver code
function minimumOperations ( arr , N ) { let oddCnt = 0 , evenCnt = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCnt ++ ; } else { oddCnt ++ ; } } document . write ( Math . min ( oddCnt , evenCnt ) ) ; } let arr = [ 4 , 1 , 2 ] ; let N = arr . length ; minimumOperations ( arr , N ) ;
Minimum increments required to make array elements alternately even and odd | Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array arr [ ] ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code
function minIncr ( arr , n ) { let forEven = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 ) { if ( ( arr [ i ] % 2 ) == 0 ) forEven += 1 ; } else { if ( arr [ i ] % 2 ) forEven += 1 ; } } return Math . min ( forEven , n - forEven ) ; } let arr = [ 1 , 4 , 6 , 8 , 9 , 5 ] ; let n = arr . length ; document . write ( minIncr ( arr , n ) ) ;
Queries to find the minimum array sum possible by removing elements from either end | Function to find the minimum sum for each query after removing element from either ends till each value Q [ i ] ; Stores the prefix sum from both the ends of the array ; Traverse the array from front ; Insert it into the map m1 ; Traverse the array in reverse ; Insert it into the map m2 ; Traverse the query array ; Print the minimum of the two values as the answer ; Driver Code ; Function Call
function minOperations ( arr , N , Q , M ) { var m1 = new Map ( ) ; var m2 = new Map ( ) ; var front = 0 , rear = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { front += arr [ i ] ; m1 . set ( arr [ i ] , front ) ; } for ( i = N - 1 ; i >= 0 ; i -- ) { rear += arr [ i ] ; m2 . set ( arr [ i ] , rear ) ; } for ( i = 0 ; i < M ; i ++ ) { document . write ( Math . min ( m1 . get ( Q [ i ] ) , m2 . get ( Q [ i ] ) ) + " " ) ; } } var arr = [ 2 , 3 , 6 , 7 , 4 , 5 , 1 ] ; var N = arr . length ; var Q = [ 7 , 6 ] ; var M = Q . length ; minOperations ( arr , N , Q , M ) ;
Check if a string contains an anagram of another string as its substring | Function to check if string s2 contains anagram of the string s1 as its substring ; Stores frequencies of characters in substrings of s2 ; Stores frequencies of characters in s1 ; If length of s2 exceeds length of s1 ; Store frequencies of characters in first substring of length s1len in string s2 ; Perform Sliding Window technique ; If hashmaps are found to be identical for any substring ; Driver Code
function checkAnagram ( s1 , s2 ) { var s2hash = Array ( 26 ) . fill ( 0 ) ; var s1hash = Array ( 26 ) . fill ( 0 ) ; var s1len = s1 . length ; var s2len = s2 . length ; if ( s1len > s2len ) return false ; var left = 0 , right = 0 ; while ( right < s1len ) { s1hash [ s1 [ right ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; s2hash [ s2 [ right ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; right ++ ; } right -= 1 ; while ( right < s2len ) { var ans = true ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( s1hash [ i ] != s2hash [ i ] ) { ans = false ; } } if ( ans ) return true ; right ++ ; if ( right != s2len ) s2hash [ s2 [ right ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; s2hash [ s2 [ left ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -= 1 ; left ++ ; } return false ; } var s1 = " " ; var s2 = " " ; if ( checkAnagram ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | package whatever Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver code
function canPlace ( s , n , k ) { var cnt = 0 ; var i = 0 ; while ( i < n ) { if ( s . charAt ( i ) == ' ' ) { i += 2 ; } else { if ( i == 0 ) { if ( s . charAt ( i + 1 ) == ' ' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s . charAt ( i - 1 ) == ' ' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s . charAt ( i + 1 ) == ' ' && s . charAt ( i - 1 ) == ' ' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var S = " " ; var K = 1 ; var N = 5 ; canPlace ( S , N , K ) ;
Maximum number of intervals that an interval can intersect | Function to count the maximum number of intervals that an interval can intersect ; Store the required answer ; Traverse all the intervals ; Store the number of intersecting intervals ; Iterate in the range [ 0 , n - 1 ] ; Check if jth interval lies outside the ith interval ; Update the overall maximum ; Print the answer ; Driver Code ; Function Call
function findMaxIntervals ( v , n ) { var maxi = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var c = n ; for ( var j = 0 ; j < n ; j ++ ) { if ( v [ i ] [ 1 ] < v [ j ] [ 0 ] v [ i ] [ 0 ] > v [ j ] [ 1 ] ) { c -- ; } } maxi = Math . max ( c , maxi ) ; } document . write ( maxi ) ; } var arr = [ [ 1 , 2 ] , [ 3 , 4 ] , [ 2 , 5 ] ] ; var N = arr . length ; findMaxIntervals ( arr , N ) ;