Given an array of numbers, except for one number all the
others occur twice. Give an algorithm to find that number
which occurs only once in the array.
Answer Posted / priyanshu
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
long int find(long int a[],long int n,long int f,long int l)
{
long int mid;
mid=(f+l)/2;
if(a[mid]==a[mid+1])
{
return find(a,mid,mid+1,n-1);
}
else if(a[mid]==a[mid-1])
{
return find(a,mid,0,mid-1);
}
else
return a[mid];
}
int main()
{
long int n,i;
scanf("%ld",&n);
long int a[n];
for(i=0;i<n;i++)
scanf("%ld",&a[i]);
sort(a,a+n);
printf("%ld",find(a,n,0,n-1));
return 0;
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
What does do in c?
how to print electricity bill according to following charges first 100 units -1rs per unit for next 200 units-1.50 rs per unit without using conditions
When do we get logical errors?
Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?
How can you allocate arrays or structures bigger than 64K?
What is character set?
Why is void main used?
What are loops c?
What is I ++ in c programming?
Where static variables are stored in memory in c?
What is c value paradox explain?
Is c language still used?
What is operator promotion?
what is a function method?give example?
What is strcmp in c?