Nishant G.

asked • 05/20/17

Meaning of "two comparisons" in the context of the provided question from geeksforgeeks.com

This is the exact question from the website.

Given a sorted array having 10 elements which contains 6 different numbers in which only 1 number is repeated five times. Your task is to find the duplicate number using two comparisons only.

I am not sure what "two comparisons" means here. Could you please shed some light on it. Thank you.
 
This is my code that I wrote as a solution to this question:
 
int FindDuplicate(vector<int>& nums)
{

int count = 1, marked = 0;

for (int i = 0; i < nums.size(); i++)
{

if (nums[i + 1] == nums[marked])
{
count++;

if (count == 5)
return nums[marked];
}

else
{
marked = i + 1;
count = 1;
}
}

return 0;
}

1 Expert Answer

By:

Matthew B. answered • 06/09/17

Tutor
5 (8)

Experienced Tutor Specializing in Computer Science

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.