If there are 1 to 100 Numbers in array of 101 elements.
Which is the easy way to find repeated number?

Answer Posted / vadivelt

1.Get i/p of 101 elements and add all the nos.And say the
result of that Addition is 'sum'.

2.We all know that n(n+1)/2 is formula to calculate the
addition of 'n' numbers.

if n = 100 then n(n+1)/2 give addtion of 1....100.
so say total = n(n+1)/2;

3.Now 'sum' holds the addition of 1....101 nos
And 'total' holds the addition of 1....100 nos

So the repeated no would be.,
Result = sum - total;

Equalent Code is.,

#include<stdio.h>
main()
{
int i, n, sum = 0, a[150], Total;
printf("ENTER SIZE OF ARRAY:\n");
scanf("%d", &n);
printf("\nENTER ELEMENTS OF ARRAY:\n");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
sum = sum + a[i];
}
n = n-1;
Total = (n *(n+1) /2);
printf("\nREPEATED NO: %d",sum - Total);
getch();
}

Is This Answer Correct ?    38 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is it possible to provide special behavior for one instance of a template but not for other instances?

626


What is the v-ptr?

633


How does class accomplish data hiding in c++?

654


Differentiate between an array and a list?

701


Why do we learn c++?

525






What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?

561


What do you mean by “this” pointer?

613


What is function overloading in C++?

721


Is nan a c++?

608


What is a try block?

634


Difference between a homogeneous and a heterogeneous container

657


How would you use the functions memcpy(), memset(), memmove()?

622


What do you mean by persistent and non persistent objects?

730


Explain the concept of dynamic allocation of memory?

616


Why we use #include iostream in c++?

572