Bellman ford – negative cycle


This is my code for detecting a negative cycle in a graph using bellman ford algorithm but I can’t figure out why it returns a wrong answer

public static final int INF = Integer.MAX_VALUE; private static int negativeCycle(ArrayList<Integer>[] adj, ArrayList<Integer>[] cost) {     int dep[] = new int[adj.length];     for(int i=0; i<adj.length; ++i)         dep[i] = INF;      dep[0] = 0;      for (int i = 0; i < adj.length-1; i++) {         for(int j = 0; j < adj.length; j++){             for (int v : adj[j]) {                 int v_index = adj[j].indexOf(v);                 if (dep[v] > dep[j] + cost[j].get(v_index)) {                     dep[v] = dep[j] + cost[j].get(v_index);                  }             }         }     }      for (int j = 0; j < adj.length; j++) {         for (int v : adj[j]) {             int v_index = adj[j].indexOf(v);             if (dep[v] > dep[j] + cost[j].get(v_index)) {                 return 1;             }         }     }      return 0; }