841. Determine Simple Graph

You are given an integer array degrees, where degrees[i] represents the desired degree of the ith vertex.

Your task is to determine if there exists an undirected simple graph with exactly these vertex degrees.

A simple graph has no self-loops or parallel edges between the same pair of vertices.

Return true if such a graph exists, otherwise return false.

Example 1:

Input: degrees = [3,1,2,2]

Output: True

Explanation:

One possible undirected simple graph is:

  • Edges: (0, 1), (0, 2), (0, 3), (2, 3)
  • Degrees: deg(0) = 3, deg(1) = 1, deg(2) = 2, deg(3) = 2.

Example 2:

Input: degrees = [1,3,3,1]

Output: false

Explanation:​​​​​​​

  • degrees[1] = 3 and degrees[2] = 3 means they must be connected to all other vertices.
  • This requires degrees[0] and degrees[3] to be at least 2, but both are equal to 1, which contradicts the requirement.
  • Thus, the answer is false.

Now Your Turn!

Pick the correct output for the given input

Input: degrees = [0]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n == degrees.length <= 10​​​​​​​5
  • 0 <= degrees[i] <= n - 1

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
bool simpleGraphExists(vector<int> degrees) {
// (your implementation here)
}
};
Test Case

Input:

Degrees