codeparrot/xlcost-text-to-code huggingface dataset:
Make given segments non | Stores the details of the Segment ; Function to check whether the graph is bipartite or not ; Mark source node as visited ; Push the source vertex in queue ; Get the front of the queue ; Assign the color to the popped node ; Traverse the adjacency list of the node u ; If any node is visited & a different colors has been assigned , then return false ; Set visited [ x ] ; Push the node x into the queue ; Update color of node ; If the graph is bipartite ; Function to add an edge between the nodes u and v ; Function to check if the assignment of direction can be possible to all the segments , such that they do not intersect after a long period of time ; Stores the adjacency list of the created graph ; Initialize ; Generate all possible pairs ; If segments do not overlap ; Otherwise , the segments overlap ; If both segments have same speed , then add an edge ; Keep the track of visited nodes ; Iterate for all possible nodes ; Check whether graph is bipartite or not ; If the graph is bipartite ; Driver Code
class Node { constructor ( L , R , V ) { this . L = L ; this . R = R ; this . V = V ; } } function check ( Adj , Src , N , visited ) { let color = new Array ( N ) ; visited [ Src ] = true ; let q = [ ] ; q . push ( Src ) ; while ( q . length != 0 ) { let u = q . shift ( ) ; let Col = color [ u ] ; for ( let x = 0 ; x < Adj [ u ] . length ; x ++ ) { if ( visited [ Adj [ u ] [ x ] ] == true && color [ Adj [ u ] [ x ] ] == Col ) { return false ; } else if ( visited [ Adj [ u ] [ x ] ] == false ) { visited [ Adj [ u ] [ x ] ] = true ; q . push ( Adj [ u ] [ x ] ) ; color [ Adj [ u ] [ x ] ] = 1 - Col ; } } } return true ; } function addEdge ( Adj , u , v ) { Adj [ u ] . push ( v ) ; Adj [ v ] . push ( u ) ; } function isPossible ( Arr , N ) { let Adj = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) Adj [ i ] = [ ] ; for ( let i = 0 ; i < N - 1 ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] . R < Arr [ j ] . L Arr [ i ] . L > Arr [ j ] . R ) { continue ; } else { if ( Arr [ i ] . V == Arr [ j ] . V ) { addEdge ( Adj , i , j ) ; } } } } let visited = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) visited [ i ] = false ; for ( let i = 0 ; i < N ; i ++ ) { if ( visited [ i ] == false && Adj [ i ] . length > 0 ) { if ( check ( Adj , i , N , visited ) == false ) { document . write ( " " ) ; return ; } } } document . write ( " " ) ; } let arr = [ new Node ( 5 , 7 , 2 ) , new Node ( 4 , 6 , 1 ) , new Node ( 1 , 5 , 2 ) , new Node ( 6 , 5 , 1 ) ] ; let N = arr . length ; isPossible ( arr , N ) ;
Generate all numbers up to N in Lexicographical Order | JavaScript program for the above approach ; Driver code
function lexNumbers ( n ) { var sol = [ ] ; dfs ( 1 , n , sol ) ; document . write ( " " + sol [ 0 ] ) ; for ( var i = 1 ; i < sol . length ; i ++ ) document . write ( " " + sol [ i ] ) ; document . write ( " " ) ; } function dfs ( temp , n , sol ) { if ( temp > n ) return ; sol . push ( temp ) ; dfs ( temp * 10 , n , sol ) ; if ( temp % 10 != 9 ) dfs ( temp + 1 , n , sol ) ; } var n = 15 ; lexNumbers ( n ) ;
Minimum number of swaps required to sort an array of first N number | Function to find minimum swaps ; Initialise count variable ; If current element is not at the right position ; Swap current element with correct position of that element ; Increment for next index when current element is at correct position ; Driver code ; Function to find minimum swaps
function minimumSwaps ( arr ) { let count = 0 ; let i = 0 ; while ( i < arr . length ) { if ( arr [ i ] != i + 1 ) { while ( arr [ i ] != i + 1 ) { let temp = 0 ; temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; count ++ ; } } i ++ ; } return count ; } let arr = [ 2 , 3 , 4 , 1 , 5 ] ; document . write ( minimumSwaps ( arr ) ) ;
Recursive Insertion Sort | Recursive function to sort an array using insertion sort ; Base case ; Sort first n - 1 elements ; Insert last element at its correct position in sorted array . ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; Driver Method
function insertionSortRecursive ( arr , n ) { if ( n <= 1 ) return ; insertionSortRecursive ( arr , n - 1 ) ; let last = arr [ n - 1 ] ; let j = n - 2 ; while ( j >= 0 && arr [ j ] > last ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = last ; } let arr = [ 12 , 11 , 13 , 5 , 6 ] ; insertionSortRecursive ( arr , arr . length ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Recursive Bubble Sort | A function to implement bubble sort ; Base case ; One pass of bubble sort . After this pass , the largest element is moved ( or bubbled ) to end . ; swap arr [ i ] , arr [ i + 1 ] ; Largest element is fixed , recur for remaining array ; Driver code
function bubbleSort ( arr , n ) { if ( n == 1 ) return ; for ( var i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) { var temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } bubbleSort ( arr , n - 1 ) ; } var arr = [ 64 , 34 , 25 , 12 , 22 , 11 , 90 ] bubbleSort ( arr , arr . length ) ; document . write ( " " + " " ) ; for ( var i = 0 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Maximum possible difference of sum of two subsets of an array | Set 2 | JavaScript Program to implement the above approach ; Stores the positive elements ; Stores the negative elements ; Stores the count of 0 s ; Sum of all positive numbers ; Sum of all negative numbers ; Iterate over the array ; Stores the difference ; Sort the positive numbers in ascending order ; Sort the negative numbers in decreasing order ; Case 1 : Include both positive and negative numbers ; Put all numbers in subset A and one 0 in subset B ; Put all numbers in subset A except the smallest positive number which is put in B ; Put all numbers in subset B and one 0 in subset A ; Place the largest negative number in subset A and remaining in B ; Driver code
function maxSumAfterPartition ( arr , n ) { let pos = [ ] ; let neg = [ ] ; let zero = 0 ; let pos_sum = 0 ; let neg_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { pos . push ( arr [ i ] ) ; pos_sum += arr [ i ] ; } else if ( arr [ i ] < 0 ) { neg . push ( arr [ i ] ) ; neg_sum += arr [ i ] ; } else { zero ++ ; } } let ans = 0 ; pos . sort ( function ( a , b ) { return a - b } ) neg . sort ( function ( a , b ) { return b - a } ) if ( pos . length > 0 && neg . length > 0 ) { ans = ( pos_sum - neg_sum ) ; } else if ( pos . length > 0 ) { if ( zero > 0 ) { ans = ( pos_sum ) ; } else { ans = ( pos_sum - 2 * pos [ 0 ] ) ; } } else { if ( zero > 0 ) { ans = ( - 1 * neg_sum ) ; } else { ans = ( neg [ 0 ] - ( neg_sum - neg [ 0 ] ) ) ; } } return ans ; } let arr = [ 1 , 2 , 3 , - 5 , - 7 ] ; let n = arr . length ; document . write ( maxSumAfterPartition ( arr , n ) ) ;
Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values | Function to find the bitwise OR of array elements ; Stores the resultant maximum value of Bitwise XOR ; Traverse the array arr [ ] ; Return the maximum value res ; Driver code
function MaxXOR ( arr , N ) { var res = 0 ; for ( var i = 0 ; i < N ; i ++ ) { res |= arr [ i ] ; } return res ; } var arr = [ 1 , 5 , 7 ] ; var N = arr . length ; document . write ( MaxXOR ( arr , N ) ) ;
Count number of common elements between a sorted array and a reverse sorted array | Function to count the number of elements common in both the arrays ; Used to traverse array A [ ] and B [ ] from the front and the back ; Stores the count of numbers common in both array ; If A [ first ] is less than B [ second ] ; Increment the value of first ; IF B [ second ] is less than A [ first ] ; Decrement the value of second ; A [ first ] is equal to B [ second ] ; Increment the value of count ; Increment the value of first ; Decrement the value of second ; Return the value of count ; Driver Code
function countEqual ( A , B , N ) { let first = 0 ; let second = N - 1 ; let count = 0 ; while ( first < N && second >= 0 ) { if ( A [ first ] < B [ second ] ) { first ++ ; } else if ( B [ second ] < A [ first ] ) { second -- ; } else { count ++ ; first ++ ; second -- ; } } return count ; } let A = [ 2 , 4 , 5 , 8 , 12 , 13 , 17 , 18 , 20 , 22 , 309 , 999 ] ; let B = [ 109 , 99 , 68 , 54 , 22 , 19 , 17 , 13 , 11 , 5 , 3 , 1 ] ; let N = A . length ; document . write ( countEqual ( A , B , N ) ) ;
Queries to count Palindrome Numbers from a range whose sum of digits is a Prime Number | JavaScript program for the above approach ; Function to check if the number N is palindrome or not ; Store the value of N ; Store the reverse of number N ; Reverse temp and store in res ; If N is the same as res , then return true ; Function to find the sum of the digits of the number N ; Stores the sum of the digits ; Add the last digit of the number N to the sum ; Remove the last digit from N ; Return the resultant sum ; Function to check if N is prime or not ; If i is 1 or 0 , then return false ; Check if i is divisible by any number in the range [ 2 , n / 2 ] ; If n is divisible by i ; Function to precompute all the numbers till 10 ^ 5 that are palindromic and whose sum of digits is prime numbers ; Iterate over the range 1 to 10 ^ 5 ; If i is a palindrome number ; Stores the sum of the digits in i ; If the sum of digits in i is a prime number ; Find the prefix sum of arr [ ] ; Function to count all the numbers in the given ranges that are palindromic and the sum of digits is prime numbers ; Function Call to precompute all the numbers till 10 ^ 5 ; Traverse the given queries Q [ ] ; Print the result for each query ; Driver Code ; Function Call
let arr = [ ] ; for ( let m = 0 ; m < 100005 ; m ++ ) { arr [ m ] = 0 ; } function isPalindrome ( N ) { int temp = N ; let res = 0 ; while ( temp != 0 ) { let rem = temp % 10 ; res = res * 10 + rem ; temp = Math . floor ( temp / 10 ) ; } if ( res == N ) { return true ; } else { return false ; } } function sumOfDigits ( N ) { let sum = 0 ; while ( N != 0 ) { sum += N % 10 ; N = Math . floor ( N / 10 ) ; } return sum ; } function isPrime ( n ) { if ( n <= 1 ) { return false ; } for ( let i = 2 ; i <= Math . floor ( n / 2 ) ; ++ i ) { if ( n % i == 0 ) return false ; } return true ; } function precompute ( ) { for ( let i = 1 ; i <= 100000 ; i ++ ) { if ( isPalindrome ( i ) ) { let sum = sumOfDigits ( i ) ; if ( isPrime ( sum ) ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } else arr [ i ] = 0 ; } for ( let i = 1 ; i <= 100000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } function countNumbers ( Q , N ) { precompute ( ) ; for ( let i = 0 ; i < N ; i ++ ) { document . write ( ( arr [ Q [ i ] [ 1 ] ] - arr [ Q [ i ] [ 0 ] - 1 ] ) + " " ) ; } } let Q = [ [ 5 , 9 ] , [ 1 , 101 ] ] ; let N = Q . length ; countNumbers ( Q , N ) ;
Smallest number greater than or equal to N having sum of digits not exceeding S | Function to calculate sum digits of n ; Function to find the smallest possible integer satisfying the given condition ; If the sum of digits is already smaller than S ; Initialize variables ; Finding last kth digit ; Add remaining to make digit 0 ; If sum of digits does not exceed S ; Update k ; Given N and S ; Function call
function sum ( n ) { var res = 0 ; while ( n > 0 ) { res += n % 10 ; n /= 10 ; } return res ; } function smallestNumber ( n , s ) { if ( sum ( n ) <= s ) { return n ; } var ans = n , k = 1 ; for ( i = 0 ; i < 9 ; ++ i ) { var digit = ( ans / k ) % 10 ; var add = k * ( ( 10 - digit ) % 10 ) ; ans += add ; if ( sum ( ans ) <= s ) { break ; } k *= 10 ; } return ans ; } var N = 3 , S = 2 ; document . write ( smallestNumber ( N , S ) ) ;
Maximize count of Decreasing Consecutive Subsequences from an Array | Function to find the maximum number number of required subsequences ; Dictionary to store number of arrows available with height of arrow as key ; Stores the maximum count of possible subsequences ; Stores the count of possible subsequences ; Check if i - th element can be part of any of the previous subsequence ; Count of subsequences possible with arr [ i ] as the next element ; If more than one such subsequence exists ; Include arr [ i ] in a subsequence ; Otherwise ; Increase count of subsequence possible with arr [ i ] - 1 as the next element ; Start a new subsequence ; Increase count of subsequence possible with arr [ i ] - 1 as the next element ; Return the answer ; Driver code
function maxSubsequences ( arr , n ) { let map = new Map ( ) ; let maxCount = 0 ; let count ; for ( let i = 0 ; i < n ; i ++ ) { if ( map . has ( arr [ i ] ) ) { count = map [ arr [ i ] ] ; if ( count > 1 ) { map . add ( arr [ i ] , count - 1 ) ; } else map . delete ( arr [ i ] ) ; if ( arr [ i ] - 1 > 0 ) if ( map . has ( arr [ i ] - 1 ) ) map [ arr [ i ] - 1 ] ++ ; else map . set ( arr [ i ] - 1 , 1 ) ; } else { maxCount ++ ; if ( arr [ i ] - 1 > 0 ) if ( map . has ( arr [ i ] - 1 ) ) map [ arr [ i ] - 1 ] ++ ; else map . set ( arr [ i ] - 1 , 1 ) ; } } return maxCount ; } let n = 5 ; let arr = [ 4 , 5 , 2 , 1 , 4 ] ; document . write ( maxSubsequences ( arr , n ) ) ;
Remove the first and last occurrence of a given Character from a String |  ; Traverse the given String from the beginning ; If ch is found ; Traverse the given String from the end ; If ch is found ; Driver Code
Function to remove first and last occurrence of a given character from the given String * / function removeOcc ( s , ch ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] === ch ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; break ; } } for ( var i = s . length - 1 ; i > - 1 ; i -- ) { if ( s [ i ] === ch ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; break ; } } return s ; } var s = " " ; var ch = " " ; document . write ( removeOcc ( s , ch ) ) ;
Minimum steps for increasing and decreasing Array to reach either 0 or N | Function that finds the minimum steps to reach either 0 or N for given increasing and decreasing arrays ; Initialize variable to find the minimum element ; Find minimum element in increasing [ ] array ; Initialize variable to find the maximum element ; Find maximum element in decreasing [ ] array ; Find the minimum steps ; Print the minimum steps ; Given N ; Given increasing and decreasing array ; Find length of arrays increasing and decreasing ; Function call
function minSteps ( N , increasing , decreasing , m1 , m2 ) { var mini = 2147483647 ; var i ; for ( i = 0 ; i < m1 ; i ++ ) { if ( mini > increasing [ i ] ) mini = increasing [ i ] ; } var maxi = - 2147483648 ; for ( i = 0 ; i < m2 ; i ++ ) { if ( maxi < decreasing [ i ] ) maxi = decreasing [ i ] ; } var minSteps = Math . max ( maxi , N - mini ) ; document . write ( minSteps ) ; } var N = 7 ; var increasing = [ 3 , 5 ] ; var decreasing = [ 6 ] ; var m1 = increasing . length ; var m2 = decreasing . length ; minSteps ( N , increasing , decreasing , m1 , m2 ) ;
Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition | Function to find the minimum number of swaps ; New array to convert to 1 - based indexing ; Keeps count of swaps ; Check if it is an ' X ' position ; Corner Case ; Swap ; Prlet the minimum swaps ; Given Number N ; Given Permutation of N numbers ; Function Call
function solve ( P , n ) { let arr = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; arr [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) arr [ i + 1 ] = P [ i ] ; let cnt = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == i ) { let t = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ i ] ; arr [ i ] = t ; cnt ++ ; } } if ( arr [ n ] == n ) { let t = arr [ n - 1 ] ; arr [ n - 1 ] = arr [ n ] ; arr [ n ] = t ; cnt ++ ; } document . write ( cnt ) ; } let N = 9 ; let P = [ 1 , 2 , 4 , 9 , 5 , 8 , 7 , 3 , 6 ] ; solve ( P , N ) ;
Check if an array is Wave Array | Function to check if array is wave array arr : input array n : size of array ; Check the wave form * If arr [ 1 ] is greater than left and right * Same pattern will be followed by whole * elements , else reverse pattern * will be followed by array elements ; Check for last element ; Check for last element ; Array
function isWaveArray ( arr , n ) { let result = true ; if ( arr [ 1 ] > arr [ 0 ] && arr [ 1 ] > arr [ 2 ] ) { for ( let i = 1 ; i < n - 1 ; i += 2 ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { result = true ; } else { result = false ; break ; } } if ( result == true && n % 2 == 0 ) { if ( arr [ n - 1 ] <= arr [ n - 2 ] ) { result = false ; } } } else if ( arr [ 1 ] < arr [ 0 ] && arr [ 1 ] < arr [ 2 ] ) { for ( let i = 1 ; i < n - 1 ; i += 2 ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { result = true ; } else { result = false ; break ; } } if ( result == true && n % 2 == 0 ) { if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { result = false ; } } } return result ; } let arr = [ 1 , 3 , 2 , 4 ] ; let n = arr . length ; if ( isWaveArray ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count distinct sequences obtained by replacing all elements of subarrays having equal first and last elements with the first element any number of times | Function to count number of sequences satisfying the given criteria ; Stores the index of the last occurrence of the element ; Initialize an array to store the number of different sequences that are possible of length i ; Base Case ; If no operation is applied on ith element ; If operation is applied on ith element ; Update the last occurrence of curEle ; Finally , prlet the answer ; Driver Code
function countPossiblities ( arr , n ) { let lastOccur = new Array ( 100000 ) ; for ( let i = 0 ; i < n ; i ++ ) { lastOccur [ i ] = - 1 ; } dp = new Array ( n + 1 ) ; dp [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { let curEle = arr [ i - 1 ] ; dp [ i ] = dp [ i - 1 ] ; if ( lastOccur [ curEle ] != - 1 & lastOccur [ curEle ] < i - 1 ) { dp [ i ] += dp [ lastOccur [ curEle ] ] ; } lastOccur [ curEle ] = i ; } document . write ( dp [ n ] + " " ) ; } let arr = [ 1 , 2 , 1 , 2 , 2 ] ; let N = arr . length ; countPossiblities ( arr , N ) ;
Maximum sum possible from given Matrix by performing given operations | Function to print the maximum sum ; Dp table ; Initializing dp array with 0 s ; Base case ; Traverse each column ; Update answer for both rows ; Print the maximum sum ; Given array ; Number of Columns ; Function calls
function maxSum ( arr , n , m ) { let dp = new Array ( n ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < 2 ; i ++ ) { for ( let j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ m - 1 ] = arr [ 0 ] [ m - 1 ] ; dp [ 1 ] [ m - 1 ] = arr [ 1 ] [ m - 1 ] ; for ( let j = m - 2 ; j >= 0 ; j -- ) { for ( let i = 0 ; i < 2 ; i ++ ) { if ( i == 1 ) { dp [ i ] [ j ] = Math . max ( arr [ i ] [ j ] + dp [ 0 ] [ j + 1 ] , arr [ i ] [ j ] + dp [ 0 ] [ j + 2 ] ) ; } else { dp [ i ] [ j ] = Math . max ( arr [ i ] [ j ] + dp [ 1 ] [ j + 1 ] , arr [ i ] [ j ] + dp [ 1 ] [ j + 2 ] ) ; } } } document . write ( Math . max ( dp [ 0 ] [ 0 ] , dp [ 1 ] [ 0 ] ) ) ; } let arr = [ [ 1 , 50 , 21 , 5 ] , [ 2 , 10 , 10 , 5 ] ] ; let N = arr [ 0 ] . length ; maxSum ( arr , 2 , N ) ;
Maximum sum possible from given Matrix by performing given operations | Function to print the maximum sum possible by selecting at most one element from each column such that no consecutive pairs are selected from a single row ; Initialize variables ; Traverse each column ; Prvar answer ; Driver Code ; Numberof columns
function maxSum ( arr , n ) { var r1 = 0 , r2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { var temp = r1 ; r1 = Math . max ( r1 , r2 + arr [ 0 ] [ i ] ) ; r2 = Math . max ( r2 , temp + arr [ 1 ] [ i ] ) ; } document . write ( Math . max ( r1 , r2 ) ) ; } var arr = [ [ 1 , 50 , 21 , 5 ] , [ 2 , 10 , 10 , 5 ] ] ; var n = arr [ 0 ] . length ; maxSum ( arr , n ) ;
Count unimodal and non | JavaScript program for the above approach ; Function to calculate the factorials up to a number ; Calculate the factorial ; Function to find power ( a , b ) ; Iterate until b exists ; If b is divisible by 2 ; Decrease the value of b ; Return the answer ; Function that counts the unimodal and non - unimodal permutations of a given integer N ; Function Call for finding factorials up to N ; Function to count unimodal permutations ; Non - unimodal permutation is N ! - unimodal permutations ; Driver Code Given Number N ; Function Call
var mod = parseInt ( 1e9 + 7 ) ; var mx = 1000000 ; var fact = new Array ( mx + 1 ) . fill ( 0 ) ; function Calculate_factorial ( ) { fact [ 0 ] = 1 ; for ( var i = 1 ; i <= mx ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; fact [ i ] %= mod ; } } function UniModal_per ( a , b ) { var res = 1 ; while ( b > 0 ) { if ( b % 2 !== 0 ) res = res * a ; res %= mod ; a = a * a ; a %= mod ; b = parseInt ( b / 2 ) ; } return res ; } function countPermutations ( n ) { Calculate_factorial ( ) ; var uni_modal = UniModal_per ( 2 , n - 1 ) ; var nonuni_modal = fact [ n ] - uni_modal ; document . write ( uni_modal + " " + nonuni_modal ) ; return ; } var N = 4 ; countPermutations ( N ) ;
Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle | JavaScript program to find the longest subsequence possible that starts and ends with 1 and filled with 0 in the middle ; Prefix array to store the occurences of '1' and '0' ; Iterate over the length of the string ; If current character is '1' ; If current character is '0' ; Add '1' available for the first string ; Add '0' available for the second string ; Add '1' available for the third string ; Update answer ; Print the readonly result ; Driver code
function longestSubseq ( s , len ) { var ones = new Array ( len + 1 ) . fill ( 0 ) ; var zeroes = new Array ( len + 1 ) . fill ( 0 ) ; for ( var i = 0 ; i < len ; i ++ ) { if ( s [ i ] === " " ) { ones [ i + 1 ] = ones [ i ] + 1 ; zeroes [ i + 1 ] = zeroes [ i ] ; } else { zeroes [ i + 1 ] = zeroes [ i ] + 1 ; ones [ i + 1 ] = ones [ i ] ; } } var answer = - 2147483648 ; var x = 0 ; for ( var i = 0 ; i <= len ; i ++ ) { for ( var j = i ; j <= len ; j ++ ) { x += ones [ i ] ; x += zeroes [ j ] - zeroes [ i ] ; x += ones [ len ] - ones [ j ] ; answer = Math . max ( answer , x ) ; x = 0 ; } } document . write ( answer ) ; } var s = " " ; var len = s . length ; longestSubseq ( s , len ) ;
Largest Square in a Binary Matrix with at most K 1 s for multiple Queries | Javascript implementation to find the largest square in the matrix such that it contains atmost K 1 's ; Function to calculate the largest square with atmost K 1 s for Q queries ; Loop to solve for each query ; Traversing the each sub square and counting total ; Breaks when exceeds the maximum count ; Driver Code
var MAX = 100 ; function largestSquare ( matrix , R , C , q_i , q_j , K , Q ) { for ( var q = 0 ; q < Q ; q ++ ) { var i = q_i [ q ] ; var j = q_j [ q ] ; var min_dist = Math . min ( Math . min ( i , j ) , Math . min ( R - i - 1 , C - j - 1 ) ) ; var ans = - 1 ; for ( var k = 0 ; k <= min_dist ; k ++ ) { var count = 0 ; for ( var row = i - k ; row <= i + k ; row ++ ) for ( var col = j - k ; col <= j + k ; col ++ ) count += matrix [ row ] [ col ] ; if ( count > K ) break ; ans = 2 * k + 1 ; } document . write ( ans + " " ) ; } } var matrix = [ [ 1 , 0 , 1 , 0 , 0 ] , [ 1 , 0 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] , [ 1 , 0 , 0 , 1 , 0 ] ] ; var K = 9 , Q = 1 ; var q_i = [ 1 ] ; var q_j = [ 2 ] ; largestSquare ( matrix , 4 , 5 , q_i , q_j , K , Q ) ;