codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree | Function to calculate and return the sum of arr [ 0. . index ] ; Traverse ancestors of BITree [ index ] ; Update the sum of current element of BIT to ans ; Update index to that of the parent node in getSum ( ) view by subtracting LSB ( Least Significant Bit ) ; Function to update the Binary Index Tree by replacing all ancestors of index by their respective sum with val ; Traverse all ancestors and sum with ' val ' . ; Add ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Function to construct the Binary Indexed Tree for the given array ; Initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Function to obtain and return the index of lower_bound of k ; Store the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx ; Driver Code
function getSum ( BITree , index ) { let ans = 0 ; index += 1 ; while ( index > 0 ) { ans += BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } function updateBIT ( BITree , n , index , val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } function constructBITree ( arr , n ) { let BITree = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } function getLowerBound ( BITree , arr , n , k ) { let lb = - 1 ; let l = 0 , r = n - 1 ; while ( l <= r ) { let mid = Math . floor ( l + ( r - l ) / 2 ) ; if ( getSum ( BITree , mid ) >= k ) { r = mid - 1 ; lb = mid ; } else l = mid + 1 ; } return lb ; } function performQueries ( A , n , q ) { let BITree = constructBITree ( A , n ) ; for ( let i = 0 ; i < q . length ; i ++ ) { let id = q [ i ] [ 0 ] ; if ( id == 1 ) { let idx = q [ i ] [ 1 ] ; let val = q [ i ] [ 2 ] ; A [ idx ] += val ; updateBIT ( BITree , n , idx , val ) ; } else { let k = q [ i ] [ 1 ] ; let lb = getLowerBound ( BITree , A , n , k ) ; document . write ( lb + " " ) ; } } } let A = [ 1 , 2 , 3 , 5 , 8 ] ; let n = A . length ; let q = [ [ 1 , 0 , 2 ] , [ 2 , 5 , 0 ] , [ 1 , 3 , 5 ] ] ; performQueries ( A , n , q ) ;
Minimum number of leaves required to be removed from a Tree to satisfy the given condition | Stores the count of safe nodes ; Function to perform DFS on the Tree to obtain the count of vertices that are not required to be deleted ; Update cost to reach the vertex ; If the vertex does not satisfy the condition ; Otherwise ; Traverse its subtree ; Driver Code ; Stores the Tree ; Perform DFS ; Print the number of nodes to be deleted
var cnt = 0 ; function dfs ( val , cost , tr , u , s ) { s = s + cost [ u ] ; if ( s < 0 ) s = 0 ; if ( s > val [ u ] ) return ; cnt ++ ; for ( var i = 0 ; i < tr [ u ] . length ; i ++ ) { dfs ( val , cost , tr , tr [ u ] [ i ] , s ) ; } } var n = 9 ; var val = [ 88 , 22 , 83 , 14 , 95 , 91 , 98 , 53 , 11 ] ; var cost = [ - 1 , 24 , - 8 , 67 , 64 , 65 , 12 , - 80 , 8 ] ; var tr = Array . from ( Array ( n + 1 ) , ( ) => Array ( ) ) ; tr [ 0 ] . push ( 3 ) ; tr [ 0 ] . push ( 4 ) ; tr [ 4 ] . push ( 6 ) ; tr [ 6 ] . push ( 2 ) ; tr [ 2 ] . push ( 1 ) ; tr [ 2 ] . push ( 8 ) ; tr [ 8 ] . push ( 5 ) ; tr [ 5 ] . push ( 7 ) ; dfs ( val , cost , tr , 0 , 0 ) ; document . write ( n - cnt ) ;
Check if an Array is made up of Subarrays of continuous repetitions of every distinct element | Function to check if the array is made up of subarrays of repetitions ; Base Case ; Stores the size of current subarray ; If a different element is encountered ; If the previous subarray was a single element ; Reset to new subarray ; Increase size of subarray ; If last element differed from the second last element ; Driver code
function ContinuousElements ( a , n ) { if ( n == 1 ) return false ; let curr = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { if ( curr == 1 ) return false ; else curr = 0 ; } curr ++ ; } if ( curr == 1 ) return false ; return true ; } let a = [ 1 , 1 , 2 , 2 , 1 , 3 , 3 ] ; let n = a . length ; if ( ContinuousElements ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Size of all connected non | Javascript program to implement the above approach ; Function to find size of all the islands from the given matrix ; Initialize a queue for the BFS traversal ; Iterate until the queue is empty ; Top element of queue ; Pop the element ; Check for boundaries ; Check if current element is 0 ; Check if current element is 1 ; Mark the cell visited ; Incrementing the size ; Traverse all neighbors ; Return the answer ; Function to print size of each connections ; Stores the size of each connected non - empty ; Check if the cell is non - empty ; Function call ; Print the answer ; Driver code
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function BFS ( mat , row , col ) { var area = 0 ; var Q = [ ] ; Q . push ( new pair ( row , col ) ) ; while ( Q . length != 0 ) { var it = Q [ 0 ] ; Q . shift ( ) ; var r = it . first , c = it . second ; if ( r < 0 c < 0 r > 4 c > 4 ) continue ; if ( mat [ r ] [ c ] == 0 ) continue ; if ( mat [ r ] == 1 ) { mat [ r ] = 0 ; area ++ ; } Q . push ( new pair ( r + 1 , c ) ) ; Q . push ( new pair ( r - 1 , c ) ) ; Q . push ( new pair ( r , c + 1 ) ) ; Q . push ( new pair ( r , c - 1 ) ) ; } return area ; } function sizeOfConnections ( mat ) { var result = [ ] ; for ( var row = 0 ; row < 5 ; ++ row ) { for ( var col = 0 ; col < 5 ; ++ col ) { if ( mat [ row ] [ col ] == 1 ) { var area = BFS ( mat , row , col ) ; result . push ( area ) ; } } } for ( var val of result ) document . write ( val + " " ) ; } var mat = [ [ 1 , 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 , 1 ] , [ 1 , 0 , 0 , 1 , 1 ] , [ 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 1 , 1 ] ] ; sizeOfConnections ( mat ) ;
Find a pair in Array with second largest product | Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Sort the array ; Initialize smallest element of the array ; Initialize largest element of the array ; Prlet second largest product pair ; Given array ; Function Call
function maxProduct ( arr , N ) { if ( N < 3 ) { return ; } arr . sort ( ( a , b ) => a - b ) let smallest1 = arr [ 0 ] ; let smallest3 = arr [ 2 ] ; let largest1 = arr [ N - 1 ] ; let largest3 = arr [ N - 3 ] ; if ( smallest1 * smallest3 >= largest1 * largest3 ) { document . write ( smallest1 + " " + smallest3 ) ; } else { document . write ( largest1 + " " + largest3 ) ; } } let arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; let N = arr . length ; maxProduct ( arr , N ) ;
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Function to find the maximum subarray length of ones ; Stores the length , starting index and ending index of the subarrays ; S : starting index of the sub - array ; Traverse only continuous 1 s ; Calculate length of the sub - array ; v [ i ] [ 0 ] : Length of subarray v [ i ] [ 1 ] : Starting Index of subarray v [ i ] [ 2 ] : Ending Index of subarray ; If no such sub - array exists ; Traversing through the subarrays ; Update maximum length ; v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 : Count of zeros between the two sub - arrays ; Update length of both subarrays to the maximum ; Update length of both subarrays - 1 to the maximum ; Check if the last subarray has the maximum length ; Driver Code
function maxLen ( A , N ) { var v = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 1 ) { var s = i , len ; while ( A [ i ] == 1 && i < N ) { i ++ ; } len = i - s ; v . push ( [ len , s , i - 1 ] ) ; } } if ( v . length == 0 ) { return - 1 ; } var ans = 0 ; for ( var i = 0 ; i < v . length - 1 ; i ++ ) { ans = Math . max ( ans , v [ i ] [ 0 ] ) ; if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 2 ) { ans = Math . max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] ) ; } if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 1 ) { ans = Math . max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] - 1 ) ; } } ans = Math . max ( v [ v . length - 1 ] [ 0 ] , ans ) ; return ans ; } var arr = [ 1 , 0 , 1 , 0 , 0 , 1 ] ; var N = arr . length ; document . write ( maxLen ( arr , N ) ) ;
Detect cycle in Directed Graph using Topological Sort | JavaScript Program to implement the above approach ; Stack to store the visited vertices in the Topological Sort ; Store Topological Order ; Adjacency list to store edges ; To ensure visited vertex ; ( Function to perform ) DFS ; Set the vertex as visited ; Visit connected vertices ; Push into the stack on complete visit of vertex ; Function to check and return if a cycle exists or not ; Stores the position of vertex in topological order ; Pop all elements from stack ; Push element to get Topological Order ; Pop from the stack ; If parent vertex does not appear first ; Cycle exists ; Return false if cycle does not exist ; Function to add edges from u to v ; Driver Code ; Insert edges ; If cycle exist
var t , n , m , a ; var s = [ ] ; var tsort = [ ] ; var adj = Array . from ( Array ( 100001 ) , ( ) => Array ( ) ) ; var visited = Array ( 100001 ) . fill ( 0 ) ; function dfs ( u ) { visited [ u ] = 1 ; adj [ u ] . forEach ( it => { if ( visited [ it ] == 0 ) dfs ( it ) ; } ) ; s . push ( u ) ; } function check_cycle ( ) { var pos = new Map ( ) ; var ind = 0 ; while ( s . length != 0 ) { pos . set ( s [ s . length - 1 ] , ind ) ; tsort . push ( s [ s . length - 1 ] ) ; ind += 1 ; s . pop ( ) ; } var ans = false ; for ( var i = 0 ; i < n ; i ++ ) { adj [ i ] . forEach ( it => { if ( ( pos . has ( i ) ? pos . get ( i ) : 0 ) > ( pos . has ( it ) ? pos . get ( it ) : 0 ) ) { ans = true ; } } ) ; } ; return ans ; } function addEdge ( u , v ) { adj [ u ] . push ( v ) ; } n = 4 , m = 5 ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 0 ) ; addEdge ( 2 , 3 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( i ) ; } } if ( check_cycle ( ) ) document . write ( " " ) ; else document . write ( " " ) ;
Rearrange an Array such that Sum of same | Function to rearrange the array such that no same - indexed subset have sum equal to that in the original array ; Initialize a vector ; Iterate the array ; Sort the vector ; Shift of elements to the index of its next cyclic element ; Print the answer ; Driver Code
function printNewArray ( a , n ) { var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { v . push ( [ a [ i ] , i ] ) ; } v . sort ( ) ; var ans = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { ans [ v [ ( i + 1 ) % n ] [ 1 ] ] = v [ i ] [ 0 ] ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } var a = [ 4 , 1 , 2 , 5 , 3 ] ; var n = a . length ; printNewArray ( a , n ) ;
Minimize count of array elements to be removed to maximize difference between any pair up to K | Function to count the number of elements to be removed from the array based on the given condition ; Sort the array ; / Initialize the variable ; Iterate for all possible pairs ; Check the difference between the numbers ; Update the minimum removals ; Return the answer ; Driver Code
function min_remove ( arr , n , k ) { arr . sort ( ) ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { if ( arr [ j ] - arr [ i ] <= k ) { ans = Math . min ( ans , n - j + i - 1 ) ; } } } return ans ; } let k = 3 ; let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; document . write ( min_remove ( arr , n , k ) ) ;
Reduce a given Binary Array to a single element by removal of Triplets | Function to check if it is possible to reduce the array to a single element ; Stores frequency of 0 's ; Stores frequency of 1 's ; Condition for array to be reduced ; Otherwise ; Driver Code
function solve ( arr , n ) { var countzeroes = 0 ; var countones = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countzeroes ++ ; else countones ++ ; } if ( Math . abs ( countzeroes - countones ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 0 , 1 , 0 , 0 , 1 , 1 , 1 ] ; var n = arr . length ; solve ( arr , n ) ;
Nearest smaller character to a character K from a Sorted Array | Function to return the nearest smaller letacter ; Stores the nearest smaller letacter ; Iterate till starts cross end ; Find the mid element ; Check if K is found ; Check if current letacter is less than K ; Increment the start ; Otherwise ; Increment end ; Return the letacter ; Driver Code
function bs ( ar , n , ele ) { let start = 0 ; let end = n - 1 ; let ch = ' ' ; while ( start <= end ) { let mid = start + Math . floor ( ( end - start ) / 2 ) ; if ( ar [ mid ] == ele ) end = mid - 1 ; else if ( ar [ mid ] < ele ) { ch = ar [ mid ] ; start = mid + 1 ; } else end = mid - 1 ; } return ch ; } let ar = [ ' ' , ' ' , ' ' , ' ' ] ; let n = ar . length ; let K = ' ' ; let ch = bs ( ar , n , K ) ; if ( ch == ' ' ) document . write ( " " ) ; else document . write ( ch ) ;
Largest possible Subset from an Array such that no element is K times any other element in the Subset | Function to find the maximum size of the required subset ; Size of the array ; Sort the array ; Stores which index is included or excluded ; Stores the indices of array elements ; Count of pairs ; Iterate through all the element ; If element is included ; Check if a [ i ] * k is present in the array or not ; Increase count of pair ; Exclude the pair ; Driver code
function findMaxLen ( a , k ) { let n = a . length ; a . sort ( ) ; let vis = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { mp . set ( a [ i ] , i ) ; } let c = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == false ) { let check = a [ i ] * k ; if ( mp . has ( check ) ) { c ++ ; vis [ mp . get ( check ) ] = true ; } } } return n - c ; } let K = 3 ; let arr = [ 1 , 4 , 3 , 2 ] ; document . write ( findMaxLen ( arr , K ) ) ;
Minimize length of prefix of string S containing all characters of another string T | JavaScript program for the above approach ; Base Case - if T is empty , it matches 0 length prefix ; Convert strings to lower case for uniformity ; Update dictCount to the letter count of T ; If new character is found , initialize its entry , and increase nUnique ; Increase count of ch ; Iterate from 0 to N ; i - th character ; Skip if ch not in targetStr ; Decrease Count ; If the count of ch reaches 0 , we do not need more ch , and can decrease nUnique ; If nUnique reaches 0 , we have found required prefix ; Otherwise ; Driver code
function getPrefixLength ( srcStr , targetStr ) { if ( targetStr . length === 0 ) return 0 ; srcStr = srcStr . toLowerCase ( ) ; targetStr = targetStr . toLowerCase ( ) ; var dictCount = { } ; var nUnique = 0 ; for ( const ch of targetStr ) { if ( dictCount . hasOwnProperty ( ch ) !== true ) { nUnique += 1 ; dictCount [ ch ] = 0 ; } dictCount [ ch ] += 1 ; } for ( var i = 0 ; i < srcStr . length ; i ++ ) { var ch = srcStr [ i ] ; if ( dictCount . hasOwnProperty ( ch ) !== true ) continue ; dictCount [ ch ] -= 1 ; if ( dictCount [ ch ] === 0 ) nUnique -= 1 ; if ( nUnique === 0 ) return i + 1 ; } return - 1 ; } var S = " " ; var T = " " ; document . write ( getPrefixLength ( S , T ) ) ;
Minimize length of Substrings containing at least one common Character | Function to check and return if subStrings of length mid has a common character a ; Length of the String ; Initialise the first occurrence of character a ; Check that distance b / w the current and previous occurrence of character a is less than or equal to mid ; If distance exceeds mid ; Function to check for all the alphabets , if subStrings of length mid have a character common ; Check for all 26 alphabets ; Check that char i + a is common in all the subStrings of length mid ; If no characters is common ; Function to calculate and return the minm length of subStrings ; Initialise low and high ; Perform binary search ; Update mid ; Check if one common character is present in the length of the mid ; Returns the minimum length that contain one common character ; Function to check if all characters are distinct ; Driver Code
function check ( str , mid , a ) { var n = str . length ; var previous = - 1 , i ; for ( i = 0 ; i < n ; ++ i ) { if ( str [ i ] === a ) { if ( i - previous > mid ) { return false ; } previous = i ; } } if ( i - previous > mid ) return false ; else return true ; } function possible ( str , mid ) { for ( var i = 0 ; i < 26 ; ++ i ) { if ( check ( str , mid , String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ) ) return true ; } return false ; } function findMinLength ( str ) { var low = 1 , high = str . length ; while ( low <= high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( possible ( str , mid ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } function ifAllDistinct ( str ) { var s = new Array ( ) ; var temp = str . split ( " " ) ; for ( const c of temp ) { s . push ( c ) ; } var set = new Set ( s ) ; return set . size === str . length ; } var str = " " ; if ( ifAllDistinct ( str ) ) document . write ( - 1 + " " ) ; else document . write ( findMinLength ( str . split ( " " ) ) ) ;
Maximize product of absolute index difference with K | Function returns maximum possible value of k ; Pointer i make sure that A [ i ] will result in max k ; Stores maximum possible k ; Possible value of k for current pair ( A [ i ] and A [ j ] ) ; If current value exceeds k ; Update the value of k ; Update pointer i ; Return the maxm possible k ; Driver code
function solve ( A , N ) { let i = 0 ; let k = 0 ; for ( let j = 1 ; j < N ; j ++ ) { let tempK = Math . min ( A [ i ] , A [ j ] ) / ( j - i ) ; if ( tempK > k ) { k = tempK ; } if ( A [ j ] >= A [ i ] / ( j - i ) ) i = j ; } return k ; } let A = [ 10 , 5 , 12 , 15 , 8 ] ; let N = A . length ; document . write ( solve ( A , N ) ) ;
Split Array into min number of subsets with difference between each pair greater than 1 | Function to split the array into minimum number of subsets with difference strictly > 1 ; Sort the array ; Traverse through the sorted array ; Check the pairs of elements with difference 1 ; If we find even a single pair with difference equal to 1 , then 2 partitions else only 1 partition ; Prlet the count of partitions ; Given array ; Size of the array ; Function call
function split ( arr , n ) { arr . sort ( ) ; let count = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } document . write ( count ) ; } let arr = [ 2 , 4 , 6 ] ; let n = arr . length ; split ( arr , n ) ;
Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver Code
function check ( str1 , str2 ) { var mark = Array ( 26 ) . fill ( false ) ; var n = str1 . length , m = str2 . length ; for ( var i = 0 ; i < n ; i ++ ) { mark [ str1 [ i ] - ' ' ] = true ; } for ( var i = 0 ; i < m ; i ++ ) { if ( mark [ str2 [ i ] - ' ' ] ) return true ; } return false ; } var str1 = " " , str2 = " " ; if ( check ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Generate K co | Function prints the required pairs ; First co - prime pair ; As a pair ( 1 n ) has already been Printed ; If i is a factor of N ; Since ( i , i ) won 't form  a coprime pair ; Driver Code
function FindPairs ( n , k ) { document . write ( 1 + " " + n + " " ) ; k -- ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { document . write ( 1 + " " + i + " " ) ; k -- ; if ( k == 0 ) break ; if ( i != n / i ) { document . write ( 1 + " " + n / i + " " ) ; k -- ; } if ( k == 0 ) break ; } } } let N = 100 ; let K = 5 ; FindPairs ( N , K ) ;
Minimum edges required to make a Directed Graph Strongly Connected | Perform DFS to count the in - degree and out - degree of the graph ; Mark the source as visited ; Traversing adjacent nodes ; Mark out - degree as 1 ; Mark in - degree as 1 ; If not visited ; DFS Traversal on adjacent vertex ; Function to return minimum number of edges required to make the graph strongly connected ; For Adjacency List ; Create the Adjacency List ; Initialize the in - degree array ; Initialize the out - degree array ; Initialize the visited array ; Perform DFS to count in - degrees and out - degreess ; To store the result ; To store total count of in - degree and out - degree ; Find total in - degree and out - degree ; Calculate the minimum edges required ; Return the minimum edges ; Driver Code ; Function call
function dfs ( u , adj , vis , inDeg , outDeg ) { vis [ u ] = 1 ; for ( var v of adj [ u ] ) { outDeg [ u ] = 1 ; inDeg [ v ] = 1 ; if ( vis [ v ] == 0 ) { dfs ( v , adj , vis , inDeg , outDeg ) ; } } } function findMinimumEdges ( source , N , M , dest ) { var adj = Array . from ( Array ( N + 1 ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < M ; i ++ ) { adj ] . push ( dest [ i ] ) ; } var inDeg = Array ( N + 1 ) . fill ( 0 ) ; var outDeg = Array ( N + 1 ) . fill ( 0 ) ; var vis = Array ( N + 1 ) . fill ( 0 ) ; dfs ( 1 , adj , vis , inDeg , outDeg ) ; var minEdges = 0 ; var totalIndegree = 0 ; var totalOutdegree = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( inDeg [ i ] == 1 ) totalIndegree ++ ; if ( outDeg [ i ] == 1 ) totalOutdegree ++ ; } minEdges = Math . max ( N - totalIndegree , N - totalOutdegree ) ; return minEdges ; } var N = 5 , M = 5 ; var source = [ 1 , 3 , 1 , 3 , 4 ] ; var destination = [ 2 , 2 , 3 , 4 , 5 ] ; document . write ( findMinimumEdges ( source , N , M , destination ) ) ;
Count of subarrays which forms a permutation from given Array elements | Function returns the required count ; Store the indices of the elements present in A [ ] . ; Store the maximum and minimum index of the elements from 1 to i . ; Update maxi and mini , to store minimum and maximum index for permutation of elements from 1 to i + 1 ; If difference between maxi and mini is equal to i ; Increase count ; Return final count ; Driver Code
function PermuteTheArray ( A , n ) { var arr = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ A [ i ] - 1 ] = i ; } var mini = n , maxi = 0 ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { mini = Math . min ( mini , arr [ i ] ) ; maxi = Math . max ( maxi , arr [ i ] ) ; if ( maxi - mini == i ) count ++ ; } return count ; } var A = [ 4 , 5 , 1 , 3 , 2 , 6 ] ; document . write ( PermuteTheArray ( A , 6 ) ) ;