codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs whose products exist in array | Returns count of pairs whose product exists in arr ; find product in an array ; if product found increment counter ; return Count of all pair whose product exist in array ; Driver Code
function countPairs ( arr , n ) { var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var product = arr [ i ] * arr [ j ] ; for ( k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; break ; } } } } return result ; } var arr = [ 6 , 2 , 4 , 12 , 5 , 3 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Count pairs whose products exist in array | Returns count of pairs whose product exists in arr ; Create an empty hash - set that store all array element ; Insert all array element into set ; Generate all pairs and check is exist in ' Hash ' or not ; if product exists in set then we increment count by 1 ; return count of pairs whose product exist in array ; Driver program
function countPairs ( arr , n ) { var result = 0 ; var Hash = new Set ( ) ; for ( i = 0 ; i < n ; i ++ ) { Hash . add ( arr [ i ] ) ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var product = arr [ i ] * arr [ j ] ; if ( Hash . has ( product ) ) { result ++ ; } } } return result ; } var arr = [ 6 , 2 , 4 , 12 , 5 , 3 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Master Theorem For Subtract and Conquer Recurrences | Javascript code for above the approach . ; Driver Code
function fib ( n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } let n = 9 ; document . write ( fib ( n ) ) ;
Tail Recursion | A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver code
function factTR ( n , a ) { if ( n == 0 ) return a ; return factTR ( n - 1 , n * a ) ; } function fact ( n ) { return factTR ( n , 1 ) ; } document . write ( fact ( 5 ) ) ;
Minimum and maximum count of elements at D distance from arr [ i ] in either direction | Function to find the minimum and maximum number of points included in a range of distance D ; Stores the minimum and maximum number of points that lies over the distance of D ; Iterate the array ; Count of elements included to left of point at index i ; Update the minimum number of points ; Update the maximum number of points ; Count of elements included to right of point at index i ; Update the minimum number of points ; Update the maximum number of points ; Return the array ; Function to perform the Binary Search to the left of arr [ i ] over the given range ; Base Case ; Binary Search for index to left ; update index ; Return the number of elements by subtracting indices ; Function to perform the Binary Search to the right of arr [ i ] over the given range ; Base Case ; Binary Search for index to right ; Update the index ; Return the number of elements by subtracting indices ; Driver Code ; Function Call
function minMaxRange ( arr , D , N ) { let max = 1 , min = N ; for ( let i = 0 ; i < N ; i ++ ) { let dist = leftSearch ( arr , arr [ i ] - D , i ) ; min = Math . min ( min , dist ) ; max = Math . max ( max , dist ) ; dist = rightSearch ( arr , arr [ i ] + D , i ) ; min = Math . min ( min , dist ) ; max = Math . max ( max , dist ) ; } return [ min , max ] ; } function leftSearch ( arr , val , i ) { if ( i == 0 ) return 1 ; let left = 0 , right = i - 1 ; let ind = - 1 ; while ( left <= right ) { let mid = Math . floor ( ( left + right ) / 2 ) ; if ( arr [ mid ] < val ) { left = mid + 1 ; } else { right = mid - 1 ; ind = mid ; } } return ind != - 1 ? i - ind + 1 : 1 ; } function rightSearch ( arr , val , i ) { if ( i == arr . length - 1 ) return 1 ; let left = i + 1 ; let right = arr . length - 1 ; let ind = - 1 ; while ( left <= right ) { let mid = Math . floor ( ( left + right ) / 2 ) ; if ( arr [ mid ] > val ) { right = mid - 1 ; } else { left = mid + 1 ; ind = mid ; } } return ind != - 1 ? ind - i + 1 : 1 ; } let arr = [ 1 , 3 , 5 , 9 , 14 ] ; let N = arr . length ; let D = 4 ; let minMax = minMaxRange ( arr , D , N ) ; document . write ( minMax [ 0 ] + " " + minMax [ 1 ] ) ;
Given two unsorted arrays , find all pairs whose sum is x | Function to print all pairs in both arrays whose sum is equal to given value x ; Driver code
function findPairs ( arr1 , arr2 , n , m , x ) { for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < m ; j ++ ) if ( arr1 [ i ] + arr2 [ j ] == x ) document . write ( arr1 [ i ] + " " + arr2 [ j ] + " " ) ; } let arr1 = [ 1 , 2 , 3 , 7 , 5 , 4 ] ; let arr2 = [ 0 , 7 , 4 , 3 , 2 , 1 ] ; let n = arr1 . length ; let m = arr2 . length ; let x = 8 ; findPairs ( arr1 , arr2 , n , m , x ) ;
Smallest possible integer K such that ceil of each Array element when divided by K is at most M | Function to check if the sum of ceil values of the arr [ ] for the K value is at most M or not ; Stores the sum of ceil values ; Update the sum ; Return true if sum is less than or equal to M , false otherwise ; Function to find the smallest possible integer K such that ceil ( arr [ 0 ] / K ) + ceil ( arr [ 1 ] / K ) + ... . + ceil ( arr [ N - 1 ] / K ) is less than or equal to M ; Stores the low value ; Stores the high value ; Stores the middle value ; Update the mid value ; Check if the mid value is K ; Update the low value ; Update the high value ; Check if low is K or high is K and return it ; Driver Code
function isvalid ( arr , K , N , M ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += Math . ceil ( arr [ i ] * 1.0 / K ) ; } return sum <= M ; } function smallestValueForK ( arr , N , M ) { let low = 1 ; let high = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { high = Math . max ( high , arr [ i ] ) ; } let mid ; while ( high - low > 1 ) { mid = ( high + low ) / 2 ; if ( ! isvalid ( arr , mid , N , M ) ) low = mid + 1 ; else high = mid ; } return isvalid ( arr , low , N , M ) ? low : high ; } let arr = [ 4 , 3 , 2 , 7 ] ; let N = arr . length ; let M = 5 ; document . write ( smallestValueForK ( arr , N , M ) ) ;
Given two unsorted arrays , find all pairs whose sum is x | Function to find all pairs in both arrays whose sum is equal to given value x ; Insert all elements of first array in a hash ; Subtract sum from second array elements one by one and check it 's present in array first  or not ; Driver code
function findPairs ( arr1 , arr2 , n , m , x ) { let s = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) s . set ( arr1 [ i ] , 0 ) ; for ( let j = 0 ; j < m ; j ++ ) if ( s . has ( x - arr2 [ j ] ) ) document . write ( x - arr2 [ j ] + " " + arr2 [ j ] + " " ) ; } let arr1 = [ 1 , 0 , - 4 , 7 , 6 , 4 ] ; let arr2 = [ 0 , 2 , 4 , - 3 , 2 , 1 ] ; let x = 8 ; findPairs ( arr1 , arr2 , arr1 . length , arr2 . length , x ) ;
Altitude of largest Triangle that can be inscribed in a Rectangle | Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Stores the maximum altitude value ; Return res ; Given Input ; Function call
function largestAltitude ( L , B ) { if ( L > B ) { var t = L ; L = B ; B = t ; } var res = Math . min ( B / 2 , L ) ; return res ; } var L = 3 ; var B = 4 ; document . write ( largestAltitude ( L , B ) ) ;
Maximum Fixed Point ( Value equal to index ) in a given Array | Function to find the maximum index i such that arr [ i ] is equal to i ; Traversing the array from backwards ; If arr [ i ] is equal to i ; If there is no such index ; Given Input ; Function Call
function findLargestIndex ( arr , n ) { for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == i ) { document . write ( i ) ; return ; } } document . write ( - 1 ) ; } var arr = [ - 10 , - 5 , 0 , 3 , 7 ] ; var n = arr . length ; findLargestIndex ( arr , n ) ;
Cumulative frequency of count of each element in an unsorted array | Function to print the cumulative frequency according to the order given ; Insert elements and their frequencies in hash map . ; traverse in the array ; add the frequencies ; if the element has not been visited previously ; mark the hash 0 as the element 's cumulative frequency  has been printed ; Driver Code
function countFreq ( a , n ) { let hm = new Array ( n ) ; for ( let i = 0 ; i < hm . length ; i ++ ) { hm [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; let cumul = 0 ; for ( let i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] != 0 ) { document . write ( a [ i ] + " " + cumul + " " ) ; } hm [ a [ i ] ] = 0 ; } } let a = [ 1 , 3 , 2 , 4 , 2 , 1 ] ; let n = a . length ; countFreq ( a , n ) ;
Find pairs in array whose sums already exist in array | Function to find pair whose sum exists in arr [ ] ; Driver code
function findPair ( arr , n ) { let found = false ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { for ( let k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { document . write ( arr [ i ] + " " + arr [ j ] + " " ) ; found = true ; } } } } if ( found == false ) document . write ( " " ) ; } let arr = [ 10 , 4 , 8 , 13 , 5 ] ; let n = arr . length ; findPair ( arr , n ) ;
Maximum number of mangoes that can be bought | Function to check if mid number of mangoes can be bought ; Store the coins ; If watermelons needed are greater than given watermelons ; Store remaining watermelons if vl watermelons are used to buy mangoes ; Store the value of coins if these watermelon get sold ; Increment coins by ex ; Number of mangoes that can be buyed if only x coins needed for one mango ; If the condition is satisfied , return true ; Otherwise return false ; Function to find the maximum number of mangoes that can be bought by selling watermelons ; Initialize the boundary values ; Store the required result ; Binary Search ; Store the mid value ; Check if it is possible to buy mid number of mangoes ; Otherwise , update r to mid - 1 ; Return the result ; Given Input ; Function Call
function check ( n , m , x , y , vl ) { var temp = m ; if ( vl > n ) return false ; var ex = n - vl ; ex *= y ; temp += ex ; var cr = parseInt ( temp / x ) ; if ( cr >= vl ) return true ; return false ; } function maximizeMangoes ( n , m , x , y ) { var l = 0 , r = n ; var ans = 0 ; while ( l <= r ) { var mid = l + parseInt ( ( r - l ) / 2 ) ; if ( check ( n , m , x , y , mid ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } return ans ; } var W = 4 , C = 8 , x = 4 , y = 4 ; document . write ( maximizeMangoes ( W , C , x , y ) ) ;
Kth Smallest Element in a sorted array formed by reversing subarrays from a random index | Function to find the Kth element in a sorted and rotated array at random point ; Set the boundaries for binary search ; Apply binary search to find R ; Initialize the middle element ; Check in the right side of mid ; Else check in the left side ; Random point either l or h ; Return the kth smallest element ; Given Input ; Function Call
function findkthElement ( arr , n , K ) { var l = 0 ; var h = n - 1 , r ; while ( l + 1 < h ) { var mid = parseInt ( ( l + h ) / 2 ) ; if ( arr [ l ] >= arr [ mid ] ) l = mid ; else h = mid ; } if ( arr [ l ] < arr [ h ] ) r = l ; else r = h ; if ( K <= r + 1 ) return arr [ r + 1 - K ] ; else return arr [ n - ( K - ( r + 1 ) ) ] ; } var arr = [ 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 ] ; var n = arr . length ; var K = 3 ; document . write ( findkthElement ( arr , n , K ) ) ;
Find all pairs ( a , b ) in an array such that a % b = k | method to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver Code
function printPairs ( arr , n , k ) { let isPairFound = true ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i != j && arr [ i ] % arr [ j ] == k ) { document . write ( " " + arr [ i ] + " " + arr [ j ] + " " + " " ) ; isPairFound = true ; } } } return isPairFound ; } let arr = [ 2 , 3 , 5 , 4 , 7 ] ; let k = 3 ; if ( printPairs ( arr , arr . length , k ) == false ) document . write ( " " ) ;
Find all pairs ( a , b ) in an array such that a % b = k | Utility method to find the divisors of n and store in vector v [ ] ; Vector is used to store the divisors ; If n is a square number , push only one occurrence ; method to find pairs such that ( a % b = k ) ; Store all the elements in the map to use map as hash for finding elements in O ( 1 ) time . ; Print all the pairs with ( a , b ) as ( k , numbers greater than k ) as k % ( num ( > k ) ) = k i . e . 2 % 4 = 2 ; Now check for the current element as ' a ' how many b exists such that a % b = k ; find all the divisors of ( arr [ i ] - k ) ; Check for each divisor i . e . arr [ i ] % b = k or not , if yes then print that pair . ; Clear vector ; Driver method
function findDivisors ( n ) { let v = [ ] ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) v . push ( i ) ; else { v . push ( i ) ; v . push ( Math . floor ( n / i ) ) ; } } } return v ; } function printPairs ( arr , n , k ) { let occ = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) occ . set ( arr [ i ] , true ) ; let isPairFound = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( occ . get ( k ) && k < arr [ i ] ) { document . write ( " " + k + " " + arr [ i ] + " " ) ; isPairFound = true ; } if ( arr [ i ] >= k ) { let v = findDivisors ( arr [ i ] - k ) ; for ( let j = 0 ; j < v . length ; j ++ ) { if ( arr [ i ] % v [ j ] == k && arr [ i ] != v [ j ] && occ . get ( v [ j ] ) ) { document . write ( " " + arr [ i ] + " " + v [ j ] + " " ) ; isPairFound = true ; } } v = [ ] ; } } return isPairFound ; } let arr = [ 3 , 1 , 2 , 5 , 4 ] ; let k = 2 ; if ( printPairs ( arr , arr . length , k ) == false ) document . write ( " " ) ;
Sum of nodes in a Binary Search Tree with values from a given range | Class for node of the Tree ; Function to create a new BST node ; Stores the sum of all nodes lying in the range [ L , R ] ; Function to perform level order traversal on the Tree and calculate the required sum ; Base Case ; Stores the nodes while performing level order traversal ; Push the root node into the queue ; Iterate until queue is empty ; Stores the front node of the queue ; If the value of the node lies in the given range ; Add it to sum ; If the left child is not null and exceeds low ; Insert into queue ; If the right child is not null and exceeds low ; Insert into queue ; Return the resultant sum ; Function to insert a new node into the Binary Search Tree ; Base Case ; If the data is less than the value of the current node ; Recur for left subtree ; Otherwise ; Recur for the right subtree ; Return the node ; Let us create following BST 10 / \ 5 15 / \ \ 3 7 18
class Node { constructor ( item ) { this . val = item ; this . left = this . right = null ; } } let sum = 0 ; function rangeSumBST ( root , low , high ) { if ( root == null ) return 0 ; let q = [ ] ; q . push ( root ) ; while ( q . length != 0 ) { let curr = q . shift ( ) ; if ( curr . val >= low && curr . val <= high ) { sum += curr . val ; } if ( curr . left != null && curr . val > low ) q . push ( curr . left ) ; if ( curr . right != null && curr . val < high ) q . push ( curr . right ) ; } return sum ; } function insert ( node , data ) { if ( node == null ) return new Node ( data ) ; if ( data <= node . val ) node . left = insert ( node . left , data ) ; else node . right = insert ( node . right , data ) ; return node ; } let root = null ; root = insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 3 ) ; insert ( root , 7 ) ; insert ( root , 18 ) ; let L = 7 , R = 15 ; document . write ( rangeSumBST ( root , L , R ) ) ;
Convert an array to reduced form | Set 1 ( Simple and Hashing ) | Javascript Program to convert an Array to reduced form ; Create a temp array and copy contents of arr [ ] to temp ; Sort temp array ; Create a hash table . ; One by one insert elements of sorted temp [ ] and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver program
function convert ( arr , n ) { let temp = [ ... arr ] ; temp . sort ( ( a , b ) => a - b ) ; let umap = new Map ( ) ; let val = 0 ; for ( let i = 0 ; i < n ; i ++ ) umap . set ( temp [ i ] , val ++ ) ; for ( let i = 0 ; i < n ; i ++ ) arr [ i ] = umap . get ( arr [ i ] ) ; } function prletArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 10 , 20 , 15 , 12 , 11 , 50 ] ; let n = arr . length ; document . write ( " " + " " ) ; prletArr ( arr , n ) ; convert ( arr , n ) ; document . write ( " " + " " + " " ) ; prletArr ( arr , n ) ;
Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix | Function to count number of substrings that starts with string S1 and ends with string S2 ; Stores the length of each string ; Stores the count of prefixes as S1 and suffixes as S2 ; Traverse string S ; Find the prefix at index i ; Find the suffix at index i ; If the prefix is S1 ; If the suffix is S2 ; Return the count of substrings ; Function to check if the number of substrings starts with S1 and ends with S2 and vice - versa is same or not ; Count the number of substrings ; Print the result ; Driver Code
function countSubstrings ( S , S1 , S2 ) { let N = S . length ; let N1 = S1 . length ; let N2 = S2 . length ; let count = 0 , totalcount = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let prefix = S . substr ( i , N1 ) ; let suffix = S . substr ( i , N2 ) ; if ( S1 == prefix ) count ++ ; if ( S2 == suffix ) totalcount += count ; } return totalcount ; } function checkSubstrings ( S , S1 , S2 ) { let x = countSubstrings ( S , S1 , S2 ) ; let y = countSubstrings ( S , S2 , S1 ) ; if ( x == y ) document . write ( " " ) ; else document . write ( " " ) ; } let S = " " ; let S1 = " " ; let S2 = " " ; checkSubstrings ( S , S1 , S2 ) ;
Return maximum occurring character in an input string | Javascript program to output the maximum occurring character in a string ; Create array to keep the count of individual characters and initialize the array as 0 ; Construct character count array from the input string . ; Initialize max count ; Initialize result ; Traversing through the string and maintaining the count of each character ; Driver Method
let ASCII_SIZE = 256 ; function getMaxOccuringChar ( str ) { let count = new Array ( ASCII_SIZE ) ; for ( let i = 0 ; i < ASCII_SIZE ; i ++ ) { count [ i ] = 0 ; } let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { count [ str [ i ] . charCodeAt ( 0 ) ] += 1 ; } let max = - 1 ; let result = ' ' ; for ( let i = 0 ; i < len ; i ++ ) { if ( max < count [ str [ i ] . charCodeAt ( 0 ) ] ) { max = count [ str [ i ] . charCodeAt ( 0 ) ] ; result = str [ i ] ; } } return result ; } let str = " " ; document . write ( " " , getMaxOccuringChar ( str ) ) ;
Queries to count sum of rows and columns of a Matrix present in given ranges | Function to preprocess the matrix to execute the queries ; Stores the sum of each row ; Stores the sum of each col ; Traverse the matrix and calculate sum of each row and column ; Insert all row sums in sum_list ; Insert all column sums in sum_list ; Sort the array in ascending order ; Traverse the array queries [ ] [ ] ; Search the leftmost index of L ; Search the rightmost index of R ; Function to search for the leftmost index of given number ; Initialize low , high and ans ; Stores mid ; If A [ mid ] >= num ; Function to search for the rightmost index of given number ; Initialise low , high and ans ; Stores mid ; If A [ mid ] <= num ; Update ans ; Update mid ; Update high ; Given dimensions of matrix ; Given matrix ; Given number of queries ; Given queries ; Function call to count the number row - sums and column - sums present in the given ranges
function totalCount ( A , N , M , queries , Q ) { let row_sum = new Array ( N ) ; let col_sum = new Array ( M ) ; for ( let i = 0 ; i < N ; i ++ ) row_sum [ i ] = 0 ; for ( let j = 0 ; j < M ; j ++ ) col_sum [ j ] = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { row_sum [ i ] += A [ i ] [ j ] ; col_sum [ j ] += A [ i ] [ j ] ; } } let sum_list = [ ] ; for ( let i = 0 ; i < N ; i ++ ) sum_list . push ( row_sum [ i ] ) ; for ( let i = 0 ; i < M ; i ++ ) sum_list . push ( col_sum [ i ] ) ; ( sum_list ) . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < Q ; i ++ ) { let L = queries [ i ] [ 0 ] ; let R = queries [ i ] [ 1 ] ; let l = left_search ( sum_list , L ) ; let r = right_search ( sum_list , R ) ; document . write ( r - l + 1 + " " ) ; } } function left_search ( A , num ) { let low = 0 , high = A . length - 1 ; let ans = 0 ; while ( low <= high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( A [ mid ] >= num ) { ans = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } return ans ; } function right_search ( A , num ) { let low = 0 , high = A . length - 1 ; let ans = high ; while ( low <= high ) { let mid = low + Math . floor ( ( high - low ) / 2 ) ; if ( A [ mid ] <= num ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } let N = 3 , M = 2 ; let A = [ [ 13 , 3 ] , [ 9 , 4 ] , [ 6 , 10 ] ] ; let Q = 2 ; let queries = [ [ 10 , 20 ] , [ 25 , 35 ] ] ; totalCount ( A , N , M , queries , Q ) ;
Maximize profit that can be earned by selling an item among N buyers | Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver Code
function maximumProfit ( arr , n ) { var ans = - 100000 ; ; var price = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } arr = [ 22 , 87 , 9 , 50 , 56 , 43 ] ; document . write ( maximumProfit ( arr , 6 ) ) ;
Lexicographically smallest permutation of the array possible by at most one swap | Function to print the elements of the array arr [ ] ; Traverse the array arr [ ] ; Function to convert given array to lexicographically smallest permutation possible by swapping at most one pair ; Stores the index of the first element which is not at its correct position ; Checks if any such array element exists or not ; Traverse the given array ; If element is found at i ; If the first array is not in correct position ; Store the index of the first elements ; Store the index of the first element ; Swap the pairs ; Print the array ; Given array ; Store the size of the array
function print ( arr , N ) { for ( i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function makeLexicographically ( arr , N ) { var index = 0 ; var temp = 0 ; var check = 0 ; var condition = 0 ; var element = 0 ; for ( i = 0 ; i < N ; ++ i ) { if ( element == arr [ i ] ) { check = i ; break ; } else if ( arr [ i ] != i + 1 && check == 0 ) { index = i ; check = 1 ; condition = - 1 ; element = i + 1 ; } } if ( condition == - 1 ) { temp = arr [ index ] ; arr [ index ] = arr [ check ] ; arr [ check ] = temp ; } print ( arr , N ) ; } var arr = [ 1 , 2 , 3 , 4 ] var N = arr . length ; makeLexicographically ( arr , N ) ;