codeparrot/xlcost-text-to-code huggingface dataset:
Minimum product subset of an array | C # 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 . ; main function
using System ; public class GFG { static int minProductSubset ( int [ ] a , int n ) { if ( n == 1 ) return a [ 0 ] ; int negmax = int . MinValue ; int posmin = int . MinValue ; int count_neg = 0 , count_zero = 0 ; int product = 1 ; for ( int 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 = product / negmax ; } return product ; } public static void Main ( ) { int [ ] a = new int [ ] { - 1 , - 1 , - 2 , 4 , 3 } ; int n = 5 ; Console . WriteLine ( minProductSubset ( a , n ) ) ; } }
Repeatedly search an element by doubling it after every successful search | C # 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
using System ; public class GFG { static int findElement ( int [ ] a , int n , int b ) { Array . Sort ( a ) ; int max = a [ n - 1 ] ; while ( b < max ) { if ( Array . BinarySearch ( a , b ) > - 1 ) b *= 2 ; else return b ; } return b ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int n = a . Length ; int b = 1 ; Console . WriteLine ( findElement ( a , n , b ) ) ; } }
Maximum sum of pairwise product in an array with negative allowed | C # 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 ; Drivers code
using System ; class GFG { static int Mod = 1000000007 ; static long findSum ( int [ ] arr , int n ) { long sum = 0 ; Array . Sort ( arr ) ; int 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 ; } int 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 ; } public static void Main ( ) { int [ ] arr = { - 1 , 9 , 4 , 5 , - 4 , 7 } ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n ) ) ; } }
Search an element in a Linked List ( Iterative and Recursive ) | Recursive C # program to search an element in linked list ; 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 point 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 ; Driver code ; Start with the empty list ; Use push ( ) to construct below list 14 -> 21 -> 11 -> 30 -> 10
using System ; public class Node { public int data ; public Node next ; public Node ( int d ) { data = d ; next = null ; } } public class LinkedList { Node head ; public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public bool search ( Node head , int x ) { if ( head == null ) return false ; if ( head . data == x ) return true ; return search ( head . next , x ) ; } public static void Main ( ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 10 ) ; llist . push ( 30 ) ; llist . push ( 11 ) ; llist . push ( 21 ) ; llist . push ( 14 ) ; if ( llist . search ( llist . head , 21 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Iteratively Reverse a linked list using only 2 pointers ( An Interesting Method ) | C # program to reverse a linked list using two pointers . ; 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 point to the new node ; Function to print linked list ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node next ; } ; static Node head_ref = null ; static void reverse ( ) { Node prev = null ; Node current = head_ref ; while ( current != null ) { Node next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; } static void printList ( ) { Node temp = head_ref ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void Main ( string [ ] args ) { push ( 20 ) ; push ( 4 ) ; push ( 15 ) ; push ( 85 ) ; Console . Write ( " Given ▁ linked ▁ list STRNEWLINE " ) ; printList ( ) ; reverse ( ) ; Console . Write ( " STRNEWLINE Reversed ▁ Linked ▁ list ▁ STRNEWLINE " ) ; 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
static Node deleteAlt ( Node head ) { if ( head == null ) return ; Node 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
static void AlternatingSplit ( Node source , Node aRef , Node bRef ) { Node aDummy = new Node ( ) ; Node aTail = aDummy ; Node bDummy = new Node ( ) ; Node bTail = bDummy ; Node 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 | An iterative C # program to check if two linked lists are identical or not ; Linked list Node ; head of list ; Returns true if linked lists a and b are identical , otherwise false ; If we reach here , then a and b are not null and their data is same , so move to next nodes in both lists ; If linked lists are identical , then ' a ' and ' b ' must be null at this point . ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the 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 ; Driver code ; The constructed linked lists are : llist1 : 3 -> 2 -> 1 llist2 : 3 -> 2 -> 1
using System ; public class Node { public int data ; public Node next ; public Node ( int d ) { data = d ; next = null ; } } public class LinkedList { Node head ; bool areIdentical ( LinkedList listb ) { Node a = this . head , b = listb . head ; while ( a != null && b != null ) { if ( a . data != b . data ) return false ; a = a . next ; b = b . next ; } return ( a == null && b == null ) ; } void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public static void Main ( String [ ] args ) { LinkedList llist1 = new LinkedList ( ) ; LinkedList llist2 = new LinkedList ( ) ; llist1 . push ( 1 ) ; llist1 . push ( 2 ) ; llist1 . push ( 3 ) ; llist2 . push ( 1 ) ; llist2 . push ( 2 ) ; llist2 . push ( 3 ) ; if ( llist1 . areIdentical ( llist2 ) == true ) Console . WriteLine ( " Identical ▁ " ) ; else Console . WriteLine ( " Not ▁ identical ▁ " ) ; } }
Identical Linked Lists | A recursive C # 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
bool areIdenticalRecur ( Node a , Node 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 ; } bool areIdentical ( LinkedList listb ) { return areIdenticalRecur ( this . head , listb . head ) ; } }
Rotate a Linked List | C # program to rotate a linked list counter clock wise ; 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 point to the new node ; Function to print linked list ; Driver code ; create a list 10.20 . 30.40 .50 . 60
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node head = null ; static void rotate ( int k ) { if ( k == 0 ) return ; Node current = head ; while ( current . next != null ) current = current . next ; current . next = head ; current = head ; for ( int i = 0 ; i < k - 1 ; i ++ ) current = current . next ; head = current . next ; current . next = null ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . next ; } } public static void Main ( String [ ] args ) { for ( int i = 60 ; i > 0 ; i -= 10 ) push ( i ) ; Console . Write ( " Given ▁ linked ▁ list ▁ STRNEWLINE " ) ; printList ( head ) ; rotate ( 4 ) ; Console . Write ( " STRNEWLINE Rotated ▁ Linked ▁ list ▁ STRNEWLINE " ) ; printList ( head ) ; } }
Flattening a Linked List | C # program for 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 point to new Node ; 5. return to link it back ; Driver program to test above functions ; * 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
using System ; public class List { Node head ; public class Node { public int data ; public Node right , down ; public Node ( int data ) { this . data = data ; right = null ; down = null ; } } Node merge ( Node a , Node b ) { if ( a == null ) return b ; if ( b == null ) return a ; Node 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 ; } Node flatten ( Node root ) { if ( root == null root . right == null ) return root ; root . right = flatten ( root . right ) ; root = merge ( root , root . right ) ; return root ; } Node Push ( Node head_ref , int data ) { Node new_node = new Node ( data ) ; new_node . down = head_ref ; head_ref = new_node ; return head_ref ; } void printList ( ) { Node temp = head ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . down ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { List L = new List ( ) ; L . head = L . Push ( L . head , 30 ) ; L . head = L . Push ( L . head , 8 ) ; L . head = L . Push ( L . head , 7 ) ; L . head = L . Push ( L . head , 5 ) ; L . head . right = L . Push ( L . head . right , 20 ) ; L . head . right = L . Push ( L . head . right , 10 ) ; L . head . right . right = L . Push ( L . head . right . right , 50 ) ; L . head . right . right = L . Push ( L . head . right . right , 22 ) ; L . head . right . right = L . Push ( L . head . right . right , 19 ) ; L . head . right . right . right = L . Push ( L . head . right . right . right , 45 ) ; L . head . right . right . right = L . Push ( L . head . right . right . right , 40 ) ; L . head . right . right . right = L . Push ( L . head . right . right . right , 35 ) ; L . head . right . right . right = L . Push ( L . head . right . right . right , 20 ) ; L . head = L . flatten ( L . head ) ; L . printList ( ) ; } }
Sort a linked list of 0 s , 1 s and 2 s | C # program to sort a linked list of 0 , 1 and 2 ; 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 ; Driver code ; Constructed Linked List is 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 8 -> 9 -> null
using System ; public class LinkedList { Node head ; class Node { public int data ; public Node next ; public Node ( int d ) { data = d ; next = null ; } } void sortList ( ) { int [ ] count = { 0 , 0 , 0 } ; Node ptr = head ; while ( ptr != null ) { count [ ptr . data ] ++ ; ptr = ptr . next ; } int i = 0 ; ptr = head ; while ( ptr != null ) { if ( count [ i ] == 0 ) i ++ ; else { ptr . data = i ; -- count [ i ] ; ptr = ptr . next ; } } } public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } void printList ( ) { Node temp = head ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 0 ) ; llist . push ( 1 ) ; llist . push ( 0 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; llist . push ( 1 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; llist . push ( 2 ) ; Console . WriteLine ( " Linked ▁ List ▁ before ▁ sorting " ) ; llist . printList ( ) ; llist . sortList ( ) ; Console . WriteLine ( " Linked ▁ List ▁ after ▁ sorting " ) ; llist . printList ( ) ; } }
Flatten a multilevel linked list |
static class List { public int data ; public List next ; public List child ; } ;
Rearrange a linked list such that all even and odd positioned nodes are together | C # program to rearrange a linked list in such a way that all odd positioned node are stored before all even positioned nodes ; 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 print a linked list ; Driver code
using System ; class GfG { class Node { public int data ; public Node next ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static Node rearrangeEvenOdd ( Node head ) { if ( head == null ) return null ; Node odd = head ; Node even = head . next ; Node 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 ; } static void printlist ( Node node ) { while ( node != null ) { Console . Write ( node . data + " - > " ) ; node = node . next ; } Console . WriteLine ( " NULL " ) ; } public static void Main ( ) { Node 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 ) ; Console . WriteLine ( " Given ▁ Linked ▁ List " ) ; printlist ( head ) ; head = rearrangeEvenOdd ( head ) ; Console . WriteLine ( " Modified ▁ Linked ▁ List " ) ; printlist ( head ) ; } }
Rearrange a Linked List in Zig | C # program for the above approach ; Node class ; Print 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
using System ; public class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; } } public class GFG { private Node head ; public void printLL ( ) { Node t = head ; while ( t != null ) { Console . Write ( t . data + " ▁ - > " ) ; t = t . next ; } Console . WriteLine ( ) ; } public void swap ( Node a , Node b ) { if ( a == null b == null ) return ; int temp = a . data ; a . data = b . data ; b . data = temp ; } public Node zigZag ( Node node , int 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 ) ; } } public static void Main ( String [ ] args ) { GFG lobj = new GFG ( ) ; lobj . head = new Node ( 11 ) ; lobj . head . next = new Node ( 15 ) ; lobj . head . next . next = new Node ( 20 ) ; lobj . head . next . next . next = new Node ( 5 ) ; lobj . head . next . next . next . next = new Node ( 10 ) ; lobj . printLL ( ) ; int flag = 0 ; lobj . zigZag ( lobj . head , flag ) ; Console . WriteLine ( " LL ▁ in ▁ zig ▁ zag ▁ fashion ▁ : ▁ " ) ; lobj . printLL ( ) ; } }
Add 1 to a number represented as linked list | Recursive C # program to add 1 to a 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
using System ; class GfG { public class Node { public int data ; public Node next ; } public static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static int addWithCarry ( Node head ) { if ( head == null ) return 1 ; int res = head . data + addWithCarry ( head . next ) ; head . data = ( res ) % 10 ; return ( res ) / 10 ; } public static Node addOne ( Node head ) { int carry = addWithCarry ( head ) ; Node newNodes = null ; if ( carry > 0 ) { newNodes = newNode ( carry ) ; newNodes . next = head ; return newNodes ; } return head ; } public static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data ) ; node = node . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 9 ) ; head . next . next = newNode ( 9 ) ; head . next . next . next = newNode ( 9 ) ; Console . Write ( " List ▁ is ▁ " ) ; printList ( head ) ; head = addOne ( head ) ; Console . WriteLine ( ) ; Console . Write ( " Resultant ▁ list ▁ is ▁ " ) ; printList ( head ) ; } }
Point arbit pointer to greatest value right side node in a linked list | C # program to point arbit pointers to highest value on its right ; 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
using System ; class GfG { class Node { public int data ; public Node next , arbit ; } static Node reverse ( Node head ) { Node prev = null , current = head , next = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node populateArbit ( Node head ) { head = reverse ( head ) ; Node max = head ; Node temp = head . next ; while ( temp != null ) { temp . arbit = max ; if ( max . data < temp . data ) max = temp ; temp = temp . next ; } return reverse ( head ) ; } static void printNextArbitPointers ( Node node ) { Console . WriteLine ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer " ) ; while ( node != null ) { Console . Write ( node . data + " TABSYMBOL TABSYMBOL " ) ; if ( node . next != null ) Console . Write ( node . next . data + " TABSYMBOL TABSYMBOL " ) ; else Console . Write ( " NULL " + " TABSYMBOL TABSYMBOL " ) ; if ( node . arbit != null ) Console . Write ( node . arbit . data ) ; else Console . Write ( " NULL " ) ; Console . WriteLine ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void Main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; head = populateArbit ( head ) ; Console . WriteLine ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }