codeparrot/xlcost-text-to-code huggingface dataset:
Print all nodes that don 't have sibling | A binary tree node ; Function to print all non - root nodes that don 't have a sibling ; Base case ; If this is an internal node , recur for left and right subtrees ; If left child is NULL and right is not , print right child and recur for right child ; If right child is NULL and left is not , print left child and recur for left child ; Driver program to test the above functions ; Let us construct the tree shown in above diagram
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } class BinaryTree { constructor ( ) { this . root = null ; } printSingles ( node ) { if ( node == null ) { return ; } if ( node . left != null && node . right != null ) { this . printSingles ( node . left ) ; this . printSingles ( node . right ) ; } else if ( node . right != null ) { document . write ( node . right . data + " " ) ; this . printSingles ( node . right ) ; } else if ( node . left != null ) { document . write ( node . left . data + " " ) ; this . printSingles ( node . left ) ; } } } var tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . left . right = new Node ( 6 ) ; tree . printSingles ( tree . root ) ;
Print all nodes that are at distance k from a leaf node | JavaScript program to print all nodes at a distance k from leaf A binary tree node ; This function prints all nodes that are distance k from a leaf node path [ ] -- > Store ancestors of a node visited [ ] -- > Stores true if a node is printed as output . A node may be k distance away from many leaves , we want to print it once ; Base case ; append this Node to the path array ; it 's a leaf, so print the ancestor at distance k only  if the ancestor is not already printed  ; If not leaf node , recur for left and right subtrees ; Given a binary tree and a nuber k , print all nodes that are k distant from a leaf ; Let us construct the tree shown in above diagram
class Node { constructor ( item ) { this . left = null ; this . right = null ; this . data = item ; } } let root ; function kDistantFromLeafUtil ( node , path , visited , pathLen , k ) { if ( node == null ) return ; path [ pathLen ] = node . data ; visited [ pathLen ] = false ; pathLen ++ ; if ( node . left == null && node . right == null && ( pathLen - k - 1 ) >= 0 && visited [ pathLen - k - 1 ] == false ) { document . write ( path [ pathLen - k - 1 ] + " " ) ; visited [ pathLen - k - 1 ] = true ; return ; } kDistantFromLeafUtil ( node . left , path , visited , pathLen , k ) ; kDistantFromLeafUtil ( node . right , path , visited , pathLen , k ) ; } function printKDistantfromLeaf ( node , k ) { let path = new Array ( 1000 ) ; path . fill ( 0 ) ; let visited = new Array ( 1000 ) ; visited . fill ( false ) ; kDistantFromLeafUtil ( node , path , visited , 0 , k ) ; } root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . right . left . right = new Node ( 8 ) ; document . write ( " " ) ; printKDistantfromLeaf ( root , 2 ) ;
Print all nodes that are at distance k from a leaf node | A binary tree node ; Given a binary tree and a nuber k , print all nodes that are k distant from a leaf ; Leaf node ; Parent of left leaf ; Parent of right leaf ; Driver code ; Let us construct the tree shown in above diagram
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } class BinaryTree { constructor ( ) { this . root = null ; } printKDistantfromLeaf ( node , k ) { if ( node == null ) return - 1 ; var lk = this . printKDistantfromLeaf ( node . left , k ) ; var rk = this . printKDistantfromLeaf ( node . right , k ) ; var isLeaf = lk == - 1 && lk == rk ; if ( lk == 0 || rk == 0 || ( isLeaf && k == 0 ) ) document . write ( " " + node . data ) ; if ( isLeaf && k > 0 ) return k - 1 ; if ( lk > 0 && lk < k ) return lk - 1 ; if ( rk > 0 && rk < k ) return rk - 1 ; return - 2 ; } } var tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; document . write ( " " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ;
Print Binary Tree in 2 | JavaScript Program to print binary tree in 2D ; A binary tree node ; Constructor that allocates a new node with the given data and null left and right pointers . ; Function to print binary tree in 2D It does reverse inorder traversal ; Base case ; Increase distance between levels ; Process right child first ; Print current node after space count ; Process left child ; Wrapper over print2DUtil ( ) ; Pass initial space count as 0 ; Driver code
let COUNT = 10 ; class Node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } function print2DUtil ( root , space ) { if ( root == null ) return ; space += COUNT ; print2DUtil ( root . right , space ) ; document . write ( " " ) ; for ( let i = COUNT ; i < space ; i ++ ) document . write ( " " ) ; document . write ( root . data + " " ) ; print2DUtil ( root . left , space ) ; } function print2D ( root ) { print2DUtil ( root , 0 ) ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . left = new Node ( 12 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; root . right . right . right = new Node ( 15 ) ; print2D ( root ) ;
Print Left View of a Binary Tree | Class containing left and right child of current node and key value ; Class to print the left view ; Recursive function to print left view ; Base Case ; If this is the first node of its level ; Recur for left and right subtrees ; A wrapper over leftViewUtil ( ) ; Driver code
class Node { constructor ( item ) { this . data = item ; this . left = null ; this . right = null ; } } var root ; var max_level = 0 ; function leftViewUtil ( node , level ) { if ( node == null ) { return ; } if ( max_level < level ) { document . write ( " " + node . data ) ; max_level = level ; } leftViewUtil ( node . left , level + 1 ) ; leftViewUtil ( node . right , level + 1 ) ; } function leftView ( ) { leftViewUtil ( root , 1 ) ; } root = new Node ( 12 ) ; root . left = new Node ( 10 ) ; root . right = new Node ( 30 ) ; root . right . left = new Node ( 25 ) ; root . right . right = new Node ( 40 ) ; leftView ( ) ;
Reduce the given Array of [ 1 , N ] by rotating left or right based on given conditions | Function to find the last element left after performing N - 1 queries of type X ; Stores the next power of 2 ; Iterate until nextPower is at most N ; If X is equal to 1 ; Stores the power of 2 less than or equal to N ; Return the final value ; Driver Code
function rotate ( arr , N , X ) { let nextPower = 1 ; while ( nextPower <= N ) nextPower *= 2 ; if ( X == 1 ) return nextPower - N ; let prevPower = nextPower / 2 ; return 2 * ( N - prevPower ) + 1 ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let X = 1 ; let N = arr . length ; document . write ( rotate ( arr , N , X ) ) ;
Check whether Matrix T is a result of one or more 90 ° rotations of Matrix mat | Function to check whether another matrix can be created by rotating mat one or more times by 90 degrees ; If the dimensions of both the arrays don 't match ; Return false ; Map to store all rows , columns and their reversed versions ; Iterate in the range [ 0 , M - 1 ] ; Increment the frequency of the i 'th row by 1 ; Reverse the i 'th row ; Increment the frequency of the i 'th row by 1 ; Iterate in the range [ 0 , N - 1 ] ; Stores the i 'th column ; Iterate in the range [ 0 , M - 1 ] ; Increment the frequency of the i 'th column by 1 ; Reverse the i 'th column ; Increment the frequency of the i 'th column by 1 ; Iterate in the range [ 0 , M - 1 ] ; If frequency of the i 'th row  is more in T[][] than in the  mat[][]. ; Decrement the frequency of the i 'th row by 1 ; Iterate in the range [ 0 , N - 1 ] ; Stores the ith column ; Iterate in the range [ 0 , M - 1 ] ; If frequency of the i 'th column  is more in T[][] than in mat[][]. ; Decrement the frequency of the i 'th  column by 1 ; Return " Yes " ; Input ; Function call
function findRotation ( mat , T ) { if ( T . length != mat . length T [ 0 ] . length != mat [ 0 ] . length ) { return " " ; } let m = new Map ( ) ; for ( let i = 0 ; i < mat . length ; i ++ ) { if ( m . has ( mat [ i ] ) ) { m . set ( mat [ i ] , m . get ( mat [ i ] ) + 1 ) } else { m . set ( mat [ i ] , 1 ) } mat [ i ] . reverse ( ) ; if ( m . has ( mat [ i ] ) ) { m . set ( mat [ i ] , m . get ( mat [ i ] ) + 1 ) } else { m . set ( mat [ i ] , 1 ) } } for ( let i = 0 ; i < mat [ 0 ] . length ; i ++ ) { let r = [ ] ; for ( let j = 0 ; j < mat . length ; j ++ ) { r . push ( mat [ j ] [ i ] ) ; } if ( m . has ( r ) ) { m . set ( r , m . get ( r ) + 1 ) } else { m . set ( r , 1 ) } r . reverse ( ) ; if ( m . has ( r ) ) { m . set ( r , m . get ( r ) + 1 ) } else { m . set ( r , 1 ) } } for ( let i = 0 ; i < T . length ; i ++ ) { if ( m . get ( T [ i ] ) <= 0 ) { return " " ; } m . set ( T [ i ] , m . get ( T [ i ] ) - 1 ) ; } for ( let i = 0 ; i < T [ 0 ] . length ; i ++ ) { let r = [ ] ; for ( let j = 0 ; j < T . length ; j ++ ) { r . push ( T [ j ] [ i ] ) ; } if ( m . get ( r ) <= 0 ) { return " " ; } m . set ( r , m . get ( r ) - 1 ) ; } return " " ; } let mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; let T = [ [ 3 , 6 , 9 ] , [ 2 , 5 , 8 ] , [ 1 , 4 , 7 ] ] ; document . write ( findRotation ( mat , T ) ) ;
Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | Function to find the maximum sum of consecutive 0 s present at the start and end of a string present in any of the rotations of the given string ; Check if all the characters in the string are 0 ; Iterate over characters of the string ; If the frequency of '1' is 0 ; Print n as the result ; Concatenate the string with itself ; Stores the required result ; Generate all rotations of the string ; Store the number of consecutive 0 s at the start and end of the string ; Count 0 s present at the start ; Count 0 s present at the end ; Calculate the sum ; Update the overall maximum sum ; Print the result ; Given string ; Store the size of the string
function findMaximumZeros ( str , n ) { var c0 = 0 ; var i ; for ( i = 0 ; i < n ; ++ i ) { if ( str [ i ] == ' ' ) c0 ++ ; } if ( c0 == n ) { document . write ( n ) ; return ; } var s = str + str ; var mx = 0 ; var j ; for ( i = 0 ; i < n ; ++ i ) { var cs = 0 ; var ce = 0 ; for ( j = i ; j < i + n ; ++ j ) { if ( s [ j ] == ' ' ) cs ++ ; else break ; } for ( j = i + n - 1 ; j >= i ; -- j ) { if ( s [ j ] == ' ' ) ce ++ ; else break ; } var val = cs + ce ; mx = Math . max ( val , mx ) ; } document . write ( mx ) ; } var s = " " ; var n = s . length ; findMaximumZeros ( s , n ) ;
Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | Function to find the maximum sum of consecutive 0 s present at the start and end of any rotation of the string str ; Stores the count of 0 s ; If the frequency of '1' is 0 ; Print n as the result ; Stores the required sum ; Find the maximum consecutive length of 0 s present in the string ; Update the overall maximum sum ; Find the number of 0 s present at the start and end of the string ; Update the count of 0 s at the start ; Update the count of 0 s at the end ; Update the maximum sum ; Print the result ; Given string ; Store the size of the string
function findMaximumZeros ( str , n ) { var c0 = 0 ; for ( var i = 0 ; i < n ; ++ i ) { if ( str [ i ] == ' ' ) c0 ++ ; } if ( c0 == n ) { document . write ( n ) ; return ; } var mx = 0 ; var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) cnt ++ ; else { mx = Math . max ( mx , cnt ) ; cnt = 0 ; } } mx = Math . max ( mx , cnt ) ; var start = 0 , end = n - 1 ; cnt = 0 ; while ( str [ start ] != ' ' && start < n ) { cnt ++ ; start ++ ; } while ( str [ end ] != ' ' && end >= 0 ) { cnt ++ ; end -- ; } mx = Math . max ( mx , cnt ) ; document . write ( mx ) ; } var s = " " ; var n = s . length ; findMaximumZeros ( s , n ) ;
Modify a matrix by rotating ith row exactly i times in clockwise direction | Function to reverse arr [ ] from start to end ; Function to rotate every i - th row of the matrix i times ; Traverse the matrix row - wise ; Reverse the current row ; Reverse the first i elements ; Reverse the last ( N - i ) elements ; Increment count ; Print final matrix ; Driver Code
function reverse ( arr , start , end ) { while ( start < end ) { let temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } function rotateMatrix ( mat ) { let i = 0 ; for ( let rows = 0 ; rows < mat . length ; rows ++ ) { reverse ( mat [ rows ] , 0 , mat [ rows ] . length - 1 ) ; reverse ( mat [ rows ] , 0 , i - 1 ) ; reverse ( mat [ rows ] , i , mat [ rows ] . length - 1 ) ; i ++ ; } for ( let rows = 0 ; rows < mat . length ; rows ++ ) { for ( let cols = 0 ; cols < mat [ rows ] . length ; cols ++ ) { document . write ( mat [ rows ] [ cols ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; rotateMatrix ( mat ) ;
Modify a string by performing given shift operations | Function to find the string obtained after performing given shift operations ; If shift [ i ] [ 0 ] = 0 , then left shift Otherwise , right shift ; Stores length of the string ; Effective shift calculation ; Stores modified string ; Right rotation ; Left rotation ; Driver Code
function stringShift ( s , shift ) { var val = 0 ; for ( var i = 0 ; i < shift . length ; ++ i ) val += shift [ i ] [ 0 ] == 0 ? - shift [ i ] [ 1 ] : shift [ i ] [ 1 ] ; var len = s . length ; val = val % len ; var result = " " ; if ( val > 0 ) result = s . substring ( len - val , len ) + s . substring ( 0 , len - val ) ; else result = s . substring ( - val , len ) + s . substring ( 0 , - val ) ; document . write ( result ) ; } var s = " " ; var shift = [ [ 0 , 1 ] , [ 1 , 2 ] ] ; stringShift ( s , shift ) ;
Modify given array to a non | Function to check if a non - decreasing array can be obtained by rotating the original array ; Stores copy of original array ; Sort the given vector ; Traverse the array ; Rotate the array by 1 ; If array is sorted ; If it is not possible to sort the array ; Given array ; Size of the array ; Function call to check if it is possible to make array non - decreasing by rotating
function rotateArray ( arr , N ) { let v = arr ; v . sort ( ( a , b ) => a - b ) ; for ( let i = 1 ; i <= N ; ++ i ) { let x = arr [ N - 1 ] ; i = N - 1 ; while ( i -- ) { arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; } let isEqual = arr . every ( ( e , i ) => { return arr [ i ] == v [ i ] } ) if ( isEqual ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let arr = [ 3 , 4 , 5 , 1 , 2 ] ; let N = arr . length ; rotateArray ( arr , N ) ;
Maximum value possible by rotating digits of a given number | Function to find the maximum value possible by rotations of digits of N ; Store the required result ; Store the number of digits ; Iterate over the range [ 1 , len - 1 ] ; Store the unit 's digit ; Store the remaining number ; Find the next rotation ; If the current rotation is greater than the overall answer , then update answer ; Print the result ; Driver Code
function findLargestRotation ( num ) { let ans = num ; let len = Math . floor ( Math . log10 ( num ) + 1 ) ; let x = Math . pow ( 10 , len - 1 ) ; for ( let i = 1 ; i < len ; i ++ ) { let lastDigit = num % 10 ; num = parseInt ( num / 10 ) ; num += ( lastDigit * x ) ; if ( num > ans ) { ans = num ; } } document . write ( ans ) ; } let N = 657 ; findLargestRotation ( N ) ;
Rotate digits of a given number by K | Function to find the count of digits in N ; Stores count of digits in N ; Calculate the count of digits in N ; Update digit ; Update N ; Function to rotate the digits of N by K ; Stores count of digits in N ; Update K so that only need to handle left rotation ; Stores first K digits of N ; Remove first K digits of N ; Stores count of digits in left_no ; Append left_no to the right of digits of N ; Driver Code ; Function Call
function numberOfDigit ( N ) { let digit = 0 ; while ( N > 0 ) { digit ++ ; N = Math . floor ( N / 10 ) ; } return digit ; } function rotateNumberByK ( N , K ) { let X = numberOfDigit ( N ) ; K = ( ( K % X ) + X ) % X ; let left_no = Math . floor ( N / Math . floor ( Math . pow ( 10 , X - K ) ) ) ; N = N % Math . floor ( Math . pow ( 10 , X - K ) ) ; let left_digit = numberOfDigit ( left_no ) ; N = ( N * Math . floor ( Math . pow ( 10 , left_digit ) ) ) + left_no ; document . write ( N ) ; } let N = 12345 , K = 7 ; rotateNumberByK ( N , K ) ;
Count rotations required to sort given array in non | Function to count minimum anti - clockwise rotations required to sort the array in non - increasing order ; Stores count of arr [ i + 1 ] > arr [ i ] ; Store last index of arr [ i + 1 ] > arr [ i ] ; Traverse the given array ; If the adjacent elements are in increasing order ; Increment count ; Update index ; Print result according to the following conditions ; Otherwise , it is not possible to sort the array ; Given array ; Function Call
function minMovesToSort ( arr , N ) { let count = 0 ; let index = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { count ++ ; index = i ; } } if ( count == 0 ) { document . write ( " " ) ; } else if ( count == N - 1 ) { document . write ( N - 1 ) ; } else if ( count == 1 && arr [ 0 ] <= arr [ N - 1 ] ) { document . write ( index + 1 ) ; } else { document . write ( " " ) ; } } let arr = [ 2 , 1 , 5 , 4 , 2 ] ; let N = arr . length ; minMovesToSort ( arr , N ) ;
Maximize sum of diagonal of a matrix by rotating all rows or all columns | Javascript program to implement the above approach ; Function to find maximum sum of diagonal elements of matrix by rotating either rows or columns ; Stores maximum diagonal sum of elements of matrix by rotating rows or columns ; Rotate all the columns by an leteger in the range [ 0 , N - 1 ] ; Stores sum of diagonal elements of the matrix ; Calculate sum of diagonal elements of the matrix ; Update curr ; Update maxDiagonalSum ; Rotate all the rows by an leteger in the range [ 0 , N - 1 ] ; Stores sum of diagonal elements of the matrix ; Calculate sum of diagonal elements of the matrix ; Update curr ; Update maxDiagonalSum ; Driver Code
let N = 3 ; function findMaximumDiagonalSumOMatrixf ( A ) { let maxDiagonalSum = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let curr = 0 ; for ( let j = 0 ; j < N ; j ++ ) { curr += A [ j ] [ ( i + j ) % N ] ; } maxDiagonalSum = Math . max ( maxDiagonalSum , curr ) ; } for ( let i = 0 ; i < N ; i ++ ) { let curr = 0 ; for ( let j = 0 ; j < N ; j ++ ) { curr += A [ ( i + j ) % N ] [ j ] ; } maxDiagonalSum = Math . max ( maxDiagonalSum , curr ) ; } return maxDiagonalSum ; } let mat = [ [ 1 , 1 , 2 ] , [ 2 , 1 , 2 ] , [ 1 , 2 , 2 ] ] ; document . write ( findMaximumDiagonalSumOMatrixf ( mat ) ) ;
Generate a matrix having sum of secondary diagonal equal to a perfect square | Function to prlet the matrix whose sum of element in secondary diagonal is a perfect square ; Iterate for next N - 1 rows ; Prlet the current row after the left shift ; Given N ; Fill the array with elements ranging from 1 to N ; Function Call
function diagonalSumPerfectSquare ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { document . write ( arr [ ( j + i ) % 7 ] + " " ) ; } document . write ( " " ) ; } } let N = 7 ; let arr = new Array ( N ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } diagonalSumPerfectSquare ( arr , N ) ;
Queries to find maximum sum contiguous subarrays of given length in a rotating array | Function to calculate the maximum sum of length k ; Calculating the max sum for the first k elements ; Find subarray with maximum sum ; Update the sum ; Return maximum sum ; Function to calculate gcd of the two numbers n1 and n2 ; Base Case ; Recursively find the GCD ; Function to rotate the array by Y ; For handling k >= N ; Dividing the array into number of sets ; Rotate the array by Y ; Update arr [ j ] ; Return the rotated array ; Function that performs the queries on the given array ; Traverse each query ; If query of type X = 1 ; Print var the array ; If query of type X = 2 ; Given array arr ; Given Queries ; Function Call
function MaxSum ( arr , n , k ) { var i , max_sum = 0 , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } max_sum = sum ; while ( i < n ) { sum = sum - arr [ i - k ] + arr [ i ] ; if ( max_sum < sum ) { max_sum = sum ; } i ++ ; } return max_sum ; } function gcd ( n1 , n2 ) { if ( n2 == 0 ) { return n1 ; } else { return gcd ( n2 , n1 % n2 ) ; } } function RotateArr ( arr , n , d ) { var i = 0 , j = 0 ; d = d % n ; var no_of_sets = gcd ( d , n ) ; for ( i = 0 ; i < no_of_sets ; i ++ ) { var temp = arr [ i ] ; j = i ; while ( true ) { var k = j + d ; if ( k >= n ) k = k - n ; if ( k == i ) break ; arr [ j ] = arr [ k ] ; j = k ; } arr [ j ] = temp ; } return arr ; } function performQuery ( arr , Q , q ) { var N = arr . length ; for ( i = 0 ; i < q ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { arr = RotateArr ( arr , N , Q [ i ] [ 1 ] ) ; for ( var t = 0 ; t < arr . length ; t ++ ) { document . write ( arr [ t ] + " " ) ; } document . write ( " " ) ; } else { document . write ( MaxSum ( arr , N , Q [ i ] [ 1 ] ) + " " ) ; } } } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var q = 5 ; var Q = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 1 , 3 ] , [ 1 , 1 ] , [ 2 , 4 ] ] ; performQuery ( arr , Q , q ) ;
Minimize characters to be changed to make the left and right rotation of a string same | Function to find the minimum characters to be removed from the string ; Initialize answer by N ; If length is even ; Frequency array for odd and even indices ; Store the frequency of the characters at even and odd indices ; Stores the most occuring frequency for even and odd indices ; Update the answer ; If length is odd ; Stores the frequency of the characters of the string ; Stores the most occuring character in the string ; Update the answer ; Driver code
function getMinimumRemoval ( str ) { var n = str . length ; var ans = n ; if ( n % 2 === 0 ) { var freqEven = new Array ( 128 ) . fill ( 0 ) ; var freqOdd = new Array ( 128 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 === 0 ) { freqEven [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } else { freqOdd [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } } var evenMax = 0 , oddMax = 0 ; for ( var chr = " " . charCodeAt ( 0 ) ; chr <= " " . charCodeAt ( 0 ) ; chr ++ ) { evenMax = Math . max ( evenMax , freqEven [ chr ] ) ; oddMax = Math . max ( oddMax , freqOdd [ chr ] ) ; } ans = ans - evenMax - oddMax ; } else { var freq = new Array ( 128 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } var strMax = 0 ; for ( var chr = " " . charCodeAt ( 0 ) ; chr <= " " . charCodeAt ( 0 ) ; chr ++ ) { strMax = Math . max ( strMax , freq [ chr ] ) ; } ans = ans - strMax ; } return ans ; } var str = " " ; document . write ( getMinimumRemoval ( str ) ) ;