codeparrot/xlcost-text-to-code huggingface dataset:
Find the largest multiple of 3 from array of digits | Set 2 ( In O ( n ) time and O ( 1 ) space ) | Number of digits ; function to sort array of digits using counts ; Store count of all elements ; Store ; Remove elements from arr [ ] at indexes ind1 and ind2 ; Returns largest multiple of 3 that can be formed using arr [ ] elements . ; Sum of all array element ; Sum is divisible by 3 , no need to delete an element ; Sort array element in increasing order ; Find reminder ; If remainder is '1' , we have to delete either one element of remainder '1' or two elements of remainder '2' ; Traverse array elements ; Store first element of remainder '1' ; If this is first occurrence of remainder 2 ; If second occurrence ; If remainder is '2' , we have to delete either one element of remainder '2' or two elements of remainder '1' ; traverse array elements ; store first element of remainder '2' ; If this is first occurrence of remainder 1 ; If second occurrence ; Driver code
const MAX_SIZE = 10 ; function sortArrayUsingCounts ( arr , n ) { let count = new Uint8Array ( MAX_SIZE ) ; for ( let i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; let index = 0 ; for ( let i = 0 ; i < MAX_SIZE ; i ++ ) while ( count [ i ] > 0 ) arr [ index ++ ] = i , count [ i ] -- ; } function removeAndPrintResult ( arr , n , ind1 , ind2 = - 1 ) { for ( let i = n - 1 ; i >= 0 ; i -- ) if ( i != ind1 && i != ind2 ) document . write ( arr [ i ] ) ; } function largest3Multiple ( arr , n ) { let sum = arr . reduce ( ( a , b ) => a + b , 0 ) ; if ( sum % 3 == 0 ) return true ; sortArrayUsingCounts ( arr , n ) ; let remainder = sum % 3 ; if ( remainder == 1 ) { let rem_2 = new Array ( 2 ) ; rem_2 [ 0 ] = - 1 , rem_2 [ 1 ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 1 ) { removeAndPrintResult ( arr , n , i ) ; return true ; } if ( arr [ i ] % 3 == 2 ) { if ( rem_2 [ 0 ] == - 1 ) rem_2 [ 0 ] = i ; else if ( rem_2 [ 1 ] == - 1 ) rem_2 [ 1 ] = i ; } } if ( rem_2 [ 0 ] != - 1 && rem_2 [ 1 ] != - 1 ) { removeAndPrintResult ( arr , n , rem_2 [ 0 ] , rem_2 [ 1 ] ) ; return true ; } } else if ( remainder == 2 ) { let rem_1 = new Array ( 2 ) ; rem_1 [ 0 ] = - 1 , rem_1 [ 1 ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 2 ) { removeAndPrintResult ( arr , n , i ) ; return true ; } if ( arr [ i ] % 3 == 1 ) { if ( rem_1 [ 0 ] == - 1 ) rem_1 [ 0 ] = i ; else if ( rem_1 [ 1 ] == - 1 ) rem_1 [ 1 ] = i ; } } if ( rem_1 [ 0 ] != - 1 && rem_1 [ 1 ] != - 1 ) { removeAndPrintResult ( arr , n , rem_1 [ 0 ] , rem_1 [ 1 ] ) ; return true ; } } document . write ( " " ) ; return false ; } let arr = [ 4 , 4 , 1 , 1 , 1 , 3 ] ; let n = arr . length ; largest3Multiple ( arr , n ) ;
Minimum swaps to reach permuted array with at most 2 positions left swaps allowed | This funt merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; method returns minimum number of swaps to reach permuted array ' arr ' ; loop over all elements to check Invalid permutation condition ; if an element is at distance more than 2 from its actual position then it is not possible to reach permuted array just by swapping with 2 positions left elements so returning - 1 ; If permuted array is not Invalid , then number of Inversion in array will be our final answer ; change below example
function merge ( arr , temp , left , mid , right ) { let inv_count = 0 ; let i = left ; let j = mid ; let k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) { temp [ k ++ ] = arr [ i ++ ] ; } while ( j <= right ) { temp [ k ++ ] = arr [ j ++ ] ; } for ( i = left ; i <= right ; i ++ ) { arr [ i ] = temp [ i ] ; } return inv_count ; } function _mergeSort ( arr , temp , left , right ) { let mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } function mergeSort ( arr , array_size ) { let temp = Array . from ( { length : array_size } , ( _ , i ) => 0 ) ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } function minSwapToReachArr ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] - 1 ) - i > 2 ) { return - 1 ; } } let numOfInversion = mergeSort ( arr , N ) ; return numOfInversion ; } let arr = [ 1 , 2 , 5 , 3 , 4 ] ; let N = arr . length ; let res = minSwapToReachArr ( arr , N ) ; document . write ( res == - 1 ? " " : res ) ;
Sort all even numbers in ascending order and then sort all odd numbers in descending order | To do two way sort . First sort even numbers in ascending order , then odd numbers in descending order . ; Make all odd numbers negative ; if ( arr [ i ] & 1 ) Check for odd ; Sort all numbers ; Retaining original array ; Driver code
function twoWaySort ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) arr [ i ] *= - 1 ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ; } let arr = [ 1 , 3 , 2 , 7 , 5 , 4 ] ; let n = arr . length ; twoWaySort ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Sort all even numbers in ascending order and then sort all odd numbers in descending order | Utility function to print the contents of the array ; To do two way sort . Make comparator function for the inbuilt sort function of c ++ such that odd numbers are placed before even in descending and ascending order respectively ; If both numbers are even , smaller number should be placed at lower index ; If both numbers are odd larger number should be placed at lower index ; If a is odd and b is even , a should be placed before b ; If b is odd and a is even , b should be placed before a ; Driver code ; Sort the array ; Print the sorted array
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function compare ( a , b ) { if ( a % 2 == 0 && b % 2 == 0 && a < b ) return - 1 ; if ( a % 2 != 0 && b % 2 != 0 && b > a ) return 1 ; if ( a % 2 != 0 ) return - 1 ; return 1 ; } var arr = [ 1 , 3 , 2 , 7 , 5 , 4 ] ; var n = arr . length ; arr . sort ( compare ) ; printArr ( arr , n ) ;
Possible to form a triangle from array values | Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver code
function isPossibleTriangle ( arr , N ) { if ( N < 3 ) return false ; arr . sort ( ) ; for ( let i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } let arr = [ 5 , 4 , 3 , 1 , 2 ] ; let N = arr . length ; if ( isPossibleTriangle ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
K | Javascript program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driver code
let MAX = 1000000 ; function ksmallest ( arr , n , k ) { let b = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( let j = 1 ; j < MAX ; j ++ ) { if ( b [ j ] != 1 ) { k -- ; } if ( k != 1 ) { return j ; } } return Number . MAX_VALUE ; } let k = 1 ; let arr = [ 1 ] ; let n = arr . length ; document . write ( ksmallest ( arr , n , k ) ) ;
Sort an array when two halves are sorted | Javascript program to Merge two sorted halves of array Into Single Sorted Array ; Sort the given array using sort function ; Driver code ; Print sorted Array
function mergeTwoHalf ( A , n ) { A . sort ( ( a , b ) => a - b ) ; } var A = [ 2 , 3 , 8 , - 1 , 7 , 10 ] ; var n = A . length ; mergeTwoHalf ( A , n ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ;
Choose k array elements such that difference of maximum and minimum is minimized | Return minimum difference of maximum and minimum of k elements of arr [ 0. . n - 1 ] . ; Sorting the array . ; Find minimum value among all K size subarray . ; Driver code
function minDiff ( arr , n , k ) { var result = Number . MAX_VALUE ; arr . sort ( ( a , b ) => a - b ) ; for ( i = 0 ; i <= n - k ; i ++ ) result = Math . min ( result , arr [ i + k - 1 ] - arr [ i ] ) ; return result ; } var arr = [ 10 , 100 , 300 , 200 , 1000 , 20 , 30 ] ; var n = arr . length ; var k = 3 ; document . write ( minDiff ( arr , n , k ) ) ;
Sort even | Javascript Program to separately sort even - placed and odd placed numbers and place them together in sorted array . ; create evenArr [ ] and oddArr [ ] ; Put elements in oddArr [ ] and evenArr [ ] as per their position ; sort evenArr [ ] in ascending order sort oddArr [ ] in descending order ; Driver code
function bitonicGenerator ( arr , n ) { let evenArr = [ ] ; let oddArr = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 != 1 ) { evenArr . push ( arr [ i ] ) ; } else { oddArr . push ( arr [ i ] ) ; } } evenArr . sort ( function ( a , b ) { return a - b ; } ) ; oddArr . sort ( function ( a , b ) { return b - a ; } ) ; let i = 0 ; for ( let j = 0 ; j < evenArr . length ; j ++ ) { arr [ i ++ ] = evenArr [ j ] ; } for ( let j = 0 ; j < oddArr . length ; j ++ ) { arr [ i ++ ] = oddArr [ j ] ; } } let arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] ; let n = arr . length ; bitonicGenerator ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Number of swaps to sort when only adjacent swapping allowed | This function merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; this is tricky -- see above / explanation diagram for merge ( ) ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; Driver Code
function merge ( arr , temp , left , mid , right ) { let inv_count = 0 ; let i = left ; let j = mid ; let k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } function _mergeSort ( arr , temp , left , right ) { let mid , inv_count = 0 ; if ( right > left ) { mid = Math . floor ( ( right + left ) / 2 ) ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } function countSwaps ( arr , n ) { let temp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { temp [ i ] = 0 ; } return _mergeSort ( arr , temp , 0 , n - 1 ) ; } let arr = [ 1 , 20 , 6 , 4 , 5 ] ; let n = arr . length ; document . write ( " " + countSwaps ( arr , n ) ) ;
Check whether a given number is even or odd | Returns true if n is even , else odd ; Driver code
function isEven ( n ) { return ( n % 2 == 0 ) ; } let n = 101 ; isEven ( n ) ? document . write ( " " ) : document . write ( " " ) ;
Find Surpasser Count of each element in array | Function to find surpasser count of each element in array ; stores surpasser count for element arr [ i ] ; Function to print an array ; Driver Code
function findSurpasser ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { let count = 0 ; for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; document . write ( count + " " ) ; } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( ) ; } let arr = [ 2 , 7 , 5 , 3 , 0 , 8 , 1 ] ; let n = arr . length ; document . write ( " " + " " ) ; printArray ( arr , n ) ; document . write ( " " ) ; document . write ( " " + " " + " " ) ; findSurpasser ( arr , n ) ;
Minimum sum of two numbers formed from digits of an array | Function to find and return minimum sum of two numbers formed from digits of the array . ; sort the array ; let two numbers be a and b ; fill a and b with every alternate digit of input array ; return the sum ; Driver Code
function solve ( arr , n ) { arr . sort ( ) ; let a = 0 , b = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) a = a * 10 + arr [ i ] ; else b = b * 10 + arr [ i ] ; } return a + b ; } let arr = [ 6 , 8 , 4 , 5 , 2 , 3 ] ; let n = arr . length ; document . write ( " " + solve ( arr , n ) ) ;
Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; will contain max product ; Driver Code
function maxProduct ( arr , n ) { if ( n < 3 ) return - 1 ; let max_product = Number . MIN_VALUE ; for ( let i = 0 ; i < n - 2 ; i ++ ) for ( let j = i + 1 ; j < n - 1 ; j ++ ) for ( let k = j + 1 ; k < n ; k ++ ) max_product = Math . max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ; return max_product ; } let arr = [ 10 , 3 , 5 , 6 , 20 ] ; let n = arr . length ; ; let max = maxProduct ( arr , n ) ; if ( max == - 1 ) document . write ( " " ) ; else document . write ( " " + max ) ;
Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Construct four auxiliary vectors of size n and initialize them by - 1 ; will contain max product ; to store maximum element on left of array ; to store minimum element on left of array ; leftMax [ i ] will contain max element on left of arr [ i ] excluding arr [ i ] . leftMin [ i ] will contain min element on left of arr [ i ] excluding arr [ i ] . ; reset max_sum to store maximum element on right of array ; reset min_sum to store minimum element on right of array ; rightMax [ i ] will contain max element on right of arr [ i ] excluding arr [ i ] . rightMin [ i ] will contain min element on right of arr [ i ] excluding arr [ i ] . ; For all array indexes i except first and last , compute maximum of arr [ i ] * x * y where x can be leftMax [ i ] or leftMin [ i ] and y can be rightMax [ i ] or rightMin [ i ] . ; Driver code
function maxProduct ( arr , n ) { if ( n < 3 ) return - 1 ; leftMin = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; rightMin = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; leftMax = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; rightMax = Array . from ( { length : n } , ( _ , i ) => - 1 ) ; var max_product = Number . MIN_VALUE ; var max_sum = arr [ 0 ] ; var min_sum = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { leftMax [ i ] = max_sum ; if ( arr [ i ] > max_sum ) max_sum = arr [ i ] ; leftMin [ i ] = min_sum ; if ( arr [ i ] < min_sum ) min_sum = arr [ i ] ; } max_sum = arr [ n - 1 ] ; min_sum = arr [ n - 1 ] ; for ( j = n - 2 ; j >= 0 ; j -- ) { rightMax [ j ] = max_sum ; if ( arr [ j ] > max_sum ) max_sum = arr [ j ] ; rightMin [ j ] = min_sum ; if ( arr [ j ] < min_sum ) min_sum = arr [ j ] ; } for ( i = 1 ; i < n - 1 ; i ++ ) { var max1 = Math . max ( arr [ i ] * leftMax [ i ] * rightMax [ i ] , arr [ i ] * leftMin [ i ] * rightMin [ i ] ) ; var max2 = Math . max ( arr [ i ] * leftMax [ i ] * rightMin [ i ] , arr [ i ] * leftMin [ i ] * rightMax [ i ] ) ; max_product = Math . max ( max_product , Math . max ( max1 , max2 ) ) ; } return max_product ; } var arr = [ 1 , 4 , 3 , - 6 , - 7 , 0 ] ; var n = arr . length ; var max = maxProduct ( arr , n ) ; if ( max == - 1 ) document . write ( " " ) ; else document . write ( " " + max ) ;
Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; If size is less than 3 , no triplet exists ; Sort the array in ascending order ; Return the maximum of product of last three elements and product of first two elements and last element ; Driver code
function maxProduct ( arr , n ) { if ( n < 3 ) { return - 1 ; } arr . sort ( ) ; return Math . max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; } var arr = [ - 10 , - 3 , 5 , 6 , - 20 ] ; var n = arr . length ; var max = maxProduct ( arr , n ) ; if ( max == - 1 ) { document . write ( " " ) ; } else { document . write ( " " + max ) ; }
Maximum product of a triplet ( subsequence of size 3 ) in array | Function to find a maximum product of a triplet in array of integers of size n ; If size is less than 3 , no triplet exists ; Initialize Maximum , second maximum and third maximum element ; Initialize Minimum and second minimum element ; Update Maximum , second maximum and third maximum element ; Update second maximum and third maximum element ; Update third maximum element ; Update Minimum and second minimum element ; Update second minimum element ; Driver code
function maxProduct ( arr , n ) { if ( n < 3 ) return - 1 ; var maxA = Number . MIN_VALUE , maxB = Number . MIN_VALUE , maxC = Number . MIN_VALUE ; var minA = Number . MAX_VALUE , minB = Number . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) maxC = arr [ i ] ; if ( arr [ i ] < minA ) { minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) minB = arr [ i ] ; } return Math . max ( minA * minB * maxA , maxA * maxB * maxC ) ; } var arr = [ 1 , - 4 , 3 , - 6 , 7 , 0 ] ; var n = arr . length ; var max = maxProduct ( arr , n ) ; if ( max == - 1 ) document . write ( " " ) ; else document . write ( " " + max ) ;
Maximize sum of ratios of N given fractions by incrementing numerator and denominators K times by 1 | Function to increment the K fractions from the given array to maximize the sum of ratios of the given fractions ; Size of the array ; Max priority queue ; Iterate through the array ; Insert the incremented value if an operation is performed on the ith index ; Loop to perform K operations ; Increment the numerator and denominator of ith fraction ; Add the incremented value ; Stores the average ratio ; Return the ratio ; Driver Code
function maxAverageRatio ( arr , K ) { var N = arr . length ; var q = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { var extra = ( ( arr [ i ] [ 0 ] + 1 ) / ( arr [ i ] [ 1 ] + 1 ) ) - ( arr [ i ] [ 0 ] / arr [ i ] [ 1 ] ) ; q . push ( [ extra , i ] ) ; } q . sort ( ) ; while ( K -- ) { var i = q [ q . length - 1 ] [ 1 ] ; q . pop ( ) ; arr [ i ] [ 0 ] += 1 ; arr [ i ] [ 1 ] += 1 ; var extra = ( ( arr [ i ] [ 0 ] + 1 ) / ( arr [ i ] [ 1 ] + 1 ) ) - ( arr [ i ] [ 0 ] / arr [ i ] [ 1 ] ) ; q . push ( [ extra , i ] ) ; q . sort ( ) ; } var ans = 0 ; for ( var i = 0 ; i < N ; i ++ ) { ans += ( arr [ i ] [ 0 ] / arr [ i ] [ 1 ] ) ; } return ans / N ; } var arr = [ [ 1 , 2 ] , [ 3 , 5 ] , [ 2 , 2 ] ] ; var K = 2 ; document . write ( maxAverageRatio ( arr , K ) . toFixed ( 6 ) ) ;
Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K | Function to count the operations required to convert matrix A to B ; Store the sizes of matrix ; Stores count of flips required ; Traverse the iven matrix ; Check if the matrix values are not equal ; Increment the count of moves ; Check if the current square sized exists or not ; Flip all the bits in this square sized submatrix ; Count of operations required ; Driver Code
function minMoves ( a , b , K ) { let n = a . length , m = a [ 0 ] . length ; let cntOperations = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { cntOperations ++ ; if ( i + K - 1 >= n j + K - 1 >= m ) { return - 1 ; } for ( let p = 0 ; p <= K - 1 ; p ++ ) { for ( let q = 0 ; q <= K - 1 ; q ++ ) { if ( a [ i + p ] [ j + q ] == ' ' ) { a [ i + p ] [ j + q ] = ' ' ; } else { a [ i + p ] [ j + q ] = ' ' ; } } } } } } return cntOperations ; } let A = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let B = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let K = 3 ; document . write ( minMoves ( A , B , K ) ) ;
Lexicographically largest permutation by sequentially inserting Array elements at ends | Function to find the lexicographically largest permutation by sequentially inserting the array elements ; Stores the current state of the new array ; Stores ythe current maximum element of array arr [ ] ; Iterate the array elements ; If the current element is smaller than the current maximum , then insert ; If the current element is at least the current maximum ; Update the value of the current maximum ; Prlet resultant permutation ; Driver Code
function largestPermutation ( arr , N ) { let p = [ ] ; let mx = arr [ 0 ] ; p . push ( arr [ 0 ] ) ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < mx ) p . push ( arr [ i ] ) ; else { p . unshift ( arr [ i ] ) ; mx = arr [ i ] ; } } for ( i of p ) document . write ( i + " " ) ; } let arr = [ 3 , 1 , 2 , 4 ] ; let N = arr . length ; largestPermutation ( arr , N ) ;
Largest value of K that a set of all possible subset | Function to find maximum count of consecutive integers from 0 in set S of all possible subset - sum ; Stores the maximum possible integer ; Sort the given array in non - decrasing order ; Iterate the given array ; If arr [ i ] <= X + 1 , then update X otherwise break the loop ; Return Answer ; Driver Code
function maxConsecutiveCnt ( arr ) { let X = 0 ; arr . sort ( function ( a , b ) { return a - b } ) for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] <= ( X + 1 ) ) { X = X + arr [ i ] ; } else { break ; } } return X + 1 ; } let arr = [ 1 , 1 , 1 , 4 ] ; document . write ( maxConsecutiveCnt ( arr ) ) ;
Maximum length of subarray with same sum at corresponding indices from two Arrays | Function to find maximum length of subarray of array A and B having equal sum ; Stores the maximum size of valid subarray ; Stores the prefix sum of the difference of the given arrays ; Traverse the given array ; Add the difference of the corresponding array element ; If current difference is not present ; If current difference is present , update the value of maxSize ; Return the maximum length ; Driver Code
const maxLength = ( A , B ) => { let n = A . length ; let maxSize = 0 ; let pre = { } ; let diff = 0 ; pre [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { diff += ( A [ i ] - B [ i ] ) ; if ( ! ( diff in pre ) ) { pre = i + 1 ; } else { maxSize = Math . max ( maxSize , i - pre + 1 ) ; } } return maxSize ; } let A = [ 1 , 2 , 3 , 4 ] ; let B = [ 4 , 3 , 2 , 1 ] ; document . write ( maxLength ( A , B ) ) ;