Advertisement
nikunjsoni

565

Sep 9th, 2021
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int arrayNesting(vector<int>& A) {
  4.         int res = 0, n = A.size();
  5.         vector<bool> seen(n);
  6.         for (int i: A) {
  7.             int cnt = 0;
  8.             while (!seen[i]) {
  9.                 seen[i] = true;
  10.                 cnt++;
  11.                 i = A[i];
  12.             }
  13.             res = max(res, cnt);
  14.         }
  15.         return res;
  16.     }
  17. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement