If there are 1 to 100 Numbers in array of 101 elements.
Which is the easy way to find repeated number?
Answers were Sorted based on User's Feedback
Answer / 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 |
What are register variables?
What is the difference between an enumeration and a set of pre-processor # defines?
Why is c++ called oops?
Search for: what is pair in c++?
Is arr and &arr are same expression for an array?
What are the differences between a struct in C and in C++?
sir i want to study the c++ course but ino what is the qualification and the study methode please reply more details in c++
How come you find out if a linked-list is a cycle or not?
Explain what data encapsulation is in c++?
What is setiosflags c++?
What are c++ redistributables?
What are dynamic type checking?