FastPrepFastPrep
Problem Brief

Converging Maze: Largest Sum Cycle

NEW GRADOA

You are given a maze with N cells. Each cell may have multiple entry points but not more than one exit (i.e. entry/exit points are unidirectional doors like valves).

The cells are named with an integer value from 0 to N-1.

You have to find: The sum of the largest sum cycle in the maze. Return -1 if there are no cycles.

Sum of a cycle is the sum of the node number of all nodes in that cycle.

Input Format:

1. The first line has the number of cells N.

2. The second line has a list of N values of the edge[] array. edge[i] contains the cell number that can be reached from cell 'i' in one step. edge[i] is -1 if the 'i'th cell doesn't have an exit.

Output Format:

The first line denotes the sum of the largest sum cycle.

🤗 A very special thanks to a fantastic friend for all the help!

1Example 1

Input
n = 23, edge = [4, 4, 1, 4, 13, 8, 8, 8, 0, 8, 14, 9, 15, 11, -1, 10, 15, 22, 22, 22, 22, 22, 21]
Output
45
Explanation
oo
public int largestSumCycle(int[] edge) {
  // write your code here
}
Input

n

23

edge

[4, 4, 1, 4, 13, 8, 8, 8, 0, 8, 14, 9, 15, 11, -1, 10, 15, 22, 22, 22, 22, 22, 21]

Output

45

Sign in to submit your solution.