Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given an undirected graph 'EDGE_LIST' of ‘N’ nodes and ‘M’ edges. Your task is to return any Euler path of the graph. If no Euler path exists then you need to return -1.

```
An Euler path is a path in a graph such that every edge must be visited exactly once. You can visit the same vertex multiple times.
```

Detailed explanation

```
1 <= T <= 5
1 <= N <= 100
1 <= M <= min(100, N*(N-1)/2)
0 <= U, V <= N - 1
Time Limit: 1 sec
```

```
2
3 2
0 1
0 2
4 5
0 1
0 2
0 3
1 2
2 3
```

```
1
1
```

```
Test Case 1 :
The given graph is shown below.
```

```
Euler path = 1 -> 0 -> 2
Also, 2 -> 0 -> 1 is also a valid Euler path.
If we return any one of the above two paths then we will get output as 1.
Test Case 2 :
The given graph is shown below.
```

```
Euler path = 2 -> 0 -> 1 -> 2 -> 3 -> 0
If we return the above path then we will get output as 1.
Note : Here we have visited node 0 and 2 multiple times but we have visited each edge exactly once.
```

```
2
5 4
0 4
0 1
1 3
1 2
5 4
0 1
1 2
2 3
3 4
```

```
1
1
```

```
Test Case 1 :
The given graph is shown below.
```

```
There is no Euler path possible for the above graph. So, we need to return -1.
Test Case 2 :
The given graph is shown below.
```

```
Euler path = 0 -> 1 -> 2 -> 3 -> 4
```