Given array of integers A[n],
 create an array B[n] such that B[i] = sum of all elements from A[1]
to A[i],

Now sort array B, and for each element B[i] ( smaller that equal to
X), do a (modified) binary search  for the closest value smaller than
equal to (X - B[i]) in array B[i+1... n]

Keep track of the closest difference...

Complexity ( n log n + n log n) = O(n log n)

On Nov 29, 7:13 pm, Mohit kumar lal <kumarmohit...@gmail.com> wrote:
> here it is similar type of question i found on spoj ,it asks only for the
> sum
>
> http://www.spoj.pl/problems/HOTELS/
>
> but it is giving TLE in O(n^2)..
>
> On Tue, Nov 29, 2011 at 5:51 PM, Nitin Garg <nitin.garg.i...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > cant think of anything better than O(N^2). Are you sure there exists such
> > algo?
>
> > On Tue, Nov 29, 2011 at 2:55 AM, Mohit kumar lal 
> > <kumarmohit...@gmail.com>wrote:
>
> >> Given a array of positive integers ,You have to find the largest sum
> >> possible from consecutive sub-array but sum should be less than or equal to
> >> K and also find that sub-array. Ex- array={2,1,3,4,5} k=12,
> >>  ans->12, sub-array={3,4,5}
>
> >> Firstly i tried with brute-force and then i also tried to solve it by DP
> >> but complexity were same ( O(n^2)) ....so plz try to provide a solution for
> >> O(nlgn) or O(n).
>
> >> --
> >> Mohit kumar lal
>
> >> IIIT ALLAHABAD
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Algorithm Geeks" group.
> >> To post to this group, send email to algogeeks@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> algogeeks+unsubscr...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/algogeeks?hl=en.
>
> > --
> > Nitin Garg
>
> > "Personality can open doors, but only Character can keep them open"
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algogeeks@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> Mohit kumar lal
> rit2009014
> IIIT ALLAHABAD
> contact@9454681805
> kumarmohit...@gmail.com
> mohitkumar...@yahoo.com
> rit2009...@iiita.ac.inhttp://profile.iiita.ac.in/rit2009014

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to