<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p>

<p>Return <em>the single element that appears only once</em>.</p>

<p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int binarysearch(vector<int> nums){
        int s = 0, e = nums.size()-1, mid = s + (e-s)/2;

        while(s<=e){
            if(s == e){
                return nums[e];
            }

            //duplicates doesn't exist
            int currentvalue = nums[mid];
            int leftvalue = -1;
            int rightvalue = -1;
            if(mid-1 >= 0){
                leftvalue = nums[mid-1];
            }
            if(mid+1 < nums.size()){
                rightvalue = nums[mid+1];
            }

            if(currentvalue != leftvalue && currentvalue != rightvalue){
                return nums[mid];
            }

            //Duplicates exist on left
            else if(currentvalue == leftvalue){
                int pairstartingIndex = mid-1;
                if(pairstartingIndex & 1){
                    e = mid-1;
                }
                else{
                    s = mid+1;
                }
            }

            //Duplicates exist on the right side
            else if(currentvalue == rightvalue){
                int pairstartingIndex = mid;
                if(pairstartingIndex & 1){
                    e = mid-1;
                }
                else{
                    s = mid+1;
                }
            }
            mid = s + (e-s)/2;
        }
        return -1;
    }
    int singleNonDuplicate(vector<int>& nums) {
        return binarysearch(nums);
    }
};
25500