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

What is runtime errors c++?

675


What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?

774


What is function overloading in C++?

813


What are smart pointers?

778


Why do we need pointers?

665






What does it mean to declare a member variable as static?

694


What is function overriding in c++?

686


Explain container class.

803


When to use “const” reference arguments in a function?

682


What is c++ prototype?

682


Explain what are accessor methods?

761


What is the exit function in c++?

629


What number of digits that can be accuratly stored in a float (based on the IEEE Standard 754)? a) 6 b) 38 c) An unlimited number

907


If a base class declares a function to be virtual, and a derived class does not use the term virtual when overriding that class, is it still virtual when inherited by a third-generation class?

697


Explain the benefits of proper inheritance.

728