codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Numbers of cells that are connected with the smallest path between 3 given cells | Function to return the minimum cells that are connected via the minimum length path ; Array to store column number of the given cells ; Sort cells in ascending order of row number ; Middle row number ; Set pair to store required cells ; Range of column number ; Store all cells of middle row within column number range ; Final step to store all the column number of 1 st and 3 rd cell upto MidRow ; vector pair to store X , Y , Z
function Minimum_Cells ( v ) { let col = new Array ( 3 ) ; let i , j ; for ( i = 0 ; i < 3 ; i ++ ) { let column_number = v [ i ] [ 1 ] ; col [ i ] = column_number ; } col . sort ( function ( a , b ) { return a - b } ) ; v . sort ( ) ; let MidRow = v [ 1 ] [ 0 ] ; let s = new Set ( ) ; let Maxcol = col [ 2 ] , MinCol = col [ 0 ] ; for ( i = MinCol ; i <= Maxcol ; i ++ ) { s . add ( [ MidRow , i ] ) ; } for ( i = 0 ; i < 3 ; i ++ ) { if ( v [ i ] [ 0 ] == MidRow ) continue ; for ( j = Math . min ( v [ i ] [ 0 ] , MidRow ) ; j <= Math . max ( v [ i ] [ 0 ] , MidRow ) ; j ++ ) { s . add ( [ j , v [ i ] [ 0 ] ] ) ; } } return s . size - 2 ; } let v = [ ] ; v . push ( [ 0 , 0 ] ) ; v . push ( [ 1 , 1 ] ) ; v . push ( [ 2 , 2 ] ) ; document . write ( Minimum_Cells ( v ) ) ;
Get maximum items when other items of total cost of an item are free | Function to count the total number of items ; Sort the prices ; Choose the last element ; Initial count of item ; Sum to keep track of the total price of free items ; If total is less than or equal to z then we will add 1 to the answer ; Driver code
function items ( n , a ) { a . sort ( function ( a , b ) { return a - b } ) ; let z = a [ n - 1 ] ; let x = 1 ; let s = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { s += a [ i ] ; if ( s <= z ) x += 1 ; else break ; } return x ; } let n = 5 ; let a = [ 5 , 3 , 1 , 5 , 6 ] ; document . write ( items ( n , a ) ) ;
Sorting array elements with set bits equal to K | Utility function ; Function to sort elements with set bits equal to k ; initialise two vectors ; first vector contains indices of required element ; second vector contains required elements ; sorting the elements in second vector ; replacing the elements with k set bits with the sorted elements ; printing the new sorted array elements ; Driver code
function bitCount ( x ) { var setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } function sortWithSetbits ( arr , n , k ) { var v1 = [ ] , v2 = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( bitCount ( arr [ i ] ) == k ) { v1 . push ( i ) ; v2 . push ( arr [ i ] ) ; } } v2 . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < v1 . length ; i ++ ) arr [ v1 [ i ] ] = v2 [ i ] ; for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ 14 , 255 , 1 , 7 , 13 ] ; var n = arr . length ; var k = 3 ; sortWithSetbits ( arr , n , k ) ;
Minimum boxes required to carry all gifts | Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver code
function numBoxes ( A , n , K ) { A . sort ( function ( a , b ) { return a - b } ) ; let i = 0 , j = ( n - 1 ) ; let ans = 0 ; while ( i <= j ) { ans ++ ; if ( A [ i ] + A [ j ] <= K ) i ++ ; j -- ; } return ans ; } let A = [ 3 , 2 , 2 , 1 ] ; let K = 3 ; let n = A . length ; document . write ( numBoxes ( A , n , K ) ) ;
Count triplets in a sorted doubly linked list whose product is equal to a given value x | JavaScript implementation to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; Structure of node of doubly linked list ; Function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; Unordered_map ' um ' implemented as hash table ; Insert the < node data , node pointer > tuple in ' um ' ; Generate all possible pairs ; p_product = product of elements in the current pair ; If ' x / p _ product ' is present in ' um ' and either of the two nodes are not equal to the ' um [ x / p _ product ] ' node ; Increment count ; Required count of triplets division by 3 as each triplet is counted 3 times ; A utility function to insert a new node at the beginning of doubly linked list ; Allocate node ; Put in the data ; Start with an empty doubly linked list ; Insert values in sorted order
class Node { constructor ( data ) { this . data = data ; this . next = this . prev = null ; } } class Node { constructor ( data ) { this . data = data ; this . next = this . prev = null ; } } function countTriplets ( head , x ) { let ptr , ptr1 , ptr2 ; let count = 0 ; let um = new Map ( ) ; for ( ptr = head ; ptr != null ; ptr = ptr . next ) { um . set ( ptr . data , ptr ) ; } for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) { for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) { let p_product = ( ptr1 . data * ptr2 . data ) ; if ( um . has ( x / p_product ) && um . get ( x / p_product ) != ptr1 && um . get ( x / p_product ) != ptr2 ) { count ++ ; } } } return ( count / 3 ) ; } function insert ( head , data ) { let temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( head == null ) { head = temp ; } else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } let head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; let x = 8 ; document . write ( " " + countTriplets ( head , x ) ) ;
Maximize the profit by selling at | Function to find profit ; Calculating profit for each gadget ; Sort the profit array in descending order ; Variable to calculate total profit ; Check for best M profits ; Driver Code
function solve ( N , M , cp , sp ) { let profit = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) profit [ i ] = sp [ i ] - cp [ i ] ; profit . sort ( function ( a , b ) { return b - a ; } ) ; let sum = 0 ; for ( let i = 0 ; i < M ; i ++ ) { if ( profit [ i ] > 0 ) sum += profit [ i ] ; else break ; } return sum ; } let N = 5 , M = 3 ; let CP = [ 5 , 10 , 35 , 7 , 23 ] ; let SP = [ 11 , 10 , 0 , 9 , 19 ] ; document . write ( solve ( N , M , CP , SP ) ) ;
Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; sort the given array in descending order ; generate the number ; Driver code
function findMaxNum ( arr , n ) { arr . sort ( function ( a , b ) { return b - a ; } ) ; var num = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { num = num * 10 + arr [ i ] ; } return num ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 0 ] ; var n = arr . length ; document . write ( findMaxNum ( arr , n ) ) ;
Minimum partitions of maximum size 2 and sum limited by given value | Javascript program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code
function minimumSets ( arr , n , key ) { var i , j ; arr . sort ( ( a , b ) => a - b ) for ( i = 0 , j = n - 1 ; i <= j ; ++ i ) if ( arr [ i ] + arr [ j ] <= key ) j -- ; return i ; } var arr = [ 3 , 5 , 3 , 4 ] ; var n = arr . length ; var key = 5 ; document . write ( minimumSets ( arr , n , key ) ) ;
Bubble Sort On Doubly Linked List | structure of a node ; Function to insert a node at the beginning of a linked list ; Function to print nodes in a given linked list ; Bubble sort the given linked list ; Checking for empty list ; Driver code ; start with empty linked list ; Create linked list from the array arr [ ] . Created linked list will be 1 -> 11 -> 2 -> 56 -> 12 ; print list before sorting ; sort the linked list ; print list after sorting
class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; } } function insertAtTheBegin ( start_ref , data ) { var ptr1 = new Node ( ) ; ptr1 . data = data ; ptr1 . next = start_ref ; if ( start_ref != null ) start_ref . prev = ptr1 ; start_ref = ptr1 ; return start_ref ; } function printList ( start ) { var temp = start ; document . write ( " " ) ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } function bubbleSort ( start ) { var swapped ; var ptr1 ; var lptr = null ; if ( start == null ) return null ; do { swapped = 0 ; ptr1 = start ; while ( ptr1 . next != lptr ) { if ( ptr1 . data > ptr1 . next . data ) { var t = ptr1 . data ; ptr1 . data = ptr1 . next . data ; ptr1 . next . data = t ; swapped = 1 ; } ptr1 = ptr1 . next ; } lptr = ptr1 ; } while ( swapped != 0 ) ; return start ; } var arr = [ 12 , 56 , 2 , 11 , 1 , 90 ] ; var i ; var start = null ; for ( i = 0 ; i < 6 ; i ++ ) start = insertAtTheBegin ( start , arr [ i ] ) ; document . write ( " " ) ; printList ( start ) ; start = bubbleSort ( start ) ; document . write ( " " ) ; printList ( start ) ;
Substring Sort | Alternative code to sort substrings ; sort the input array ; repeated length should be the same string ; two different strings with the same length input array cannot be sorted ; validate that each string is a substring of the following one ; get first element ; The array is valid and sorted print the strings in order ; Test 1 ; Test 2
function substringSort ( arr , n , maxLen ) { let count = new Array ( maxLen ) ; let sortedArr = new Array ( maxLen ) ; count . fill ( 0 ) ; for ( let i = 0 ; i < maxLen ; i ++ ) { sortedArr [ i ] = " " ; } for ( let i = 0 ; i < n ; i ++ ) { let s = arr [ i ] ; let len = s . length ; if ( count [ len - 1 ] == 0 ) { sortedArr [ len - 1 ] = s ; count [ len - 1 ] = 1 ; } else if ( sortedArr [ len - 1 ] == s ) { count [ len - 1 ] ++ ; } else { document . write ( " " + " " ) ; return ; } } let index = 0 ; while ( count [ index ] == 0 ) index ++ ; let prev = index ; let prevString = sortedArr [ prev ] ; index ++ ; for ( ; index < maxLen ; index ++ ) { if ( count [ index ] != 0 ) { let current = sortedArr [ index ] ; if ( current . includes ( prevString ) ) { prev = index ; prevString = current ; } else { document . write ( " " ) ; return ; } } } for ( let i = 0 ; i < maxLen ; i ++ ) { let s = sortedArr [ i ] ; for ( let j = 0 ; j < count [ i ] ; j ++ ) { document . write ( s + " " ) ; } } } let maxLen = 100 ; let arr1 = [ " " , " " , " " , " " , " " , " " ] ; substringSort ( arr1 , arr1 . length , maxLen ) ; let arr2 = [ " " , " " ] ; substringSort ( arr2 , arr2 . length , maxLen ) ;
Print all the pairs that contains the positive and negative values of an element | Function ; Sort the array ; Traverse the array ; For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0. ; If found , print the pair . ; Driver code
function printPairs ( arr , n ) { let pair_exists = false ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr . includes ( - arr [ i ] ) ) { document . write ( arr [ i ] + " " + - arr [ i ] + " " ) ; pair_exists = true ; } } else break ; } if ( pair_exists == false ) document . write ( " " ) ; } let arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] ; let n = arr . length ; printPairs ( arr , n ) ;
Multiset Equivalence Problem | JavaScript program to check if two given multisets are equivalent ; sort the elements of both multisets ; Return true if both multisets are same . ; Driver code
function areSame ( a , b ) { a . sort ( ( a , b ) => a - b ) ; b . sort ( ( a , b ) => a - b ) ; return ( a == b ) ; } let a = [ 7 , 7 , 5 ] , b = [ 7 , 5 , 5 ] ; if ( areSame ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum swaps so that binary search can be applied | Function to find minimum swaps . ; Here we are getting number of smaller and greater elements of k . ; Here we are calculating the actual position of k in the array . ; Implementing binary search as per the above - discussed cases . ; If we find the k . ; If we need minimum element swap . ; Else the element is at the right position . ; If we need maximum element swap . ; Else element is at the right position ; Calculating the required swaps . ; If it is impossible . ; Driver function
function findMinimumSwaps ( arr , n , k ) { let pos , num_min , num_max , need_minimum , need_maximum , swaps ; num_min = num_max = need_minimum = 0 ; need_maximum = swaps = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < k ) num_min ++ ; else if ( arr [ i ] > k ) num_max ++ ; } for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == k ) { pos = i ; break ; } } let left , right , mid ; left = 0 ; right = n - 1 ; while ( left <= right ) { mid = parseInt ( ( left + right ) / 2 , 10 ) ; if ( arr [ mid ] == k ) { break ; } else if ( arr [ mid ] > k ) { if ( pos > mid ) need_minimum ++ ; else num_min -- ; left = mid + 1 ; } else { if ( pos < mid ) need_maximum ++ ; else num_max -- ; right = mid - 1 ; } } if ( need_minimum > need_maximum ) { swaps = swaps + need_maximum ; num_min = num_min - need_maximum ; need_minimum = need_minimum - need_maximum ; need_maximum = 0 ; } else { swaps = swaps + need_minimum ; num_max = num_max - need_minimum ; need_maximum = need_maximum - need_minimum ; need_minimum = 0 ; } if ( need_maximum > num_max need_minimum > num_min ) return - 1 ; else return ( swaps + need_maximum + need_minimum ) ; } let arr = [ 3 , 10 , 6 , 7 , 2 , 5 , 4 ] , k = 4 ; let n = arr . length ; document . write ( findMinimumSwaps ( arr , n , k ) ) ;
Stable sort for descending order | javascript program to demonstrate descending order ; Driver code
function reverse ( a ) { var i , k , n = a . length ; var t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } var arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] ; var n = arr . length ; arr . sort ( ) ; reverse ( arr ) ; document . write ( " " + " " ) ; for ( var i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] + " " ) ; }
Check if both halves of the string have at least one different character | Function which break string into two halves Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver code
function Function ( str ) { let l = str . length ; str = ( str . slice ( 0 , Math . floor ( l / 2 ) ) . sort ( ) ) . concat ( str . slice ( Math . floor ( l / 2 ) , l ) . sort ( ) ) ; for ( let i = 0 ; i < Math . floor ( l / 2 ) ; i ++ ) if ( str [ i ] != str [ Math . floor ( l / 2 ) + i ] ) return true ; return false ; } let str = " " . split ( " " ) ; if ( Function ( str ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Example input = [ 0 , 1 , 2 , 2 , 0 , 0 ] output = [ 0 , 0 , 0 , 1 , 2 , 2 ]
let inputArray = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] ; let outputArray = [ ] ; let indexOfOne = 0 ; function print ( ) { for ( let item of inputArray . values ( ) ) if ( item == 2 ) outputArray . push ( item ) ; else if ( item == 1 ) { outputArray . splice ( indexOfOne , 0 , item ) ; indexOfOne += 1 ; } else if ( item == 0 ) { outputArray . splice ( 0 , 0 , item ) ; indexOfOne += 1 ; } else { document . write ( " " ) ; continue ; } } print ( ) ; for ( let item of outputArray . values ( ) ) document . write ( item + " " ) ;
Number of visible boxes after putting one inside another | return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; driver Program
function minimumBox ( arr , n ) { var q = [ ] ; arr . sort ( ( a , b ) => a - b ) q . push ( arr [ 0 ] ) ; for ( var i = 1 ; i < n ; i ++ ) { var now = q [ 0 ] ; if ( arr [ i ] >= 2 * now ) q . pop ( 0 ) ; q . push ( arr [ i ] ) ; } return q . length ; } var arr = [ 4 , 1 , 2 , 8 ] ; var n = arr . length ; document . write ( minimumBox ( arr , n ) ) ;
Sort a binary array using one traversal | Javascript Code for Sort a binary array using one traversal ; if number is smaller than 1 then swap it with j - th number ; driver function
function sortBinaryArray ( a , n ) { let j = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 ) { j ++ ; let temp = a [ j ] ; a [ j ] = a [ i ] ; a [ i ] = temp ; } } } let a = [ 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 ] ; let n = a . length ; sortBinaryArray ( a , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ;
Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code
function findDuplicate ( arr , n , k ) { arr . sort ( ) ; let i = 0 ; while ( i < n ) { let j , count = 1 ; for ( j = i + 1 ; j < n && arr [ j ] == arr [ i ] ; j ++ ) count ++ ; if ( count == k ) return arr [ i ] ; i = j ; } return - 1 ; } let arr = [ 2 , 2 , 1 , 3 , 1 ] ; let k = 2 ; let n = arr . length ; document . write ( findDuplicate ( arr , n , k ) ) ;
Longest Common Prefix using Sorting | Javascript program to find longest common prefix of given array of words . ; if size is 0 , return empty string ; sort the array of strings ; find the minimum length from first and last string ; find the common prefix between the first and last string ; Driver Function to test other function
function longestCommonPrefix ( a ) { let size = a . length ; if ( size == 0 ) return " " ; if ( size == 1 ) return a [ 0 ] ; a . sort ( ) ; let end = Math . min ( a [ 0 ] . length , a [ size - 1 ] . length ) ; let i = 0 ; while ( i < end && a [ 0 ] [ i ] == a [ size - 1 ] [ i ] ) i ++ ; let pre = a [ 0 ] . substring ( 0 , i ) ; return pre ; } let input = [ " " , " " , " " , " " ] ; document . write ( " " + longestCommonPrefix ( input ) ) ;
Find the minimum and maximum amount to buy all N candies | function to find the maximum and the minimum cost required ; Sort the array ; print the minimum cost ; print the maximum cost ; Driver Code ; Function call
function find ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let b = Math . floor ( Math . ceil ( n / k ) ) ; let min_sum = 0 , max_sum = 0 ; for ( let i = 0 ; i < b ; i ++ ) min_sum += arr [ i ] ; for ( let i = 2 ; i < arr . length ; i ++ ) max_sum += arr [ i ] ; document . write ( " " + min_sum + " " ) ; document . write ( " " + max_sum + " " ) ; } let arr = [ 3 , 2 , 1 , 4 ] ; let n = arr . length ; let k = 2 ; find ( arr , n , k ) ;
Check if it is possible to sort an array with conditional swapping of adjacent allowed | Returns true if it is possible to sort else false ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver Code
function checkForSorting ( arr , n ) { let temp = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) { temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } else return false ; } } return true ; } let arr = new Array ( 1 , 0 , 3 , 2 ) ; let n = arr . length ; if ( checkForSorting ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sort an array of strings according to string lengths | Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Function to print the sorted array of string ; Function to perform sorting ; Calling the function to print result
function sort ( s , n ) { for ( let i = 1 ; i < n ; i ++ ) { let temp = s [ i ] ; let j = i - 1 ; while ( j >= 0 && temp . length < s [ j ] . length ) { s [ j + 1 ] = s [ j ] ; j -- ; } s [ j + 1 ] = temp ; } } function printArraystring ( str , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( str [ i ] + " " ) ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; sort ( arr , n ) ; printArraystring ( arr , n ) ;
Hoare 's vs Lomuto partition scheme in QuickSort | This function takes first element as pivot , and places all the elements smaller than the pivot on the left side and all the elements greater than the pivot on the right side . It returns the index of the last element on the smaller side ; Find leftmost element greater than or equal to pivot ; Find rightmost element smaller than or equal to pivot ; If two pointers met . ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print an array ; Driver Code
function partition ( arr , low , high ) { let pivot = arr [ low ] ; let i = low - 1 , j = high + 1 ; while ( true ) { do { i ++ ; } while ( arr [ i ] < pivot ) ; do { j -- ; } while ( arr [ j ] > pivot ) ; if ( i >= j ) return j ; let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } function quickSort ( arr , low , high ) { if ( low < high ) { let pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi ) ; quickSort ( arr , pi + 1 , high ) ; } } function printArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( " " + arr [ i ] ) ; document . write ( " " ) ; } let arr = [ 10 , 7 , 8 , 9 , 1 , 5 ] ; let n = arr . length ; quickSort ( arr , 0 , n - 1 ) ; document . write ( " " + " " ) ; printArray ( arr , n ) ;
K | Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest  element after removing  array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver code
function ksmallest ( arr , n , k ) { arr . sort ( function ( a , b ) { return a - b } ) ; if ( k < arr [ 0 ] ) return k ; if ( k == arr [ 0 ] ) return arr [ 0 ] + 1 ; if ( k > arr [ n - 1 ] ) return k + n ; if ( arr [ 0 ] == 1 ) k -- ; else k -= ( arr [ 0 ] - 1 ) ; for ( let i = 1 ; i < n ; i ++ ) { let c = arr [ i ] - arr [ i - 1 ] - 1 ; if ( k <= c ) return arr [ i - 1 ] + k ; else k -= c ; } return arr [ n - 1 ] + k ; } let k = 1 ; let arr = [ 1 ] ; let n = arr . length ; document . write ( ksmallest ( arr , n , k ) ) ;
Check whether a given number is even or odd | Returns true if n is even , else odd ; n & 1 is 1 , then odd , else even ; Driver code
function isEven ( n ) { return ( ! ( n & 1 ) ) ; } let n = 101 ; isEven ( n ) ? document . write ( " " ) : document . write ( " " ) ;