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 how can I pad a string to a known length?
What are enums in c?
`write a program to display the recomended action depends on a color of trafic light using nested if statments
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
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
What is the difference between printf and scanf in c?
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?
Explain what is the heap?
Explain what is the advantage of a random access file?
What are the types of data files?
How do I send escape sequences to control a terminal or other device?
Is there a way to have non-constant case labels (i.e. Ranges or arbitrary expressions)?
Explain zero based addressing.
What is the difference between Printf(..) and sprint(...) ?
How do you determine a file’s attributes?