Missing and Repeated :Vinisha Ma'am
+2
ramthegreatcv
ballu
6 posters
Page 1 of 1
Missing and Repeated :Vinisha Ma'am
in array there are numbers only upto n
one is repeated and one is missing
find both the numbers
one is repeated and one is missing
find both the numbers
ballu- Posts : 58
Join date : 2009-02-01
Re: Missing and Repeated :Vinisha Ma'am
use the following method:
mark the missing number as M and the duplicated as D
1) compute the sum of regular list of numbers from 1 to N call it RegularSum
2) compute the sum of your array (the one with M and D) call it MySum
now you know that MySum+M-D=RegularSum
this is one equation.
the second one uses multiplication:
3) compute the multiplication of numbers of regular list of numbers from 1 to N call it RegularMultiplication
4) compute the multiplication of numbers of your list (the one with M and D) call it MyMultiplication
now you know that MyMultiplication=RegularMultiplication*D/M
at this point you have two equations with two parameters, solve and rule!
mark the missing number as M and the duplicated as D
1) compute the sum of regular list of numbers from 1 to N call it RegularSum
2) compute the sum of your array (the one with M and D) call it MySum
now you know that MySum+M-D=RegularSum
this is one equation.
the second one uses multiplication:
3) compute the multiplication of numbers of regular list of numbers from 1 to N call it RegularMultiplication
4) compute the multiplication of numbers of your list (the one with M and D) call it MyMultiplication
now you know that MyMultiplication=RegularMultiplication*D/M
at this point you have two equations with two parameters, solve and rule!
ramthegreatcv- Posts : 55
Join date : 2009-01-30
Age : 35
Re: Missing and Repeated :Vinisha Ma'am
sexy Ramp...mast solution..
$corpion- Posts : 25
Join date : 2009-01-30
Age : 37
@ramu
your case fails when sum/multiplication exceed the integer limit there exists better solutions for it
mnnit.rahul- Posts : 51
Join date : 2009-02-01
Re: Missing and Repeated :Vinisha Ma'am
as the numbers are upto n we can use count sort ..
in the array that saves the number of occurences of each number
1. value of count for the repeated number will be 2 and
2. for the missing number will be zero.
in the array that saves the number of occurences of each number
1. value of count for the repeated number will be 2 and
2. for the missing number will be zero.
udita- Posts : 11
Join date : 2009-02-01
Re: Missing and Repeated :Vinisha Ma'am
this is very popular interview question, the actual version goes like.
1) no Auxillary space(map,array etc) to be used.
2)O(n) time complexity.
LIMIT EXCEEDED:i agree that the limit might exceed.but this is the answer the interviewer is expecting from us.
NO MAPS:so map etc cannot be used.and for questions like that it is a trivial ans,not much impressive.for that matter take the question from subjective paper.that was solved by XOR,not by maps.
1) no Auxillary space(map,array etc) to be used.
2)O(n) time complexity.
LIMIT EXCEEDED:i agree that the limit might exceed.but this is the answer the interviewer is expecting from us.
NO MAPS:so map etc cannot be used.and for questions like that it is a trivial ans,not much impressive.for that matter take the question from subjective paper.that was solved by XOR,not by maps.
@ramu
yes one solution is we can solve it by xor
but explain to karo ramu bhai
there exist a solution without xor also which even doesn't use single extra variable
but explain to karo ramu bhai
there exist a solution without xor also which even doesn't use single extra variable
mnnit.rahul- Posts : 51
Join date : 2009-02-01
Similar topics
» Structure - Vinisha Ma'am
» QUEUE using STacks:Vinisha Ma'am
» List vs arrays:Vinisha Ma'am
» Copy alink list:Vinisha Ma'am
» QUEUE using STacks:Vinisha Ma'am
» List vs arrays:Vinisha Ma'am
» Copy alink list:Vinisha Ma'am
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|