codeparrot/xlcost-text-to-code huggingface dataset:
Maximize length of subarray having equal elements by adding at most K | Function to find the maximum number of indices having equal elements after adding at most k numbers ; Sort the array in ascending order ; Make prefix sum array ; Initialize variables ; Update mid ; Check if any subarray can be obtained of length mid having equal elements ; Decrease max to mid ; Function to check if a subarray of length len consisting of equal elements can be obtained or not ; Sliding window ; Last element of the sliding window will be having the max size in the current window ; The current number of element in all indices of the current sliding window ; If the current number of the window , added to k exceeds totalNumbers ; Driver Code ; Function call
function maxEqualIdx ( arr , k ) { arr . sort ( ) ; let prefixSum = new Array ( arr . length + 1 ) . fill ( 0 ) ; prefixSum [ 1 ] = arr [ 0 ] ; for ( let i = 1 ; i < prefixSum . length - 1 ; ++ i ) { prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] ; } let max = arr . length ; let min = 1 ; let ans = 1 ; while ( min <= max ) { let mid = Math . floor ( ( max + min ) / 2 ) ; if ( check ( prefixSum , mid , k , arr ) ) { ans = mid ; min = mid + 1 ; } else { max = mid - 1 ; } } return ans ; } function check ( pSum , len , k , a ) { let i = 0 ; let j = len ; while ( j <= a . length ) { let maxSize = a [ j - 1 ] ; let totalNumbers = maxSize * len ; let currNumbers = pSum [ j ] - pSum [ i ] ; if ( currNumbers + k >= totalNumbers ) { return true ; } else { i ++ ; j ++ ; } } return false ; } let arr = [ 1 , 1 , 1 ] ; let k = 7 ; document . write ( maxEqualIdx ( arr , k ) ) ;
Sum of product of all pairs of a Binary Array | Function to print the sum of product of all pairs of the given array ; Stores count of one in the given array ; If current element is 1 ; Increase count ; Return the sum of product of all pairs ; Driver Code ; Stores the size of the given array
function productSum ( arr , N ) { let cntOne = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } let arr = [ 0 , 1 , 1 , 0 , 1 ] ; let n = arr . length ; document . write ( productSum ( arr , n ) + " " ) ;
Minimum value of X that can be added to N to minimize sum of the digits to Ã ¢ â €°¤ K | Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; Find the number of digits ; Calculate sum of the digits ; Add the digits of num2 ; If the sum of the digits of N is less than or equal to K ; No number needs to be added ; Otherwise ; Calculate the sum of digits from least significant digit ; If sum exceeds K ; Increase previous digit by 1 ; Add zeros to the end ; Calculate difference between the result and N ; Return the result ; Driver Code
function minDigits ( N , K ) { let digits_num = Math . floor ( Math . log ( N ) / Math . log ( 10 ) + 1 ) ; let temp_sum = 0 ; let temp = digits_num ; let result ; let X , var1 ; let sum = 0 ; let num2 = N ; while ( num2 != 0 ) { sum += num2 % 10 ; num2 = parseInt ( num2 / 10 ) ; } if ( sum <= K ) { X = 0 ; } else { while ( temp > 0 ) { var1 = parseInt ( N / ( Math . pow ( 10 , temp - 1 ) ) ) ; temp_sum += var1 % 10 ; if ( temp_sum >= K ) { var1 = parseInt ( var1 / 10 ) ; var1 ++ ; result = var1 * Math . pow ( 10 , temp ) ; break ; } temp -- ; } X = result - N ; return X ; } } let N = 11 , K = 1 ; document . write ( minDigits ( N , K ) ) ;
Count of Ways to obtain given Sum from the given Array elements | Function to call dfs to calculate the number of ways ; Iterate till the length of array ; Initialize the memorization table ; Function to perform the DFS to calculate the number of ways ; Base case : Reached the end of array ; If current sum is obtained ; Otherwise ; If previously calculated subproblem occurred ; Check if the required sum can be obtained by adding current element or by subtracting the current index element ; Store the count of ways ; Driver Code
function findWays ( nums , S ) { let sum = 0 ; for ( let i = 0 ; i < nums . length ; i ++ ) sum += nums [ i ] ; let memo = new Array ( [ nums . length + 1 ] [ 2 * sum + 1 ] ) ; for ( let i = 0 ; i < nums . length + 1 ; i ++ ) { memo [ i ] = new Array ( 2 * sum + 1 ) ; for ( let j = 0 ; j < 2 * sum + 1 ; j ++ ) { memo [ i ] [ j ] = Number . MIN_VALUE ; } } return dfs ( memo , nums , S , 0 , 0 , sum ) ; } function dfs ( memo , nums , S , curr_sum , index , sum ) { if ( index == nums . length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } if ( memo [ index ] [ curr_sum + sum ] != Number . MIN_VALUE ) { return memo [ index ] [ curr_sum + sum ] ; } let ans = dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum ) ; memo [ index ] [ curr_sum + sum ] = ans ; return ans ; } let S = 3 ; let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let answer = findWays ( arr , S ) ; document . write ( answer ) ;
Number of times Maximum and minimum value updated during traversal of array | Function to find the number of times minimum and maximum value updated during the traversal of the given array ; Increment i if new highest value occurs Increment j if new lowest value occurs ; Driver Code
function maxUpdated ( arr ) { let h_score = arr [ 0 ] ; let l_score = arr [ 0 ] ; let i = 1 , j = 1 ; for ( let n in arr ) { if ( h_score < arr [ n ] ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } document . write ( " " ) ; document . write ( " " + i + " " ) ; document . write ( " " ) ; document . write ( " " + j ) ; } let arr = [ 10 , 5 , 20 , 22 ] ; maxUpdated ( arr ) ;
Find the farthest smaller number in the right side | Function to find the farthest smaller number in the right side ; To store minimum element in the range i to n ; If current element in the suffix_min is less than a [ i ] then move right ; Print the required answer ; Driver code
function farthest_min ( a , n ) { let suffix_min = new Array ( n ) ; suffix_min [ n - 1 ] = a [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { suffix_min [ i ] = Math . min ( suffix_min [ i + 1 ] , a [ i ] ) ; } for ( let i = 0 ; i < n ; i ++ ) { let low = i + 1 , high = n - 1 , ans = - 1 ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( suffix_min [ mid ] < a [ i ] ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } document . write ( ans + " " ) ; } } let a = [ 3 , 1 , 5 , 2 , 4 ] ; let n = a . length ; farthest_min ( a , n ) ;
Print numbers in descending order along with their frequencies | Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code
function printElements ( a , n ) { a = a . sort ( compare ) ; a = reverse ( a ) ; var cnt = 1 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { document . write ( a [ i ] + " " + cnt + " " + " " ) ; cnt = 1 ; } else cnt += 1 ; } document . write ( a [ n - 1 ] + " " + cnt + " " + " " ) ; } function reverse ( a ) { var i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function compare ( a , b ) { if ( a < b ) { return - 1 ; } else if ( a > b ) { return 1 ; } else { return 0 ; } } var a = [ 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 ] ; var n = a . length ; printElements ( a , n ) ;
Find element position in given monotonic sequence | Javascript implementation of the approach ; Function to return the value of f ( n ) for given values of a , b , c , n ; If c is 0 , then value of n can be in order of 10 ^ 15. If c != 0 , then n ^ 3 value has to be in order of 10 ^ 18 so maximum value of n can be 10 ^ 6. ; For efficient searching , use binary search . ; Driver code
const SMALL_N = 1000000 ; const LARGE_N = 1000000000000000 ; function func ( a , b , c , n ) { let res = a * n ; let logVlaue = Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ; res += b * n * logVlaue ; res += c * ( n * n * n ) ; return res ; } function getPositionInSeries ( a , b , c , k ) { let start = 1 , end = SMALL_N ; if ( c == 0 ) { end = LARGE_N ; } let ans = 0 ; while ( start <= end ) { let mid = parseInt ( ( start + end ) / 2 ) ; let val = func ( a , b , c , mid ) ; if ( val == k ) { ans = mid ; break ; } else if ( val > k ) { end = mid - 1 ; } else { start = mid + 1 ; } } return ans ; } let a = 2 , b = 1 , c = 1 ; let k = 12168587437017 ; document . write ( getPositionInSeries ( a , b , c , k ) ) ;
Check if array can be sorted with one swap | javascript program to check if an array can be sorted with at - most one swap ; Create a sorted copy of original array ; Check if 0 or 1 swap required to get the sorted array ; Driver Program to test above function
function checkSorted ( n , arr ) { var b = Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) b [ i ] = arr [ i ] ; b . sort ( ) ; var ct = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] != b [ i ] ) ct ++ ; if ( ct == 0 ct == 2 ) return true ; else return false ; } var arr = [ 1 , 5 , 3 , 4 , 2 ] ; var n = arr . length ; if ( checkSorted ( n , arr ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether ( i , j ) exists such that arr [ i ] != arr [ j ] and arr [ arr [ i ] ] is equal to arr [ arr [ j ] ] | Function that will tell whether such Indices present or Not . ; Checking 1 st condition i . e whether Arr [ i ] equal to Arr [ j ] or not ; Checking 2 nd condition i . e whether Arr [ Arr [ i ] ] equal to Arr [ Arr [ j ] ] or not . ; Driver Code ; Calling function .
function checkIndices ( Arr , N ) { for ( var i = 0 ; i < N - 1 ; i ++ ) { for ( var j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] != Arr [ j ] ) { if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) return true ; } } } return false ; } var Arr = [ 3 , 2 , 1 , 1 , 4 ] ; var N = Arr . length ; checkIndices ( Arr , N ) ? document . write ( " " ) : document . write ( " " ) ;
Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Consider all possible pairs and check their sums ; Driver function to test the above function
function printPairs ( arr , n , sum ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) document . write ( " " + arr [ i ] + " " + arr [ j ] + " " + " " ) ; } let arr = [ 1 , 5 , 7 , - 1 , 5 ] ; let n = arr . length ; let sum = 6 ; printPairs ( arr , n , sum ) ;
Ways to choose three points with distance between the most distant points <= L | Returns the number of triplets with the distance between farthest points <= L ; Sort the array ; Find index of element greater than arr [ i ] + L ; Find Number of elements between the ith index and indexGreater since the Numbers are sorted and the elements are distinct from the points btw these indices represent points within range ( a [ i ] + 1 and a [ i ] + L ) both inclusive ; If there are at least two elements in between i and indexGreater find the Number of ways to select two points out of these ; Set of n points on the X axis
function countTripletsLessThanL ( n , L , arr ) { arr . sort ( function ( a , b ) { return a - b } ) ; let ways = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let indexGreater = upper_bound ( arr , 0 , n , arr [ i ] + L ) ; let numberOfElements = indexGreater - ( i + 1 ) ; if ( numberOfElements >= 2 ) { ways += ( numberOfElements * ( numberOfElements - 1 ) / 2 ) ; } } return ways ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + parseInt ( ( high - low ) / 2 , 10 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; let L = 4 ; let ans = countTripletsLessThanL ( n , L , arr ) ; document . write ( " " + ans ) ;
Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; While current element is same as previous or has become smaller than previous . ; Driver code
function minSum ( arr , n ) { let sum = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { let j = i ; while ( j < n && arr [ j ] <= arr [ j - 1 ] ) { arr [ j ] = arr [ j ] + 1 ; j ++ ; } } sum = sum + arr [ i ] ; } return sum ; } let arr = [ 2 , 2 , 3 , 5 , 6 ] ; let n = arr . length ; document . write ( minSum ( arr , n ) ) ;
Making elements distinct in a sorted array by minimum increments | To find minimum sum of unique elements . ; If violation happens , make current value as 1 plus previous value and add to sum . ; No violation . ; Driver code
function minSum ( arr , n ) { let sum = arr [ 0 ] , prev = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= prev ) { prev = prev + 1 ; sum = sum + prev ; } else { sum = sum + arr [ i ] ; prev = arr [ i ] ; } } return sum ; } let arr = [ 2 , 2 , 3 , 5 , 6 ] ; let n = arr . length ; document . write ( minSum ( arr , n ) ) ;
Program to remove vowels from a String | Javascript program to remove vowels from a String ; Driver code
function remVowel ( str ) { let al = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let result = " " ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ! al . includes ( str [ i ] ) ) { result += str [ i ] ; } } return result ; } let str = " " + " " ; document . write ( remVowel ( str ) ) ;
Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first . ; If there are odd elements , we choose middle element ; If there are even elements , then we choose the average of middle two . ; After deciding the final value , find the result . ; Driver program
function minCostToMakeElementEqual ( a , n ) { let y ; if ( n % 2 == 1 ) y = a [ Math . trunc ( n / 2 ) ] ; else y = Math . trunc ( ( a [ n / 2 ] + a [ ( n - 2 ) / 2 ] ) / 2 ) ; let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) s += Math . abs ( a [ i ] - y ) ; return s ; } let a = [ 1 , 100 , 101 ] ; let n = a . length ; document . write ( minCostToMakeElementEqual ( a , n ) ) ;
Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Given array
function isSorted ( arr , n ) { for ( i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } function sortPoss ( arr , n ) { for ( i = 0 ; i < n ; i ++ ) { var idx = - 1 ; var minVar = arr [ i ] ; var j = i ; while ( j < n ) { if ( arr [ j ] < minVar ) { minVar = arr [ j ] ; idx = j ; } j = j + 2 ; } if ( idx != - 1 ) { var t ; t = arr [ i ] ; arr [ i ] = arr [ idx ] ; arr [ idx ] = t ; } } if ( isSorted ( arr , n ) ) return true ; else return false ; } var arr = [ 3 , 5 , 1 , 2 , 6 ] ; var n = arr . length ; if ( sortPoss ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print all array elements appearing more than N / K times | function to sort the array ; Function to find the upper_bound of an array element ; Stores minimum index in which K lies ; Stores maximum index in which K lies ; Calculate the upper bound of K ; Stores mid element of l and r ; If arr [ mid ] is less than or equal to K ; Right subarray ; Left subarray ; Function to print all array elements whose frequency is greater than N / K ; Sort the array arr [ ] ; Stores index of an array element ; Traverse the array ; Stores upper bound of arr [ i ] ; If frequency of arr [ i ] is greater than N / 4 ; Update i ; Given array arr [ ] ; Size of array ; Function Call
function sortt ( number ) { var n = number . length ; for ( i = 0 ; i < n ; ++ i ) { for ( j = i + 1 ; j < n ; ++ j ) { if ( number [ i ] > number [ j ] ) { a = number [ i ] ; number [ i ] = number [ j ] ; number [ j ] = a ; } } } } function upperBound ( arr , N , K ) { var l = 0 ; var r = N ; while ( l < r ) { var mid = parseInt ( ( l + r ) / 2 ) ; if ( arr [ mid ] <= K ) { l = mid + 1 ; } else { r = mid ; } } return l ; } function NDivKWithFreq ( arr , N , K ) { sortt ( arr ) ; var i = 0 ; while ( i < N ) { var X = upperBound ( arr , N , arr [ i ] ) ; if ( ( X - i ) > parseInt ( N / 4 ) ) { document . write ( arr [ i ] + " " ) ; } i = X ; } } var arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] ; var N = arr . length ; var K = 4 ; NDivKWithFreq ( arr , N , K ) ;
Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 | Function to find maximum possible sum after changing the array elements as per the given constraints ; Stores the frequency of elements in given array ; Update frequency ; Stores the previously selected integer ; Stores the maximum possible sum ; Traverse over array count [ ] ; Run loop for each k ; Update ans ; Return maximum possible sum ; Given array arr [ ] ; Size of array ; Function Call
function maxSum ( a , n ) { var count = Array ( n + 1 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) count [ Math . min ( a [ i ] , n ) ] ++ ; var size = 0 ; var ans = 0 ; for ( var k = 1 ; k <= n ; k ++ ) { while ( count [ k ] > 0 && size < k ) { size ++ ; ans += size ; count [ k ] -- ; } ans += k * count [ k ] ; } return ans ; } var arr = [ 3 , 5 , 1 ] ; var n = arr . length ; document . write ( maxSum ( arr , n ) ) ;
Find sum of product of every number and its frequency in given range | Function to solve queries ; Calculating answer for every query ; The end points of the ith query ; map for storing frequency ; Incrementing the frequency ; Iterating over map to find answer ; Adding the contribution of ith number ; Print answer ; Driver code
function answerQueries ( arr , n , queries ) { for ( var i = 0 ; i < queries . length ; i ++ ) { var ans = 0 ; var l = queries [ i ] [ 0 ] - 1 ; var r = queries [ i ] [ 1 ] - 1 ; var freq = new Map ( ) ; for ( var j = l ; j <= r ; j ++ ) { if ( freq . has ( arr [ j ] ) ) freq . set ( arr [ j ] , freq . get ( arr [ j ] ) + 1 ) else freq . set ( arr [ j ] , 1 ) } freq . forEach ( ( value , key ) => { ans += ( key * value ) ; } ) ; document . write ( ans + " " ) ; } } var arr = [ 1 , 2 , 1 ] ; var n = arr . length ; var queries = [ [ 1 , 2 ] , [ 1 , 3 ] ] ; answerQueries ( arr , n , queries ) ;
Check if both halves of the string have at least one different character | JavaScript program implementation of the problem ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver code
let MAX = 26 ; function functions ( str ) { let l = str . length ; let counter1 = [ ] ; let counter2 = [ ] ; for ( let i = 0 ; i < MAX ; i ++ ) { counter1 [ i ] = 0 ; counter2 [ i ] = 0 ; } for ( let i = 0 ; i < l / 2 ; i ++ ) counter1 [ str [ i ] - ' ' ] ++ ; for ( let i = l / 2 ; i < l ; i ++ ) counter2 [ str [ i ] - ' ' ] ++ ; for ( let i = 0 ; i < MAX ; i ++ ) { if ( counter2 [ i ] != counter1 [ i ] ) return true ; } return false ; } let str = " " ; if ( functions ( str ) != 1 ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Check if both halves of the string have at least one different character | Javascript implementation of the problem ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
let MAX = 26 ; function Function ( str ) { let l = str . length ; let counter = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) counter [ i ] = 0 ; for ( let i = 0 ; i < l / 2 ; i ++ ) counter [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = Math . floor ( l / 2 ) ; i < l ; i ++ ) counter [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; for ( let i = 0 ; i < MAX ; i ++ ) if ( counter [ i ] != 0 ) return true ; return false ; } let str = " " ; if ( Function ( str ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Minimum difference between max and min of all K | returns min difference between max and min of any K - size subset ; sort the array so that close elements come together . ; initialize result by a big integer number ; loop over first ( N - K ) elements of the array only ; get difference between max and min of current K - sized segment ; Driver Code
function minDifferenceAmongMaxMin ( arr , N , K ) { arr . sort ( ( a , b ) => a - b ) ; let res = 2147483647 ; for ( let i = 0 ; i <= ( N - K ) ; i ++ ) { let curSeqDiff = arr [ i + K - 1 ] - arr [ i ] ; res = Math . min ( res , curSeqDiff ) ; } return res ; } let arr = [ 10 , 20 , 30 , 100 , 101 , 102 ] ; let N = arr . length ; let K = 3 ; document . write ( minDifferenceAmongMaxMin ( arr , N , K ) ) ;
Position of an element after stable sort | Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver Code
function getIndexInSortedArray ( arr , n , idx ) { let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ idx ] ) result ++ ; if ( arr [ i ] == arr [ idx ] && i < idx ) result ++ ; } return result ; } let arr = [ 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 ] ; let n = arr . length ; let idxOfEle = 5 ; document . write ( getIndexInSortedArray ( arr , n , idxOfEle ) ) ;
Find permutation of numbers upto N with a specific sum in a specific range | Function to check if sum is possible with remaining numbers ; Stores the minimum sum possible with x numbers ; Stores the maximum sum possible with x numbers ; If S lies in the range [ minSum , maxSum ] ; Function to find the resultant permutation ; Stores the count of numbers in the given segment ; If the sum is not possible with numbers in the segment ; Output - 1 ; Stores the numbers present within the given segment ; Iterate over the numbers from 1 to N ; If ( S - i ) is a positive non - zero sum and if it is possible to obtain ( S - i ) remaining numbers ; Update sum S ; Update required numbers in the segement ; Push i in vector v ; If sum has been obtained ; Break from the loop ; If sum is not obtained ; Output - 1 ; Stores the numbers which are not present in given segment ; Loop to check the numbers not present in the segment ; Pointer to check if i is present in vector v or not ; If i is not present in v ; Push i in vector v1 ; Polet to the first elements of v1 and v respectively ; Prlet the required permutation ; Driver Code
function possible ( x , S , N ) { let minSum = ( x * ( x + 1 ) ) / 2 ; let maxSum = ( x * ( 2 * N - x + 1 ) ) / 2 ; if ( S < minSum S > maxSum ) { return false ; } return true ; } function findPermutation ( N , L , R , S ) { let x = R - L + 1 ; if ( ! possible ( x , S , N ) ) { document . write ( - 1 ) ; return ; } else { let v = [ ] ; for ( let i = N ; i >= 1 ; -- i ) { if ( S - i >= 0 && possible ( x - 1 , S - i , i - 1 ) ) { S = S - i ; x -- ; v . push ( i ) ; } if ( S == 0 ) { break ; } } if ( S != 0 ) { document . write ( - 1 ) ; return ; } let v1 = [ ] ; for ( let i = 1 ; i <= N ; ++ i ) { it = v . includes ( i ) ; if ( ! it ) { v1 . push ( i ) ; } } let j = 0 , f = 0 ; for ( let i = 1 ; i < L ; ++ i ) { document . write ( v1 [ j ] + " " ) ; j ++ ; } for ( let i = L ; i <= R ; ++ i ) { document . write ( v [ f ] + " " ) ; f ++ ; } for ( let i = R + 1 ; i <= N ; ++ i ) { document . write ( v1 [ j ] + " " ) ; j ++ ; } } return ; } let N = 6 , L = 3 , R = 5 , S = 8 ; findPermutation ( N , L , R , S ) ;