<p>Given an integer array <code>nums</code> of length <code>n</code> where all the integers of <code>nums</code> are in the range <code>[1, n]</code> and each integer appears <strong>once</strong> or <strong>twice</strong>, return <em>an array of all the integers that appears <strong>twice</strong></em>.</p>

<p>You must write an algorithm that runs in <code>O(n)</code> time and uses only <em>constant</em> auxiliary space, excluding the space needed to store the output</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [4,3,2,7,8,2,3,1]
<strong>Output:</strong> [2,3]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [1,1,2]
<strong>Output:</strong> [1]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> nums = [1]
<strong>Output:</strong> []
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= n</code></li>
	<li>Each element in <code>nums</code> appears <strong>once</strong> or <strong>twice</strong>.</li>
</ul>

class Solution {
//     What if we are asked to solve this without space, also in O(n) time. Can we do that?

// Think in terms of on and off. Turning on and turning off a switch. If a switch is already turned on, you can not turn it on again.

// Given this: [2,5,2,1,1,4]

// Go to index 0 and flip whatever value index 0 pointing to[if it was not flipped(turned on) before]...

// What does that mean?

// Index 0 has value 2. Flip whatever 2 is pointing-to. Since the array is 0 based, so we will flip 2-1 = 1 and that is 5.

// So, our result will look like:[2,-5,2,1,1,4]

// Next, we will go to index 1. After modifying the array, value at index 1 might or might not become negative. So, for this, we get the absolute value of that position. So, flip(if it was not flipped before) whatever Math.abs(arr[1])-1 = 4 pointing to.

// Since 4 is pointing to 1 and it is not negative(not flipped before), so we flip it to -1.

// [2,-5,2,1,-1,4]

// Next index 2, value 2, and it is pointing -5, which must be flipped by some other 2 in the array. So, we get our first duplicate value. Add 2 in our result and continue.
public:
    vector<int> findDuplicates(vector<int>& nums) {
         ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    vector<int> a;
    int index;
    for(int i=0;i<nums.size();i++){
        index=(abs(nums[i]))-1;
        if(nums[index]<0){
            a.push_back(abs(nums[i]));
        }
        nums[index]=-nums[index];
    }
        
return a;
    }
};
35962