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
main(){char *str;scanf("%s",str);printf("%s",str); }The error in the above program is: a) Variable 'str' is not initialised b) Format control for a string is not %s c) Parameter to scanf is passed by value. It should be an address d) none
Write a program to know whether the input number is an armstrong number.
How pointers are declared?
What are disadvantages of C language.
Why isn't any of this standardized in c? Any real program has to do some of these things.
What is the right type to use for boolean values in c? Is there a standard type? Should I use #defines or enums for the true and false values?
Write a program that accept anumber in words
Why is c used in embedded systems?
Sir i need notes for structure,functions,pointers in c language can you help me please
What is the process to generate random numbers in c programming language?
Explain c preprocessor?
Write a function which takes as parameters one regular expression(only ? and * are the special characters) and a string and returns whether the string matched the regular expression.
Write a program to implement queue.
Write a program to print ASCII code for a given digit.
show how link list can be used to repersent the following polynomial i) 5x+2