FIND ELEMENTS: Bansal Sir
3 posters
Page 1 of 1
FIND ELEMENTS: Bansal Sir
let there be n elements
a1,a2,....,an
given an array with n*(n-1)/2 elements like this
a1+a2,a1+a3,a1+a4,..a1+an, a2+a3,a2+a4,a2+a5,...,a2+an,............................,aN-1,aN
In O(n)time and no extra space find the elements a1,..,aN
a1,a2,....,an
given an array with n*(n-1)/2 elements like this
a1+a2,a1+a3,a1+a4,..a1+an, a2+a3,a2+a4,a2+a5,...,a2+an,............................,aN-1,aN
In O(n)time and no extra space find the elements a1,..,aN
ballu- Posts : 58
Join date : 2009-02-01
Re: FIND ELEMENTS: Bansal Sir
in first iteration,
find sum of all numbers, so sum is n*(a1+a2----an) divide no by n so we get s=(a1+a2+a3+............an)
then traverse again after n step we get (a*n+(a2+a3+.....an)) = (n-1)a1 + s so we can calculate a1
similarly after next n-1 we get (n-1)*n2+(s-n2-n1) as we know a1 and s we can calculate a2
similarly we can calculate other values
complexity=o(2n)
am i right??????????
find sum of all numbers, so sum is n*(a1+a2----an) divide no by n so we get s=(a1+a2+a3+............an)
then traverse again after n step we get (a*n+(a2+a3+.....an)) = (n-1)a1 + s so we can calculate a1
similarly after next n-1 we get (n-1)*n2+(s-n2-n1) as we know a1 and s we can calculate a2
similarly we can calculate other values
complexity=o(2n)
am i right??????????
mnnit.rahul- Posts : 51
Join date : 2009-02-01
Re: FIND ELEMENTS: Bansal Sir
3rd last element =An-2 + An-1
2nd last element = An-1 + An
subtracting these we get An - An-1
now last element = An + An-1
now using these two we can obtain An and An-1
similarly we can find other values
2nd last element = An-1 + An
subtracting these we get An - An-1
now last element = An + An-1
now using these two we can obtain An and An-1
similarly we can find other values
mnnit.rahul- Posts : 51
Join date : 2009-02-01
Re: FIND ELEMENTS: Bansal Sir
ab to 2 no ko subtract kar raha hoon ab kyu hoga overflow
mnnit.rahul- Posts : 51
Join date : 2009-02-01
Similar topics
» ASCII VALUE : Bansal Sir
» PRODUCTS: Bansal Sir
» Guess Output - Bansal Sir
» from ACM modified first problem:Bansal Sir
» NESTED INDENTATION:Bansal Sir
» PRODUCTS: Bansal Sir
» Guess Output - Bansal Sir
» from ACM modified first problem:Bansal Sir
» NESTED INDENTATION:Bansal Sir
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|