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
Should I learn data structures in c or python?
Define C in your own Language.
In C programming, how do you insert quote characters (‘ and “) into the output screen?
How can I implement a delay, or time a users response, with sub-second resolution?
What is the difference between fread buffer() and fwrite buffer()?
What are the similarities between c and c++?
What is difference between %d and %i in c?
What is difference between structure and union with example?
Explain what header files do I need in order to define the standard library functions I use?
What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25
What is the use of typedef in structure in c?
What is the difference between struct and typedef struct in c?
Explain pointers in c programming?
Create a registration form application by taking the details like username, address, phone number, email with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 3 users and display the details. While taking input password must appear as “****”.
Why do we use namespace feature?