Find if a degree sequence can form a simple graph | Havel | 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
function graphExists ( a , n ) { while ( 1 ) { a . sort ( ( a , b ) => b - a ) if ( a [ 0 ] == 0 ) return true ; var v = a [ 0 ] ; a . shift ( ) ; if ( v > a . length ) return false ; for ( var i = 0 ; i < v ; i ++ ) { a [ i ] -- ; if ( a [ i ] < 0 ) return false ; } } } var a = [ 3 , 3 , 3 , 3 ] ; var n = a . length ; graphExists ( a , n ) ? document . write ( " " ) : document . write ( " " ) ;