Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Odd Even Graph Check

Medium Acceptance 63.45% Points 30.00

You are given an undirected graph. Your task is to determine whether the graph contains an odd-length cycle or an even-length cycle.

Return "Odd" if an odd-length cycle exists; otherwise, return "Even".

Examples
Example 1

image Input: V = 3, edges = [[0,1],[1,2],[2,0]]
Output: "Odd"
Explanation: The graph contains a cycle of length 3, which is odd.

Example 2

image Input: V = 4, edges = [[0,1],[1,2],[2,3],[3,0]]
Output: "Even"
Explanation: The graph contains a cycle of length 4, which is even.

Example 3

image Input: V = 2, edges = [[0,1]]
Output: "Even"
Explanation: The graph contains no odd-length cycle.

Constraints
    • 1 = V = 105
    • 0 = E = 2 × 105
Companies
Flipkart Samsung
Topics
Graph
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions