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
Explain what does a function declared as pascal do differently?
How do you determine whether to use a stream function or a low-level function?
Can stdout be forced to print somewhere other than the screen?
State two uses of pointers in C?
Explain what are compound statements?
What is an array? What the different types of arrays in c?
How to declare a variable?
Difference between malloc() and calloc() function?
What is a keyword?
What is #define in c?
Where in memory are my variables stored?
Explain the use of keyword 'register' with respect to variables.
What is the default value of local and global variables in c?
What is wrong with this statement? Myname = 'robin';
What is the general form of function in c?