Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6454 |

@blackshadows | 6380 |

@mb1973 | 5710 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3709 |

@mantu_singh | 3556 |

@marius_valentin_dragoi | 3523 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1285 |

@gurshehzadsingh | 1052 |

@pritsahkar2000 | 812 |

@satyamchhatrala | 804 |

@nithinreddy3210 | 801 |

@iib2019024 | 745 |

@mohanreddy8847 | 704 |

@iamchaitanyahegde | 662 |

@shafeeq3543 | 647 |

@workit | 646 |

@AkashLahoty | 642 |

Complete Leaderboard |

Given a weighted directed graph with n nodes and m edges. Nodes are labeled from 0 to n-1, the task is to check if it contains a negative weight cycle or not.

**Note: **edges[i] is defined as u, v and weight.

**Example 1:**

**Input: **n = 3, edges = {{0,1,-1},{1,2,-2},
{2,0,-3}}
**Output: **1
**Explanation: **The graph contains negative weight
cycle as 0->1->2->0 with weight -1,-2,-3,-1.

**Example 2:**

**Input: **n = 3, edges = {{0,1,-1},{1,2,-2},
{2,0,3}}
**Output: **0
**Explanation: **The graph does not contain any
negative weight cycle.

**Your Task:**

You don't need to read or print anyhting. Your task is to complete the function **isNegativeWeightCycle() **which takes n and edges as input paramater and returns 1 if graph contains negative weight cycle otherwise returns 0.

**Expected Time Complexity: **O(n*m)

**Expected Space Compelxity: **O(n)

**Constraints:**

1 <= n <= 100

1 <= m <= n*(n-1), where m is the total number of Edges in the directed graph.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Negative weight cycle

...