codeparrot/xlcost-text-to-code huggingface dataset:
Print all numbers up to N in words in lexicographical order | Function to convert a number to words ; Stores the digits ; Base cases ; Stores strings of unit place ; Stores strings for corner cases ; Stores strings for ten 's place digits ; Stores strings for powers of 10 ; If given number contains a single digit ; Iterate over all the digits ; Represent first 2 digits in words ; Represent last 2 digits in words ; Explicitly handle corner cases [ 10 , 19 ] ; Explicitly handle corner case 20 ; For rest of the two digit numbers i . e . , 21 to 99 ; Function to print all the numbers up to n in lexicographical order ; Convert all numbers in words ; Sort all strings ; Print answer ; Driver Code
function convert_to_words ( n ) { let num = ( n ) . toString ( ) . split ( " " ) ; let len = num . length ; let ans = " " ; if ( len == 0 ) { ans += " " ; return ans ; } let single_digits = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let two_digits = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let tens_multiple = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let tens_power = [ " " , " " ] ; if ( len == 1 ) { ans += single_digits [ num [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; return ans ; } let x = 0 ; while ( x < num . length ) { if ( len >= 3 ) { if ( num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) != 0 ) { ans += single_digits [ num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; ans += " " ; ans += tens_power [ len - 3 ] ; ans += " " ; } -- len ; } else { if ( num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) == 1 ) { let sum = num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; ans += two_digits [ sum ] ; return ans ; } else if ( num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) == 2 && num [ x + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) == 0 ) { ans += " " ; return ans ; } else { let i = ( num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; if ( i > 0 ) { ans += tens_multiple [ i ] ; ans += " " ; } else ans += " " ; ++ x ; if ( num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) != 0 ) ans += single_digits [ num [ x ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } } ++ x ; } return " " ; } function lexNumbers ( n ) { let s = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) { s . push ( convert_to_words ( i ) ) ; } s . sort ( ) let ans = [ ] ; for ( let i = 0 ; i < n ; i ++ ) ans . push ( s [ i ] ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) document . write ( ans [ i ] + " " ) ; document . write ( ans [ n - 1 ] ) ; } let n = 5 ; lexNumbers ( n ) ;
Minimize cost to convert all array elements to 0 | Function to calculate the minimum cost of converting all array elements to 0 s ; Stores subarrays of 0 s only ; Traverse the array ; If consecutive 0 s occur ; Increment if needed ; Push the current length of consecutive 0 s in a vector ; Update lengths as 0 ; Sorting vector ; Stores the number of subarrays consisting of 1 s ; Traverse the array ; If current element is 1 ; Otherwise ; Increment count of consecutive ones ; Stores the minimum cost ; Traverse the array ; First element ; Traverse the subarray sizes ; Update cost ; Cost of performing X and Y operations ; Find the minimum cost ; Print the minimum cost ; Driver Code ; Function Call
function minimumCost ( binary , n , a , b ) { var groupOfZeros = [ ] ; var len = 0 , i = 0 ; var increment_need = true ; while ( i < n ) { increment_need = true ; while ( i < n && binary [ i ] == 0 ) { len ++ ; i ++ ; increment_need = false ; } if ( increment_need == true ) { i ++ ; } if ( len != 0 ) { groupOfZeros . push ( len ) ; } len = 0 ; } groupOfZeros . sort ( ( a , b ) => a - b ) ; i = 0 ; var found_ones = false ; var NumOfOnes = 0 ; while ( i < n ) { found_ones = false ; while ( i < n && binary [ i ] == 1 ) { i ++ ; found_ones = true ; } if ( found_ones == false ) i ++ ; else NumOfOnes ++ ; } var ans = 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { var curr = 0 , totalOnes = NumOfOnes ; if ( i == 0 ) { curr = totalOnes * a ; } else { var mark = i , num_of_changes = 0 ; groupOfZeros . forEach ( x => { if ( mark >= x ) { totalOnes -- ; mark -= x ; num_of_changes += x ; } } ) ; curr = ( num_of_changes * b ) + ( totalOnes * a ) ; } ans = Math . min ( ans , curr ) ; } document . write ( ans ) ; } var arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] ; var N = arr . length ; var X = 10 , Y = 4 ; minimumCost ( arr , N , X , Y ) ;
Reduce array to longest sorted array possible by removing either half of given array in each operation | Function to check if the subarray arr [ i . . j ] is a sorted subarray or not ; Traverse all elements of the subarray arr [ i ... j ] ; If the previous element of the subarray exceeds current element ; Return 0 ; Return 1 ; Recursively partition the array into two equal halves ; If atmost one element is left in the subarray arr [ i . . j ] ; Checks if subarray arr [ i . . j ] is a sorted subarray or not ; If the subarray arr [ i ... j ] is a sorted subarray ; Stores middle element of the subarray arr [ i . . j ] ; Recursively partition the current subarray arr [ i . . j ] into equal halves ; Driver Code
function isSortedparitions ( arr , i , j ) { for ( var k = i + 1 ; k <= j ; k ++ ) { if ( arr [ k ] < arr [ k - 1 ] ) { return 0 ; } } return 1 ; } function partitionsArr ( arr , i , j ) { if ( i >= j ) return 1 ; var flag = isSortedparitions ( arr , i , j ) ; if ( flag ) { return ( j - i + 1 ) ; } var mid = parseInt ( ( i + j ) / 2 ) ; var X = partitionsArr ( arr , i , mid ) ; var Y = partitionsArr ( arr , mid + 1 , j ) ; return Math . max ( X , Y ) ; } var arr = [ 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 ] ; var N = arr . length ; document . write ( partitionsArr ( arr , 0 , N - 1 ) ) ;
Check if two sorted arrays can be merged to form a sorted array with no adjacent pair from the same array | Function to check if it is possible to merge the two given arrays with given conditions ; Stores index of the array A [ ] ; Stores index of the array B [ ] ; Check if the previous element are from the array A [ ] or from the array B [ ] ; Check if it is possible to merge the two given sorted arrays with given conditions ; Traverse both the arrays ; If A [ i ] is less than B [ j ] and previous element are not from A [ ] ; Update prev ; Update i ; If B [ j ] is less than A [ i ] and previous element are not from B [ ] ; Update prev ; Update j ; If A [ i ] equal to B [ j ] ; If previous element are not from B [ ] ; Update prev ; Update j ; If previous element is not from A [ ] ; Update prev ; Update i ; If it is not possible to merge two sorted arrays with given conditions ; Update flag ; Driver Code
function checkIfPossibleMerge ( A , B , N ) { let i = 0 ; let j = 0 ; let prev = - 1 ; let flag = true ; while ( i < N && j < N ) { if ( A [ i ] < B [ j ] && prev != 0 ) { prev = 0 ; i ++ ; } else if ( B [ j ] < A [ i ] && prev != 1 ) { prev = 1 ; j ++ ; } else if ( A [ i ] == B [ j ] ) { if ( prev != 1 ) { prev = 1 ; j ++ ; } else { prev = 0 ; i ++ ; } } else { flag = false ; break ; } } return flag ; } let A = [ 3 , 5 , 8 ] ; let B = [ 2 , 4 , 6 ] ; let N = A . length ; if ( checkIfPossibleMerge ( A , B , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times | Function to find the minimum sum of array elements by given operations ; Base case ; Return 0 ; Base case ; Perform K operations ; Stores smallest element in the array ; Stores index of the smallest array element ; Stores largest element in the array ; Stores index of the largest array element ; Traverse the array elements ; If current element exceeds largest_element ; Update the largest element ; Update index of the largest array element ; If current element is smaller than smallest_element ; Update the smallest element ; Update index of the smallest array element ; Stores the value of smallest element by given operations ; Stores the value of largest element by given operations ; If the value of a + b less than the sum of smallest and largest element of the array ; Update smallest element of the array ; Update largest element of the array ; Stores sum of elements of the array by given operations ; Traverse the array ; Update ans ; Driver Code
function minimum_possible_sum ( arr , n , k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } for ( let i = 0 ; i < k ; i ++ ) { let smallest_element = arr [ 0 ] ; let smallest_pos = 0 ; let largest_element = arr [ 0 ] ; let largest_pos = 0 ; for ( let j = 1 ; j < n ; j ++ ) { if ( arr [ j ] >= largest_element ) { largest_element = arr [ j ] ; largest_pos = j ; } if ( arr [ j ] < smallest_element ) { smallest_element = arr [ j ] ; smallest_pos = j ; } } let a = smallest_element * 2 ; let b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { arr [ smallest_pos ] = a ; arr [ largest_pos ] = b ; } } let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ans += arr [ i ] ; } return ans ; } let arr = [ 50 , 1 , 100 , 100 , 1 ] ; let K = 2 ; let n = arr . length ; document . write ( minimum_possible_sum ( arr , n , K ) ) ;
Rearrange array elements excluded by given ranges to maximize sum of subarrays starting from the first index | Function that finds the maximum sum all subarrays from the starting index after rearranging the array ; Stores elements after rearranging ; Keeps track of visited elements ; Traverse the queries ; Mark elements that are not allowed to rearranged ; Stores the indices ; Get indices and elements that are allowed to rearranged ; Store the current index and element ; Sort vector v in descending order ; Insert elements in array ; Stores the resultant sum ; Stores the prefix sums ; Traverse the given array ; Return the maximum sum ; Given array ; Given size ; Queries ; Number of queries ; Function Call
function maxSum ( n , a , l , q ) { let v = [ ] ; let d = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { d [ i ] = 0 ; } for ( let i = 0 ; i < q ; i ++ ) { for ( let x = l [ i ] [ 0 ] ; x <= l [ i ] [ 1 ] ; x ++ ) { if ( d [ x ] == 0 ) { d [ x ] = 1 ; } } } let st = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( d [ i ] == 0 ) { v . push ( a [ i ] ) ; st . add ( i ) ; } } v . sort ( function ( a , b ) { return b - a ; } ) ; let c = 0 ; for ( let it of st . values ( ) ) { a [ it ] = v ; c ++ ; } let pref_sum = 0 ; let temp_sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { temp_sum += a [ i ] ; pref_sum += temp_sum ; } return pref_sum ; } let arr = [ - 8 , 4 , - 2 , - 6 , 4 , 7 , 1 ] ; let N = arr . length ; let q = [ [ 0 , 0 ] , [ 4 , 5 ] ] ; let queries = q . length ; document . write ( maxSum ( N , arr , q , queries ) ) ;
Maximum occurred integers after M circular operations in given range | Function to find the maximum occurred integer after completing all circular operations ; Stores the highest visit count for any element ; Stors the number of times an element is visited ; Iterate over the array ; Iterate over the range circularly form start to end ; Count number of times an element is visited ; Increment frequency of N ; Update maxVisited ; Increment frequency of start ; Update maxVisited ; Increment the start ; Increment the count for the last visited element ; Print most visited elements ; Driver Code ; Function Call
function mostvisitedsector ( N , A ) { var maxVisited = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < A . length - 1 ; i ++ ) { var start = A [ i ] % N ; var end = A [ i + 1 ] % N ; while ( start != end ) { if ( start == 0 ) { if ( mp . has ( N ) ) mp . set ( N , mp . get ( N ) + 1 ) ; else mp . set ( N , 1 ) ; if ( mp . get ( N ) > maxVisited ) { maxVisited = mp . get ( N ) ; } } else { if ( mp . has ( start ) ) mp . set ( start , mp . get ( start ) + 1 ) else mp . set ( start , 1 ) ; if ( mp . get ( start ) > maxVisited ) { maxVisited = mp . get ( start ) ; } } start = ( start + 1 ) % N ; } } var back = A [ A . length - 1 ] ; if ( mp . has ( back ) ) mp . set ( back , mp . get ( back ) + 1 ) else mp . set ( back , 1 ) ; if ( mp . get ( back ) > maxVisited ) { maxVisited = mp . get ( back ) ; } mp . forEach ( ( value , key ) => { if ( value == maxVisited ) { document . write ( key + " " ) ; } } ) ; } var N = 4 ; var arr = [ 1 , 2 , 1 , 2 ] ; mostvisitedsector ( N , arr ) ;
Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points | Function to return the area of the largest rectangle formed by lines parallel to X and Y axis from given set of polets ; Initialize two arrays ; Store x and y coordinates ; Sort arrays ; Initialize max differences ; Find max adjacent differences ; Return answer ; Given points ; Total points ; Function call
function maxRectangle ( sequence , size ) { let X_Cord = [ ] ; let Y_Cord = [ ] ; for ( let i = 0 ; i < size ; i ++ ) { X_Cord [ i ] = sequence [ i ] [ 0 ] ; Y_Cord [ i ] = sequence [ i ] [ 1 ] ; } X_Cord . sort ( ) ; Y_Cord . sort ( ) ; let X_Max = 0 , Y_Max = 0 ; for ( let i = 0 ; i < size - 1 ; i ++ ) { X_Max = Math . max ( X_Max , X_Cord [ i + 1 ] - X_Cord [ i ] ) ; Y_Max = Math . max ( Y_Max , Y_Cord [ i + 1 ] - Y_Cord [ i ] ) ; } return X_Max * Y_Max ; } let point = [ [ - 2 , 0 ] , [ 2 , 0 ] , [ 4 , 0 ] , [ 4 , 2 ] ] ; let n = point . length ; document . write ( maxRectangle ( point , n ) ) ;
Rearrange two given arrays such that sum of same indexed elements lies within given range | Function to check if there exists any arrangements of the arrays such that sum of element lie in the range [ K / 2 , K ] ; Sort the array arr1 [ ] in increasing order ; Sort the array arr2 [ ] in decreasing order ; Traverse the array ; If condition is not satisfied break the loop ; Prlet the result ; Driver Code
function checkArrangement ( A1 , A2 , n , k ) { A1 . sort ( ) ; A2 . sort ( ) ; A2 . reverse ( ) ; let flag = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( A1 [ i ] + A2 [ i ] > k ) || ( A1 [ i ] + A2 [ i ] < k / 2 ) ) { flag = 1 ; break ; } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } let arr1 = [ 1 , 3 , 4 , 5 ] ; let arr2 = [ 2 , 0 , 1 , 1 ] ; let K = 6 ; let N = arr1 . length ; checkArrangement ( arr1 , arr2 , N , K ) ;
Rearrange two given arrays to maximize sum of same indexed elements | Function to find the maximum possible sum by rearranging the given array elements ; Sort the array A [ ] in ascending order ; Sort the array B [ ] in descending order ; Stores maximum possible sum by rearranging array elements ; Traverse both the arrays ; Update maxSum ; Driver Code
function MaxRearrngeSum ( A , B , N ) { A . sort ( ) ; B . sort ( ) ; B . reverse ( ) ; let maxSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { maxSum += Math . abs ( A [ i ] - B [ i ] ) ; } return maxSum ; } let A = [ 1 , 2 , 2 , 4 , 5 ] ; let B = [ 5 , 5 , 5 , 6 , 6 ] ; let N = A . length ; document . write ( MaxRearrngeSum ( A , B , N ) ) ;
Area of the largest rectangle possible from given coordinates | Function to find the maximum possible area of a rectangle ; Initialize variables ; Sort array arr1 [ ] ; Sort array arr2 [ ] ; Traverse arr1 [ ] and arr2 [ ] ; If arr1 [ i ] is same as arr2 [ j ] ; If no starting point is found yet ; Update maximum end ; If arr [ i ] > arr2 [ j ] ; If no rectangle is found ; Return the area ; Given point ; Given length ; Given points ; Given length ; Function Call
function largestArea ( arr1 , n , arr2 , m ) { var end = 0 , start = 0 , i = 0 , j = 0 ; arr1 . sort ( ) ; arr2 . sort ( ) ; while ( i < n && j < m ) { if ( arr1 [ i ] == arr2 [ j ] ) { if ( start == 0 ) start = arr1 [ i ] ; else end = arr1 [ i ] ; i ++ ; j ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) j ++ ; else i ++ ; } if ( end == 0 start == 0 ) return 0 ; else return ( end - start ) ; } var arr1 = [ 1 , 2 , 4 ] ; var N = arr1 . length ; var arr2 = [ 1 , 3 , 4 ] ; var M = arr2 . length ; document . write ( largestArea ( arr1 , N , arr2 , M ) ) ;
Count pairs from two arrays with difference exceeding K | Function to count pairs that satisfy the given conditions ; Stores index of the left pointer . ; Stores index of the right pointer ; Stores count of total pairs that satisfy the conditions ; Sort arr [ ] array ; Sort brr [ ] array ; Traverse both the array and count then pairs ; If the value of ( brr [ j ] - arr [ i ] ) exceeds K ; Update cntPairs ; Update ; Update j ; Driver Code
function count_pairs ( arr , brr , N , M , K ) { let i = 0 ; let j = 0 ; let cntPairs = 0 ; ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; ( brr ) . sort ( function ( a , b ) { return a - b ; } ) ; while ( i < N && j < M ) { if ( brr [ j ] - arr [ i ] > K ) { cntPairs += ( M - j ) ; i ++ ; } else { j ++ ; } } return cntPairs ; } let arr = [ 5 , 9 , 1 , 8 ] ; let brr = [ 10 , 12 , 7 , 4 , 2 , 3 ] ; let K = 3 ; let N = arr . length ; let M = brr . length ; document . write ( count_pairs ( arr , brr , N , M , K ) ) ;
Merge two sorted arrays in O ( 1 ) extra space using Heap | Function to perform min heapify on array brr [ ] ; Stores index of left child of i . ; Stores index of right child of i . ; Stores index of the smallest element in ( arr [ i ] , arr [ left ] , arr [ right ] ) ; Check if arr [ left ] less than arr [ smallest ] ; Update smallest ; Check if arr [ right ] less than arr [ smallest ] ; Update smallest ; if i is not the index of smallest element ; Swap arr [ i ] and arr [ smallest ] ; Perform heapify on smallest ; Function to merge two sorted arrays ; Traverse the array arr [ ] ; Check if current element is less than brr [ 0 ] ; Swap arr [ i ] and brr [ 0 ] ; Perform heapify on brr [ ] ; Sort array brr [ ] ; Function to print array elements ; Traverse array arr [ ] ; Driver Code ; Function call to merge two array ; Print first array ; Print Second array .
function minHeapify ( brr , i , M ) { let left = 2 * i + 1 ; let right = 2 * i + 2 ; let smallest = i ; if ( left < M && brr [ left ] < brr [ smallest ] ) { smallest = left ; } if ( right < M && brr [ right ] < brr [ smallest ] ) { smallest = right ; } if ( smallest != i ) { let temp = brr [ i ] ; brr [ i ] = brr [ smallest ] ; brr [ smallest ] = temp ; minHeapify ( brr , smallest , M ) ; } } function merge ( arr , brr , N , M ) { for ( let i = 0 ; i < N ; ++ i ) { if ( arr [ i ] > brr [ 0 ] ) { let temp = arr [ i ] ; arr [ i ] = brr [ 0 ] ; brr [ 0 ] = temp ; minHeapify ( brr , 0 , M ) ; } } brr . sort ( ) ; } function printArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 2 , 23 , 35 , 235 , 2335 ] ; let brr = [ 3 , 5 ] ; let N = arr . length ; let M = brr . length ; merge ( arr , brr , N , M ) ; printArray ( arr , N ) ; printArray ( brr , M ) ;
Count array elements with rank not exceeding K | Function to find count of array elements with rank less than or equal to k ; Initialize rank and position ; Sort the given array ; Traverse array from right to left ; Update rank with position , if adjacent elements are unequal ; Return position - 1 , if rank greater than k ; Increase position ; Given array ; Given K ; Function Call
function rankLessThanK ( arr , k , n ) { let rank = 1 ; let position = 1 ; arr . sort ( ) ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( i == n - 1 arr [ i ] != arr [ i + 1 ] ) { rank = position ; if ( rank > k ) return position - 1 ; } position ++ ; } return n ; } let arr = [ 2 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; let K = 4 ; document . write ( rankLessThanK ( arr , K , N ) ) ;
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Given N ; Given arr [ ] ; Function Calling
function minCost ( arr , n ) { if ( n == 1 ) { document . write ( 0 ) ; return ; } arr . sort ( ( a , b ) => a - b ) ; var raised = 1 / ( n - 1 ) ; var temp = Math . pow ( arr [ n - 1 ] , raised ) ; var r = Math . round ( temp ) + 1 ; var i , j , min_cost = 1000000000 ; var common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { var curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += Math . abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = Math . min ( min_cost , curr_cost ) ; common_ratio = j ; } } document . write ( min_cost + ' ' ) ; document . write ( common_ratio + ' ' ) ; } var N = 6 ; var arr = [ 1 , 11 , 4 , 27 , 15 , 33 ] ; minCost ( arr , N ) ;
Mth bit in Nth binary string from a sequence generated by the given operations | JavaScript program for above approach ; Function to calculate N Fibonacci numbers ; Function to find the mth bit in the string Sn ; Base case ; Length of left half ; Length of the right half ; Recursive check in the left half ; Recursive check in the right half ; Driver Code
const maxN = 10 function calculateFib ( fib , n ) { fib [ 0 ] = fib [ 1 ] = 1 ; for ( let x = 2 ; x < n ; x ++ ) { fib [ x ] = fib [ x - 1 ] + fib [ x - 2 ] ; } } function find_mth_bit ( n , m , fib ) { if ( n <= 1 ) { return n ; } let len_left = fib [ n - 2 ] ; let len_right = fib [ n - 1 ] ; if ( m <= len_left ) { return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) ; } else { return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) ; } } function find_mth_bitUtil ( n , m ) { let fib = new Array ( maxN ) ; calculateFib ( fib , maxN ) ; let ans = find_mth_bit ( n , m , fib ) ; document . write ( ans + " " ) ; } let n = 5 , m = 3 ; find_mth_bitUtil ( n , m ) ;
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix | Function to count the minimum number of adjacent swaps ; Stores the size of the given matrix ; Stores the count of zero at the end of each row ; Traverse the given matrix ; Count of 0 s at the end of the ith row ; Stores the count of swaps ; Traverse the cntZero array ; If count of zero in the i - th row < ( N - i - 1 ) ; Stores the index of the row where count of zero > ( N - i - 1 ) ; If no row found that satisfy the condition ; Swap the adjacent row ; Driver Code
function minAdjSwaps ( mat ) { var N = mat . length ; var cntZero = Array ( N ) . fill ( 0 ) ; for ( i = 0 ; i < N ; i ++ ) { for ( j = N - 1 ; j >= 0 && mat [ i ] [ j ] == 0 ; j -- ) { cntZero [ i ] ++ ; } } var cntSwaps = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( cntZero [ i ] < ( N - i - 1 ) ) { var First = i ; while ( First < N && cntZero [ First ] < ( N - i - 1 ) ) { First ++ ; } if ( First == N ) { return - 1 ; } while ( First > i ) { cntZero = swap ( cntZero , First , First - 1 ) ; First -- ; cntSwaps ++ ; } } } return cntSwaps ; } function swap ( arr , i , j ) { var temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } var mat = [ [ 0 , 0 , 2 ] , [ 3 , 1 , 0 ] , [ 4 , 0 , 0 ] ] ; document . write ( minAdjSwaps ( mat ) ) ;
Largest area in a grid unbounded by towers | Function to calculate the largest area unguarded by towers ; Sort the x - coordinates of the list ; Sort the y - coordinates of the list ; dx -- > maximum uncovered tiles in x coordinates ; dy -- > maximum uncovered tiles in y coordinates ; Calculate the maximum uncovered distances for both x and y coordinates ; Largest unguarded area is max ( dx ) - 1 * max ( dy ) - 1 ; Length and width of the grid ; No of guard towers ; Array to store the x and y coordinates ; Function call
function maxArea ( polet_x , polet_y , n , length , width ) { polet_x . sort ( ( a , b ) => a - b ) ; ; polet_y . sort ( ( a , b ) => a - b ) ; ; let dx = polet_x [ 0 ] ; let dy = polet_y [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { dx = Math . max ( dx , polet_x [ i ] - polet_x [ i - 1 ] ) ; dy = Math . max ( dy , polet_y [ i ] - polet_y [ i - 1 ] ) ; } dx = Math . max ( dx , ( length + 1 ) - polet_x [ n - 1 ] ) ; dy = Math . max ( dy , ( width + 1 ) - polet_y [ n - 1 ] ) ; document . write ( ( dx - 1 ) * ( dy - 1 ) ) ; } let length = 15 , width = 8 ; let n = 3 ; let polet_x = [ 3 , 11 , 8 ] ; let polet_y = [ 8 , 2 , 6 ] ; maxArea ( polet_x , polet_y , n , length , width ) ;