codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to pair people | Function to find number of ways to pair people in party ; To store count of number of ways . ; Using the recurrence defined find count for different values of p . ; Driver code
function findWaysToPair ( p ) { var dp = Array ( p + 1 ) ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; for ( var i = 3 ; i <= p ; i ++ ) { dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ p ] ; } var p = 3 ; document . write ( findWaysToPair ( p ) ) ;
Count ways to reach a score using 1 and 2 with no consecutive 2 s | A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; base cases ; For cases n > 2 ; Driver code
function CountWays ( n , flag ) { if ( n == 0 ) { return 1 ; } if ( n == 1 ) { return 1 ; } if ( n == 2 ) { return 1 + 1 ; } return CountWays ( n - 1 ) + CountWays ( n - 3 ) ; } let n = 5 ; document . write ( CountWays ( n , false ) ) ;
Weird Number | code to find all the factors of the number excluding the number itself ; vector to store the factors ; note that this loop runs till sqrt ( n ) ; if the value of i is a factor ; condition to check the divisor is not the number itself ; return the vector ; Function to check if the number is abundant or not ; find the divisors using function ; sum all the factors ; check for abundant or not ; Function to check if the number is semi - perfect or not ; find the divisors ; sorting the vector ; subset to check if no is semiperfect ; initialising 1 st column to true ; initialing 1 st row except zero position to 0 ; loop to find whether the number is semiperfect ; calculation to check if the number can be made by summation of divisors ; if not possible to make the number by any combination of divisors ; Function to check for weird or not ; Driver Code
function factors ( n ) { var v = [ ] ; v . push ( 1 ) ; for ( var i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . push ( i ) ; if ( n / i != i ) { v . push ( n / i ) ; } } } return v ; } function checkAbundant ( n ) { var v = [ ] ; var sum = 0 ; v = factors ( n ) ; for ( var i = 0 ; i < v . length ; i ++ ) { sum += v [ i ] ; } if ( sum > n ) return true ; else return false ; } function checkSemiPerfect ( n ) { var v = [ ] ; v = factors ( n ) ; v . sort ( ) var r = v . length ; var subset = Array . from ( Array ( r + 1 ) , ( ) => Array ( n + 1 ) ) ; for ( var i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( var i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( var i = 1 ; i <= r ; i ++ ) { for ( var j = 1 ; j <= n ; j ++ ) { if ( j < v [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v [ i - 1 ] ] ; } } } if ( ( subset [ r ] [ n ] ) == 0 ) return false ; else return true ; } function checkweird ( n ) { if ( checkAbundant ( n ) == true && checkSemiPerfect ( n ) == false ) return true ; else return false ; } var n = 70 ; if ( checkweird ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum subarray sum in an array created after repeated concatenation | Returns sum of maximum sum subarray created after concatenating a [ 0. . n - 1 ] k times . ; This is where it differs from Kadane 's algorithm. We use modular  arithmetic to find next element. ; Driver code
function maxSubArraySumRepeated ( a , n , k ) { let max_so_far = 0 ; let INT_MIN , max_ending_here = 0 ; for ( let i = 0 ; i < n * k ; i ++ ) { max_ending_here = max_ending_here + a [ i % n ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } let a = [ 10 , 20 , - 30 , - 1 ] ; let n = a . length ; let k = 3 ; document . write ( " " + maxSubArraySumRepeated ( a , n , k ) ) ;
Longest Increasing Odd Even Subsequence | function to find the longest increasing odd even subsequence ; lioes [ i ] stores longest increasing odd even subsequence ending at arr [ i ] ; to store the length of longest increasing odd even subsequence ; Initialize LIOES values for all indexes ; Compute optimized LIOES values in bottom up manner ; Pick maximum of all LIOES values ; required maximum length ; Driver code
function longOddEvenIncSeq ( arr , n ) { let lioes = [ ] ; let maxLen = 0 ; for ( let i = 0 ; i < n ; i ++ ) lioes [ i ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) for ( let j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ] + 1 ) lioes [ i ] = lioes [ j ] + 1 ; for ( let i = 0 ; i < n ; i ++ ) if ( maxLen < lioes [ i ] ) maxLen = lioes [ i ] ; return maxLen ; } let arr = [ 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 ] ; let n = 10 ; document . write ( " " + " " + longOddEvenIncSeq ( arr , n ) ) ;
Minimum and Maximum values of an expression with * and + | Utility method to check whether a character is operator or not ; method prints minimum and maximum value obtainable from an expression ; store operator and numbers in different vectors ; storing last number in vector ; initializing minval and maxval 2D array ; initializing main diagonal by num values ; looping similar to matrix chain multiplication and updating both 2D arrays ; if current operator is ' + ' , updating tmp variable by addition ; if current operator is ' * ' , updating tmp variable by multiplication ; updating array values by tmp variables ; last element of first row will store the result ; Driver code to test above methods
function isOperator ( op ) { return ( op == ' ' op == ' ' ) ; } function printMinAndMaxValueOfExp ( exp ) { let num = [ ] ; let opr = [ ] ; let tmp = " " ; for ( let i = 0 ; i < exp . length ; i ++ ) { if ( isOperator ( exp [ i ] ) ) { opr . push ( exp [ i ] ) ; num . push ( parseInt ( tmp ) ) ; tmp = " " ; } else { tmp += exp [ i ] ; } } num . push ( parseInt ( tmp ) ) ; let len = num . length ; let minVal = new Array ( len ) ; let maxVal = new Array ( len ) ; for ( let i = 0 ; i < len ; i ++ ) { minVal [ i ] = new Array ( len ) ; maxVal [ i ] = new Array ( len ) ; for ( let j = 0 ; j < len ; j ++ ) { minVal [ i ] [ j ] = Number . MAX_VALUE ; maxVal [ i ] [ j ] = 0 ; if ( i == j ) minVal [ i ] [ j ] = maxVal [ i ] [ j ] = num [ i ] ; } } for ( let L = 2 ; L <= len ; L ++ ) { for ( let i = 0 ; i < len - L + 1 ; i ++ ) { let j = i + L - 1 ; for ( let k = i ; k < j ; k ++ ) { let minTmp = 0 , maxTmp = 0 ; if ( opr [ k ] == ' ' ) { minTmp = minVal [ i ] [ k ] + minVal [ k + 1 ] [ j ] ; maxTmp = maxVal [ i ] [ k ] + maxVal [ k + 1 ] [ j ] ; } else if ( opr [ k ] == ' ' ) { minTmp = minVal [ i ] [ k ] * minVal [ k + 1 ] [ j ] ; maxTmp = maxVal [ i ] [ k ] * maxVal [ k + 1 ] [ j ] ; } if ( minTmp < minVal [ i ] [ j ] ) minVal [ i ] [ j ] = minTmp ; if ( maxTmp > maxVal [ i ] [ j ] ) maxVal [ i ] [ j ] = maxTmp ; } } } document . write ( " " + minVal [ 0 ] [ len - 1 ] + " " + maxVal [ 0 ] [ len - 1 ] ) ; } let expression = " " ; printMinAndMaxValueOfExp ( expression ) ;
Matrix Chain Multiplication | DP | Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; place parenthesis at different places between first and last matrix , recursively calculate count of multiplications for each parenthesis placement and return the minimum count ; Return minimum count ; Driver code
function MatrixChainOrder ( p , i , j ) { if ( i == j ) return 0 ; var min = Number . MAX_VALUE ; var k = 0 ; for ( k = i ; k < j ; k ++ ) { var count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( count < min ) min = count ; } return min ; } var arr = [ 1 , 2 , 3 , 4 , 3 ] ; var n = arr . length ; document . write ( " " + MatrixChainOrder ( arr , 1 , n - 1 ) ) ;
Numbers formed by flipping common set bits in two given integers | Function to flip bits of A and B which are set in both of them ; Clear the bits of A which are set in both A and B ; Clear the bits of B which are set in both A and B ; Print updated A and B ; Driver Code
function flipBitsOfAandB ( A , B ) { A = A ^ ( A & B ) ; B = B ^ ( A & B ) ; document . write ( A + " " + B ) ; } var A = 10 , B = 20 ; flipBitsOfAandB ( A , B ) ;
Sum of Hamming difference of consecutive numbers from 0 to N | Set 2 | Function to calculate and return the hamming distance between all consecutive numbers from 0 to N ; Driver Code
function TotalHammingDistance ( n ) { let i = 1 , sum = 0 ; while ( Math . floor ( n / i ) > 0 ) { sum = sum + Math . floor ( n / i ) ; i = i * 2 ; } return sum ; } let N = 9 ; document . write ( TotalHammingDistance ( N ) ) ;
Sum of all divisors from 1 to N | Set 3 | Javascript program implementation of the approach ; Function to find the sum of all divisors of all numbers from 1 to N ; Stores the sum ; Marks the last polet of occurence with same count ; Calculate the sum ; Return the result ; Driver Code
let m = 1000000007 ; function solve ( n ) { let s = 0 ; for ( let l = 1 ; l <= n ; ) { let r = ( n / Math . floor ( n / l ) ) ; let x = Math . floor ( ( ( r % m ) * ( ( r + 1 ) % m ) ) / 2 ) % m ; let y = Math . floor ( ( ( l % m ) * ( ( l - 1 ) % m ) ) / 2 ) % m ; let p = ( Math . floor ( n / l ) % m ) ; s = ( s + ( ( ( x - y ) % m ) * p ) % m + m ) % m ; s %= m ; l = r + 1 ; } document . write ( ( s + m ) % m ) ; } let n = 12 ; solve ( n ) ;
Minimize number of cuts required to break N length stick into N unit length sticks | Function to return the minimum time required to split stick of N into length into unit pieces ; Return the minimum unit of time required ; Driver code
function min_time_to_cut ( N ) { if ( N == 0 ) return 0 ; return Math . ceil ( Math . log ( N ) / Math . log ( 2 ) ) ; } let N = 100 ; document . write ( min_time_to_cut ( N ) ) ;
Count of distinct pair sum between two 1 to N value Arrays | Function to find the distinct sums ; Set to store distinct sums ; Inserting every sum ; returning distinct sums ; Driver code
function findDistinctSums ( n ) { s = new Set ( ) ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = i ; j <= n ; j ++ ) { s . add ( i + j ) ; } } return s . size ; } var N = 3 ; document . write ( findDistinctSums ( N ) ) ;
Print Triangle separated pattern | Function to print pattern recursively ; Base Case ; Conditions to print slash ; Condition to print forword slash ; Condition to print backward slash ; Else print ' * ' ; Recursive call for rows ; Recursive call for changing the rows ; Driver Code ; Function Call
function printPattern ( i , j , n ) { if ( j >= n ) { return 0 ; } if ( i >= n ) { return 1 ; } if ( j == i j == n - 1 - i ) { if ( i == n - 1 - j ) { document . write ( " " ) ; } else { document . write ( " \\ " ) ; } } else { document . write ( " " ) ; } if ( printPattern ( i , j + 1 , n ) == 1 ) { return 1 ; } document . write ( " " ) ; return printPattern ( i + 1 , 0 , n ) ; } let N = 9 ; printPattern ( 0 , 0 , N ) ;
Find starting index for every occurrence of given array B in array A using Z | Function to calculate Z - Array ; Loop to calculate Z - Array ; Outside the Z - box ; Inside Z - box ; Helper function to merge two arrays and create a single array ; Array to store merged array ; Copying array B ; Adding a separator ; Copying array A ; Calling Z - function ; Function to help compute the Z array ; Printing indexes where array B occur ; Driver Code
function zArray ( arr ) { let n = arr . length ; let z = new Array ( n ) ; let r = 0 , l = 0 ; for ( let k = 1 ; k < n ; k ++ ) { if ( k > r ) { r = l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } else { let k1 = k - l ; if ( z [ k1 ] < r - k + 1 ) z [ k ] = z [ k1 ] ; else { l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } } } return z ; } function mergeArray ( A , B ) { let n = A . length ; let m = B . length ; let z = new Array ( ) ; let c = new Array ( n + m + 1 ) ; for ( let i = 0 ; i < m ; i ++ ) c [ i ] = B [ i ] ; c [ m ] = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) c [ m + i + 1 ] = A [ i ] ; z = zArray ( c ) ; return z ; } function findZArray ( A , B , n ) { let flag = 0 ; let z = [ ] ; z = mergeArray ( A , B ) ; for ( let i = 0 ; i < z . length ; i ++ ) { if ( z [ i ] == n ) { document . write ( ( i - n - 1 ) + " " ) ; flag = 1 ; } } if ( flag == 0 ) { document . write ( " " ) ; } } let A = [ 1 , 2 , 3 , 2 , 3 , 2 ] ; let B = [ 2 , 3 ] ; let n = B . length ; findZArray ( A , B , n ) ;
Check if a string can be repeated to make another string | Function to return the count of repetitions of string a to generate string b ; If b cannot be generated by repeating a ; Repeat a count number of times ; Driver code
function getCount ( a , b ) { if ( b . length % a . length != 0 ) return - 1 ; var count = parseInt ( b . length / a . length ) ; var str = " " ; for ( i = 0 ; i < count ; i ++ ) { str = str + a ; } if ( str == ( b ) ) return count ; return - 1 ; } var a = " " ; var b = " " ; document . write ( getCount ( a , b ) ) ;
Check if a string can be formed from another string using given constraints | Function to check if S2 can be formed of S1 ; Length of Strings ; hash - table to store count ; Store count of each character ; Traverse and check for every character ; If the character of s2 is present in s1 ; If the character of s2 is not present in S1 , then check if previous two ASCII characters are present in S1 ; Driver Code ; Calling function to check
function check ( S1 , S2 ) { var n1 = S1 . length ; var n2 = S2 . length ; var mp = { } ; for ( var i = 0 ; i < n1 ; i ++ ) { if ( mp . hasOwnProperty ( S1 [ i ] ) ) { mp [ S1 [ i ] ] = mp [ S1 [ i ] ] + 1 ; } else { mp [ S1 [ i ] ] = 1 ; } } for ( var i = 0 ; i < n2 ; i ++ ) { if ( mp . hasOwnProperty ( S2 [ i ] ) ) { mp [ S2 [ i ] ] = mp [ S2 [ i ] ] - 1 ; } else if ( mp . hasOwnProperty ( String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 1 ) ) && mp . hasOwnProperty ( String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 2 ) ) ) { mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 1 ) ] = mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 1 ) ] - 1 ; mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 2 ) ] = mp [ String . fromCharCode ( S2 [ i ] . charCodeAt ( 0 ) - 2 ) ] - 1 ; } else { return false ; } } return true ; } var S1 = " " ; var S2 = " " ; if ( check ( S1 , S2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of occurrences of a "1(0 + ) 1" pattern in a string | Returns count of occurrences of "1(0 + ) 1" int str . ; let count = 0 ; Initialize result ; Check if encountered '1' forms a valid pattern as specified ; if 1 encountered for first time set oneSeen to 1 ; Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern ; Driver program to test above function
function countPattern ( str ) { let len = str . length ; let oneSeen = false ; for ( let i = 0 ; i < len ; i ++ ) { let getChar = str [ i ] ; if ( getChar == ' ' && oneSeen == true ) { if ( str [ i - 1 ] == ' ' ) count ++ ; } if ( getChar == ' ' && oneSeen == false ) oneSeen = true ; if ( getChar != ' ' && str [ i ] != ' ' ) oneSeen = false ; } return count ; } let str = " " ; document . write ( countPattern ( str ) ) ;
Convert given Strings into T by replacing characters in between strings any number of times | Function to check if it possible to make all the strings equal to the string T ; Stores the frequency of all the strings in the array arr [ ] ; Stores the frequency of the string T ; Iterate over the characters of the string T ; Iterate in the range [ 0 , N - 1 ] ; Iterate over the characters of the string arr [ i ] ; If freqT [ i ] is 0 and freqS [ i ] is not 0 ; If freqS [ i ] is 0 and freqT [ i ] is not 0 ; If freqS [ i ] is not freqT [ i ] * N ; Otherwise , return " Yes " ; Driver Code
function checkIfPossible ( N , arr , T ) { let freqS = new Array ( 256 ) . fill ( 0 ) ; let freqT = new Array ( 256 ) . fill ( 0 ) ; for ( let ch of T ) { freqT [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let ch of arr [ i ] ) { freqS [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } } for ( let i = 0 ; i < 256 ; i ++ ) { if ( freqT [ i ] == 0 && freqS [ i ] != 0 ) { return " " ; } else if ( freqS [ i ] == 0 && freqT [ i ] != 0 ) { return " " ; } else if ( freqT [ i ] != 0 && freqS [ i ] != ( freqT [ i ] * N ) ) { return " " ; } } return " " ; } let arr = [ " " , " " , " " ] ; let T = " " ; let N = arr . length ; document . write ( checkIfPossible ( N , arr , T ) ) ;
Count of groups of consecutive 1 s in a given Binary String | Function to find the number of the groups of 1 s only in the binary string ; Stores number of groups of 1 s ; Initialization of the stack ; Traverse the string S ; If S [ i ] is '1' ; Otherwise ; If st is empty ; If st is not empty ; Return answer ; Input ; Function call
function groupsOfOnes ( S , N ) { let count = 0 ; var st = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) st . push ( 1 ) ; else { if ( st . length != 0 ) { count ++ ; while ( st . length != 0 ) { st . pop ( ) ; } } } } if ( st . length != 0 ) count ++ ; return count ; } var S = " " ; let N = S . length ; document . write ( groupsOfOnes ( S , N ) ) ;
Palindromic strings of length 3 possible by using characters of a given string | Function to print all palindromic strings of length 3 that can be formed using characters of string S ; Stores the count of character ; Traverse the string S ; Stores all palindromic strings ; Iterate over the charchaters over the range [ ' a ' , ' z ' ] ; If Hash [ ch ] is equal to 2 ; Iterate over the characters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; Push the s into the set st ; If Hash [ i ] is greater than or equal to 3 ; Iterate over charchaters over the range [ ' a ' , ' z ' ] ; Stores all the palindromic string ; If Hash [ j ] is positive ; Push s into the set st ; Iterate over the set ; Driver Code
function generatePalindrome ( S ) { let Hash = new Map ( ) ; for ( let ch = 0 ; ch < S . length ; ch ++ ) { if ( ! Hash . has ( S [ ch ] ) ) Hash . set ( S [ ch ] , 1 ) ; else { Hash . set ( S [ ch ] , Hash . get ( S [ ch ] ) + 1 ) } } let st = new Set ( ) ; for ( let i = ' ' . charCodeAt ( 0 ) ; i <= ' ' . charCodeAt ( 0 ) ; i ++ ) { if ( Hash . get ( String . fromCharCode ( i ) ) == 2 ) { for ( let j = ' ' . charCodeAt ( 0 ) ; j <= ' ' . charCodeAt ( 0 ) ; j ++ ) { let s = " " ; if ( Hash . get ( String . fromCharCode ( j ) ) && i != j ) { s += String . fromCharCode ( i ) ; s += String . fromCharCode ( j ) ; s += String . fromCharCode ( i ) ; st . add ( s ) ; } } } if ( Hash . get ( String . fromCharCode ( i ) ) >= 3 ) { for ( let j = ' ' . charCodeAt ( 0 ) ; j <= ' ' . charCodeAt ( 0 ) ; j ++ ) { let s = " " ; if ( Hash . get ( String . fromCharCode ( j ) ) ) { s += String . fromCharCode ( i ) ; s += String . fromCharCode ( j ) ; s += String . fromCharCode ( i ) ; st . add ( s ) ; } } } } for ( let item of st . values ( ) ) { document . write ( item + " " ) } } let S = " " ; generatePalindrome ( S ) ;
Count occurrences of substring X before every occurrence of substring Y in a given string | Function to count occurrences of the string Y in the string S for every occurrence of X in S ; Stores the count of occurrences of X ; Stores the lengths of the three strings ; Traverse the string S ; If the current substring is Y , then increment the value of count by 1 ; If the current substring is X , then print the count ; Driver Code
function countOccurrences ( S , X , Y ) { let count = 0 ; let N = S . length , A = X . length ; let B = Y . length ; for ( let i = 0 ; i < N ; i ++ ) { if ( S . substr ( i , B ) == Y ) count ++ ; if ( S . substr ( i , A ) == X ) document . write ( count , " " ) ; } } let S = " " , X = " " , Y = " " ; countOccurrences ( S , X , Y ) ;
Program to construct DFA for Regular Expression C ( A + B ) + | Function to find whether the given String is Accepted by the DFA ; If n <= 1 , then print No ; To count the matched characters ; Check if the first character is C ; Traverse the rest of String ; If character is A or B , increment count by 1 ; If the first character is not C , print - 1 ; If all characters matches ; Driver Code
function DFA ( str , N ) { if ( N <= 1 ) { document . write ( " " ) ; return ; } let count = 0 ; if ( str [ 0 ] == ' ' ) { count ++ ; for ( let i = 1 ; i < N ; i ++ ) { if ( str [ i ] == ' ' str [ i ] == ' ' ) count ++ ; else break ; } } else { document . write ( " " ) ; return ; } if ( count == N ) document . write ( " " ) ; else document . write ( " " ) ; } let str = " " ; let N = str . length ; DFA ( str , N ) ;
Minimum and maximum number of digits required to be removed to make a given number divisible by 3 | Function to find the maximum and minimum number of digits to be removed to make str divisible by 3 ; Convert the string leto array of digits ; Count of 0 s , 1 s , and 2 s ; Traverse the array ; Find the sum of digits % 3 ; Cases to find minimum number of digits to be removed ; Cases to find maximum number of digits to be removed ; Driver code ; Function Call
function minMaxDigits ( str , N ) { let arr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) arr [ i ] = ( str [ i ] - ' ' ) % 3 ; let zero = 0 , one = 0 , two = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero ++ ; if ( arr [ i ] == 1 ) one ++ ; if ( arr [ i ] == 2 ) two ++ ; } let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum = ( sum + arr [ i ] ) % 3 ; } if ( sum == 0 ) { document . write ( 0 + " " ) ; } if ( sum == 1 ) { if ( ( one != 0 ) && ( N > 1 ) ) document . write ( 1 + " " ) ; else if ( two > 1 && N > 2 ) document . write ( 2 + " " ) ; else document . write ( - 1 + " " ) ; } if ( sum == 2 ) { if ( two != 0 && N > 1 ) document . write ( 1 + " " ) ; else if ( one > 1 && N > 2 ) document . write ( 2 + " " ) ; else document . write ( - 1 + " " ) ; } if ( zero > 0 ) document . write ( N - 1 + " " ) ; else if ( one > 0 && two > 0 ) document . write ( N - 2 + " " ) ; else if ( one > 2 two > 2 ) document . write ( N - 3 + " " ) ; else document . write ( - 1 + " " ) ; } let str = " " ; let N = str . length ; minMaxDigits ( str , N ) ;
Minimum replacements required to obtain a K | Function to find the minimum number of changes to make the string K - periodic and palindrome ; Initialize ans with 0 ; Iterate from 0 to ( K + 1 ) / 2 ; Store frequency of character ; Iterate through all indices , i , i + K , i + 2 k ... . and store the frequency of character ; Increase the frequency of current character ; Iterate through all indices K - i , 2 K - i , 3 K - i ... . and store the frequency of character ; If K is odd & i is samw as K / 2 , break the loop ; Increase the frequency of current character ; Find the maximum frequency of a character among all visited characters ; If K is odd and i is same as K / 2 then , only N / K characters is visited ; Otherwise N / K * 2 characters has visited ; Return the result ; Driver Code ; Function Call
function findMinimumChanges ( N , K , S ) { var ans = 0 ; for ( var i = 0 ; i < parseInt ( ( K + 1 ) / 2 ) ; i ++ ) { var mp = new Map ( ) ; for ( var j = i ; j < N ; j += K ) { if ( mp . has ( S [ j ] ) ) { mp . set ( S [ j ] , mp . get ( S [ j ] ) + 1 ) ; } else { mp . set ( S [ j ] , 1 ) ; } } for ( var j = N - i - 1 ; j >= 0 ; j -= K ) { if ( ( K & 1 ) && i == parseInt ( K / 2 ) ) break ; if ( mp . has ( S [ j ] ) ) { mp . set ( S [ j ] , mp . get ( S [ j ] ) + 1 ) ; } else { mp . set ( S [ j ] , 1 ) ; } } var curr_max = - 1000000000 ; mp . forEach ( ( value , key ) => { curr_max = Math . max ( curr_max , value ) ; } ) ; if ( K & 1 && i == parseInt ( K / 2 ) ) ans += ( parseInt ( N / K ) - curr_max ) ; else ans += ( parseInt ( N / K ) * 2 - curr_max ) ; } return ans ; } var S = " " ; var N = S . length ; var K = 3 ; document . write ( findMinimumChanges ( N , K , S ) ) ;