codeparrot/xlcost-text-to-code huggingface dataset:
Collect maximum points in a grid using two traversals | A Memoization based program to find maximum collection using two traversals of a grid ; checks whether a given input is valid or not ; Driver function to collect Math . max value ; if P1 or P2 is at an invalid cell ; if both traversals reach their destinations ; If both traversals are at last row but not at their destination ; If subproblem is already solved ; Initialize answer for this subproblem ; this variable is used to store gain of current cell ( s ) ; Recur for all possible cases , then store and return the one with max value ; This is mainly a wrapper over recursive function getMaxUtil ( ) . This function creates a table for memoization and calls getMaxUtil ( ) ; Create a memoization table and initialize all entries as - 1 ; Calculation maximum value using memoization based function getMaxUtil ( ) ; Driver code
using System ; class GFG { static readonly int R = 5 ; static readonly int C = 4 ; static bool isValid ( int x , int y1 , int y2 ) { return ( x >= 0 && x < R && y1 >= 0 && y1 < C && y2 >= 0 && y2 < C ) ; } static int getMaxUtil ( int [ , ] arr , int [ , , ] mem , int x , int y1 , int y2 ) { if ( ! isValid ( x , y1 , y2 ) ) return int . MinValue ; if ( x == R - 1 && y1 == 0 && y2 == C - 1 ) return ( y1 == y2 ) ? arr [ x , y1 ] : arr [ x , y1 ] + arr [ x , y2 ] ; if ( x == R - 1 ) return int . MinValue ; if ( mem [ x , y1 , y2 ] != - 1 ) return mem [ x , y1 , y2 ] ; int ans = int . MinValue ; int temp = ( y1 == y2 ) ? arr [ x , y1 ] : arr [ x , y1 ] + arr [ x , y2 ] ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 - 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 + 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 - 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 + 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 - 1 ) ) ; ans = Math . Max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 + 1 ) ) ; return ( mem [ x , y1 , y2 ] = ans ) ; } static int geMaxCollection ( int [ , ] arr ) { int [ , , ] mem = new int [ R , C , C ] ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { for ( int l = 0 ; l < C ; l ++ ) mem [ i , j , l ] = - 1 ; } } return getMaxUtil ( arr , mem , 0 , 0 , C - 1 ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 3 , 6 , 8 , 2 } , { 5 , 2 , 4 , 3 } , { 1 , 1 , 20 , 10 } , { 1 , 1 , 20 , 10 } , { 1 , 1 , 20 , 10 } , } ; Console . Write ( " Maximum ▁ collection ▁ is ▁ " + geMaxCollection ( arr ) ) ; } }
Find length of the longest consecutive path from a given starting character | C # program to find the longest consecutive path ; tool matrices to recur for adjacent cells . ; dp [ i ] [ j ] Stores length of longest consecutive path starting at arr [ i ] [ j ] . ; check whether mat [ i ] [ j ] is a valid cell or not . ; Check whether current character is adjacent to previous character ( character processed in parent call ) or not . ; i , j are the indices of the current cell and prev is the character processed in the parent call . . also mat [ i ] [ j ] is our current character . ; If this cell is not valid or current character is not adjacent to previous one ( e . g . d is not adjacent to b ) or if this cell is already included in the path than return 0. ; If this subproblem is already solved , return the answer ; Initialize answer ; recur for paths with different adjacent cells and store the length of longest path . ; save the answer and return ; Returns length of the longest path with all characters consecutive to each other . This function first initializes dp array that is used to store results of subproblems , then it calls recursive DFS based function getLenUtil ( ) to find max length path ; check for each possible starting point ; recur for all eight adjacent cells ; Driver code `
using System ; class GFG { static int [ ] x = { 0 , 1 , 1 , - 1 , 1 , 0 , - 1 , - 1 } ; static int [ ] y = { 1 , 0 , 1 , 1 , - 1 , - 1 , 0 , - 1 } ; static int R = 3 ; static int C = 3 ; static int [ , ] dp = new int [ R , C ] ; static bool isvalid ( int i , int j ) { if ( i < 0 j < 0 i >= R j >= C ) return false ; return true ; } static bool isadjacent ( char prev , char curr ) { return ( ( curr - prev ) == 1 ) ; } static int getLenUtil ( char [ , ] mat , int i , int j , char prev ) { if ( ! isvalid ( i , j ) || ! isadjacent ( prev , mat [ i , j ] ) ) return 0 ; if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; int ans = 0 ; for ( int k = 0 ; k < 8 ; k ++ ) ans = Math . Max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , mat [ i , j ] ) ) ; return dp [ i , j ] = ans ; } static int getLen ( char [ , ] mat , char s ) { for ( int i = 0 ; i < R ; ++ i ) for ( int j = 0 ; j < C ; ++ j ) dp [ i , j ] = - 1 ; int ans = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( mat [ i , j ] == s ) { for ( int k = 0 ; k < 8 ; k ++ ) ans = Math . Max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , s ) ) ; } } } return ans ; } public static void Main ( ) { char [ , ] mat = { { ' a ' , ' c ' , ' d ' } , { ' h ' , ' b ' , ' a ' } , { ' i ' , ' g ' , ' f ' } } ; Console . WriteLine ( getLen ( mat , ' a ' ) ) ; Console . WriteLine ( getLen ( mat , ' e ' ) ) ; Console . WriteLine ( getLen ( mat , ' b ' ) ) ; Console . WriteLine ( getLen ( mat , ' f ' ) ) ; } }
Find the longest path in a matrix with given constraints | C # program to find the longest path in a matrix with given constraints ; Function that returns length of the longest path beginning with mat [ i ] [ j ] This function mainly uses lookup table dp [ n ] [ n ] ; Base case ; If this subproblem is already solved ; To store the path lengths in all the four directions ; Since all numbers are unique and in range from 1 to n * n , there is atmost one possible direction from any cell ; If none of the adjacent fours is one greater we will take 1 otherwise we will pick maximum from all the four directions ; Function that returns length of the longest path beginning with any cell ; Initialize result ; Create a lookup table and fill all entries in it as - 1 ; Compute longest path beginning from all cells ; Update result if needed ;  ; Driver Code
using System ; class GFG { public static int n = 3 ; public static int findLongestFromACell ( int i , int j , int [ ] [ ] mat , int [ ] [ ] dp ) { if ( i < 0 i >= n j < 0 j >= n ) { return 0 ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } int x = int . MinValue , y = int . MinValue , z = int . MinValue , w = int . MinValue ; if ( j < n - 1 && ( ( mat [ i ] [ j ] + 1 ) == mat [ i ] [ j + 1 ] ) ) { x = dp [ i ] [ j ] = 1 + findLongestFromACell ( i , j + 1 , mat , dp ) ; } if ( j > 0 && ( mat [ i ] [ j ] + 1 == mat [ i ] [ j - 1 ] ) ) { y = dp [ i ] [ j ] = 1 + findLongestFromACell ( i , j - 1 , mat , dp ) ; } if ( i > 0 && ( mat [ i ] [ j ] + 1 == mat [ i - 1 ] [ j ] ) ) { z = dp [ i ] [ j ] = 1 + findLongestFromACell ( i - 1 , j , mat , dp ) ; } if ( i < n - 1 && ( mat [ i ] [ j ] + 1 == mat [ i + 1 ] [ j ] ) ) { w = dp [ i ] [ j ] = 1 + findLongestFromACell ( i + 1 , j , mat , dp ) ; } dp [ i ] [ j ] = Math . Max ( x , Math . Max ( y , Math . Max ( z , Math . Max ( w , 1 ) ) ) ) ; return dp [ i ] [ j ] ; } public static int finLongestOverAll ( int [ ] [ ] mat ) { int result = 1 ; int [ ] [ ] dp = RectangularArrays . ReturnRectangularIntArray ( n , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( dp [ i ] [ j ] == - 1 ) { findLongestFromACell ( i , j , mat , dp ) ; } result = Math . Max ( result , dp [ i ] [ j ] ) ; } } return result ; } public static class RectangularArrays { public static int [ ] [ ] ReturnRectangularIntArray ( int size1 , int size2 ) { int [ ] [ ] newArray = new int [ size1 ] [ ] ; for ( int array1 = 0 ; array1 < size1 ; array1 ++ ) { newArray [ array1 ] = new int [ size2 ] ; } return newArray ; } } public static void Main ( string [ ] args ) { int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 1 , 2 , 9 } , new int [ ] { 5 , 3 , 8 } , new int [ ] { 4 , 6 , 7 } } ; Console . WriteLine ( " Length ▁ of ▁ the ▁ longest ▁ path ▁ is ▁ " + finLongestOverAll ( mat ) ) ; } }
Minimum Initial Points to Reach Destination | C # program Minimum Initial Points to Reach Destination ; dp [ i ] [ j ] represents the minimum initial points player should have so that when starts with cell ( i , j ) successfully reaches the destination cell ( m - 1 , n - 1 ) ; Base case ; Fill last row and last column as base to fill entire table ; fill the table in bottom - up fashion ; Driver Code
using System ; class GFG { static int minInitialPoints ( int [ , ] points , int R , int C ) { int [ , ] dp = new int [ R , C ] ; int m = R , n = C ; dp [ m - 1 , n - 1 ] = points [ m - 1 , n - 1 ] > 0 ? 1 : Math . Abs ( points [ m - 1 , n - 1 ] ) + 1 ; for ( int i = m - 2 ; i >= 0 ; i -- ) dp [ i , n - 1 ] = Math . Max ( dp [ i + 1 , n - 1 ] - points [ i , n - 1 ] , 1 ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) dp [ m - 1 , j ] = Math . Max ( dp [ m - 1 , j + 1 ] - points [ m - 1 , j ] , 1 ) ; for ( int i = m - 2 ; i >= 0 ; i -- ) { for ( int j = n - 2 ; j >= 0 ; j -- ) { int min_points_on_exit = Math . Min ( dp [ i + 1 , j ] , dp [ i , j + 1 ] ) ; dp [ i , j ] = Math . Max ( min_points_on_exit - points [ i , j ] , 1 ) ; } } return dp [ 0 , 0 ] ; } public static void Main ( ) { int [ , ] points = { { - 2 , - 3 , 3 } , { - 5 , - 10 , 1 } , { 10 , 30 , - 5 } } ; int R = 3 , C = 3 ; Console . Write ( " Minimum ▁ Initial ▁ Points ▁ Required : ▁ " + minInitialPoints ( points , R , C ) ) ; } }
Convert an arbitrary Binary Tree to a tree that holds Children Sum Property | C # program to convert an arbitrary binary tree to a tree that holds children sum property ; A binary tree node ; This function changes a tree to hold children sum property ; If tree is empty or it 's a leaf   node then return true  ; convert left and right subtrees ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; get the diff of node 's data   and children sum  ; If node ' s ▁ children ▁ sum ▁ is ▁ greater ▁ ▁ ▁ than ▁ the ▁ node ' s data ; THIS IS TRICKY -- > If node 's data is   greater than children sum, then increment  subtree by diff  ; - diff is used to make diff positive ; This function is used to increment subtree by diff ; IF left child is not NULL then increment it ; Recursively call to fix the descendants of node -> left ; Else increment right child ; Recursively call to fix the descendants of node -> right ; Given a binary tree , printInorder ( ) prints out its inorder traversal ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void convertTree ( Node node ) { int left_data = 0 , right_data = 0 , diff ; if ( node == null || ( node . left == null && node . right == null ) ) { return ; } else { convertTree ( node . left ) ; convertTree ( node . right ) ; if ( node . left != null ) { left_data = node . left . data ; } if ( node . right != null ) { right_data = node . right . data ; } diff = left_data + right_data - node . data ; if ( diff > 0 ) { node . data = node . data + diff ; } if ( diff < 0 ) { increment ( node , - diff ) ; } } } public virtual void increment ( Node node , int diff ) { if ( node . left != null ) { node . left . data = node . left . data + diff ; increment ( node . left , diff ) ; } else if ( node . right != null ) { node . right . data = node . right . data + diff ; increment ( node . right , diff ) ; } } public virtual void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 50 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 1 ) ; tree . root . right . right = new Node ( 30 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " before ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; tree . convertTree ( tree . root ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " after ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; } }
Queue using Stacks | C # program to implement Queue using two stacks with costly enQueue ( ) ; Move all elements from s1 to s2 ; Push item into s1 ; Push everything back to s1 ; Dequeue an item from the queue ; if first stack is empty ; Return top of s1 ; Driver code
using System ; using System . Collections ; class GFG { public class Queue { public Stack s1 = new Stack ( ) ; public Stack s2 = new Stack ( ) ; public void enQueue ( int x ) { while ( s1 . Count > 0 ) { s2 . Push ( s1 . Pop ( ) ) ; s1 . Pop ( ) ; } s1 . Push ( x ) ; while ( s2 . Count > 0 ) { s1 . Push ( s2 . Pop ( ) ) ; s2 . Pop ( ) ; } } public int deQueue ( ) { if ( s1 . Count == 0 ) { Console . WriteLine ( " Q ▁ is ▁ Empty " ) ; } int x = ( int ) s1 . Peek ( ) ; s1 . Pop ( ) ; return x ; } } ; public static void Main ( ) { Queue q = new Queue ( ) ; q . enQueue ( 1 ) ; q . enQueue ( 2 ) ; q . enQueue ( 3 ) ; Console . Write ( q . deQueue ( ) + " ▁ " ) ; Console . Write ( q . deQueue ( ) + " ▁ " ) ; Console . Write ( q . deQueue ( ) ) ; } }
Convert left | C # program to convert left - right to down - right representation of binary tree ; A Binary Tree Node ; An Iterative level order traversal based function to convert left - right to down - right representation . ; Base Case ; Recursively convert left an right subtrees ; If left child is NULL , make right child as left as it is the first child . ; If left child is NOT NULL , then make right child as right of left child ; Set root 's right as NULL  ; A utility function to traverse a tree stored in down - right form . ; Utility function to create a new tree node ; Driver Code ; 1 / \ 2 3 / \ 4 5 / / \ 6 7 8
using System ; class GFG { public class node { public int key ; public node left , right ; public node ( int key ) { this . key = key ; this . left = null ; this . right = null ; } } public static void convert ( node root ) { if ( root == null ) { return ; } convert ( root . left ) ; convert ( root . right ) ; if ( root . left == null ) { root . left = root . right ; } else { root . left . right = root . right ; } root . right = null ; } public static void downRightTraversal ( node root ) { if ( root != null ) { Console . Write ( root . key + " ▁ " ) ; downRightTraversal ( root . right ) ; downRightTraversal ( root . left ) ; } } public static node newNode ( int key ) { node temp = new node ( 0 ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } public static void Main ( string [ ] args ) { node root = new node ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . right . left . left = newNode ( 6 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; convert ( root ) ; Console . WriteLine ( " Traversal ▁ of ▁ the ▁ tree ▁ " + " converted ▁ to ▁ down - right ▁ form " ) ; downRightTraversal ( root ) ; } }
Convert a given tree to its Sum Tree | C # program to convert a tree into its sum tree ; A binary tree node ; Convert a given tree to a tree where every node contains sum of values of nodes in left and right subtrees in the original tree ; Base case ; Store the old value ; Recursively call for left and right subtrees and store the sum as new value of this node ; Return the sum of values of nodes in left and right subtrees old_value of this node ; A utility function to print inorder traversal of a Binary Tree ; Driver Code ; Constructing tree given in the above figure ; Print inorder traversal of the converted tree to test result of toSumTree ( )
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual int toSumTree ( Node node ) { if ( node == null ) { return 0 ; } int old_val = node . data ; node . data = toSumTree ( node . left ) + toSumTree ( node . right ) ; return node . data + old_val ; } public virtual void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 5 ) ; tree . toSumTree ( tree . root ) ; Console . WriteLine ( " Inorder ▁ Traversal ▁ of ▁ " + " the ▁ resultant ▁ tree ▁ is : " ) ; tree . printInorder ( tree . root ) ; } }
Find a peak element | A C # program to find a peak element ; Find the peak element in the array ; First or last element is peak element ; Check for every other element ; Check if the neighbors are smaller ; Driver Code
using System ; public class GFG { static int findPeak ( int [ ] arr , int n ) { if ( n == 1 ) return 0 ; if ( arr [ 0 ] >= arr [ 1 ] ) return 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) return n - 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i - 1 ] && arr [ i ] >= arr [ i + 1 ] ) return i ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 20 , 4 , 1 , 0 } ; int n = arr . Length ; Console . Write ( " Index ▁ of ▁ a ▁ peak ▁ point ▁ is ▁ " + findPeak ( arr , n ) ) ; } }
Find a peak element | A C # program to find a peak element element using divide and conquer ; A binary search based function that returns index of a peak element ; Find index of middle element ; Compare middle element with its neighbours ( if neighbours exist ) ; If middle element is not peak and its left neighbor is greater than it , then left half must have a peak element ; If middle element is not peak and its right neighbor is greater than it , then right half must have a peak element ; A wrapper over recursive function findPeakUtil ( ) ; Driver Code
using System ; class GFG { static int findPeakUtil ( int [ ] arr , int low , int high , int n ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == 0 arr [ mid - 1 ] <= arr [ mid ] ) && ( mid == n - 1 arr [ mid + 1 ] <= arr [ mid ] ) ) return mid ; else if ( mid > 0 && arr [ mid - 1 ] > arr [ mid ] ) return findPeakUtil ( arr , low , ( mid - 1 ) , n ) ; else return findPeakUtil ( arr , ( mid + 1 ) , high , n ) ; } static int findPeak ( int [ ] arr , int n ) { return findPeakUtil ( arr , 0 , n - 1 , n ) ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 20 , 4 , 1 , 0 } ; int n = arr . Length ; Console . WriteLine ( " Index ▁ of ▁ a ▁ peak ▁ " + " point ▁ is ▁ " + findPeak ( arr , n ) ) ; } }
Find the two repeating elements in a given array |  ; Print Repeating function ; driver code
using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int i , j ; Console . Write ( " Repeated ▁ Elements ▁ are ▁ : " ) ; for ( i = 0 ; i < size ; i ++ ) { for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }
Find the two repeating elements in a given array | C # program to Find the two repeating elements in a given array ; Function ; driver code
using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int [ ] count = new int [ size ] ; int i ; Console . Write ( " Repeated ▁ elements ▁ are : ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( count [ arr [ i ] ] == 1 ) Console . Write ( arr [ i ] + " ▁ " ) ; else count [ arr [ i ] ] ++ ; } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }
Find the two repeating elements in a given array |  ; printRepeating function ; S is for sum of elements in arr [ ] ; P is for product of elements in arr [ ] ; x and y are two repeating elements ; D is for difference of x and y , i . e . , x - y ; Calculate Sum and Product of all elements in arr [ ] ; S is x + y now ; P is x * y now ; D is x - y now ; factorial of n ; driver code
using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int S = 0 ; int P = 1 ; int x , y ; int D ; int n = size - 2 , i ; for ( i = 0 ; i < size ; i ++ ) { S = S + arr [ i ] ; P = P * arr [ i ] ; } S = S - n * ( n + 1 ) / 2 ; P = P / fact ( n ) ; D = ( int ) Math . Sqrt ( S * S - 4 * P ) ; x = ( D + S ) / 2 ; y = ( S - D ) / 2 ; Console . WriteLine ( " The ▁ two " + " ▁ repeating ▁ elements ▁ are ▁ : " ) ; Console . Write ( x + " ▁ " + y ) ; } static int fact ( int n ) { return ( n == 0 ) ? 1 : n * fact ( n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }
Find the two repeating elements in a given array | C # code to Find the two repeating elements in a given array ; Will hold xor of all elements ; Will have only single set bit of xor ; Get the xor of all elements in arr [ ] and { 1 , 2 . . n } ; Get the rightmost set bit in set_bit_no ; Now divide elements in two sets by comparing rightmost set bit of xor with bit at same position in each element . ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver program to test the above function
using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int xor = arr [ 0 ] ; int set_bit_no ; int i ; int n = size - 2 ; int x = 0 , y = 0 ; for ( i = 1 ; i < size ; i ++ ) xor ^= arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) xor ^= i ; set_bit_no = ( xor & ~ ( xor - 1 ) ) ; for ( i = 0 ; i < size ; i ++ ) { int a = arr [ i ] & set_bit_no ; if ( a != 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( i = 1 ; i <= n ; i ++ ) { int a = i & set_bit_no ; if ( a != 0 ) x = x ^ i ; else y = y ^ i ; } Console . WriteLine ( " The ▁ two " + " ▁ reppeated ▁ elements ▁ are ▁ : " ) ; Console . Write ( x + " ▁ " + y ) ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }
Find the two repeating elements in a given array | C # code for Find the two repeating elements in a given array ; Function to print repeating ; Driver program to test the above function
using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int i ; Console . Write ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ Math . Abs ( arr [ i ] ) ] > 0 ) arr [ Math . Abs ( arr [ i ] ) ] = - arr [ Math . Abs ( arr [ i ] ) ] ; else Console . Write ( Math . Abs ( arr [ i ] ) + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }