codeparrot/xlcost-text-to-code huggingface dataset:
Sort the given string using character search | function to sort the given string without using any sorting technique ; to store the final sorted string ; for each character ' i ' ; if character ' i ' is present at a particular index then add character ' i ' to ' new _ str ' ; required final sorted string ; Driver program to test above
function sortString ( str , n ) { var new_str = " " ; for ( var i = ' ' . charCodeAt ( 0 ) ; i <= ' ' . charCodeAt ( 0 ) ; i ++ ) for ( var j = 0 ; j < n ; j ++ ) if ( str [ j ] . charCodeAt ( 0 ) == i ) new_str += str [ j ] ; return new_str ; } var str = " " ; var n = str . length ; document . write ( sortString ( str , n ) ) ;
Sort the given string using character search | Javascript implementation to sort the given string without using any sorting technique ; A character array to store the no . of occurrences of each character between ' a ' to ' z ' ; to store the final sorted string ; To store each occurrence of character by relative indexing ; To traverse the character array and append it to new_str ; Driver program to test above
function sortString ( str , n ) { var i ; var arr = Array ( 26 ) . fill ( 0 ) ; var new_str = " " ; for ( i = 0 ; i < n ; i ++ ) ++ arr [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; for ( i = 0 ; i < 26 ; i ++ ) while ( arr [ i ] -- ) new_str += String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; return new_str ; } var str = " " ; var n = str . length ; document . write ( sortString ( str , n ) ) ;
Maximum occurring character in a linked list | Link list node ; Storing element 's frequencies  in a hash table. ; Calculating the first maximum element ; Push a node to linked list . Note that this function changes the head ; Driver Code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var head = null ; function maxChar ( head ) { var p = head ; var hash = new Array ( 256 ) . fill ( 0 ) ; while ( p != null ) { hash [ p . data . charCodeAt ( 0 ) ] ++ ; p = p . next ; } p = head ; var max = - 1 ; var res = " " ; while ( p != null ) { if ( max < hash [ p . data . charCodeAt ( 0 ) ] ) { res = p . data ; max = hash [ p . data . charCodeAt ( 0 ) ] ; } p = p . next ; } return res ; } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } head = null ; var str = " " . split ( " " ) ; for ( var i = 0 ; i < str . length ; i ++ ) { push ( head , str [ i ] ) ; } document . write ( maxChar ( head ) + " " ) ;
Maximum sum of elements from each row in the matrix | JavaScript Program to find row - wise maximum element sum considering elements in increasing orderers ; Function to perform given task ; Getting the maximum element from last row ; Comparing it with the elements of above rows ; Maximum of current row . ; If we could not an element smaller than prev_max . ; Driver Code
let N = 3 ; function getGreatestSum ( a ) { let prev_max = 0 ; for ( let j = 0 ; j < N ; j ++ ) if ( prev_max < a [ N - 1 ] [ j ] ) prev_max = a [ N - 1 ] [ j ] ; let sum = prev_max ; for ( let i = N - 2 ; i >= 0 ; i -- ) { let curr_max = - 2147483648 ; for ( let j = 0 ; j < N ; j ++ ) if ( prev_max > a [ i ] [ j ] && a [ i ] [ j ] > curr_max ) curr_max = a [ i ] [ j ] ; if ( curr_max == - 2147483648 ) return - 1 ; prev_max = curr_max ; sum += prev_max ; } return sum ; } let a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; document . write ( getGreatestSum ( a ) + " " ) ; let b = [ [ 4 , 5 , 6 ] , [ 4 , 5 , 6 ] , [ 4 , 5 , 6 ] ] ; document . write ( getGreatestSum ( b ) ) ;
Find the number of times every day occurs in a month | function to find occurrences ; stores days in a week ; Initialize all counts as 4. ; find index of the first day ; number of days whose occurrence will be 5 ; mark the occurrence to be 5 of n - 28 days ; print the days ; Driver code
function occurrenceDays ( n , firstday ) { let days = [ " " , " " , " " , " " , " " , " " , " " ] ; let count = [ ] ; for ( let i = 0 ; i < 7 ; i ++ ) count [ i ] = 4 ; let pos = 0 ; for ( let i = 0 ; i < 7 ; i ++ ) { if ( firstday == days [ i ] ) { pos = i ; break ; } } let inc = n - 28 ; for ( let i = pos ; i < pos + inc ; i ++ ) { if ( i > 6 ) count [ i % 7 ] = 5 ; else count [ i ] = 5 ; } for ( let i = 0 ; i < 7 ; i ++ ) { document . write ( days [ i ] + " " + count [ i ] + " " ) ; } } let n = 31 ; let firstday = " " ; occurrenceDays ( n , firstday ) ;
Value of k | JavaScript program to fin k - th element after append and insert middle operations ; Middle element of the sequence ; length of the resulting sequence . ; Updating the middle element of next sequence ; Moving to the left side of the middle element . ; Moving to the right side of the middle element . ; Driver code
function findElement ( n , k ) { let ans = n ; let left = 1 ; let right = ( Math . pow ( 2 , n ) - 1 ) ; while ( true ) { let mid = ( left + right ) / 2 ; if ( k == mid ) { document . write ( ans ) ; break ; } ans -- ; if ( k < mid ) { right = mid - 1 ; } else { left = mid + 1 ; } } } let n = 4 , k = 8 ; findElement ( n , k ) ;
First common element in two linked lists | Link list node ; A utility function to insert a node at the beginning of a linked list ; Returns the first repeating element in linked list ; Traverse through every node of first list ; If current node is present in second list ; If no common node ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function firstCommon ( head1 , head2 ) { for ( ; head1 != null ; head1 = head1 . next ) for ( var p = head2 ; p != null ; p = p . next ) if ( p . data == head1 . data ) return head1 . data ; return 0 ; } var head1 = null ; head1 = push ( head1 , 20 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 15 ) ; head1 = push ( head1 , 10 ) ; var head2 = null ; head2 = push ( head2 , 10 ) ; head2 = push ( head2 , 2 ) ; head2 = push ( head2 , 15 ) ; head2 = push ( head2 , 8 ) ; document . write ( firstCommon ( head1 , head2 ) ) ;
Print pair with maximum AND value in an array | Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Find the elements ; print the pair of elements ; inc count value after prleting element ; return the result value ; Driver code
function checkBit ( pattern , arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } function maxAND ( arr , n ) { let res = 0 , count ; for ( let bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } if ( res == 0 ) System . out . prletln ( " " ) ; else { document . write ( " " ) ; count = 0 ; for ( let i = 0 ; i < n && count < 2 ; i ++ ) { if ( ( arr [ i ] & res ) == res ) { count ++ ; document . write ( arr [ i ] + " " ) ; } } document . write ( " " ) ; } return res ; } let arr = [ 4 , 8 , 6 , 2 ] ; let n = arr . length ; document . write ( " " + maxAND ( arr , n ) ) ;
Probability of choosing a random pair with maximum sum in an array | Function to get max first and second ; If current element is smaller than first , then update both first and second ; If arr [ i ] is in between first and second then update second ; frequency of first maximum ; frequency of second maximum ; Returns probability of choosing a pair with maximum sum . ; Driver code
function countMaxSumPairs ( a , n ) { let first = Number . MIN_VALUE , second = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > first ) { second = first ; first = a [ i ] ; } else if ( a [ i ] > second && a [ i ] != first ) second = a [ i ] ; } let cnt1 = 0 , cnt2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == first ) cnt1 ++ ; if ( a [ i ] == second ) cnt2 ++ ; } if ( cnt1 == 1 ) return cnt2 ; if ( cnt1 > 1 ) return cnt1 * ( cnt1 - 1 ) / 2 ; return 0 ; } function findMaxSumProbability ( a , n ) { let total = n * ( n - 1 ) / 2 ; let max_sum_pairs = countMaxSumPairs ( a , n ) ; return max_sum_pairs / total ; } let a = [ 1 , 2 , 2 , 3 ] ; let n = a . length ; ; document . write ( findMaxSumProbability ( a , n ) ) ;
The painter 's partition problem |
for ( var i = k - 1 ; i <= n ; i ++ ) best = Math . min ( best , Math . max ( partition ( arr , i , k - 1 ) , sum ( arr , i , n - 1 ) ) ) ;
Find if given number is sum of first n natural numbers | Function to find no . of elements to be added from 1 to get n ; Start adding numbers from 1 ; If sum becomes equal to s return n ; Drivers code
function findS ( s ) { var sum = 0 ; for ( n = 1 ; sum < s ; n ++ ) { sum += n ; if ( sum == s ) return n ; } return - 1 ; } var s = 15 ; var n = findS ( s ) ; if ( n == - 1 ) document . write ( " " ) ; else document . write ( n ) ;
Find if given number is sum of first n natural numbers | Function to check if the s is the sum of first N natural number ; Solution of Quadratic Equation ; Condition to check if the solution is a integer ; Driver code ; Function call
function isvalid ( s ) { let k = ( - 1.0 + Math . sqrt ( 1 + 8 * s ) ) / 2 ; if ( Math . ceil ( k ) == Math . floor ( k ) ) return k ; else return - 1 ; } let s = 15 ; document . write ( isvalid ( s ) ) ;
Save from Bishop in chessboard | function to calc total safe position ; i , j denotes row and column of position of bishop ; calc distance in four direction ; calc total sum of distance + 1 for unsafe positions ; return total safe positions ; driver code
function calcSafe ( pos ) { let j = pos % 10 ; let i = Math . floor ( pos / 10 ) ; let dis_11 = Math . min ( Math . abs ( 1 - i ) , Math . abs ( 1 - j ) ) ; let dis_18 = Math . min ( Math . abs ( 1 - i ) , Math . abs ( 8 - j ) ) ; let dis_81 = Math . min ( Math . abs ( 8 - i ) , Math . abs ( 1 - j ) ) ; let dis_88 = Math . min ( Math . abs ( 8 - i ) , Math . abs ( 8 - j ) ) ; let sum = dis_11 + dis_18 + dis_81 + dis_88 + 1 ; return ( 64 - sum ) ; } let pos = 34 ; document . write ( " " + calcSafe ( pos ) ) ;
Counting cross lines in an array | Javascript program to count cross line in array ; Merges two subarrays of arr [ ] . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] ; create temp arrays ; Copy data to temp arrays L [ ] and R [ ] ; i = 0 ; Initial index of first subarray j = 0 ; Initial index of second subarray k = l ; Initial index of merged subarray ; == == == == == == == == == == == == == == == == == == == == == = MAIN PORTION OF CODE == == == == == == == == == == == == == == == == == == == == = add all line which is cross by current element ; Copy the remaining elements of L [ ] , if there are any ; Copy the remaining elements of R [ ] , if there are any ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; function return count of cross line in an array ; Driver code
let count_crossline = 0 ; function merge ( arr , l , m , r ) { let i , j , k ; let n1 = m - l + 1 ; let n2 = r - m ; let L = [ ] ; let R = [ ] ; for ( i = 0 ; i < n1 ; i ++ ) { L [ i ] = arr [ l + i ] ; } for ( j = 0 ; j < n2 ; j ++ ) { R [ j ] = arr [ m + 1 + j ] ; } while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; count_crossline += ( n1 - i ) ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } function mergeSort ( arr , l , r ) { if ( l < r ) { let m = l + Math . floor ( ( r - l ) / 2 ) ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } function countCrossLine ( arr , n ) { mergeSort ( arr , 0 , n - 1 ) ; document . write ( count_crossline ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] ; let n = arr . length ; countCrossLine ( arr , n ) ;
Count number of elements between two given elements in array | Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements between the two elements . ; Driver program
function getCount ( arr , n , num1 , num2 ) { let i = 0 ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == num1 ) break ; if ( i >= n - 1 ) return 0 ; let j ; for ( j = n - 1 ; j >= i + 1 ; j -- ) if ( arr [ j ] == num2 ) break ; if ( j == i ) return 0 ; return ( j - i - 1 ) ; } let arr = [ 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 ] ; let n = arr . length ; let num1 = 5 , num2 = 4 ; document . write ( getCount ( arr , n , num1 , num2 ) ) ;
Best meeting point in 2D binary array | Javascript program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver code
let ROW = 3 ; let COL = 5 ; function minTotalDistance ( grid ) { if ( ROW == 0 COL == 0 ) return 0 ; let vertical = [ ] ; let horizontal = [ ] ; for ( let i = 0 ; i < ROW ; i ++ ) { for ( let j = 0 ; j < COL ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { vertical . push ( i ) ; horizontal . push ( j ) ; } } } ( vertical ) . sort ( function ( a , b ) { return a - b ; } ) ; ( horizontal ) . sort ( function ( a , b ) { return a - b ; } ) ; let size = vertical . length / 2 ; let x = vertical [ size ] ; let y = horizontal [ size ] ; let distance = 0 ; for ( let i = 0 ; i < ROW ; i ++ ) for ( let j = 0 ; j < COL ; j ++ ) if ( grid [ i ] [ j ] == 1 ) distance += Math . abs ( x - i ) + Math . abs ( y - j ) ; return distance ; } let grid = [ [ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] ] ; document . write ( minTotalDistance ( grid ) ) ;
Count numbers with difference between number and its digit sum greater than specific value | method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit is greater than or equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ ▁ found ' and ' N ' as result ; Driver Code
function sumOfDigit ( K ) { let sod = 0 ; while ( K ) { sod += K % 10 ; K /= 10 ; } return sod ; } function totalNumbersWithSpecificDifference ( N , diff ) { let low = 1 ; let high = N ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } let N = 13 ; let diff = 2 ; document . write ( totalNumbersWithSpecificDifference ( N , diff ) ) ;
Randomized Binary Search Algorithm | To generate random number between x and y ie . . [ x , y ] ; A iterative randomized binary search function . It returns location of x in given array arr [ l . . r ] if present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; If we reach here , then element was not present ; Driver code
function getRandom ( x , y ) { return Math . floor ( x + Math . floor ( Math . random ( ) * 10 ) % ( y - x + 1 ) ) ; } function randomizedBinarySearch ( arr , l , r , x ) { while ( l <= r ) { let m = getRandom ( l , r ) ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } let arr = [ 2 , 3 , 4 , 10 , 40 ] ; let n = arr . length ; let x = 10 ; let result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) document . write ( " " ) ; else document . write ( " " , result ) ;
Number of buildings facing the sun | Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver program
function countBuildings ( arr , n ) { let count = 1 ; let curr_max = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > curr_max arr [ i ] == curr_max ) { count ++ ; curr_max = arr [ i ] ; } } return count ; } let arr = [ 7 , 4 , 8 , 2 , 9 ] ; let n = arr . length ; document . write ( countBuildings ( arr , n ) ) ;
Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays
function findExtra ( arr1 , arr2 , n ) { for ( let i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } let arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ] ; let arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ] ; let n = arr2 . length ; document . write ( findExtra ( arr1 , arr2 , n ) ) ;
Find index of an extra element present in one sorted array | Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left our search is complete . ; Driver program ; Solve is passed both arrays
function findExtra ( arr1 , arr2 , n ) { let index = n ; let left = 0 , right = n - 1 ; while ( left <= right ) { let mid = Math . floor ( ( left + right ) / 2 ) ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } let arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ] ; let arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ] ; let n = arr2 . length ; document . write ( findExtra ( arr1 , arr2 , n ) ) ;
Make all array elements equal with minimum cost | Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver Code
function computeCost ( arr , N , X ) { let cost = 0 ; for ( let i = 0 ; i < N ; i ++ ) cost += Math . abs ( arr [ i ] - X ) ; return cost ; } function minCostToMakeElementEqual ( arr , N ) { let low , high ; low = high = arr [ 0 ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( low > arr [ i ] ) low = arr [ i ] ; if ( high < arr [ i ] ) high = arr [ i ] ; } while ( ( high - low ) > 2 ) { let mid1 = low + ( high - low ) / 3 ; let mid2 = high - ( high - low ) / 3 ; let cost1 = computeCost ( arr , N , mid1 ) ; let cost2 = computeCost ( arr , N , mid2 ) ; if ( cost1 < cost2 ) high = mid2 ; else low = mid1 ; } return Math . round ( computeCost ( arr , N , ( low + high ) / 2 ) ) ; } let arr = [ 1 , 100 , 101 ] ; let N = arr . length ; document . write ( minCostToMakeElementEqual ( arr , N ) ) ;
Minimum number of jumps required to sort numbers placed on a number line | Function to find the minimum number of jumps required to sort the array ; Base Case ; Store the required result ; Stores the current position of elements and their respective maximum jump ; Used to check if a position is already taken by another element ; Stores the sorted array a [ ] ; Traverse the array w [ ] & update positions jumps array a [ ] ; Sort the array a [ ] ; Traverse the array a [ ] over the range [ 1 , N - 1 ] ; Store the index of current element and its just smaller element in array w [ ] ; Iterate until current element position is at most its just smaller element position ; Update the position of the current element ; Print the result ; Driver Code
function minJumps ( w , l , n ) { if ( n == 1 ) { document . write ( 0 ) ; return ; } var ans = 0 ; var i ; var pos = new Map ( ) ; var jump = new Map ( ) ; var filled = new Map ( ) ; var a = new Array ( n ) ; for ( i = 0 ; i < n ; i ++ ) { pos . set ( w [ i ] , i ) ; filled . set ( i , true ) ; jump . set ( w [ i ] , l [ i ] ) ; a [ i ] = w [ i ] ; } a = a . sort ( function ( p , q ) { return p - q ; } ) ; for ( curr = 1 ; curr < n ; curr ++ ) { var currElementPos = pos . get ( a [ curr ] ) ; var prevElementPos = pos . get ( a [ curr - 1 ] ) ; if ( currElementPos > prevElementPos ) continue ; while ( currElementPos <= prevElementPos filled [ currElementPos ] ) { currElementPos += jump . get ( a [ curr ] ) ; ans += 1 ; } pos . set ( a [ curr ] , currElementPos ) ; filled . set ( currElementPos , true ) ; } document . write ( ans ) ; } var W = [ 2 , 1 , 4 , 3 ] ; var L = [ 4 , 1 , 2 , 4 ] ; var N = W . length ; minJumps ( W , L , N ) ;
Make an array strictly increasing by repeatedly subtracting and adding arr [ i | Function to check if an array can be made strictly increasing ; Traverse the given array arr ; Update the value of p , arr [ i ] , and arr [ i - 1 ] ; Traverse the given array ; Check if the array arr is strictly increasing or not ; Otherwise , array is increasing order , prvar Yes ; Driver Code
function check ( arr , n ) { for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= ( i - 1 ) ) { var p = arr [ i - 1 ] - ( i - 1 ) ; arr [ i ] += p ; arr [ i - 1 ] -= p ; } } for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= arr [ i - 1 ] ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var arr = [ 1 , 5 , 2 , 7 , 6 ] ; var N = arr . length ; check ( arr , N ) ;
Rearrange array to make decimal equivalents of reversed binary representations of array elements sorted | Function to reverse the bits of a number ; Stores the reversed number ; Divide rev by 2 ; If the value of N is odd ; Update the value of N ; Return the final value of rev ; Function for rearranging the array element according to the given rules ; Stores the new array elements ; Function for rearranging the array ; Stores the new array ; Function to sort the array by reversing binary representation ; Creating a new array ; Sort the array with the key ; Get arr from newArr ; Print the sorted array ; Driver Code
function keyFunc ( n ) { let rev = 0 ; while ( n > 0 ) { rev = rev << 1 ; if ( n & 1 == 1 ) rev = rev ^ 1 ; n = n >> 1 ; } return rev ; } function getNew ( arr ) { let ans = [ ] ; for ( let i of arr ) ans . push ( [ keyFunc ( i ) , i ] ) ; return ans ; } function getArr ( arr ) { let ans = [ ] ; for ( let i of arr ) ans . push ( i [ 1 ] ) ; return ans ; } function sortArray ( arr ) { let newArr = getNew ( arr ) ; newArr . sort ( ) ; arr = getArr ( newArr ) ; let n = arr . length ; document . write ( " " ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( arr [ n - 1 ] + " " ) ; } let arr = [ 43 , 52 , 61 , 41 ] ; sortArray ( arr ) ;