codeparrot/xlcost-text-to-code huggingface dataset:
Remove all nodes which don 't lie in any path with sum>= k | C # program to remove all nodes which donot lie on path having sum >= k ; Class representing binary tree node ; for print traversal ; class to truncate binary tree ; base case ; recur for left and right subtree ; if node is a leaf node whose data is smaller than the sum we delete the leaf . An important thing to note is a non - leaf node can become leaf when its children are deleted . ; utility method to check if node is leaf ; Driver class to test above function
using System ; public class Node { public int data ; public Node left ; public Node right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public virtual void print ( Node root ) { if ( root == null ) { return ; } print ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; print ( root . right ) ; } } public class BinaryTree { public Node root ; public virtual Node prune ( Node root , int sum ) { if ( root == null ) { return null ; } root . left = prune ( root . left , sum - root . data ) ; root . right = prune ( root . right , sum - root . data ) ; if ( isLeaf ( root ) ) { if ( sum > root . data ) { root = null ; } } return root ; } public virtual bool isLeaf ( Node root ) { if ( root == null ) { return false ; } if ( root . left == null && root . right == null ) { return true ; } return false ; } public class GFG { public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 10 ) ; tree . root . right . right . left . right = new Node ( 11 ) ; tree . root . left . left . right . left = new Node ( 13 ) ; tree . root . left . left . right . right = new Node ( 14 ) ; tree . root . left . left . right . right . left = new Node ( 15 ) ; Console . WriteLine ( " Tree ▁ before ▁ truncation " ) ; tree . print ( tree . root ) ; tree . prune ( tree . root , 45 ) ; Console . WriteLine ( " STRNEWLINE Tree ▁ after ▁ truncation " ) ; tree . print ( tree . root ) ; } }
Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | C # program for the above approach ; Function to count the unique sum ; Set fsum [ 0 ] as ar [ 0 ] ; Set rsum [ 0 ] as ar [ n ] ; For each i update fsum [ i ] with ar [ i ] + fsum [ i - 1 ] ; For each i from n - 1 , update rsum [ i ] with ar [ i ] + fsum [ i + 1 ] ; K represent size of subset as explained above ; Using above relation ; Return the result ; Driver Code ; Given a number N ; Function Call
using System ; class GFG { static int count_unique_sum ( int n ) { int i ; int [ ] ar = new int [ n + 1 ] ; int [ ] fsum = new int [ n + 1 ] ; int [ ] rsum = new int [ n + 1 ] ; int ans = 1 ; for ( i = 0 ; i <= n ; i ++ ) { ar [ i ] = i ; } fsum [ 0 ] = ar [ 0 ] ; rsum [ n ] = ar [ n ] ; for ( i = 1 ; i <= n ; i ++ ) { fsum [ i ] = ar [ i ] + fsum [ i - 1 ] ; } for ( i = n - 1 ; i >= 0 ; i -- ) { rsum [ i ] = ar [ i ] + rsum [ i + 1 ] ; } for ( int k = 2 ; k <= n ; k ++ ) { ans += 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ; } return ans ; } public static void Main ( String [ ] args ) { int N = 4 ; Console . Write ( count_unique_sum ( N ) ) ; } }
Merge an array of size n into another array of size m + n | C # program to Merge an array of size n into another array of size m + n ; Function to move m elements at the end of array mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Driver Code ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN
using System ; class GFG { public virtual void moveToEnd ( int [ ] mPlusN , int size ) { int i , j = size - 1 ; for ( i = size - 1 ; i >= 0 ; i -- ) { if ( mPlusN [ i ] != - 1 ) { mPlusN [ j ] = mPlusN [ i ] ; j -- ; } } } public virtual void merge ( int [ ] mPlusN , int [ ] N , int m , int n ) { int i = n ; int j = 0 ; int k = 0 ; while ( k < ( m + n ) ) { if ( ( j == n ) ( i < ( m + n ) && mPlusN [ i ] <= N [ j ] ) ) { mPlusN [ k ] = mPlusN [ i ] ; k ++ ; i ++ ; } else { mPlusN [ k ] = N [ j ] ; k ++ ; j ++ ; } } } public virtual void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } public static void Main ( string [ ] args ) { GFG mergearray = new GFG ( ) ; int [ ] mPlusN = new int [ ] { 2 , 8 , - 1 , - 1 , - 1 , 13 , - 1 , 15 , 20 } ; int [ ] N = new int [ ] { 5 , 7 , 9 , 25 } ; int n = N . Length ; int m = mPlusN . Length - n ; mergearray . moveToEnd ( mPlusN , m + n ) ; mergearray . merge ( mPlusN , N , m , n ) ; mergearray . printArray ( mPlusN , m + n ) ; } }
Count of N | C # program to implement the above approach ; Function to return the count of such numbers ; For 1 - digit numbers , the count is 10 irrespective of K ; dp [ j ] stores the number of such i - digit numbers ending with j ; Stores the results of length i ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find actual count of i - digit numbers ending with j ; Update [ ] dp ; Stores the readonly answer ; Return the readonly answer ; Driver Code
using System ; class GFG { public static long getCount ( int n , int k ) { if ( n == 1 ) return 10 ; long [ ] dp = new long [ 11 ] ; long [ ] next = new long [ 11 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp [ i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { int l = Math . Max ( 0 , j - k ) ; int r = Math . Min ( 9 , j + k ) ; next [ l ] += dp [ j ] ; next [ r + 1 ] -= dp [ j ] ; } for ( int j = 1 ; j <= 9 ; j ++ ) next [ j ] += next [ j - 1 ] ; for ( int j = 0 ; j < 10 ; j ++ ) { dp [ j ] = next [ j ] ; next [ j ] = 0 ; } } long count = 0 ; for ( int i = 0 ; i <= 9 ; i ++ ) count += dp [ i ] ; return count ; } public static void Main ( String [ ] args ) { int n = 2 , k = 1 ; Console . WriteLine ( getCount ( n , k ) ) ; } }
Sort 1 to N by swapping adjacent elements | C # program to test whether array can be sorted by swapping adjacent elements using boolean array ; Return true if array can be sorted otherwise false ; Check bool array B and sorts elements for continuous sequence of 1 ; Sort array A from i to j ; Check if array is sorted or not ; Driver Code
using System ; class GFG { static bool sortedAfterSwap ( int [ ] A , bool [ ] B , int n ) { int i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { j = i ; while ( B [ j ] ) { j ++ ; } Array . Sort ( A , i , 1 + j ) ; i = j ; } } for ( i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) { return false ; } } return true ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 5 , 3 , 4 , 6 } ; bool [ ] B = { false , true , true , true , false } ; int n = A . Length ; if ( sortedAfterSwap ( A , B , n ) ) { Console . WriteLine ( " A ▁ can ▁ be ▁ sorted " ) ; } else { Console . WriteLine ( " A ▁ can ▁ not ▁ be ▁ sorted " ) ; } } }
Print negative weight cycle in a Directed Graph | C # program for the above approach ; Structure to represent a weighted edge in graph ; Structure to represent a directed and weighted graph ; V . Number of vertices , E . Number of edges ; graph is represented as an array of edges . ; Creates a new graph with V vertices and E edges ; Function runs Bellman - Ford algorithm and prints negative cycle ( if present ) ; Initialize distances from src to all other vertices as INFINITE and all parent as - 1 ; Relax all edges | V | - 1 times . ; Check for negative - weight cycles ; Store one of the vertex of the negative weight cycle ; To store the cycle vertex ; Reverse cycle [ ] ; Printing the negative cycle ; Driver Code ; Number of vertices in graph ; Number of edges in graph ; Given Graph ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class Edge { public int src , dest , weight ; } class Graph { public int V , E ; public Edge [ ] edge ; } static Graph createGraph ( int V , int E ) { Graph graph = new Graph ( ) ; graph . V = V ; graph . E = E ; graph . edge = new Edge [ graph . E ] ; for ( int i = 0 ; i < graph . E ; i ++ ) { graph . edge [ i ] = new Edge ( ) ; } return graph ; } static void NegCycleBellmanFord ( Graph graph , int src ) { int V = graph . V ; int E = graph . E ; int [ ] dist = new int [ V ] ; int [ ] parent = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { dist [ i ] = 1000000 ; parent [ i ] = - 1 ; } dist [ src ] = 0 ; for ( int i = 1 ; i <= V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { int u = graph . edge [ j ] . src ; int v = graph . edge [ j ] . dest ; int weight = graph . edge [ j ] . weight ; if ( dist [ u ] != 1000000 && dist [ u ] + weight < dist [ v ] ) { dist [ v ] = dist [ u ] + weight ; parent [ v ] = u ; } } } int C = - 1 ; for ( int i = 0 ; i < E ; i ++ ) { int u = graph . edge [ i ] . src ; int v = graph . edge [ i ] . dest ; int weight = graph . edge [ i ] . weight ; if ( dist [ u ] != 1000000 && dist [ u ] + weight < dist [ v ] ) { C = v ; break ; } } if ( C != - 1 ) { for ( int i = 0 ; i < V ; i ++ ) C = parent [ C ] ; ArrayList cycle = new ArrayList ( ) ; for ( int v = C ; ; v = parent [ v ] ) { cycle . Add ( v ) ; if ( v == C && cycle . Count > 1 ) break ; } cycle . Reverse ( ) ; foreach ( int v in cycle ) Console . Write ( v + " ▁ " ) ; Console . WriteLine ( ) ; } else Console . WriteLine ( - 1 ) ; } public static void Main ( string [ ] args ) { int V = 5 ; int E = 5 ; Graph graph = createGraph ( V , E ) ; graph . edge [ 0 ] . src = 0 ; graph . edge [ 0 ] . dest = 1 ; graph . edge [ 0 ] . weight = 1 ; graph . edge [ 1 ] . src = 1 ; graph . edge [ 1 ] . dest = 2 ; graph . edge [ 1 ] . weight = 2 ; graph . edge [ 2 ] . src = 2 ; graph . edge [ 2 ] . dest = 3 ; graph . edge [ 2 ] . weight = 3 ; graph . edge [ 3 ] . src = 3 ; graph . edge [ 3 ] . dest = 4 ; graph . edge [ 3 ] . weight = - 3 ; graph . edge [ 4 ] . src = 4 ; graph . edge [ 4 ] . dest = 1 ; graph . edge [ 4 ] . weight = - 3 ; NegCycleBellmanFord ( graph , 0 ) ; } }
Sort 1 to N by swapping adjacent elements | C # program to test whether array can be sorted by swapping adjacent elements using boolean array ; Return true if array can be sorted otherwise false ; Check if array is sorted or not ; Driver Code
using System ; class GFG { static int sortedAfterSwap ( int [ ] A , int [ ] B , int n ) { int t = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] != 0 ) { if ( A [ i ] != i + 1 ) t = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = t ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return 0 ; } return 1 ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 5 , 3 , 4 , 6 } ; int [ ] B = { 0 , 1 , 1 , 1 , 0 } ; int n = A . Length ; if ( sortedAfterSwap ( A , B , n ) == 0 ) Console . WriteLine ( " A ▁ can ▁ be ▁ sorted " ) ; else Console . WriteLine ( " A ▁ can ▁ not ▁ be ▁ sorted " ) ; } }
Sort an array containing two types of elements | C # program to sort an array with two types of values in one traversal . ; Method for segregation 0 and 1 given input array ; Driver program
using System ; class GFG { static void segregate0and1 ( int [ ] arr , int n ) { int type0 = 0 ; int type1 = n - 1 ; while ( type0 < type1 ) { if ( arr [ type0 ] == 1 ) { arr [ type0 ] = arr [ type0 ] + arr [ type1 ] ; arr [ type1 ] = arr [ type0 ] - arr [ type1 ] ; arr [ type0 ] = arr [ type0 ] - arr [ type1 ] ; type1 -- ; } else { type0 ++ ; } } } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 } ; segregate0and1 ( arr , arr . Length ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Pentanacci Numbers | A simple recursive program to print Nth Pentanacci number ; Recursive function to find the Nth Pentanacci number ; Function to print the Nth Pentanacci number ; Driver code
using System ; class GFG { static int printpentaRec ( int n ) { if ( n == 0 n == 1 n == 2 n == 3 n == 4 ) return 0 ; else if ( n == 5 ) return 1 ; else return ( printpentaRec ( n - 1 ) + printpentaRec ( n - 2 ) + printpentaRec ( n - 3 ) + printpentaRec ( n - 4 ) + printpentaRec ( n - 5 ) ) ; } static void printPenta ( int n ) { Console . WriteLine ( printpentaRec ( n ) ) ; } static void Main ( ) { int n = 10 ; printPenta ( n ) ; } }
Count Inversions in an array | Set 1 ( Using Merge Sort ) | C # program to count inversions in an array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int [ ] arr = new int [ ] { 1 , 20 , 6 , 4 , 5 } ; static int getInvCount ( int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; } public static void Main ( ) { Console . WriteLine ( " Number ▁ of ▁ " + " inversions ▁ are ▁ " + getInvCount ( arr . Length ) ) ; } }
Range queries for alternatively addition and subtraction on given Array | C # program for the above approach ; Structure to represent a range query ; Function to find the result of alternatively adding and subtracting elements in the range [ L , R ] ; A bool variable flag to alternatively add and subtract ; Iterate from [ L , R ] ; If flag is false , then add & toggle the flag ; If flag is true subtract and toggle the flag ; Return the readonly result ; Function to find the value for each query ; Iterate for each query ; Driver Code ; Given array ; Given Queries ; Function call
using System ; class GFG { class Query { public int L , R ; public Query ( int l , int r ) { L = l ; R = r ; } } ; static int findResultUtil ( int [ ] arr , int L , int R ) { int result = 0 ; bool flag = false ; for ( int i = L ; i <= R ; i ++ ) { if ( flag == false ) { result = result + arr [ i ] ; flag = true ; } else { result = result - arr [ i ] ; flag = false ; } } return result ; } static void findResult ( int [ ] arr , int n , Query [ ] q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { Console . Write ( findResultUtil ( arr , q [ i ] . L , q [ i ] . R ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 } ; int n = arr . Length ; Query [ ] q = { new Query ( 2 , 5 ) , new Query ( 6 , 8 ) , new Query ( 1 , 7 ) , new Query ( 4 , 8 ) , new Query ( 0 , 5 ) } ; int m = q . Length ; findResult ( arr , n , q , m ) ; } }
Two elements whose sum is closest to zero | C # code to find Two elements whose sum is closest to zero ; Array should have at least two elements ; Initialization of values ; main function
using System ; class GFG { static void minAbsSumPair ( int [ ] arr , int arr_size ) { int l , r , min_sum , sum , min_l , min_r ; if ( arr_size < 2 ) { Console . Write ( " Invalid ▁ Input " ) ; return ; } min_l = 0 ; min_r = 1 ; min_sum = arr [ 0 ] + arr [ 1 ] ; for ( l = 0 ; l < arr_size - 1 ; l ++ ) { for ( r = l + 1 ; r < arr_size ; r ++ ) { sum = arr [ l ] + arr [ r ] ; if ( Math . Abs ( min_sum ) > Math . Abs ( sum ) ) { min_sum = sum ; min_l = l ; min_r = r ; } } } Console . Write ( " ▁ The ▁ two ▁ elements ▁ whose ▁ " + " sum ▁ is ▁ minimum ▁ are ▁ " + arr [ min_l ] + " ▁ and ▁ " + arr [ min_r ] ) ; } public static void Main ( ) { int [ ] arr = { 1 , 60 , - 10 , 70 , - 80 , 85 } ; minAbsSumPair ( arr , 6 ) ; } }
Maximize sum of topmost elements of S stacks by popping at most N elements | C # program to maximize the sum of top of the stack values of S stacks by popping at most N elements ; Function for computing the maximum sum at the top of the stacks after popping at most N elements from S stack ; Constructing a dp matrix of dimensions ( S + 1 ) x ( N + 1 ) ; Loop over all i stacks ; Store the maximum of popping j elements up to the current stack by popping k elements from current stack and j - k elements from all previous stacks combined ; Store the maximum sum of popping N elements across all stacks ; dp [ S , N ] has the maximum sum ; Driver code ; Number of stacks ; Length of each stack ; Maximum elements that can be popped
using System ; class GFG { static int maximumSum ( int S , int M , int N , int [ , ] stacks ) { int [ , ] dp = new int [ S + 1 , N + 1 ] ; for ( int i = 0 ; i < S ; i ++ ) { for ( int j = 0 ; j <= N ; j ++ ) { for ( int k = 0 ; k <= Math . Min ( j , M ) ; k ++ ) { dp [ i + 1 , j ] = Math . Max ( dp [ i + 1 , j ] , stacks [ i , k ] + dp [ i , j - k ] ) ; } } } int result = int . MinValue ; for ( int i = 0 ; i <= N ; i ++ ) { result = Math . Max ( result , dp [ S , i ] ) ; } return result ; } public static void Main ( String [ ] args ) { int S = 2 ; int M = 4 ; int [ , ] stacks = { { 2 , 6 , 4 , 5 } , { 1 , 6 , 15 , 10 } } ; int N = 3 ; Console . Write ( maximumSum ( S , M , N , stacks ) ) ; } }
Abstraction of Binary Search | C # program for the above example ; Function to find X such that it is less than the target value and function is f ( x ) = x ^ 2 ; Initialise start and end ; Loop till start <= end ; Find the mid ; Check for the left half ; Store the result ; Reinitialize the start point ; Check for the right half ; Print the maximum value of x such that x ^ 2 is less than the targetValue ; Driver Code ; Given targetValue ; ; Function Call
using System ; class GFG { static void findX ( int targetValue ) { int start = 0 , end = targetValue ; int mid = 0 , result = 0 ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } Console . Write ( result + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int targetValue = 81 ; findX ( targetValue ) ; } }
Two elements whose sum is closest to zero | C # implementation using STL ; Modified to sort by abolute values ; Absolute value shows how close it is to zero ; If found an even close value update min and store the index ; Driver Code
using System ; class GFG { static void findMinSum ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( ! ( Math . Abs ( arr [ i - 1 ] ) < Math . Abs ( arr [ i ] ) ) ) { int temp = arr [ i - 1 ] ; arr [ i - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } int min = Int32 . MaxValue ; int x = 0 , y = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( Math . Abs ( arr [ i - 1 ] + arr [ i ] ) <= min ) { min = Math . Abs ( arr [ i - 1 ] + arr [ i ] ) ; x = i - 1 ; y = i ; } } Console . WriteLine ( " The ▁ two ▁ elements ▁ whose ▁ " + " sum ▁ is ▁ minimum ▁ are ▁ " + arr [ x ] + " ▁ and ▁ " + arr [ y ] ) ; } static void Main ( ) { int [ ] arr = { 1 , 60 , - 10 , 70 , - 80 , 85 } ; int n = arr . Length ; findMinSum ( arr , n ) ; } }
Probability of getting all possible values on throwing N dices | C # program to calculate the probability of all the possible values that can be obtained throwing N dices ; Store the probabilities ; Precompute the probabilities for values possible using 1 dice ; Compute the probabilies for all values from 2 to N ; Print the result ; Driver Code
using System ; class GFG { static void dicesSum ( int n ) { double [ , ] dp = new double [ n + 1 , 6 * n + 1 ] ; for ( int i = 1 ; i <= 6 ; i ++ ) dp [ 1 , i ] = 1 / 6.0 ; for ( int i = 2 ; i <= n ; i ++ ) for ( int j = i - 1 ; j <= 6 * ( i - 1 ) ; j ++ ) for ( int k = 1 ; k <= 6 ; k ++ ) { dp [ i , j + k ] += ( dp [ i - 1 , j ] * dp [ 1 , k ] ) ; } for ( int i = n ; i <= 6 * n ; i ++ ) { Console . WriteLine ( i + " ▁ " + Math . Round ( dp [ n , i ] * 1000.0 ) / 1000.0 ) ; } } static void Main ( ) { int n = 2 ; dicesSum ( n ) ; } }
Convert undirected connected graph to strongly connected directed graph | C # program for the above approach ; To store the assigned Edges ; Flag variable to check Bridges ; Function to implement DFS Traversal ; Mark the current node as visited ; Update the order of node v ; Update the bridge_detect for node v ; Traverse the adjacency list of Node v ; Ignores if same edge is traversed ; Ignores the edge u -- > v as v -- > u is already processed ; Finds a back Edges , cycle present ; Update the bridge_detect [ v ] ; Else DFS traversal for current node in the adjacency list ; Update the bridge_detect [ v ] ; Store the current directed Edge ; Condition for Bridges ; Return flag ; Function to print the direction of edges to make graph SCCs ; Arrays to store the visited , bridge_detect and order of Nodes ; DFS Traversal from vertex 1 ; If flag is zero , then Bridge is present in the graph ; Else print the direction of Edges assigned ; Function to create graph ; Traverse the Edges ; Push the edges in an adjacency list ; Driver code ; N vertices and M Edges ; To create Adjacency List ; Create an undirected graph ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static ArrayList ans ; static int flag = 1 ; static int dfs ( ArrayList adj , int [ ] order , int [ ] bridge_detect , bool [ ] mark , int v , int l ) { mark [ v ] = true ; order [ v ] = order [ l ] + 1 ; bridge_detect [ v ] = order [ v ] ; for ( int i = 0 ; i < ( ( ArrayList ) adj [ v ] ) . Count ; i ++ ) { int u = ( int ) ( ( ArrayList ) adj [ v ] ) [ i ] ; if ( u == l ) { continue ; } if ( order [ v ] < order [ u ] ) { continue ; } if ( mark [ u ] ) { bridge_detect [ v ] = Math . Min ( order [ u ] , bridge_detect [ v ] ) ; } else { dfs ( adj , order , bridge_detect , mark , u , v ) ; } bridge_detect [ v ] = Math . Min ( bridge_detect [ u ] , bridge_detect [ v ] ) ; ans . Add ( new int [ ] { v , u } ) ; } if ( bridge_detect [ v ] == order [ v ] && l != 0 ) { flag = 0 ; } return flag ; } static void convert ( ArrayList adj , int n ) { int [ ] order = new int [ n ] ; int [ ] bridge_detect = new int [ n ] ; bool [ ] mark = new bool [ n ] ; int flag = dfs ( adj , order , bridge_detect , mark , 1 , 0 ) ; if ( flag == 0 ) { Console . Write ( " - 1" ) ; } else { foreach ( int [ ] it in ans ) { Console . WriteLine ( it [ 0 ] + " - > " + it [ 1 ] ) ; } } } static void createGraph ( int [ , ] Edges , ArrayList adj , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; ( ( ArrayList ) adj [ u ] ) . Add ( v ) ; ( ( ArrayList ) adj [ v ] ) . Add ( u ) ; } } public static void Main ( string [ ] args ) { int N = 5 , M = 6 ; int [ , ] Edges = { { 0 , 1 } , { 0 , 2 } , { 1 , 2 } , { 1 , 4 } , { 2 , 3 } , { 3 , 4 } } ; ArrayList adj = new ArrayList ( ) ; ans = new ArrayList ( ) ; for ( int i = 0 ; i < N ; i ++ ) adj . Add ( new ArrayList ( ) ) ; createGraph ( Edges , adj , M ) ; convert ( adj , N ) ; } }
Shortest Un | Program to find the shortest subarray which is unsorted . ; boolean function to check array elements are in the increasing order or not ; boolean function to check array elements are in the decreasing order or not ; increasing and decreasing are two functions . function return true value then print 0 else 3 ; Driver program
using System ; class GFG { public static bool increasing ( int [ ] a , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] >= a [ i + 1 ] ) return false ; return true ; } public static bool decreasing ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] < arr [ i + 1 ] ) return false ; return true ; } public static int shortestUnsorted ( int [ ] a , int n ) { if ( increasing ( a , n ) == true || decreasing ( a , n ) == true ) return 0 ; else return 3 ; } public static void Main ( ) { int [ ] ar = new int [ ] { 7 , 9 , 10 , 8 , 11 } ; int n = ar . Length ; Console . WriteLine ( shortestUnsorted ( ar , n ) ) ; } }