Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sep19 2023 - Find-the-duplicate-number #330

Open
1 task done
Nabil-Salah opened this issue Feb 10, 2024 · 1 comment
Open
1 task done

Sep19 2023 - Find-the-duplicate-number #330

Nabil-Salah opened this issue Feb 10, 2024 · 1 comment

Comments

@Nabil-Salah
Copy link

Description

https://leetcode.com/problems/find-the-duplicate-number/?envType=daily-question&envId=2023-09-19

Code of Conduct

@crazyGru
Copy link

crazyGru commented Jun 12, 2024

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);

        int tortoise = nums[0];
        int hare = nums[0];

        // Phase 1: Find the intersection point of the two pointers
        do {
            tortoise = nums[tortoise];
            hare = nums[nums[hare]];
        } while (tortoise != hare);

        // Phase 2: Find the entrance of the cycle
        tortoise = nums[0];
        while (tortoise != hare) {
            tortoise = nums[tortoise];
            hare = nums[hare];
        }

        return hare;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants