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 |
Differentiate between late binding and early binding.
What is static function? Explain with an example
Inline parameters : What does the compiler do with the parameters of inline function, that can be evaluated in runtime ?
Difference between delete and free.
which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1?
Explain bubble sorting.
What is virtual destructors? Why they are used?
Which software is used for c++ programming?
What are iterators in c++?
Given the following seqment of code containing a group of nested if instructions: y = 9; if ((x==3) || (x == 5)) y++; else if (x == 2) y *= 2; else if (x == 4 ) y-= 7; else y = 8; Enter a segment of code (without any IF statements) that does exectly the same thing using the switch structure.
Is c++ the best programming language?
What is an overflow error?