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
What are the advantages of inheritance in c++?
What do the keywords volatile and mean mutable?
What is a volatile variable in c++?
How to demonstrate the use of a variable?
When the constructor of a base class calls a virtual function, why doesn't the override function of the derived class gets called?
What relational operators if statements in c++?
Do you need a main function in c++?
What is a template in c++?
What is the purpose of ios::basefield in the following statement?
How come you find out if a linked-list is a cycle or not?
Name the debugging methods that are used to solve problems?
Draw a flow chart and write a program for the difference between the sum of elements with odd and even numbers. Two dimensional array.
Can c++ do everything c can?
Can we define a constructor as virtual in c++?
Who invented turbo c++?