<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p>

<p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p>

<p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,4,2,2]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,3,4,2]
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,3,3,3,3]
<strong>Output:</strong> 3</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>nums.length == n + 1</code></li>
	<li><code>1 &lt;= nums[i] &lt;= n</code></li>
	<li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li>
</ul>

<p>&nbsp;</p>
<p><b>Follow up:</b></p>

<ul>
	<li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li>
	<li>Can you solve the problem in linear runtime complexity?</li>
</ul>

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:    
        """
        =============================================
        Brute Force Algorithm:
        Time: O(n^2)
        Space: O(1)
        Cons: TLE
        Pros: No modification and constant space complexity
        ###########################################
        
        n = len(nums)
        for i in range(n):
            for j in range(i+1, n):
                if nums[i] == nums[j]:
                    return nums[i]

        ============================================
        Sorting based algorithm
        Time: O(n log n)
        Memory: O(n) or O(log n)
        Cons: Memory is not constant, modifued array
        Pros: Time improved.
        ############################################
        nums.sort()
        n = len(nums)
        for i in range(1, n):
            if nums[i] == nums[i-1]:
                return nums[i]

        ============================================
        Negative Marking
        Time: O(n)
        Memory: O(1)
        Pros: Linear Time Complexity, no additional space
        Cons: Modifies the given array and restores it
        #############################################
        n = len(nums)
        for i in range(n):
            curr = abs(nums[i])
            if nums[curr] < 0:
                duplicate = curr
                break
            nums[curr] *= -1

        for i in range(n):
            nums[i] = abs(nums[i])
        return duplicate

        ================================================
        Array as hashmap

        """

        def store(nums: List[int], cur: int):
            if cur == nums[cur]:
                return cur
            nxt = nums[cur]
            nums[cur] = cur
            return store(nums, nxt)
        
        return store(nums, 0)
        
        
        

37744