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++
#include <bits/stdc++.h>
using namespace std;
int maxDisconnected( int N, int E)
{
int curr = 1;
int rem = E;
while (rem > 0) {
rem = rem
- min(
curr, rem);
curr++;
}
if (curr > 1) {
return N - curr;
}
else {
return N;
}
}
int main()
{
int N = 5, E = 1;
cout << maxDisconnected(N, E);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxDisconnected( int N, int E)
{
int curr = 1 ;
int rem = E;
while (rem > 0 )
{
rem = rem - Math.min(
curr, rem);
curr++;
}
if (curr > 1 )
{
return N - curr;
}
else
{
return N;
}
}
public static void main(String[] args)
{
int N = 5 , E = 1 ;
System.out.print(maxDisconnected(N, E));
}
}
|
Python3
def maxDisconnected(N, E):
curr = 1
rem = E
while (rem > 0 ):
rem = rem - min (curr, rem)
curr + = 1
if (curr > 1 ):
return N - curr
else :
return N
if __name__ = = '__main__' :
N = 5
E = 1
print (maxDisconnected(N, E))
|
C#
using System;
class GFG{
static int maxDisconnected( int N,
int E)
{
int curr = 1;
int rem = E;
while (rem > 0)
{
rem = rem - Math.Min(curr, rem);
curr++;
}
if (curr > 1)
{
return N - curr;
}
else
{
return N;
}
}
public static void Main(String[] args)
{
int N = 5, E = 1;
Console.Write(maxDisconnected(N, E));
}
}
|
Javascript
<script>
function maxDisconnected(N,E)
{
let curr = 1;
let rem = E;
while (rem > 0)
{
rem = rem - Math.min(
curr, rem);
curr++;
}
if (curr > 1)
{
return N - curr;
}
else
{
return N;
}
}
let N = 5, E = 1;
document.write(maxDisconnected(N, E));
</script>
|
Time Complexity: O(E)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Jun, 2021
Like Article
Save Article