codeparrot/xlcost-text-to-code huggingface dataset:
Absolute distinct count in a sorted array | The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code
function distinctCount ( arr , n ) { let count = n ; let i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) count -- , i ++ ; while ( i != j && arr [ j ] == arr [ j - 1 ] ) count -- , j -- ; if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ , j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } let arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; let n = arr . length ; document . write ( " " + distinctCount ( arr , n ) ) ;
Sort an almost sorted array where only two elements are swapped | This function sorts an array that can be sorted by single swap ; Traverse the given array from rightmost side ; Check if arr [ i ] is not in order ; Find the other element to be swapped with arr [ i ] ; Swap the pair ; A utility function to print an array of size n ; Driver Code
function sortByOneSwap ( arr , n ) { for ( let i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] < arr [ i - 1 ] ) { let j = i - 1 ; while ( j >= 0 && arr [ i ] < arr [ j ] ) j -- ; let temp = arr [ i ] ; arr [ i ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; break ; } } } function printArray ( arr , n ) { let i ; for ( i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 10 , 30 , 20 , 40 , 50 , 60 , 70 ] ; let n = arr . length ; document . write ( " " + " " ) ; printArray ( arr , n ) ; sortByOneSwap ( arr , n ) ; document . write ( " " + " " ) ; printArray ( arr , n ) ;
Pancake sorting | Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already at  the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; Utility function to print array arr [ ] ; Driver code
function flip ( arr , i ) { let temp , start = 0 ; while ( start < i ) { temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i -- ; } } function findMax ( arr , n ) { let mi , i ; for ( mi = 0 , i = 0 ; i < n ; ++ i ) if ( arr [ i ] > arr [ mi ] ) mi = i ; return mi ; } function pancakeSort ( arr , n ) { for ( let curr_size = n ; curr_size > 1 ; -- curr_size ) { let mi = findMax ( arr , curr_size ) ; if ( mi != curr_size - 1 ) { flip ( arr , mi ) ; flip ( arr , curr_size - 1 ) ; } } return 0 ; } function printArray ( arr , arr_size ) { for ( let i = 0 ; i < arr_size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } let arr = [ 23 , 10 , 20 , 11 , 12 , 6 , 7 ] ; let n = arr . length ; pancakeSort ( arr , n ) ; document . write ( " " + " " ) ; printArray ( arr , n ) ;
Lexicographically smallest numeric string having odd digit counts | Function to construct lexicographically smallest numeric string having an odd count of each characters ; Stores the resultant string ; If N is even ; Otherwise ; Driver code
function genString ( N ) { let ans = " " ; if ( N % 2 == 0 ) { ans = ' ' . repeat ( N - 1 ) + ' ' ; } else { ans = ' ' . repeat ( N ) ; } return ans ; } let N = 5 ; document . write ( genString ( N ) ) ;
Modify given string such that odd and even indices is lexicographically largest and smallest | Function to modify the given string satisfying the given criteria ; Traverse the string S ; If i is even ; If the S [ i ] is ' a ' , then change S [ i ] to ' b ' ; Otherwise , change S [ i ] to ' a ' ; If S [ i ] is ' z ' , then change S [ i ] to ' y ' ; Otherwise , change S [ i ] to ' z ' ; Return the result ; Driver Code
function performOperation ( S , N ) { for ( var i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S . charAt ( i ) == ' ' ) { S [ i ] = ' ' ; } else { S [ i ] = ' ' ; } } else { if ( S . charAt ( i ) == ' ' ) { S . charAt ( i ) = ' ' ; } else { S [ i ] = ' ' ; } } } return S ; } var S = " " ; var N = S . length ; document . write ( performOperation ( S , N ) ) ;
Count of values chosen for X such that N is reduced to 0 after given operations | Function to check if the value of X reduces N to 0 or not ; Update the value of N as N - x ; Check if x is a single digit leteger ; Function to find the number of values X such that N can be reduced to 0 after performing the given operations ; Number of digits in N ; Stores the count of value of X ; Iterate over all possible value of X ; Check if x follow the conditions ; Return total count ; Driver Code
function check ( x , N ) { while ( true ) { N -= x ; if ( x < 10 ) break ; let temp2 = 0 ; while ( x > 0 ) { temp2 += ( x % 10 ) ; x = Math . floor ( x / 10 ) ; } x = temp2 ; } if ( ( x < 10 ) && ( N == 0 ) ) { return true ; } return false ; } function countNoOfsuchX ( N ) { let k = Math . floor ( Math . log10 ( N ) ) + 1 ; let count = 1 ; for ( let x = ( N - ( k * ( k + 1 ) * 5 ) ) ; x <= N ; x ++ ) { if ( check ( x , N ) ) { count += 1 ; } } return count ; } let N = 9399 ; document . write ( countNoOfsuchX ( N ) ) ;
Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element | Returns the count of subarrays which contains both the maximum and minimum elements in the given vector ; Initialize the low and high of array ; If current element is less than least element ; If current element is more than highest element ; If current element is equal to low or high then update the pointers ; Update number of subarrays ; Return the result ; Function to find the maximum count of subarrays ; Iterate the array to find the maximum and minimum element ; Vector after removing the minimum element ; Using assignment operator to copy one vector to other ; Vector after removing the maximum element ; Given array ; Function Call
function proc ( v ) { let n = v . length ; let low = v [ n - 1 ] , high = v [ n - 1 ] ; let p1 = n , p2 = n ; let ans = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { let x = v [ i ] ; if ( x < low ) { low = x ; ans = 0 ; } else if ( x > high ) { high = x ; ans = 0 ; } if ( x == low ) p1 = i ; if ( x == high ) p2 = i ; ans += n - Math . max ( p1 , p2 ) ; } return ans ; } function subarray ( v ) { let n = v . length ; if ( n <= 1 ) { return n ; } let ans = proc ( v ) ; let low = v [ 0 ] , pos_low = 0 , high = v [ 0 ] , pos_high = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let x = v [ i ] ; if ( x < low ) { low = x ; pos_low = i ; } else if ( x > high ) { high = x ; pos_high = i ; } } let u = [ ... v ] ; u . splice ( pos_low , 1 ) ; ans = Math . max ( ans , proc ( u ) ) ; let w = [ ... v ] ; w . splice ( pos_high , 1 ) ; return Math . max ( ans , proc ( w ) ) ; } let v = [ ] ; v . push ( 7 ) ; v . push ( 2 ) ; v . push ( 5 ) ; v . push ( 4 ) ; v . push ( 3 ) ; v . push ( 1 ) ; document . write ( subarray ( v ) ) ;
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 | Function to find the minimum number of increment and decrement of pairs required to make all array elements equal ; Stores the sum of the array ; If sum is not divisible by N ; Update sum ; Store the minimum number of operations ; Iterate while i is less than N ; Add absolute difference of current element with k to ans ; Increase i bye 1 ; Return the value in ans2 ; Given Input ; Function Call
function find ( arr , N ) { let Sum = 0 ; let i = 0 ; let ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { Sum += arr [ i ] ; } if ( Sum % N ) { return - 1 ; } else { k = Math . floor ( Sum / N ) ; ans = 0 ; i = 0 ; while ( i < N ) { ans = ans + Math . abs ( k - arr [ i ] ) ; i += 1 ; } } return Math . floor ( ans / 2 ) ; } let arr = [ 5 , 4 , 1 , 10 ] let N = arr . length ; document . write ( find ( arr , N ) ) ;
Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times | Function to find minimum number of moves required to convert A into B ; Stores the minimum number of moves required ; Stores the absolute difference ; FInd the number of moves ; Return cnt ; Input ; Function call
function minimumSteps ( a , b ) { let cnt = 0 ; a = Math . abs ( a - b ) ; cnt = Math . floor ( a / 5 ) + Math . floor ( ( a % 5 ) / 2 ) + ( a % 5 ) % 2 ; return cnt ; } let A = 3 , B = 9 ; document . write ( minimumSteps ( A , B ) ) ;
Minimum distance between duplicates in a String | This function is used to find minimum distance between any two repeating characters using two - pointers and hashing technique ; hash array to store character 's last index ; Traverse through the String ; If the character is present in visited array find if its forming minimum distance ; update current character 's last index ; Return minimum distance found , else - 1 ; Given Input ; Function Call
function shortestDistance ( s , n ) { var visited = Array ( 128 ) . fill ( - 1 ) ; var ans = Number . MAX_VALUE ; for ( var right = 0 ; right < n ; right ++ ) { var left = visited [ s . charCodeAt ( right ) ] ; if ( left != - 1 ) ans = Math . min ( ans , right - left - 1 ) ; visited [ s . charCodeAt ( right ) ] = right ; } return ans == Number . MAX_VALUE ? - 1 : ans ; } var s = " " ; var n = 13 ; document . write ( shortestDistance ( s , n ) ) ;
Print all numbers that can be obtained by adding A or B to N exactly M times | Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Given Input ; Function Call
function possibleNumbers ( N , M , A , B ) { var i ; if ( A > B ) { var temp = A ; A = B ; B = temp ; } var number = N + M * A ; document . write ( number + " " ) ; if ( A != B ) { for ( i = 0 ; i < M ; i ++ ) { number = number - A + B ; document . write ( number + " " ) ; } } } var N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ;
Maximize number of circular buildings that can be covered by L length wire | JavaScript program for the above approach ; Function to find the maximum number of buildings covered ; Store the current sum ; Traverse the array ; Add the length of wire required for current building to cur_sum ; Add extra unit distance 1 ; If curr_sum <= length of wire increment count by 1 ; If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum ; Update the max_count ; Return the max_count ; Given Input ; Size of the array ; Function Call
var Pi = 3.141592 ; function MaxBuildingsCovered ( arr , N , L ) { var curr_sum = 0 ; var start = 0 , curr_count = 0 , max_count = 0 ; for ( var i = 0 ; i < N ; i ++ ) { curr_sum = curr_sum + parseFloat ( arr [ i ] ) * Pi ; if ( i != 0 ) curr_sum += 1 ; if ( curr_sum <= L ) { curr_count ++ ; } else if ( curr_sum > L ) { curr_sum = curr_sum - parseFloat ( arr [ start ] ) * Pi ; curr_sum -= 1 ; start ++ ; curr_count -- ; } max_count = Math . max ( curr_count , max_count ) ; } return max_count ; } var arr = [ 4 , 1 , 6 , 2 ] ; var L = 24 ; var N = arr . length ; document . write ( MaxBuildingsCovered ( arr , N , L ) ) ;
Count numbers less than N whose Bitwise AND with N is zero | Function to count number of unset bits in the integer N ; Stores the number of unset bits in N ; Check if N is even ; Increment the value of c ; Right shift N by 1 ; Return the value of count of unset bits ; Function to count numbers whose Bitwise AND with N equal to 0 ; Stores the number of unset bits in N ; Print the value of 2 to the power of unsetBits ; Driver Code
function countUnsetBits ( N ) { let c = 0 ; while ( N != 0 ) { if ( N % 2 == 0 ) { c += 1 ; } N = N >> 1 ; } return c ; } function countBitwiseZero ( N ) { let unsetBits = countUnsetBits ( N ) ; document . write ( 1 << unsetBits ) ; } let N = 9 ; countBitwiseZero ( N ) ;
Check if a Binary String can be split into disjoint subsequences which are equal to "010" | Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Given string ; Function Call
function isPossible ( s ) { let n = s . length ; let count_0 = 0 , count_1 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } if ( count_0 != ( 2 * count_1 ) ) return false ; count_0 = 0 ; count_1 = 0 ; for ( let i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == ' ' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } return true ; } let s = " " ; if ( isPossible ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum number of bricks that can be intersected | Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Given Input ; Function Call
function leastBricks ( wall ) { let map = new Map ( ) ; let res = 0 ; for ( let list of wall ) { let width = 0 ; for ( let i = 0 ; i < list . length - 1 ; i ++ ) { width += list [ i ] ; if ( map . has ( width ) ) { map . set ( width , map . get ( width ) + 1 ) ; } else { map . set ( width , 1 ) } res = Math . max ( res , map . get ( width ) ) ; } } document . write ( wall . length - res ) ; } let arr = [ [ 1 , 2 , 2 , 1 ] , [ 3 , 1 , 2 ] , [ 1 , 3 , 2 ] , [ 2 , 4 ] , [ 3 , 1 , 2 ] , [ 1 , 3 , 1 , 1 ] ] ; leastBricks ( arr ) ;
Find an N | Function to print the required permutation ; Driver code
function findPermutation ( N ) { for ( var i = 1 ; i <= N ; i ++ ) document . write ( i + " " ) ; document . write ( " " ) ; } var N = 5 ; findPermutation ( N ) ;
Maximize count of distinct profits possible by N transactions | Function to count distinct profits possible ; Stores the minimum total profit ; Stores the maximum total profit ; Return count of distinct profits ; Input ; Function call
function numberOfWays ( N , X , Y ) { let S1 = ( N - 1 ) * X + Y ; let S2 = ( N - 1 ) * Y + X ; return ( S2 - S1 + 1 ) ; } let N = 3 ; let X = 13 ; let Y = 15 ; document . write ( numberOfWays ( N , X , Y ) ) ;
Minimum number of operations required to make a permutation of first N natural numbers equal | Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Given Input
function MinimumOperations ( A , N , K ) { let Count = 0 ; let i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } let A = [ 5 , 4 , 3 , 1 , 2 ] ; let K = 3 ; let N = A . length ; document . write ( MinimumOperations ( A , N , K ) ) ;
Consecutive Prime numbers greater than equal to given number . | Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver code
function is_prime ( n ) { if ( n == 1 ) { return false ; } for ( var i = 2 ; i <= parseInt ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } function consecutive_primes ( n ) { var first = - 1 , second = - 1 ; for ( var i = parseInt ( Math . sqrt ( n ) ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( var i = parseInt ( Math . sqrt ( n ) ) + 1 ; i <= parseInt ( n / 2 ) ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { document . write ( first + " " + second ) ; } else { for ( var i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { document . write ( second + " " + i ) ; return ; } } } } var n = 14 ; consecutive_primes ( n ) ;
Reorder characters of a string to valid English representations of digits | Function to construct the original set of digits from the string in ascending order ; Store the unique characters corresponding to word and number ; Store the required result ; Store the frequency of each character of S ; Traverse the unique characters ; Store the count of k [ i ] in S ; Traverse the corresponding word ; Decrement the frequency of characters by x ; Append the digit x times to ans ; Sort the digits in ascending order ; Given string , s ; Function Call
function construct_digits ( s ) { let k = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let l = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let c = [ 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 ] ; let ans = [ ] ; let d = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( ! d . has ( s [ i ] ) ) d . set ( s [ i ] , 0 ) ; d . set ( s [ i ] , d . get ( s [ i ] ) + 1 ) ; } for ( let i = 0 ; i < k . length ; i ++ ) { let x = 0 ; if ( d . has ( k [ i ] ) ) x = d . get ( k [ i ] ) ; for ( let j = 0 ; j < l [ i ] . length ; j ++ ) { if ( d . has ( l [ i ] [ j ] ) ) d . set ( l [ i ] [ j ] , d . get ( l [ i ] [ j ] ) - x ) ; } for ( let j = 0 ; j < x ; j ++ ) ans . push ( c [ i ] ) ; } ans . sort ( ) ; return ans . join ( " " ) ; } let s = " " ; document . write ( construct_digits ( s ) ) ;
Minimum decrements required to make all pairs of adjacent matrix elements distinct | Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; The given matrix ; Function Call to count the minimum number of decrements required
const n = 3 ; const m = 3 ; function countDecrements ( arr ) { let count_1 = 0 ; let count_2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_2 ++ ; } } document . write ( Math . min ( count_1 , count_2 ) ) ; } let arr = [ [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ] ; countDecrements ( arr ) ;
Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code
function check ( X , Y ) { if ( X > 3 ) { document . write ( " " ) ; } else if ( X == 1 && Y == 1 ) { document . write ( " " ) ; } else if ( X == 2 && Y <= 3 ) { document . write ( " " ) ; } else if ( X == 3 && Y <= 3 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let X = 6 , Y = 8 ; check ( X , Y ) ;
Count distinct sum of pairs possible from a given range | Function to count distinct sum of pairs possible from the range [ L , R ] ; Return the count of distinct sum of pairs ; Driver Code
function distIntegers ( L , R ) { return 2 * R - 2 * L + 1 ; } let L = 3 , R = 8 ; document . write ( distIntegers ( L , R ) ) ;
Count subarrays having even Bitwise XOR | Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Given array ; Stores the size of the array
function evenXorSubarray ( arr , n ) { let ans = 0 ; let freq = [ 0 , 0 ] ; let XOR = 0 ; for ( let i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } document . write ( ans ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; evenXorSubarray ( arr , N ) ;
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Given N and X ; Function Call
function countOccurrences ( N , X ) { var count = 0 ; for ( var i = 1 ; i < Math . sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { var a = i ; var b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } var N = 7 ; var X = 12 ; document . write ( countOccurrences ( N , X ) ) ;
Maximize array product by changing any array element arr [ i ] to ( | Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Print the elements of the array ; Driver Code ; Function Call
function findArrayWithMaxProduct ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 === 1 ) { var max_element = - 1 ; var index = - 1 ; for ( var i = 0 ; i < N ; i ++ ) if ( Math . abs ( arr [ i ] ) > max_element ) { max_element = Math . abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( var i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ - 3 , 0 , 1 ] ; var N = arr . length ; findArrayWithMaxProduct ( arr , N ) ;
Lexicographically smallest permutation of first N natural numbers having K perfect indices | Function to prvar the lexicographically smallest permutation with K perfect indices ; Iterator to traverse the array ; Traverse first K array indices ; Traverse remaining indices ; Driver Code
function findPerfectIndex ( N , K ) { var i = 0 ; for ( ; i < K ; i ++ ) { document . write ( ( N - K + 1 ) + i + " " ) ; } for ( ; i < N ; i ++ ) { document . write ( i - K + 1 + " " ) ; } } var N = 10 , K = 3 ; findPerfectIndex ( N , K ) ;
Count pairs having distinct sum from a given range | Function to count pairs made up of elements from the range [ L , R ] having distinct sum ; Stores the least sum which can be formed by the pairs ; Stores the highest sum which can be formed by the pairs ; Stores the count of pairs having distinct sum ; Print the count of pairs ; Driver Code ; Function call to count the number of pairs having distinct sum in the range [ L , R ]
function countPairs ( L , R ) { let firstNum = 2 * L ; let lastNum = 2 * R ; let Cntpairs = lastNum - firstNum + 1 ; document . write ( Cntpairs + " " ) ; } let L = 2 , R = 3 ; countPairs ( L , R ) ;