Duplicate in array
You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3, and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array.
Note :
Input format :
Output Format :
Constraints :
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
int findDuplicate(int *arr, int n)
{
//Write your code here
int sum1 = ((n-2)*(n-1))/2;
int sum2=0;
for(int i=0;i<n; i++){
sum2=sum2+arr[i];
}
return sum2-sum1;
}
Comments
Post a Comment