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


Please Help Members By Posting Answers For Below Questions

Explain how can I pad a string to a known length?

647


What are enums in c?

655


`write a program to display the recomended action depends on a color of trafic light using nested if statments

1629


Calculate the weighted average of a list of n numbers using the formula xavg = f1x1+f2x2+ ….+ fnxn where the f’s are fractional weighting factors, i.e., 0<=fi<1, and f1+f2+….+fn = 1

3651


we called a function and passed something do it we have always passed the "values" of variables to the called function. such functions calles are called a) calls by reference b) calls by value c) calls by zero d) none of the above

631






What is the difference between printf and scanf in c?

745


An application package has been provided to you without any documents for the following application. The application needs to be tested. How will you proceed?

665


Explain what is the heap?

618


Explain what is the advantage of a random access file?

658


What are the types of data files?

723


How do I send escape sequences to control a terminal or other device?

604


Is there a way to have non-constant case labels (i.e. Ranges or arbitrary expressions)?

574


Explain zero based addressing.

602


What is the difference between Printf(..) and sprint(...) ?

780


How do you determine a file’s attributes?

598