codeparrot/xlcost-text-to-code huggingface dataset:
Minimum product subset of an array | Javascript program to find maximum product of a subset . ; Find count of negative numbers , count of zeros , maximum valued negative number , minimum valued positive number and product of non - zero numbers ; If number is zero , count it but dont multiply ; Count the negative numbers and find the max negative number ; Find the minimum positive number ; If there are all zeroes or zero is present but no negative number is present ; If there are all positive ; If there are even number except zero of negative numbers ; Otherwise result is product of all non - zeros divided by maximum valued negative . ; Driver code
function minProductSubset ( a , n ) { if ( n == 1 ) return a [ 0 ] ; let negmax = Number . MIN_VALUE ; let posmin = Number . MIN_VALUE ; let count_neg = 0 , count_zero = 0 ; let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ++ ; negmax = Math . max ( negmax , a [ i ] ) ; } if ( a [ i ] > 0 && a [ i ] < posmin ) { posmin = a [ i ] ; } product *= a [ i ] ; } if ( count_zero == n || ( count_neg == 0 && count_zero > 0 ) ) return 0 ; if ( count_neg == 0 ) return posmin ; if ( count_neg % 2 == 0 && count_neg != 0 ) { product = parseInt ( product / negmax , 10 ) ; } return product ; } let a = [ - 1 , - 1 , - 2 , 4 , 3 ] ; let n = 5 ; document . write ( minProductSubset ( a , n ) ) ;
Repeatedly search an element by doubling it after every successful search | JavaScript program to repeatedly search an element by doubling it after every successful search ; Sort the given array so that binary search can be applied on it ; Maximum array element ; Search for the element b present or not in array ; Driver code
function binary_search ( a , x , lo = 0 , hi = null ) { if ( hi == null ) hi = a . length ; while ( lo < hi ) { mid = Math . floor ( ( lo + hi ) / 2 ) ; midval = a [ mid ] ; if ( midval < x ) lo = mid + 1 ; else if ( midval > x ) hi = mid ; else return mid ; } return - 1 ; } function findElement ( a , n , b ) { a . sort ( ( a , b ) => a - b ) ; let max = a [ n - 1 ] ; while ( b < max ) { if ( binary_search ( a , a + n , b ) ) b *= 2 ; else return b ; } return b ; } let a = [ 1 , 2 , 3 ] ; let n = a . length ; let b = 1 ; document . write ( findElement ( a , n , b ) ) ;
Maximum sum of pairwise product in an array with negative allowed | JavaScript program for above implementation ; Function to find the maximum sum ; Sort the array first ; First multiply negative numbers pairwise and sum up from starting as to get maximum sum . ; Second multiply positive numbers pairwise and summed up from the last as to get maximum sum . ; To handle case if positive and negative numbers both are odd in counts . ; If one of them occurs odd times ; Driver code
let Mod = 1000000007 ; function findSum ( arr , n ) { let sum = 0 ; arr . sort ( ) ; let i = 0 ; while ( i < n && arr [ i ] < 0 ) { if ( i != n - 1 && arr [ i + 1 ] <= 0 ) { sum = ( sum + ( arr [ i ] * arr [ i + 1 ] ) % Mod ) % Mod ; i += 2 ; } else break ; } let j = n - 1 ; while ( j >= 0 && arr [ j ] > 0 ) { if ( j != 0 && arr [ j - 1 ] > 0 ) { sum = ( sum + ( arr [ j ] * arr [ j - 1 ] ) % Mod ) % Mod ; j -= 2 ; } else break ; } if ( j > i ) sum = ( sum + ( arr [ i ] * arr [ j ] ) % Mod ) % Mod ; else if ( i == j ) sum = ( sum + arr [ i ] ) % Mod ; return sum ; } let arr = [ - 1 , 9 , 4 , 5 , - 4 , 7 ] ; let n = arr . length ; document . write ( findSum ( arr , n ) ) ;
Search an element in a Linked List ( Iterative and Recursive ) | Node class ; Linked list class Head of list ; Inserts a new node at the front of the list ; Allocate new node and putting data ; Make next of new node as head ; Move the head to povar to new Node ; Checks whether the value x is present in linked list ; Base case ; If key is present in current node , return true ; Recur for remaining list ; * Use push ( ) to construct below list 14 -> 21 -> 11 -> 30 -> 10
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } var head ; function push ( new_data ) { var new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } function search ( head , x ) { if ( head == null ) return false ; if ( head . data == x ) return true ; return search ( head . next , x ) ; } push ( 10 ) ; push ( 30 ) ; push ( 11 ) ; push ( 21 ) ; push ( 14 ) ; if ( search ( head , 21 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Iteratively Reverse a linked list using only 2 pointers ( An Interesting Method ) | Link list node ; Function to reverse the linked list using 2 pointers ; At last prev points to new head ; This expression evaluates from left to right current . next = prev , changes the link fron next to prev node prev = current , moves prev to current node for next reversal of node This example of list will clear it more 1.2 . 3.4 initially prev = 1 , current = 2 Final expression will be current = 1 ^ 2 ^ 3 ^ 2 ^ 1 , as we know that bitwise XOR of two same numbers will always be 0 i . e ; 1 ^ 1 = 2 ^ 2 = 0 After the evaluation of expression current = 3 that means it has been moved by one node from its previous position ; Function to push a node ; Allocate node ; Put in the data ; Link the old list off the new node ; Move the head to povar to the new node ; Function to prvar linked list ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var head_ref = null ; function reverse ( ) { var prev = null ; var current = head_ref ; while ( current != null ) { var next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; } function push ( new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; } function printList ( ) { var temp = head_ref ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } push ( 20 ) ; push ( 4 ) ; push ( 15 ) ; push ( 85 ) ; document . write ( " " ) ; printList ( ) ; reverse ( ) ; document . write ( " " ) ; printList ( ) ;
Delete alternate nodes of a Linked List | deletes alternate nodes of a list starting with head ; Change the next link of head ; Recursively call for the new next of head
function deleteAlt ( head ) { if ( head == null ) return ; var node = head . next ; if ( node == null ) return ; head . next = node . next ; head . next = deleteAlt ( head . next ) ; }
Alternating split of a given Singly Linked List | Set 1 |  ; points to the last node in ' a ' ; points to the last node in ' b ' ; add at ' a ' tail ; advance the ' a ' tail
function AlternatingSplit ( source , aRef , bRef ) { var aDummy = new Node ( ) ; var aTail = aDummy ; var bDummy = new Node ( ) ; var bTail = bDummy ; var current = source ; aDummy . next = null ; bDummy . next = null ; while ( current != null ) { MoveNode ( ( aTail . next ) , current ) ; aTail = aTail . next ; if ( current != null ) { MoveNode ( ( bTail . next ) , current ) ; bTail = bTail . next ; } } aRef = aDummy . next ; bRef = bDummy . next ; }
Identical Linked Lists | A recursive javascript method to check if two linked lists are identical or not ; If both lists are empty ; If both lists are not empty , then data of current nodes must match , and same should be recursively true for rest of the nodes . ; If we reach here , then one of the lists is empty and other is not ; Returns true if linked lists a and b are identical , otherwise false
function areIdenticalRecur ( a , b ) { if ( a == null && b == null ) return true ; if ( a != null && b != null ) return ( a . data == b . data ) && areIdenticalRecur ( a . next , b . next ) ; return false ; } function areIdentical ( listb ) { return areIdenticalRecur ( this . head , listb . head ) ; }
Rotate a Linked List | Link list node ; This function rotates a linked list counter - clockwise and updates the head . The function assumes that k is smaller than size of linked list . ; Let us understand the below code for example k = 4 and list = 10.20 . 30.40 .50 . 60. ; Traverse till the end . ; traverse the linked list to k - 1 position which will be last element for rotated array . ; update the head_ref and last element pointer to null ; Function to push a node ; allocate node ; put in the data ; link the old list off the new node ; move the head to povar to the new node ; Function to prvar linked list ; create a list 10.20 . 30.40 .50 . 60
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var head = null ; function rotate ( k ) { if ( k == 0 ) return ; var current = head ; while ( current . next != null ) current = current . next ; current . next = head ; current = head ; for ( i = 0 ; i < k - 1 ; i ++ ) current = current . next ; head = current . next ; current . next = null ; } function push ( new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } } for ( i = 60 ; i > 0 ; i -= 10 ) push ( i ) ; document . write ( " " ) ; printList ( head ) ; rotate ( 4 ) ; document . write ( " " ) ; printList ( head ) ;
Flattening a Linked List | head of list ; Linked list Node ; An utility function to merge two sorted linked lists ; if first linked list is empty then second is the answer ; if second linked list is empty then first is the result ; compare the data members of the two linked lists and put the larger one in the result ; Base Cases ; recur for list on right ; now merge ; return the root it will be in turn merged with its left ; * Utility function to insert a node at beginning of the linked list ; * 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to povar to new Node ; 5. return to link it back ; * Let us create the following linked list 5 -> 10 -> 19 -> 28 | | | | V V V V 7 * 20 22 35 | | | V V V 8 50 40 | | V V 30 45 ; flatten the list
var head ; class Node { constructor ( val ) { this . data = val ; this . down = null ; this . next = null ; } } function merge ( a , b ) { if ( a == null ) return b ; if ( b == null ) return a ; var result ; if ( a . data < b . data ) { result = a ; result . down = merge ( a . down , b ) ; } else { result = b ; result . down = merge ( a , b . down ) ; } result . right = null ; return result ; } function flatten ( root ) { if ( root == null root . right == null ) return root ; root . right = flatten ( root . right ) ; root = merge ( root , root . right ) ; return root ; } function push ( head_ref , data ) { var new_node = new Node ( data ) ; new_node . down = head_ref ; head_ref = new_node ; return head_ref ; } function printList ( ) { var temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . down ; } document . write ( ) ; } head = push ( head , 30 ) ; head = push ( head , 8 ) ; head = push ( head , 7 ) ; head = push ( head , 5 ) ; head . right = push ( head . right , 20 ) ; head . right = push ( head . right , 10 ) ; head . right . right = push ( head . right . right , 50 ) ; head . right . right = push ( head . right . right , 22 ) ; head . right . right = push ( head . right . right , 19 ) ; head . right . right . right = push ( head . right . right . right , 45 ) ; head . right . right . right = push ( head . right . right . right , 40 ) ; head . right . right . right = push ( head . right . right . right , 35 ) ; head . right . right . right = push ( head . right . right . right , 20 ) ; head = flatten ( head ) ; printList ( ) ;
Sort a linked list of 0 s , 1 s and 2 s | head of list ; Linked list Node ; initialise count of 0 1 and 2 as 0 ; count total number of '0' , '1' and '2' count [ 0 ] will store total number of '0' s count [ 1 ] will store total number of '1' s count [ 2 ] will store total number of '2' s ; Let say count [ 0 ] = n1 , count [ 1 ] = n2 and count [ 2 ] = n3 now start traversing list from head node , 1 ) fill the list with 0 , till n1 > 0 2 ) fill the list with 1 , till n2 > 0 3 ) fill the list with 2 , till n3 > 0 ; Inserts a new Node at front of the list . ; * 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Function to print linked list ; Constructed Linked List is 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 8 -> 9 -> null
var head ; class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function sortList ( ) { var count = [ 0 , 0 , 0 ] ; var ptr = head ; while ( ptr != null ) { count [ ptr . data ] ++ ; ptr = ptr . next ; } var i = 0 ; ptr = head ; while ( ptr != null ) { if ( count [ i ] == 0 ) i ++ ; else { ptr . data = i ; -- count [ i ] ; ptr = ptr . next ; } } } function push ( new_data ) { var new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } function printList ( ) { var temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( " " ) ; } push ( 0 ) ; push ( 1 ) ; push ( 0 ) ; push ( 2 ) ; push ( 1 ) ; push ( 1 ) ; push ( 2 ) ; push ( 1 ) ; push ( 2 ) ; document . write ( " " ) ; printList ( ) ; sortList ( ) ; document . write ( " " ) ; printList ( ) ;
Flatten a multilevel linked list |
class List { constructor ( ) { this . data = 0 ; this . next = null ; this . child = null ; } }
Rearrange a linked list such that all even and odd positioned nodes are together | Linked List Node ; A utility function to create a new node ; Rearranges given linked list such that all even positioned nodes are before odd positioned . Returns new head of linked List . ; Corner case ; Initialize first nodes of even and odd lists ; Remember the first node of even list so that we can connect the even list at the end of odd list . ; If there are no more nodes , then connect first node of even list to the last node of odd list ; Connecting odd nodes ; If there are NO more even nodes after current odd . ; Connecting even nodes ; A utility function to prvar a linked list ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( key ) { var temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } function rearrangeEvenOdd ( head ) { if ( head == null ) return null ; var odd = head ; var even = head . next ; var evenFirst = even ; while ( 1 == 1 ) { if ( odd == null || even == null || ( even . next ) == null ) { odd . next = evenFirst ; break ; } odd . next = even . next ; odd = even . next ; if ( odd . next == null ) { even . next = null ; odd . next = evenFirst ; break ; } even . next = odd . next ; even = odd . next ; } return head ; } function printlist ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } document . write ( " " ) ; } var head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; document . write ( " " ) ; printlist ( head ) ; head = rearrangeEvenOdd ( head ) ; document . write ( " " ) ; printlist ( head ) ;
Rearrange a Linked List in Zig | Node class ; Prvar Linked List ; Swap both nodes ; Rearrange the linked list in zig zag way ; Driver Code ; 0 means the current element should be smaller than the next
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } var head ; function printLL ( ) { var t = head ; while ( t != null ) { document . write ( t . data + " " ) ; t = t . next ; } document . write ( ) ; } function swap ( a , b ) { if ( a == null b == null ) return ; var temp = a . data ; a . data = b . data ; b . data = temp ; } function zigZag ( node , flag ) { if ( node == null node . next == null ) { return node ; } if ( flag == 0 ) { if ( node . data > node . next . data ) { swap ( node , node . next ) ; } return zigZag ( node . next , 1 ) ; } else { if ( node . data < node . next . data ) { swap ( node , node . next ) ; } return zigZag ( node . next , 0 ) ; } } head = new Node ( 11 ) ; head . next = new Node ( 15 ) ; head . next . next = new Node ( 20 ) ; head . next . next . next = new Node ( 5 ) ; head . next . next . next . next = new Node ( 10 ) ; printLL ( ) ; var flag = 0 ; zigZag ( head , flag ) ; document . write ( " " ) ; printLL ( ) ;
Add 1 to a number represented as linked list | Linked list node ; Function to create a new node with given data ; Recursively add 1 from end to beginning and returns carry after all nodes are processed . ; If linked list is empty , then return carry ; Add carry returned be next node call ; Update data and return new carry ; This function mainly uses addWithCarry ( ) . ; Add 1 to linked list from end to beginning ; If there is carry after processing all nodes , then we need to add a new node to linked list ; New node becomes head now ; A utility function to print a linked list ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function newNode ( data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } function addWithCarry ( head ) { if ( head == null ) return 1 ; var res = head . data + addWithCarry ( head . next ) ; head . data = res % 10 ; return parseInt ( res / 10 ) ; } function addOne ( head ) { var carry = addWithCarry ( head ) ; var newNodes = null ; if ( carry > 0 ) { newNodes = newNode ( carry ) ; newNodes . next = head ; return newNodes ; } return head ; } function printList ( node ) { while ( node != null ) { document . write ( node . data ) ; node = node . next ; } document . write ( " " ) ; } var head = newNode ( 1 ) ; head . next = newNode ( 9 ) ; head . next . next = newNode ( 9 ) ; head . next . next . next = newNode ( 9 ) ; document . write ( " " ) ; printList ( head ) ; head = addOne ( head ) ; document . write ( " " ) ; document . write ( " " ) ; printList ( head ) ;
Point arbit pointer to greatest value right side node in a linked list | Link list node ; Function to reverse the linked list ; This function populates arbit pointer in every node to the greatest value to its right . ; Reverse given linked list ; Initialize pointer to maximum value node ; Traverse the reversed list ; Connect max through arbit pointer ; Update max if required ; Move ahead in reversed list ; Reverse modified linked list and return head . ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code
class Node { constructor ( val ) { this . data = val ; this . arbit = null ; this . next = null ; } } function reverse ( head ) { var prev = null , current = head , next = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } function populateArbit ( head ) { head = reverse ( head ) ; var max = head ; var temp = head . next ; while ( temp != null ) { temp . arbit = max ; if ( max . data < temp . data ) max = temp ; temp = temp . next ; } return reverse ( head ) ; } function printNextArbitPointers ( node ) { document . write ( " " ) ; while ( node != null ) { document . write ( node . data + " " ) ; if ( node . next != null ) document . write ( node . next . data + " " ) ; else document . write ( " " + " " ) ; if ( node . arbit != null ) document . write ( node . arbit . data ) ; else document . write ( " " ) ; document . write ( " " ) ; node = node . next ; } } function newNode ( data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } var head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; head = populateArbit ( head ) ; document . write ( " " ) ; printNextArbitPointers ( head ) ;
Point arbit pointer to greatest value right side node in a linked list | Link list node ; This function populates arbit pointer in every node to the greatest value to its right . ; if head is null simply return the list ; if head -> next is null it means we reached at the last node just update the max and maxNode ; Calling the populateArbit to the next node ; updating the arbit node of the current node with the maximum value on the right side ; if current Node value id greater then the previous right node then update it ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . arbit = null ; this . next = null ; } } var maxNode ; function populateArbit ( head ) { if ( head == null ) return ; if ( head . next == null ) { maxNode = head ; return ; } populateArbit ( head . next ) ; head . arbit = maxNode ; if ( head . data > maxNode . data ) maxNode = head ; return ; } function printNextArbitPointers ( node ) { document . write ( " " ) ; while ( node != null ) { document . write ( node . data + " " ) ; if ( node . next != null ) document . write ( node . next . data + " " ) ; else document . write ( " " + " " ) ; if ( node . arbit != null ) document . write ( node . arbit . data ) ; else document . write ( " " ) ; document . write ( " " ) ; node = node . next ; } } function newNode ( data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } var head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; populateArbit ( head ) ; document . write ( " " ) ; printNextArbitPointers ( head ) ;
Delete last occurrence of an item from linked list | A linked list Node ; Function to delete the last occurrence ; If found key , update ; If the last occurrence is the last node ; If it is not the last node ; Utility function to create a new node with given key ; This function prints contents of linked list starting from the given Node ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function deleteLast ( head , x ) { var temp = head , ptr = null ; while ( temp != null ) { if ( temp . data == x ) ptr = temp ; temp = temp . next ; } if ( ptr != null && ptr . next == null ) { temp = head ; while ( temp . next != ptr ) temp = temp . next ; temp . next = null ; } if ( ptr != null && ptr . next != null ) { ptr . data = ptr . next . data ; temp = ptr . next ; ptr . next = ptr . next . next ; } } function newNode ( x ) { var node = new Node ( ) ; node . data = x ; node . next = null ; return node ; } function display ( head ) { var temp = head ; if ( head == null ) { document . write ( " " ) ; return ; } while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( " " ) ; } var head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = newNode ( 4 ) ; head . next . next . next . next . next . next = newNode ( 4 ) ; document . write ( " " ) ; display ( head ) ; deleteLast ( head , 4 ) ; document . write ( " " ) ; display ( head ) ;
Flatten a multi |
function flattenList2 ( head ) { var headcop = head ; var save = new Stack ( ) ; save . push ( head ) ; var prev = null ; while ( ! save . isEmpty ( ) ) { var temp = save . pop ( ) ; if ( temp . next ) save . push ( temp . next ) ; if ( temp . down ) save . push ( temp . down ) ; if ( prev != null ) prev . next = temp ; prev = temp ; } return headcop ; }