codeparrot/xlcost-text-to-code huggingface dataset:
Count number of triplets in an array having sum in the range [ a , b ] | Function to find count of triplets having sum less than or equal to val . ; sort the input array . ; Initialize result ; to store sum ; Fix the first element ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept . ; If sum of current triplet is greater , then to reduce it decrease k . ; If sum is less than or equal to given value , then add possible triplets ( k - j ) to result . ; Function to return count of triplets having sum in range [ a , b ] . ; to store count of triplets . ; Find count of triplets having sum less than or equal to b and subtract count of triplets having sum less than or equal to a - 1. ; Driver Code
function countTripletsLessThan ( arr , n , val ) { arr . sort ( ) ; var ans = 0 ; var j , k ; var sum ; for ( var i = 0 ; i < n - 2 ; i ++ ) { j = i + 1 ; k = n - 1 ; while ( j != k ) { sum = arr [ i ] + arr [ j ] + arr [ k ] ; if ( sum > val ) k -- ; else { ans += k - j ; j ++ ; } } } return ans ; } function countTriplets ( arr , n , a , b ) { var res ; res = countTripletsLessThan ( arr , n , b ) - countTripletsLessThan ( arr , n , a - 1 ) ; return res ; } var arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ] ; var n = arr . length ; var a = 8 , b = 16 ; document . write ( " " + countTriplets ( arr , n , a , b ) ) ;
Multiset Equivalence Problem | JavaScript program to check if two given multisets are equivalent ; Create two unordered maps m1 and m2 and insert values of both vectors . ; Now we check if both unordered_maps are same of not . ; Driver code
function areSame ( a , b ) { if ( a . length != b . length ) return false ; var m1 , m2 ; m1 = { } ; m2 = { } ; for ( var i = 0 ; i < a . length ; i ++ ) { if ( m1 . hasOwnProperty ( a [ i ] ) ) { m1 [ a [ i ] ] = m1 [ a [ i ] ] + 1 ; } else { m1 [ a [ i ] ] = 1 ; } if ( m2 . hasOwnProperty ( b [ i ] ) ) { m2 [ b [ i ] ] = m2 [ b [ i ] ] + 1 ; } else { m2 [ b [ i ] ] = 1 ; } } for ( const [ key , value ] of Object . entries ( m1 ) ) { if ( ! m2 . hasOwnProperty ( key ) m2 [ key ] != value ) return false ; } return true ; } var a = [ 7 , 7 , 5 ] ; var b = [ 7 , 7 , 5 ] ; if ( areSame ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of Areas of Rectangles possible for an array | Function to find area of rectangles ; sorting the array in descending order ; store the final sum of all the rectangles area possible ; temporary variable to store the length of rectangle ; Selecting the length of rectangle so that difference between any two number is 1 only . Here length is selected so flag is set ; flag is set means we have got length of rectangle ; length is set to a [ i + 1 ] so that if a [ i ] a [ i + 1 ] is less than by 1 then also we have the correct choice for length ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Selecting the width of rectangle so that difference between any two number is 1 only . Here width is selected so now flag is again unset for next rectangle ; area is calculated for rectangle ; flag is set false for another rectangle which we can get from elements in array ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Driver Code
function MaxTotalRectangleArea ( a , n ) { a . sort ( ) ; a . reverse ( ) ; let sum = 0 ; let flag = false ; let len ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ( ! flag ) ) { flag = true ; len = a [ i + 1 ] ; i ++ ; } else if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ( flag ) ) { sum = sum + a [ i + 1 ] * len ; flag = false ; i ++ ; } } return sum ; } let a = [ 10 , 10 , 10 , 10 , 11 , 10 , 11 , 10 , 9 , 9 , 8 , 8 ] ; let n = a . length ; document . write ( MaxTotalRectangleArea ( a , n ) ) ;
Insertion sort to sort even and odd positioned elements in different orders | Function to calculate the given problem . ; checking for odd positioned . ; Inserting even positioned elements in ascending order . ; sorting the even positioned . ; Inserting odd positioned elements in descending order . ; A utility function to print an array of size n ; Driver code
function evenOddInsertionSort ( arr , n ) { for ( let i = 2 ; i < n ; i ++ ) { let j = i - 2 ; let temp = arr [ i ] ; if ( ( ( i + 1 ) & 1 ) == 1 ) { while ( j >= 0 && temp >= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } else { while ( j >= 0 && temp <= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 ] ; let n = arr . length ; evenOddInsertionSort ( arr , n ) ; printArray ( arr , n ) ;
Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code
function print ( a , n ) { for ( let i = 0 ; i <= n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; } function sort ( a , n ) { for ( let i = n ; i >= 0 ; i -- ) for ( let j = n ; j > n - i ; j -- ) if ( a [ j ] > a [ j - 1 ] ) { let tempswap = a [ j ] ; a [ j ] = a [ j - 1 ] ; a [ j - 1 ] = tempswap ; } print ( a , n ) ; } let n = 6 ; let a = [ ] ; a . push ( 2 ) ; a . push ( 4 ) ; a . push ( 3 ) ; a . push ( 2 ) ; a . push ( 4 ) ; a . push ( 5 ) ; a . push ( 3 ) ; sort ( a , n ) ;
Subtraction in the Array | Function to perform the given operation on arr [ ] ; Skip elements which are 0 ; Pick smallest non - zero element ; If all the elements of arr [ ] are 0 ; Driver code
function operations ( arr , n , k ) { arr . sort ( ) ; let i = 0 , sum = 0 ; while ( k -- > 0 ) { while ( i < n && arr [ i ] - sum == 0 ) i ++ ; if ( i < n && arr [ i ] - sum > 0 ) { document . write ( arr [ i ] - sum + " " ) ; sum = arr [ i ] ; } else document . write ( " " ) ; } } let k = 5 ; let arr = [ 3 , 6 , 4 , 2 ] ; let n = arr . length ; operations ( arr , n , k ) ;
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | JavaScript program to sort an array of 0 s 1 s and 2 s . ; Variables to maintain the count of 0 ' s , ▁ ▁ 1' s and 2 's in the array ; Putting the 0 's in the  array in starting. ; Putting the 1 ' s ▁ in ▁ the ▁ ▁ array ▁ after ▁ the ▁ 0' s . ; Putting the 2 ' s ▁ in ▁ the ▁ ▁ array ▁ after ▁ the ▁ 1' s ; Prints the array ; Driver code
function sort012 ( arr , n ) { let count0 = 0 , count1 = 0 ; let count2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; if ( arr [ i ] == 1 ) count1 ++ ; if ( arr [ i ] == 2 ) count2 ++ ; } for ( let i = 0 ; i < count0 ; i ++ ) arr [ i ] = 0 ; for ( let i = count0 ; i < ( count0 + count1 ) ; i ++ ) arr [ i ] = 1 ; for ( let i = ( count0 + count1 ) ; i < n ; i ++ ) arr [ i ] = 2 ; printArray ( arr , n ) ; } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( ) ; } let arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] ; let n = 12 ; sort012 ( arr , n ) ;
Alternate Lower Upper String Sort | JavaScript program for unusual string sorting ; Function for alternate sorting of string ; Count occurrences of individual lower case and upper case characters ; Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant . ; Driver function
const MAX = 26 ; function alternateSort ( s1 ) { var n = s1 . length ; var l = 0 , j = 0 , k = 0 ; var s = s1 . split ( " " ) ; var lCount = new Array ( MAX ) . fill ( 0 ) ; var uCount = new Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] === s [ i ] . toUpperCase ( ) ) uCount [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; else lCount [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } while ( k < n ) { while ( l < MAX && uCount [ l ] === 0 ) l ++ ; if ( l < MAX ) { s [ k ++ ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + l ) ; uCount [ l ] -- ; } while ( j < MAX && lCount [ j ] === 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + j ) ; lCount [ j ] -- ; } } return s . join ( " " ) ; } var str = " " ; document . write ( alternateSort ( str ) ) ;
Sum of Manhattan distances between all pairs of points | Return the sum of distance between all the pair of points . ; for each point , finding distance to rest of the point ; Driver code
function distancesum ( x , y , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) sum += ( Math . abs ( x [ i ] - x [ j ] ) + Math . abs ( y [ i ] - y [ j ] ) ) ; return sum ; } let x = [ - 1 , 1 , 3 , 2 ] ; let y = [ 5 , 6 , 5 , 3 ] ; let n = x . length ; document . write ( distancesum ( x , y , n ) ) ;
Sum of Manhattan distances between all pairs of points | Return the sum of distance of one axis . ; sorting the array . ; for each point , finding the distance . ; Driver code
function distancesum ( arr , n ) { arr . sort ( ) ; let res = 0 , sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { res += ( arr [ i ] * i - sum ) ; sum += arr [ i ] ; } return res ; } function totaldistancesum ( x , y , n ) { return distancesum ( x , n ) + distancesum ( y , n ) ; } let x = [ - 1 , 1 , 3 , 2 ] ; let y = [ 5 , 6 , 5 , 3 ] ; let n = x . length ; document . write ( totaldistancesum ( x , y , n ) ) ;
Sort an array with swapping only with a special element is allowed | n is total number of elements . index is index of 999 or space . k is number of elements yet to be sorted . ; Print the sorted array when loop reaches the base case ; Else if k > 0 and space is at 0 th index swap each number with space and store index at second last index ; If space is neither start of array nor last element of array and element before it greater than / the element next to space ; First swap space and element next to space in case of { 3 , 999 , 2 } make it { 3 , 2 , 999 } ; Than swap space and greater element convert { 3 , 2 , 999 } to { 999 , 2 , 3 } ; Wrapper over sortRec . ; Find index of space ( or 999 ) ; Invalid input ; Driver Code
function sortRec ( arr , index , k , n ) { if ( k == 0 ) { for ( let i = 1 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( 999 ) ; return ; } else if ( k > 0 && index == 0 ) { index = n - 2 ; for ( let i = 1 ; i <= index ; i ++ ) { arr [ i - 1 ] = arr [ i ] ; } arr [ index ] = 999 ; } if ( index - 1 >= 0 && index + 1 < n && arr [ index - 1 ] > arr [ index + 1 ] ) { swap ( arr , index , index + 1 ) ; swap ( arr , index - 1 , index + 1 ) ; } else swap ( arr , index , index - 1 ) ; sortRec ( arr , index - 1 , k - 1 , n ) ; } function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } function sort ( arr , n ) { let index = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 999 ) { index = i ; break ; } } if ( index == - 1 ) return ; sortRec ( arr , index , n , n ) ; } let arr = [ 3 , 2 , 999 , 1 ] ; let n = arr . length ; sort ( arr , n ) ;
Find array with k number of merge sort calls | JavaScript program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code
var k ; function unsort ( l , r , a ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; var mid = parseInt ( ( l + r ) / 2 ) ; [ a [ mid - 1 ] , a [ mid ] ] = [ a [ mid ] , a [ mid - 1 ] ] ; unsort ( l , mid , a ) ; unsort ( mid , r , a ) ; } function arrayWithKCalls ( n ) { if ( k % 2 == 0 ) { document . write ( " " ) ; return ; } var a = Array ( n + 1 ) ; a [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a ) ; for ( var i = 0 ; i < n ; ++ i ) document . write ( a [ i ] + ' ' ) ; } var n = 10 k = 17 ; arrayWithKCalls ( n ) ;
Program to sort string in descending order | Javascript program to sort a string of characters in descending order ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , ; so for location of character in count array we wil do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver code
let MAX_CHAR = 26 ; function sortString ( str ) { let charCount = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < charCount . length ; i ++ ) { charCount [ i ] = 0 ; } for ( let i = 0 ; i < str . length ; i ++ ) { charCount [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { for ( let j = 0 ; j < charCount [ i ] ; j ++ ) { document . write ( String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ) ; } } } let s = " " ; sortString ( s ) ;
Median after K additional integers | Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; driver program
function printMedian ( arr , n , K ) { arr . sort ( ) ; document . write ( arr [ Math . floor ( ( n + K ) / 2 ) ] ) ; } let arr = [ 5 , 3 , 2 , 8 ] ; let k = 3 ; let n = arr . length ; printMedian ( arr , n , k ) ;
A sorting algorithm that slightly improves on selection sort | Javascript program to implement min max selection sort . ; Shifting the min . ; Shifting the max . The equal condition happens if we shifted the max to arr [ min_i ] in the previous swap . ; Driver code
function swap ( arr , xp , yp ) { var temp = arr [ xp ] ; arr [ xp ] = arr [ yp ] ; arr [ yp ] = temp ; } function minMaxSelectionSort ( arr , n ) { for ( var i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { var min = arr [ i ] ; var max = arr [ i ] ; var min_i = i ; var max_i = i ; for ( var k = i ; k <= j ; k ++ ) { if ( arr [ k ] > max ) { max = arr [ k ] ; max_i = k ; } else if ( arr [ k ] < min ) { min = arr [ k ] ; min_i = k ; } } swap ( arr , i , min_i ) ; if ( arr [ min_i ] == max ) swap ( arr , j , min_i ) ; else swap ( arr , j , max_i ) ; } } var arr = [ 23 , 78 , 45 , 8 , 32 , 56 , 1 ] ; var n = 7 ; minMaxSelectionSort ( arr , n ) ; document . write ( " " ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ;
Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Find maximum element of both array ; Increment by one to avoid collision of 0 and maximum element of array in modulo operation ; Recover back original element to compare ; Update element by adding multiplication with new number ; Update element by adding multiplication with new number ; Process those elements which are left in array a ; Process those elements which are left in array b ; Finally update elements by dividing with maximum element ; Finally update elements by dividing with maximum element ; Driver Code ; Length of a ; Length of b ; Function Call
function mergeArray ( a , b , n , m ) { let mx = 0 ; for ( let i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , a [ i ] ) ; } for ( let i = 0 ; i < m ; i ++ ) { mx = Math . max ( mx , b [ i ] ) ; } mx ++ ; let i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { let e1 = a [ i ] % mx ; let e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { let el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { let el = b [ j ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( let i = 0 ; i < n ; i ++ ) a [ i ] = parseInt ( a [ i ] / mx ) ; for ( let i = 0 ; i < m ; i ++ ) b [ i ] = parseInt ( b [ i ] / mx ) ; return ; } var a = [ 3 , 5 , 6 , 8 , 12 ] ; var b = [ 1 , 4 , 9 , 13 ] ; var n = a . length ; var m = b . length ; mergeArray ( a , b , n , m ) ; document . write ( " " ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( ' ' ) document . write ( " " ) ; for ( let i = 0 ; i < m ; i ++ ) document . write ( b [ i ] + " " ) ;
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | Javascript program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Driver code
function arrange ( arr , n , x ) { for ( let i = 1 ; i < n ; i ++ ) { let diff = Math . abs ( arr [ i ] - x ) ; let j = i - 1 ; if ( Math . abs ( arr [ j ] - x ) > diff ) { let temp = arr [ i ] ; while ( j >= 0 && Math . abs ( arr [ j ] - x ) > diff ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } } function print ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 10 , 5 , 3 , 9 , 2 ] ; let n = arr . length ; let x = 7 ; arrange ( arr , n , x ) ; print ( arr , n ) ;
Check if a grid can become row | v [ ] is vector of strings . len is length of strings in every row . ; Driver code ; let len = 5 ; Length of strings
function check ( v , len ) { let n = v . length ; let tempArray ; for ( let i = 0 ; i < n ; i ++ ) { tempArray = v [ i ] . split ( " " ) ; ( tempArray ) . sort ( ) ; v [ i ] = ( tempArray ) . join ( " " ) ; } for ( let i = 0 ; i < len - 1 ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) if ( v [ i ] [ j ] > v [ i + 1 ] [ j ] ) return false ; return true ; } let v = [ " " , " " , " " , " " , " " ] ; if ( check ( v , len ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sort first half in ascending and second half in descending order | 1 | function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; Driver code
function printOrder ( arr , n ) { arr . sort ( ) ; for ( i = 0 ; i < parseInt ( n / 2 ) ; i ++ ) { document . write ( arr [ i ] + " " ) ; } for ( j = n - 1 ; j >= parseInt ( n / 2 ) ; j -- ) { document . write ( arr [ j ] + " " ) ; } } var arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 ] ; var n = arr . length ; printOrder ( arr , n ) ;
Find the Sub | Function to find the subarray ; Pick a starting point ; Consider current starting point as a subarray and update minimum sum and subarray indexes ; Try all subarrays starting with i ; update minimum sum and subarray indexes ; Return starting and ending indexes ; Drivers code
function findSubArray ( arr , n ) { let start , end , min_sum = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { let curr_sum = arr [ i ] ; if ( min_sum > Math . abs ( curr_sum ) ) { min_sum = Math . abs ( curr_sum ) ; start = i ; end = i ; } for ( let j = i + 1 ; j < n ; j ++ ) { curr_sum = curr_sum + arr [ j ] ; if ( min_sum > Math . abs ( curr_sum ) ) { min_sum = Math . abs ( curr_sum ) ; start = i ; end = j ; } } } let p = [ start , end ] ; return p ; } let arr = [ 2 , - 5 , 4 , - 6 , - 3 ] ; let n = arr . length ; let point = findSubArray ( arr , n ) ; document . write ( " " ) ; document . write ( point [ 0 ] + " " + point [ 1 ] ) ;
Find the minimum and maximum amount to buy all N candies | Function to find the minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver code ; Function call
function findMinimum ( arr , n , k ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { res += arr [ i ] ; n = n - k ; } return res ; } function findMaximum ( arr , n , k ) { let res = 0 , index = 0 ; for ( let i = n - 1 ; i >= index ; i -- ) { res += arr [ i ] ; index += k ; } return res ; } let arr = [ 3 , 2 , 1 , 4 ] ; let n = arr . length ; let k = 2 ; arr . sort ( function ( a , b ) { return a - b ; } ) ; document . write ( findMinimum ( arr , n , k ) + " " + findMaximum ( arr , n , k ) ) ;
Print array of strings in sorted order without copying one string into another | function to print strings in sorted order ; Initially the index of the strings are assigned to the ' index [ ] ' ; selection sort technique is applied ; with the help of ' index [ ] ' strings are being compared ; index of the smallest string is placed at the ith index of ' index [ ] ' ; printing strings in sorted order ; Driver program to test above
function printInSortedOrder ( arr , n ) { let index = new Array ( n ) ; let i , j , min ; for ( i = 0 ; i < n ; i ++ ) { index [ i ] = i ; } for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] > ( arr [ index [ j ] ] ) ) { min = j ; } } if ( min != i ) { let temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ index [ i ] ] + " " ) ; } } let arr = [ " " , " " , " " , " " ] ; let n = 4 ; printInSortedOrder ( arr , n ) ;
Find maximum height pyramid from the given array of objects | Returns maximum number of pyramidcal levels n boxes of given widths . ; Sort objects in increasing order of widths ; Total width of previous level and total number of objects in previous level ; Number of object in current level . ; Width of current level . ; Picking the object . So increase current width and number of object . ; If current width and number of object are greater than previous . ; Update previous width , number of object on previous level . ; Reset width of current level , number of object on current level . ; Increment number of level . ; Driver Code
function maxLevel ( boxes , n ) { boxes . sort ( ) ; let prev_width = boxes [ 0 ] ; let prev_count = 1 ; let curr_count = 0 ; let curr_width = 0 ; for ( let i = 1 ; i < n ; i ++ ) { curr_width += boxes [ i ] ; curr_count += 1 ; if ( curr_width > prev_width && curr_count > prev_count ) { prev_width = curr_width ; prev_count = curr_count ; curr_count = 0 ; curr_width = 0 ; ans ++ ; } } return ans ; } let boxes = [ 10 , 20 , 30 , 50 , 60 , 70 ] ; let n = boxes . length ; document . write ( maxLevel ( boxes , n ) ) ;