Maximize count of nodes disconnected from all other nodes in a Graph
Given two integers N and E which denotes the number of nodes and the number of edges of an undirected graph, the task is to maximize the number of nodes which is not connected to any other node in the graph, without using any self-loops.
Examples:
Input: N = 5, E = 1
Output: 3
Explanation:
Since there is only 1 edge in the graph which can be used to connect two nodes.
Therefore, three node remains disconnected.Input: N = 5, E = 2
Output: 2
Approach: The approach is based on the idea that to maximize the number of disconnected nodes, the new nodes will not be added to the graph until every two distinct nodes become connected. Below are the steps to solve this problem:
- Initialize two variables curr and rem to store the nodes connected and the edges remaining unassigned respectively.
- If rem becomes 0, then the required answer will be N – curr.
- Otherwise, increment the value of curr by 1.
- So, the maximum edges needed in the current step to keep every two distinct nodes connected is min(rem, curr). Subtract it from rem and increment curr.
- Repeat this process until rem reduces to zero.
- Finally, print N – curr.
Below is the implementation of the above approach:
C++
// C++ implementation of// the above approach#include <bits/stdc++.h>using namespace std;// Function which returns// the maximum number of// isolated nodesint maxDisconnected(int N, int E){ // Used nodes int curr = 1; // Remaining edges int rem = E; // Count nodes used while (rem > 0) { rem = rem - min( curr, rem); curr++; } // If given edges are non-zero if (curr > 1) { return N - curr; } else { return N; }}// Driver Codeint main(){ // Given N and E int N = 5, E = 1; // Function Call cout << maxDisconnected(N, E); return 0;} |
Java
// Java implementation of// the above approachimport java.util.*;class GFG{// Function which returns// the maximum number of// isolated nodesstatic int maxDisconnected(int N, int E){ // Used nodes int curr = 1; // Remaining edges int rem = E; // Count nodes used while (rem > 0) { rem = rem - Math.min( curr, rem); curr++; } // If given edges are non-zero if (curr > 1) { return N - curr; } else { return N; }}// Driver Codepublic static void main(String[] args){ // Given N and E int N = 5, E = 1; // Function call System.out.print(maxDisconnected(N, E));}}// This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of# the above approach# Function which returns# the maximum number of# isolated nodesdef maxDisconnected(N, E): # Used nodes curr = 1 # Remaining edges rem = E # Count nodes used while (rem > 0): rem = rem - min(curr, rem) curr += 1 # If given edges are non-zero if (curr > 1): return N - curr else: return N# Driver Codeif __name__ == '__main__': # Given N and E N = 5 E = 1 # Function call print(maxDisconnected(N, E))# This code is contributed by mohit kumar 29 |
C#
// C# implementation of// the above approachusing System;class GFG{// Function which returns// the maximum number of// isolated nodesstatic int maxDisconnected(int N, int E){ // Used nodes int curr = 1; // Remaining edges int rem = E; // Count nodes used while (rem > 0) { rem = rem - Math.Min(curr, rem); curr++; } // If given edges are non-zero if (curr > 1) { return N - curr; } else { return N; }}// Driver Codepublic static void Main(String[] args){ // Given N and E int N = 5, E = 1; // Function call Console.Write(maxDisconnected(N, E));}} // This code is contributed by 29AjayKumar |
Javascript
<script>// Javascript implementation of// the above approach // Function which returns// the maximum number of// isolated nodesfunction maxDisconnected(N,E){ // Used nodes let curr = 1; // Remaining edges let rem = E; // Count nodes used while (rem > 0) { rem = rem - Math.min( curr, rem); curr++; } // If given edges are non-zero if (curr > 1) { return N - curr; } else { return N; }}// Driver Code// Given N and Elet N = 5, E = 1;// Function calldocument.write(maxDisconnected(N, E));// This code is contributed by unknown2108</script> |
3
Time Complexity: O(E)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


