Find the Country without any outgoing path from given Graph
Last Updated :
01 Dec, 2022
Given a 2D array arr[][], where arr[i] = [Ai, Bi] means there exists a direct path going from country Ai to country Bi, the task is to find a country without an outgoing path.
Note: It is guaranteed that there will be only one such country and the given graph does not contain any cycle.
Examples:
Input: arr = {{“Germany”, ” Japan”}, {“United States”, “India”}, {“Japan”, “United States”}}
Output: “India”
Explanation: Starting from “Germany” you will reach the destination country “India”. Trip consists of: “Germany” -> “Japan” -> “United States” -> “India”.
Input: arr= [[“B”, “C”], [“D”, “B”], [“C”, “A”]]
Output: “A”
An approach using Hashing:
The idea is to map every route A -> B. This will keep a piece of information that from A we can reach to B. Now Iterate over all country B, and check if there exists any route from B to any other country. If not then this is the required answer.
Follow the steps below to implement the above idea:
- Initialize a map for mapping all country routes.
- Iterate over the given array arr[][].
- Map all country routes A->B into the map.
- Initialize a variable result, which will store the desired country.
- Iterate over the given array arr[][]
- Check if there exists any outgoing route to another country.
- If not, then save this as the desired country.
- Return the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string destCountry(vector<vector<string> >& arr)
{
unordered_map<string, string> unmap;
for ( auto s : arr)
unmap[s[0]] = s[1];
string result = "" ;
for ( auto s : arr) {
if (unmap.find(s[1]) == unmap.end()) {
result = s[1];
break ;
}
}
return result;
}
int main()
{
vector<vector<string> > arr
= { { "Germany" , "Japan" },
{ "United States" , "India" },
{ "Japan" , "United States" } };
cout << destCountry(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String destCountry(String[][] arr)
{
HashMap<String, String> unmap = new HashMap<>();
for (var s : arr) {
unmap.put(s[ 0 ], s[ 1 ]);
}
String result = "" ;
for (var s : arr) {
if (!unmap.containsKey(s[ 1 ])) {
result = s[ 1 ];
break ;
}
}
return result;
}
public static void main(String[] args)
{
String[][] arr = { { "Germany" , "Japan" },
{ "United States" , "India" },
{ "Japan" , "United States" } };
System.out.print(destCountry(arr));
}
}
|
Python3
def destCountry(arr):
unmap = dict ()
for s in arr:
unmap[s[ 0 ]] = s[ 1 ]
result = ""
for s in arr:
if s[ 1 ] not in unmap:
result = s[ 1 ]
break
return result
arr = [[ "Germany" , "Japan" ],[ "United States" , "India" ],[ "Japan" , "United States" ]]
print (destCountry(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static string destCountry( string [, ] arr)
{
Dictionary< string , string > unmap
= new Dictionary< string , string >();
for ( int i = 0; i < arr.GetLength(0); i++) {
unmap.Add(arr[i, 0], arr[i, 1]);
}
string result = "" ;
for ( int i = 0; i < arr.GetLength(0); i++) {
if (!unmap.ContainsKey(arr[i, 1])) {
result = arr[i, 1];
break ;
}
}
return result;
}
static public void Main()
{
string [, ] arr = { { "Germany" , "Japan" },
{ "United States" , "India" },
{ "Japan" , "United States" } };
Console.WriteLine(destCountry(arr));
}
}
|
Javascript
function destCountry(arr) {
let unmap = new Map();
for (let s of arr)
unmap.set(s[0], s[1]);
let result = "" ;
for (let s of arr) {
if (!unmap.has(s[1])) {
result = s[1];
break ;
}
}
return result;
}
let arr
= [[ "Germany" , "Japan" ],
[ "United States" , "India" ],
[ "Japan" , "United States" ]];
console.log(destCountry(arr));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...