codeparrot/xlcost-text-to-code huggingface dataset:
Sort the given Matrix | Memory Efficient Approach | Javascript implementation to sort the given matrix in strict order ; Function to sort the matrix ; Number of elements in matrix ; Loop to sort the matrix using Bubble Sort ; Condition to check if the Adjacent elements ; Swap if previous value is greater ; Loop to print the matrix ; Driver Code ; Function call to sort ; Function call to print matrix
let N = 3 ; let M = 3 ; function sortMat ( data , row , col ) { let size = row * col ; for ( let i = 0 ; i < size ; i ++ ) { for ( let j = 0 ; j < size - 1 ; j ++ ) { if ( data [ Math . floor ( j / col ) ] [ j % col ] > data [ Math . floor ( ( j + 1 ) / col ) ] [ ( j + 1 ) % col ] ) { let temp = data [ Math . floor ( j / col ) ] [ j % col ] ; data [ Math . floor ( j / col ) ] [ j % col ] = data [ Math . floor ( ( j + 1 ) / col ) ] [ ( j + 1 ) % col ] ; data [ Math . floor ( ( j + 1 ) / col ) ] [ ( j + 1 ) % col ] = temp ; } } } } function printMat ( mat , row , col ) { for ( let i = 0 ; i < row ; i ++ ) { for ( let j = 0 ; j < col ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 5 , 4 , 7 ] , [ 1 , 3 , 8 ] , [ 2 , 9 , 6 ] ] ; let row = N ; let col = M ; sortMat ( mat , row , col ) ; printMat ( mat , row , col ) ;
Partition the array into two odd length groups with minimized absolute difference between their median | Function to find minimise the median between partition array ; Sort the given array arr [ ] ; Return the difference of two middle element of the arr [ ] ; Driver Code ; Size of arr [ ] ; Function that returns the minimum the absolute difference between median of partition array
function minimiseMedian ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; return Math . abs ( arr [ n / 2 ] - arr [ ( n / 2 ) - 1 ] ) ; } let arr = [ 15 , 25 , 35 , 50 ] ; let n = arr . length ; document . write ( minimiseMedian ( arr , n ) ) ;
Maximum number of overlapping Intervals | Function that print maximum overlap among ranges ; Variable to store the maximum count ; Storing the x and y coordinates in data vector ; Pushing the x coordinate ; Pushing the y coordinate ; Sorting of ranges ; Traverse the data vector to count number of overlaps ; If x occur it means a new range is added so we increase count ; If y occur it means a range is ended so we decrease count ; Updating the value of ans after every traversal ; Printing the maximum value ; Driver code
function overlap ( v ) { var ans = 0 ; var count = 0 ; var data = [ ] ; for ( var i = 0 ; i < v . length ; i ++ ) { data . push ( [ v [ i ] [ 0 ] , ' ' ] ) ; data . push ( [ v [ i ] [ 1 ] , ' ' ] ) ; } data . sort ( ) ; for ( var i = 0 ; i < data . length ; i ++ ) { if ( data [ i ] [ 1 ] == ' ' ) count ++ ; if ( data [ i ] [ 1 ] == ' ' ) count -- ; ans = Math . max ( ans , count ) ; } document . write ( ans + " " ) ; } var v = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 3 , 6 ] ] ; overlap ( v ) ;
Making three numbers equal with the given operations | Function that returns true if a , b and c can be made equal with the given operations ; Sort the three numbers ; Find the sum of difference of the 3 rd and 2 nd element and the 3 rd and 1 st element ; Subtract the difference from k ; Check the required condition ; Driver code
function canBeEqual ( a , b , c , k ) { var arr = Array ( 3 ) ; arr [ 0 ] = a ; arr [ 1 ] = b ; arr [ 2 ] = c ; arr . sort ( ( a , b ) => a - b ) ; var diff = 2 * arr [ 2 ] - arr [ 1 ] - arr [ 0 ] ; k = k - diff ; if ( k < 0 k % 3 != 0 ) return false ; return true ; } var a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ; if ( canBeEqual ( a1 , b1 , c1 , k1 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Arrange numbers to form a valid sequence | Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code
function orgazineInOrder ( vec , op , n ) { let result = [ n ] ; vec . sort ( ) ; let i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } let vec = [ 8 , 2 , 7 , 1 , 5 , 9 ] ; let op = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let result = orgazineInOrder ( vec , op , vec . length ) ; for ( let i = 0 ; i < result . length ; i ++ ) { document . write ( result [ i ] + " " ) ; }
Find the distance between two person after reconstruction of queue | Function to find the correct order and then return the distance between the two persons ; Make pair of both height & infront and insert to vector ; Sort the vector in ascending order ; Find the correct place for every person ; Insert into position vector according to infront value ; Driver code
function getDistance ( arr , n , a , b ) { let vp = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { vp . push ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ) ; } vp . sort ( function ( c , d ) { return c [ 0 ] - d [ 0 ] ; } ) ; let pos = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { pos [ i ] = 0 ; } for ( let i = 0 ; i < vp . length ; i ++ ) { let height = vp [ i ] [ 0 ] ; let k = vp [ i ] [ 1 ] ; pos [ k ] = height } let first = - 1 , second = - 1 ; for ( let i = 0 ; i < pos . length ; i ++ ) { if ( pos [ i ] == a ) first = i ; if ( pos [ i ] == b ) second = i ; } return Math . abs ( first - second ) ; } let arr = [ [ 5 , 0 ] , [ 3 , 0 ] , [ 2 , 0 ] , [ 6 , 4 ] , [ 1 , 0 ] , [ 4 , 3 ] ] ; let n = arr . length ; let a = 6 , b = 5 ; document . write ( getDistance ( arr , n , a , b ) ) ;
Minimize the sum of differences of consecutive elements after removing exactly K elements | function to find minimum sum ; variable to store final answer and initialising it with the values when 0 elements is removed from the left and K from the right . ; loop to simulate removal of elements ; removing i elements from the left and and K - i elements from the right and updating the answer correspondingly ; returning final answer ; input values ; callin the required function ;
function findSum ( arr , n , k ) { var ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( var i = 1 ; i <= k ; i ++ ) { ans = Math . min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } var arr = [ 1 , 2 , 100 , 120 , 140 ] ; var k = 2 ; var n = arr . length ; document . write ( findSum ( arr , n , k ) ) ;
Minimum elements to be removed from the ends to make the array sorted | Function to return the minimum number of elements to be removed from the ends of the array to make it sorted ; To store the final answer ; Two pointer loop ; While the array is increasing increment j ; Updating the ans ; Updating the left pointer ; Returning the final answer ; Driver code
function findMin ( arr , n ) { let ans = 1 ; for ( let i = 0 ; i < n ; i ++ ) { let j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; ans = Math . max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } let arr = [ 3 , 2 , 1 ] ; let n = arr . length ; document . write ( findMin ( arr , n ) ) ;
Number of subsequences of maximum length K containing no repeated elements | Returns number of subsequences of maximum length k and contains no repeated element ; Sort the array a [ ] ; Store the frequencies of all the distinct element in the vector arr ; count is the the number of such subsequences ; Create a 2 - d array dp [ n + 1 ] [ m + 1 ] to store the intermediate result ; Initialize the first row to 1 ; Update the dp [ ] [ ] array based on the recurrence relation ; Return the number of subsequences ; Driver code
function countSubSeq ( a , n , k ) { a . sort ( ) ; var arr = [ ] ; for ( var i = 0 ; i < n ; ) { var count = 1 , x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . push ( count ) ; } var m = arr . length ; n = Math . min ( m , k ) ; var count = 1 ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( m + 1 ) ) ; for ( var i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = m ; j >= 0 ; j -- ) { if ( j > m - i ) dp [ i ] [ j ] = 0 ; else { dp [ i ] [ j ] = dp [ i ] [ j + 1 ] + arr [ j ] * dp [ i - 1 ] [ j + 1 ] ; } } count = count + dp [ i ] [ 0 ] ; } return count ; } var a = [ 2 , 2 , 3 , 3 , 5 ] ; var n = a . length ; var k = 3 ; document . write ( countSubSeq ( a , n , k ) ) ;
Check whether the string S1 can be made equal to S2 with the given operation | Function to return the string formed by the odd indexed characters of s ; Function to return the string formed by the even indexed characters of s ; Function that returns true if s1 can be made equal to s2 with the given operation ; Get the string formed by the even indexed characters of s1 ; Get the string formed by the even indexed characters of s2 ; Get the string formed by the odd indexed characters of s1 ; Get the string formed by the odd indexed characters of s2 ; Sorting all the lists ; If the strings can be made equal ; Driver Code
function partOdd ( s ) { var st = " " ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( i % 2 !== 0 ) st += s [ i ] ; } return st ; } function partEven ( str ) { var s = " " ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( i % 2 === 0 ) s += str [ i ] ; } return s ; } function canBeMadeEqual ( s1 , s2 ) { var even1 = partEven ( s1 ) . split ( " " ) ; var even2 = partEven ( s2 ) . split ( " " ) ; var odd1 = partOdd ( s1 ) . split ( " " ) ; var odd2 = partOdd ( s2 ) . split ( " " ) ; even1 . sort ( ) ; even2 . sort ( ) ; odd1 . sort ( ) ; odd2 . sort ( ) ; var even_s1 = even1 . join ( " " ) ; var even_s2 = even2 . join ( " " ) ; var odd_s1 = odd1 . join ( " " ) ; var odd_s2 = odd2 . join ( " " ) ; if ( even_s1 === even_s2 && odd_s1 === odd_s2 ) return true ; return false ; } var s1 = " " ; var s2 = " " ; if ( canBeMadeEqual ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sort the given stack elements based on their modulo with K | Function to sort the stack using another stack based on the values of elements modulo k ; Pop out the first element ; While temporary stack is not empty ; The top of the stack modulo k is greater than ( temp & k ) or if they are equal then compare the values ; Pop from temporary stack and push it to the input stack ; Push temp in temporary of stack ; Push all the elements in the original stack to get the ascending order ; Print the sorted elements ; Driver code
function sortStack ( input , k ) { let tmpStack = [ ] ; while ( input . length != 0 ) { let tmp = input . pop ( ) ; while ( tmpStack . length != 0 ) { let tmpStackMod = tmpStack [ tmpStack . length - 1 ] % k ; let tmpMod = tmp % k ; if ( ( tmpStackMod > tmpMod ) || ( tmpStackMod == tmpMod && tmpStack [ tmpStack . length - 1 ] > tmp ) ) { input . push ( tmpStack [ tmpStack . length - 1 ] ) ; tmpStack . pop ( ) ; } else break ; } tmpStack . push ( tmp ) ; } while ( tmpStack . length != 0 ) { input . push ( tmpStack [ tmpStack . length - 1 ] ) ; tmpStack . pop ( ) ; } while ( input . length != 0 ) { document . write ( input . pop ( ) + " " ) ; } } let input = [ ] ; input . push ( 10 ) ; input . push ( 3 ) ; input . push ( 2 ) ; input . push ( 6 ) ; input . push ( 12 ) ; let k = 4 ; sortStack ( input , k ) ;
Longest sub | Function to return the length of the largest subsequence with non - negative sum ; To store the current sum ; Sort the input array in non - increasing order ; Traverse through the array ; Add the current element to the sum ; Condition when c_sum falls below zero ; Complete array has a non - negative sum ; Driver code
function maxLen ( arr , n ) { var c_sum = 0 ; arr . sort ( ( a , b ) => b - a ) for ( var i = 0 ; i < n ; i ++ ) { c_sum += arr [ i ] ; if ( c_sum < 0 ) return i ; } return n ; } var arr = [ 3 , 5 , - 6 ] ; var n = arr . length ; document . write ( maxLen ( arr , n ) ) ;
Find Partition Line such that sum of values on left and right is equal | Javascript implementation of the approach ; Function that returns true if the required line exists ; To handle negative values from x [ ] ; Update arr [ ] such that arr [ i ] contains the sum of all v [ j ] such that x [ j ] = i for all valid values of j ; Update arr [ i ] such that arr [ i ] contains the sum of the subarray arr [ 0. . . i ] from the original array ; If all the points add to 0 then the line can be drawn anywhere ; If the line is drawn touching the leftmost possible points ; If the line is drawn just before the current point ; If the line is drawn touching the current point ; If the line is drawn just after the current point ; If the line is drawn touching the rightmost possible points ; Driver code
const MAX = 1000 ; function lineExists ( x , y , v , n ) { let size = ( 2 * MAX ) + 1 ; let arr = new Array ( size ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ x [ i ] + MAX ] += v [ i ] ; } for ( let i = 1 ; i < size ; i ++ ) arr [ i ] += arr [ i - 1 ] ; if ( arr [ size - 1 ] == 0 ) return true ; if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) return true ; for ( let i = 1 ; i < size - 1 ; i ++ ) { if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) return true ; if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) return true ; if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) return true ; } if ( arr [ size - 2 ] == 0 ) return true ; return false ; } let x = [ - 3 , 5 , 8 ] ; let y = [ 8 , 7 , 9 ] let v = [ 8 , 2 , 10 ] ; let n = x . length ; if ( lineExists ( x , y , v , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum sum of minimums of pairs in an array | Function to return the maximum required sum of the pairs ; Sort the array ; To store the sum ; Start making pairs of every two consecutive elements as n is even ; Minimum element of the current pair ; Return the maximum possible sum ; Driver code
function maxSum ( a , n ) { a . sort ( ( a , b ) => a - b ) ; let sum = 0 ; for ( let i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } let arr = [ 1 , 3 , 2 , 1 , 4 , 5 ] ; let n = arr . length ; document . write ( maxSum ( arr , n ) ) ;
Sort elements by modulo with K | Utility function to prlet the contents of an array ; Function to sort the array elements based on their modulo with K ; Create K empty vectors ; Update the vectors such that v [ i ] will contain all the elements that give remainder as i when divided by k ; Sorting all the vectors separately ; Replacing the elements in arr [ ] with the required modulo sorted elements ; Add all the elements of the current vector to the array ; Prlet the sorted array ; Driver code
function prletArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function sortWithRemainder ( arr , n , k ) { let v = new Array ( ) ; for ( let i = 0 ; i < k ; i ++ ) { v . push ( [ ] ) } for ( let i = 0 ; i < n ; i ++ ) { v [ arr [ i ] % k ] . push ( arr [ i ] ) ; } for ( let i = 0 ; i < k ; i ++ ) v [ i ] . sort ( ( a , b ) => a - b ) ; console . log ( v ) let j = 0 ; for ( let i = 0 ; i < k ; i ++ ) { for ( let it of v [ i ] ) { arr [ j ] = it ; j ++ ; } } prletArr ( arr , n ) ; } let arr = [ 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 ] ; let n = arr . length ; let k = 4 ; sortWithRemainder ( arr , n , k ) ;
Concatenate the strings in an order which maximises the occurrence of subsequence " ab " | Custom sort function to sort the given string in the order which maximises the final score ; To store the count of occurrences of ' a ' and ' b ' in s1 ; Count the number of occurrences of ' a ' and ' b ' in s1 ; To store the count of occurrences of ' a ' and ' b ' in s2 ; Count the number of occurrences of ' a ' and ' b ' in s2 ; Since the number of subsequences ' ab ' is more when s1 is placed before s2 we return 1 so that s1 occurs before s2 in the combined string ; Function that return the concatenated string as S [ 0 ] + S [ 1 ] + ... + S [ N - 1 ] ; To store the concatenated string ; Concatenate every string in the order of appearance ; Return the concatenated string ; Function to return the maximum required score ; Sort the strings in the order which maximizes the score that we can get ; Get the concatenated string combined string ; Calculate the score of the combined string i . e . the count of occurrences of " ab " as subsequences ; Number of ' a ' has increased by one ; There are count_a number of ' a ' that can form subsequence ' ab ' with this ' b ' ; Driver code
function customSort ( s1 , s2 ) { let count_a1 = 0 , count_b1 = 0 ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == ' ' ) { count_a1 ++ ; } else { count_b1 ++ ; } } let count_a2 = 0 , count_b2 = 0 ; for ( let i = 0 ; i < s2 . length ; i ++ ) { if ( s2 [ i ] == ' ' ) { count_a2 ++ ; } else { count_b2 ++ ; } } if ( count_a1 * count_b2 > count_b1 * count_a2 ) { return true ; } else { return false ; } } function concatenateStrings ( S , N ) { let str = " " ; for ( let i = 0 ; i < N ; i ++ ) { str += S [ i ] ; } return str ; } function getMaxScore ( S , N ) { S . sort ( ) ; let combined_string = concatenateStrings ( S , N ) ; let final_score = 0 , count_a = 0 ; for ( let i = 0 ; i < combined_string . length ; i ++ ) { if ( combined_string [ i ] == ' ' ) { count_a ++ ; } else { final_score += count_a ; } } return final_score ; } let S = [ " " , " " , " " , " " ] ; let N = S . length ; document . write ( getMaxScore ( S , N ) - 10 ) ;
Minimum increment operations to make K elements equal | Function to return the minimum number of increment operations required to make any k elements of the array equal ; Sort the array in increasing order ; Calculate the number of operations needed to make 1 st k elements equal to the kth element i . e . the 1 st window ; Answer will be the minimum of all possible k sized windows ; Find the operations needed to make k elements equal to ith element ; Slide the window to the right and subtract increments spent on leftmost element of the previous window ; Add increments needed to make the 1 st k - 1 elements of this window equal to the kth element of the current window ; Driver code
function minOperations ( ar , k ) { ar . sort ( function ( a , b ) { return a - b } ) ; let opsNeeded = 0 ; for ( let i = 0 ; i < k ; i ++ ) { opsNeeded += ar [ k - 1 ] - ar [ i ] ; } let ans = opsNeeded ; for ( let i = k ; i < ar . length ; i ++ ) { opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) ; opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) ; ans = Math . min ( ans , opsNeeded ) ; } return ans ; } let arr = [ 3 , 1 , 9 , 100 ] ; let n = arr . length ; let k = 3 ; document . write ( minOperations ( arr , k ) ) ;
Find Kth element in an array containing odd elements first and then even elements | Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
function getNumber ( n , k ) { var arr = Array ( n ) . fill ( n ) ; var i = 0 ; var odd = 1 ; while ( odd <= n ) { arr [ i ++ ] = odd ; odd += 2 ; } var even = 2 ; while ( even <= n ) { arr [ i ++ ] = even ; even += 2 ; } return arr [ k - 1 ] ; } var n = 8 , k = 5 ; document . write ( getNumber ( n , k ) ) ;
Swap Alternate Boundary Pairs | Utility function to print the contents of an array ; Function to update the array ; Initialize the pointers ; While there are elements to swap ; Update the pointers ; Print the updated array ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function UpdateArr ( arr , n ) { let i = 0 , j = n - 1 ; while ( i < j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i += 2 ; j -= 2 ; } printArr ( arr , n ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let n = arr . length ; ; UpdateArr ( arr , n ) ;
Count permutation such that sequence is non decreasing | Javascript implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver code ; Pre - calculating factorials
const N = 20 ; let fact = new Array ( N ) ; function pre ( ) { fact [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } function CountPermutation ( a , n ) { let ways = 1 ; a . sort ( ) ; let size = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } let a = [ 1 , 2 , 4 , 4 , 2 , 4 ] ; let n = a . length ; pre ( ) ; document . write ( CountPermutation ( a , n ) ) ;
Smallest element greater than X not present in the array | Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code
function Next_greater ( a , n , x ) { a . sort ( function ( aa , bb ) { return aa - bb } ) ; let low = 0 , high = n - 1 , ans = x + 1 ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( a [ mid ] <= ans ) { if ( a [ mid ] == ans ) { ans ++ ; high = n - 1 ; } low = mid + 1 ; } else high = mid - 1 ; } return ans ; } let a = [ 1 , 5 , 10 , 4 , 7 ] let x = 4 ; let n = a . length ; document . write ( Next_greater ( a , n , x ) ) ;
Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | Function to prvar the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code
function printArrangement ( a , n ) { a . sort ( ) ; var b = Array ( n ) . fill ( 0 ) ; var low = 0 , high = n - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { document . write ( - 1 ) ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { document . write ( - 1 ) ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { document . write ( - 1 ) ; return ; } } } for ( i = 0 ; i < n ; i ++ ) document . write ( b [ i ] + " " ) ; } var a = [ 1 , 4 , 4 , 3 , 2 ] ; var n = a . length ; printArrangement ( a , n ) ;
Greatest contiguous sub | Function that returns the sub - array ; Data - structure to store all the sub - arrays of size K ; Iterate to find all the sub - arrays ; Store the sub - array elements in the array ; Push the vector in the container ; Sort the vector of elements ; The last sub - array in the sorted order will be the answer ; Driver code ; Get the sub - array
function findSubarray ( a , k , n ) { var vec = [ ] ; for ( var i = 0 ; i < n - k + 1 ; i ++ ) { var temp = [ ] ; for ( var j = i ; j < i + k ; j ++ ) { temp . push ( a [ j ] ) ; } vec . push ( temp ) ; } vec . sort ( ) return vec [ vec . length - 1 ] ; } var a = [ 1 , 4 , 3 , 2 , 5 ] ; var k = 4 ; var n = a . length ; var ans = findSubarray ( a , k , n ) ; ans . forEach ( it => { document . write ( it + " " ) } ) ;