codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest subsequence whose XOR value is odd | Function for find max XOR subsequence having odd value ; Initialize odd and even count ; Count the number of odd and even numbers in given array ; If all values are odd in given array ; If all values are even in given array ; If both odd and even are present in given array ; Given array arr [ ] ; Function Call
function maxXORSubsequence ( arr , n ) { let odd = 0 , even = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } let maxlen ; if ( odd == n ) { if ( odd % 2 == 0 ) maxlen = n - 1 ; else maxlen = n ; } else if ( even == n ) { maxlen = 0 ; } else { if ( odd % 2 == 0 ) maxlen = even + odd - 1 ; else maxlen = even + odd ; } return maxlen ; } let arr = [ 2 , 3 , 4 , 5 , 6 , 7 ] ; let n = arr . length ; document . write ( maxXORSubsequence ( arr , n ) ) ;
Count of pairs with sum N from first N natural numbers | Function to return the count of pairs ; If n is even ; Count of pairs ; Otherwise ; Driver code
function numberOfPairs ( n ) { if ( n % 2 == 0 ) return ( n / 2 - 1 ) ; else return ( n / 2 ) ; } let n = 8 ; document . write ( numberOfPairs ( n ) ) ;
Count pair of strings whose concatenation has every vowel | Function to return the count of all concatenated string with each vowel at least once ; Concatenating all possible pairs of string ; Creating an array which checks , the presence of each vowel ; Checking for each vowel by traversing the concatenated string ; Checking if all the elements are set in vowel [ ] ; Check if all vowels are present or not ; Return the final count ; Given array of strings ; Function Call
function good_pair ( str , N ) { let countStr = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { let res = str [ i ] + str [ j ] ; let vowel = new Array ( 5 ) ; vowel . fill ( 0 ) ; for ( let k = 0 ; k < res . length ; k ++ ) { if ( res [ k ] == ' ' ) vowel [ 0 ] = 1 ; else if ( res [ k ] == ' ' ) vowel [ 1 ] = 1 ; else if ( res [ k ] == ' ' ) vowel [ 2 ] = 1 ; else if ( res [ k ] == ' ' ) vowel [ 3 ] = 1 ; else if ( res [ k ] == ' ' ) vowel [ 4 ] = 1 ; } let temp = 0 ; for ( let ind = 0 ; ind < 5 ; ind ++ ) { if ( vowel [ ind ] == 1 ) temp ++ ; } if ( temp == 5 ) countStr ++ ; } } return countStr ; } let arr = [ " " , " " ] ; let N = arr . length ; document . write ( good_pair ( arr , N ) ) ;
Find sum of exponents of prime factors of numbers 1 to N | Function to implement sieve of erastosthenes ; Create a boolean array and initialize all entries as false ; Initializing smallest factor equal to 2 for all the even numbers ; Iterate for odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " ; Function to generate prime factors and its power ; s [ i ] is going to store smallest prime factor of i ; Current prime factor of N ; Power of current prime factor ; Calculating prime factors and their powers sum ; Increment the count and continue the process ; Add count to the sum ; Reinitialize count ; Return the result ; Function to find the sum of all the power of prime factors of N ; Iterate for in [ 2 , N ] ; Given Number N ; Function Call
function sieveOfEratosthenes ( N , s ) { let prime = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( let i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( let j = i ; j * i <= N ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } function generatePrimeFactors ( N ) { let s = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; let sum = 0 ; sieveOfEratosthenes ( N , s ) ; let curr = s [ N ] ; let cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } sum = sum + cnt ; curr = s [ N ] ; cnt = 1 ; } return sum ; } function findSum ( N ) { let sum = 0 ; for ( let i = 2 ; i <= N ; i ++ ) { sum += generatePrimeFactors ( i ) ; } document . write ( sum + " " ) ; } let N = 4 ; findSum ( N ) ;
Minimum LCM of all subarrays of length at least 2 | Function to find LCM of two numbers ; Initialise lcm value ; Check for divisibility of a and b by the lcm ; Function to find the Minimum LCM of all subarrays of length greater than 1 ; Store the minimum LCM ; Traverse the array ; Find LCM of consecutive element ; Check if the calculated LCM is less than the minLCM then update it ; Print the minimum LCM ; Given array arr [ ] ; Size of the array ; Function Call
function LCM ( a , b ) { let lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } function findMinLCM ( arr , n ) { let minLCM = Number . MAX_VALUE ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } document . write ( minLCM + " " ) ; } let arr = [ 4 , 8 , 12 , 16 , 20 , 24 ] ; let n = arr . length ; findMinLCM ( arr , n ) ;
Minimum integer with at most K bits set such that their bitwise AND with N is maximum | Function to find the integer with maximum bitwise with N ; Store answer in the bitset Initialized with 0 ; To maintain the count of set bits that should exceed k ; Start traversing from the Most significant if that bit is set in n then we will set in our answer i . e in X ; Checking if the ith bit is set in n or not ; Converting into integer ; Driver function ; Function Call
function find_max ( n , k ) { var X = Array . from ( { length : 32 } , ( _ , i ) => 0 ) ; var cnt = 0 ; for ( i = 31 ; i >= 0 && cnt != k ; i -- ) { if ( ( n & ( 1 << i ) ) != 0 ) { X [ i ] = 1 ; cnt ++ ; } } var s = " " ; for ( i = 31 ; i >= 0 ; i -- ) s += X [ i ] == 0 ? ' ' : ' ' ; return parseInt ( s , 2 ) ; } var n = 10 , k = 2 ; document . write ( find_max ( n , k ) ) ;
Count of unique digits in a given number N | Function that returns the count of unique digits of the given number ; Initialize a variable to store count of unique digits ; Initialize cnt array to store digit count ; Iterate through the digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Iterate through the cnt array ; If frequency of digit is 1 ; Increment the count of unique digits ; Return the count / of unique digit ; Given array arr [ ] ; Function Call
function countUniqueDigits ( N ) { let res = 0 ; let cnt = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ; while ( N > 0 ) { let rem = N % 10 ; cnt [ rem ] ++ ; N = Math . floor ( N / 10 ) ; } for ( let i = 0 ; i < 10 ; i ++ ) { if ( cnt [ i ] == 1 ) { res ++ ; } } return res ; } let N = 2234262 ; document . write ( countUniqueDigits ( N ) ) ;
Check if frequency of each element in given array is unique or not | Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Given array arr [ ] ; Function call ; Print the result
function checkUniqueFrequency ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } let uniqueFreq = new Set ( ) ; for ( let [ key , value ] of freq . entries ( ) ) { if ( uniqueFreq . has ( value ) ) return false ; else uniqueFreq . add ( value ) ; } return true ; } let arr = [ 1 , 1 , 2 , 5 , 5 ] ; let n = arr . length ; let res = checkUniqueFrequency ( arr , n ) ; if ( res ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Sum of first N natural numbers with all powers of 2 added twice | Function to raise N to the power P and return the value ; Function to calculate the log base 2 of an integer ; Calculate log2 ( N ) indirectly using log ( ) method ; Function to calculate and return the required sum ; Sum of first N natural numbers ; Sum of all powers of 2 up to N ; Driver Code
function power ( N , P ) { return Math . pow ( N , P ) ; } function Log2 ( N ) { let result = ( Math . floor ( Math . log ( N ) / Math . log ( 2 ) ) ) ; return result ; } function specialSum ( n ) { let sum = n * ( n + 1 ) / 2 ; let a = Log2 ( n ) ; sum = sum + power ( 2 , a + 1 ) - 1 ; return sum ; } let n = 4 ; document . write ( specialSum ( n ) + " " ) ;
Rearrange an array such that product of every two consecutive elements is a multiple of 4 | Function to rearrange array elements such that the every two consecutive elements is a multiple of 4 ; If element is odd ; Odd ; If element is divisible by 4 ; Divisible by 4 ; If element is not divisible by 4 ; Even but not divisible by 4 ; Condition for rearrangement to be possible ; Print ODD . get ( i ) and FOUR . get ( i ) consecutively ; Print the remaining FOUR . get ( i ) , if any ; Condition for rearrangement to be possible ; Print ODD . get ( i ) and FOUR . get ( i ) consecutively ; Print the remaining FOUR . get ( i ) , if any ; Print the NON_FOUR . get ( i ) elements at the end ; No possible configuration ; Driver Code
function Permute ( arr , n ) { let odd = 0 , four = 0 ; let non_four = 0 ; let ODD = [ ] ; let FOUR = [ ] ; let NON_FOUR = [ ] ; for ( let x = 0 ; x < arr . length ; x ++ ) { if ( arr [ x ] % 2 != 0 ) { odd ++ ; ODD . push ( arr [ x ] ) ; } else if ( arr [ x ] % 4 == 0 ) { four ++ ; FOUR . push ( arr [ x ] ) ; } else { non_four ++ ; NON_FOUR . push ( arr [ x ] ) ; } } if ( non_four == 0 && four >= odd - 1 ) { let x = ODD . length ; let y = FOUR . length ; let i ; for ( i = 0 ; i < x ; i ++ ) { document . write ( ODD [ i ] + " " ) ; if ( i < y ) document . write ( FOUR [ i ] + " " ) ; } while ( i < y ) document . write ( FOUR [ i ] + " " ) ; document . write ( " " ) ; } else if ( non_four > 0 && four >= odd ) { let x = ODD . length ; let y = FOUR . length ; let i ; for ( i = 0 ; i < x ; i ++ ) { document . write ( ODD [ i ] + " " ) ; if ( i < y ) document . write ( FOUR [ i ] + " " ) ; } while ( i < y ) document . write ( FOUR [ i ] + " " ) ; for ( let j = 0 ; j < NON_FOUR . length ; j ++ ) document . write ( NON_FOUR [ j ] + " " ) ; document . write ( " " ) ; } else document . write ( " " ) ; } let arr = [ 2 , 7 , 1 , 8 , 2 , 8 ] Permute ( arr , arr . length ) ;
Construct the smallest possible Array with given Sum and XOR | Function to find array ; Array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Given sum and value of Bitwise XOR ; Function call
function findArray ( sum , xorr ) { if ( xorr > sum sum % 2 != xorr % 2 ) { System . out . println ( " " ) ; return ; } if ( sum == xorr ) { if ( sum == 0 ) document . write ( " " + " " ) ; else document . write ( " " + 1 ) ; document . write ( " " + sum ) ; return ; } var mid = ( sum - xorr ) / 2 ; if ( xorr == 1 && mid == 1 ) { document . write ( " " + 3 ) ; document . write ( " " + xorr + " " + mid + " " + mid ) ; } else { document . write ( " " + 2 ) ; document . write ( " " ) document . write ( " " + ( xorr + mid ) + " " + mid ) ; } } var sum = 4 , xorr = 2 ; findArray ( sum , xorr ) ;
Smallest number greater than X which is K | Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary string ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the final string ; Driver Code
function Kperiodicinteger ( X , N , K ) { var temp = X for ( var i = 0 ; i < K ; i ++ ) { var j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( parseInt ( X . join ( ' ' ) ) < parseInt ( temp . join ( ' ' ) ) ) { return X . join ( ' ' ) ; } var POS ; for ( var i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != ' ' ) { X [ i ] = String . fromCharCode ( X [ i ] . charCodeAt ( 0 ) + 1 ) ; POS = i ; break ; } } for ( var i = POS + 1 ; i < K ; i ++ ) { X [ i ] = ' ' ; } for ( var i = 0 ; i < K ; i ++ ) { var j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return X . join ( ' ' ) ; } var N = 4 , K = 2 ; var X = " " ; document . write ( Kperiodicinteger ( X . split ( ' ' ) , N , K ) ) ;
Maximum cost path in an Undirected Graph such that no edge is visited twice in a row | Javascript program for the above approach ; To store the resulting sum of the cost ; To store largest cost leaf vertex ; DFS Traversal to find the update the maximum cost of from any node to leaf ; Mark vertex as visited ; Store vertex initial cost ; Initially assuming edge not to be traversed ; Back edge found so , edge can be part of traversal ; New vertex is found ; Bitwise AND the current check with the returned check by the previous DFS Call ; Adds parent and its children cost ; Updates total cost of parent including child nodes ; Edge is part of the cycle ; push cost of vertex to the answer ; Updates the largest cost leaf vertex ; Function to find the maximum cost from source vertex such that no two edges is traversed twice ; DFS call ; Print the maximum cost ; Driver Code ; Cost Array ; Given Graph ; Given Source Node ; Function call
var N = 100000 ; var canTake = 0 ; var best = 0 ; var dp = Array ( N ) . fill ( 0 ) ; var vis = Array ( N ) . fill ( false ) ; function dfs ( g , cost , u , pre ) { vis [ u ] = true ; dp [ u ] = cost [ u ] ; var check = true ; var cur = cost [ u ] ; for ( var x of g [ u ] ) { if ( vis [ x ] && x != pre ) { check = false ; } else if ( ! vis [ x ] ) { check = dfs ( g , cost , x , u ) ? false : true ; cur = Math . max ( cur , cost [ u ] + dp [ x ] ) ; } } dp [ u ] = cur ; if ( ! check ) { canTake += cost [ u ] ; } else { best = Math . max ( best , dp [ u ] ) ; } return check ; } function FindMaxCost ( g , cost , source ) { dfs ( g , cost , source , - 1 ) ; document . write ( canTake + best ) ; } var n = 5 , m = 5 ; var cost = [ 2 , 2 , 8 , 6 , 9 ] ; var g = Array . from ( Array ( n ) , ( ) => Array ( ) ) ; g [ 0 ] . push ( 1 ) ; g [ 1 ] . push ( 0 ) ; g [ 0 ] . push ( 2 ) ; g [ 2 ] . push ( 0 ) ; g [ 0 ] . push ( 3 ) ; g [ 3 ] . push ( 0 ) ; g [ 1 ] . push ( 2 ) ; g [ 2 ] . push ( 1 ) ; g [ 1 ] . push ( 4 ) ; g [ 4 ] . push ( 1 ) ; var source = 1 ; FindMaxCost ( g , cost , source ) ;
Reverse a subarray of the given array to minimize the sum of elements at even position | Javascript implementation to reverse a subarray of the given array to minimize the sum of elements at even position ; Function that will give the max negative value ; Check for count greater than 0 as we require only negative solution ; Function to print the minimum sum ; Taking sum of only even index elements ; Initialize two vectors v1 , v2 ; v1 will keep account for change if 1 th index element goes to 0 ; v2 will keep account for change if 1 th index element goes to 2 ; Get the max negative value ; Driver code
var N = 3 ; function after_rev ( v ) { var mini = 0 , count = 0 ; for ( var i = 0 ; i < v . length ; i ++ ) { count += v [ i ] ; if ( count > 0 ) count = 0 ; if ( mini > count ) mini = count ; } return mini ; } function print ( arr ) { var sum = 0 ; for ( var i = 0 ; i < N ; i += 2 ) sum += arr [ i ] ; var v1 = [ ] , v2 = [ ] ; for ( var i = 0 ; i + 1 < N ; i += 2 ) v1 . push ( arr [ i + 1 ] - arr [ i ] ) ; for ( var i = 1 ; i + 1 < N ; i += 2 ) v2 . push ( arr [ i ] - arr [ i + 1 ] ) ; var change = Math . min ( after_rev ( v1 ) , after_rev ( v2 ) ) ; if ( change < 0 ) sum += change ; document . write ( sum ) ; } var arr = [ 0 , 1 , 4 , 3 ] ; print ( arr ) ;
Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver Code
function moduloEquality ( A , B , n , m ) { var mapA = new Map ( ) , mapB = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( mapA . has ( A [ i ] ) ) mapA . set ( A [ i ] , mapA . get ( A [ i ] ) + 1 ) else mapA . set ( A [ i ] , 1 ) if ( mapB . has ( B [ i ] ) ) mapB . set ( B [ i ] , mapB . get ( B [ i ] ) + 1 ) else mapB . set ( B [ i ] , 1 ) } var possibleValues = new Set ( ) ; var FirstElement = B [ 0 ] ; for ( var i = 0 ; i < n ; i ++ ) { var cur = A [ i ] ; possibleValues . add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } var ans = 1000000000 ; possibleValues . forEach ( it => { var possible = true ; mapA . forEach ( ( value , key ) => { if ( value != mapB . get ( ( key + it ) % m ) ) { possible = false ; } } ) ; if ( possible ) { ans = Math . min ( ans , it ) ; } } ) ; return ans ; } var n = 4 ; var m = 3 ; var A = [ 0 , 0 , 2 , 1 ] ; var B = [ 2 , 0 , 1 , 1 ] ; document . write ( moduloEquality ( A , B , n , m ) ) ;
Count all indices of cyclic regular parenthesis | Function to find all indices which cyclic shift leads to get balanced parenthesis ; Create auxiliary array ; Finding prefix sum and minimum element ; Update the minimum element ; Check if count of ' ( ' and ' ) ' are equal ; Find count of minimum element ; Find the frequency of mn ; Return the count ; Given string S ; length of the string S
function countCyclicShifts ( S , n ) { let aux = [ ] ; for ( let i = 0 ; i < n ; ++ i ) { if ( S [ i ] == ' ' ) aux [ i ] = 1 ; else aux [ i ] = - 1 ; } let mn = aux [ 0 ] ; for ( let i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = Math . min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; let count = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } let S = " " ; let N = S . length ; document . write ( countCyclicShifts ( S , N ) ) ;
Maximize Profit by trading stocks based on given rate per day | Function to find the maximum profit ; Start from the last day ; Traverse and keep adding the profit until a day with price of stock higher than currentDay is obtained ; Set this day as currentDay with maximum cost of stock currently ; Return the profit ; Given array of prices ; Function call
function maxProfit ( prices , n ) { let profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { let day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } let prices = [ 2 , 3 , 5 ] ; let N = prices . length ; document . write ( maxProfit ( prices , N ) ) ;
Check given string is oddly palindrome or not | Set 2 | Function to check if string formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic string is not compared ; If stack is empty then return true ; Driver Code ; Given string
function isOddStringPalindrome ( str , n ) { var oddStringSize = parseInt ( n / 2 ) ; var lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; var s = [ ] ; var i = 1 ; var c = 0 ; while ( i < n && c < parseInt ( oddStringSize / 2 ) ) { s . push ( str [ i ] ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . length > 0 ) { if ( s [ s . length - 1 ] == str [ i ] ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . length == 0 ) return true ; return false ; } var N = 10 ; var s = " " ; if ( isOddStringPalindrome ( s , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check given string is oddly palindrome or not | Set 2 | Functions checks if characters at odd index of the string forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call
function isOddStringPalindrome ( str , n ) { var left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str [ left ] != str [ right ] ) return false ; left += 2 ; right -= 2 ; } return true ; } var n = 10 ; var s = " " ; if ( isOddStringPalindrome ( s , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Remove minimum characters from string to split it into three substrings under given constraints | Function that counts minimum character that must be removed ; Length of string ; Create prefix array ; Initialize first position ; Fill prefix array ; Initialise maxi ; Check all the possibilities by putting i and j at different position & find maximum among them ; Prlet the characters to be removed ; Given String ; Function call
function min_remove ( str ) { let N = str . length ; let prefix_a = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; let prefix_b = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; let prefix_c = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( ( str [ i - 1 ] == ' ' ) ? 1 : 0 ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( ( str [ i - 1 ] == ' ' ) ? 1 : 0 ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( ( str [ i - 1 ] == ' ' ) ? 1 : 0 ) ; } let maxi = Number . MIN_VALUE ; for ( let i = 0 ; i <= N ; i ++ ) { for ( let j = i ; j <= N ; j ++ ) { maxi = Math . max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } document . write ( ( N - maxi ) ) ; } let str = " " ; min_remove ( str ) ;
Create an array of size N with sum S such that no subarray exists with sum S or S | Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Prlet the array as prlet ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Given N and sum S ; Function call
function createArray ( n , s ) { if ( 2 * n <= s ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { document . write ( 2 + " " ) ; s -= 2 ; } document . write ( s + " " ) ; document . write ( 1 ) ; } else document . write ( " " ) ; } let N = 1 ; let S = 4 ; createArray ( N , S ) ;
Maximize partitions such that no two substrings have any common character | Function to calculate and return the maximum number of partitions ; r : Stores the maximum number of partitions k : Stores the ending index of the partition ; Stores the last index of every unique character of the string ; Traverse the string and store the last index of every character ; Store the last index of the first character from map ; Update K to find the end of partition ; Otherwise , the end of partition is found ; Increment r ; Update k for the next partition ; Add the last partition ; Driver Program
function maximum_partition ( str ) { var i = 0 , j = 0 , k = 0 ; var c = 0 , r = 0 ; var m = new Map ( ) ; for ( i = str . length - 1 ; i >= 0 ; i -- ) { if ( ! m . has ( str [ i ] ) ) { m . set ( str [ i ] , i ) ; } } i = 0 ; k = m . get ( str [ i ] ) ; for ( i = 0 ; i < str . length ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = Math . max ( k , m . get ( str [ i ] ) ) ; } else { r = r + 1 ; c = 1 ; k = Math . max ( k , m . get ( str [ i ] ) ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } var str = " " ; document . write ( maximum_partition ( str ) ) ;