Find if a degree sequence can form a simple graph | Havel | C # implementation of the approach ; Function that returns true if a simple graph exists ; Keep performing the operations until one of the stopping condition is met ; Sort the list in non - decreasing order ; Check if all the elements are equal to 0 ; Store the first element in a variable and delete it from the list ; Check if enough elements are present in the list ; Subtract first element from next v elements ; Check if negative element is encountered after subtraction ; Driver Code
using System ; using System . Collections ; class GFG { static bool graphExists ( ArrayList a , int n ) { while ( true ) { a . Sort ( ) ; a . Reverse ( ) ; if ( ( int ) a [ 0 ] == 0 ) return true ; int v = ( int ) a [ 0 ] ; a . Remove ( a [ 0 ] ) ; if ( v > a . Count ) return false ; for ( int i = 0 ; i < v ; i ++ ) { a [ i ] = ( int ) a [ i ] - 1 ; if ( ( int ) a [ i ] < 0 ) return false ; } } } public static void Main ( string [ ] args ) { ArrayList a = new ArrayList ( ) { 3 , 3 , 3 , 3 } ; int n = a . Count ; if ( graphExists ( a , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " NO " ) ; } } }