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 pointers used for c++?
When one must use recursion function? Mention what happens when recursion functions are declared inline?
Write my own zero-argument manipulator that should work same as hex?
What is data hiding c++?
What are guid?
What will happen if a pointer is deleted twice?
Must accepts "Maestro Cards" Tax for bike should be less than 15 Total number of lanes is more than 10 Must provides monthly pass Write a method: boolean isGoodTollBridge(String[] cardsAccepted, String[] tollTax, boolean hasMonthlyPass, int numberOfLanes); String[] cardsAccepted A String array of names of card types accepted for payment of toll tax, it can be null if the toll does not accept any card String[] tollTax A String array of toll tax chart (say “Train : 300â€Â,â€ÂBullCart : 10â€Â) boolean hasMonthlyPass This parameter defines whether there is any monthly pass available or not int numberOfLanes This parameter defines the number of lanes for each side
Does c++ support exception handling?
What is a .lib file in c++?
What is c++ stringstream?
Describe friend function & its advantages.
What is the need of a destructor? Explain with the help of an example.
What do you mean by friend class & friend function in c++?
why and when we can declar member fuction as a private in the class?
Is string data type in c++?