we can use BIT or segment trees. algorithm using segment tree
1. intialize all node wid zeros 2. read the array element according their index update the node value. void update(int s, int e, int k, int node) { if (tree[node] < ar[k]) tree[node] = ar[k]; if (s==e) return; mid = (s+e)>> 1; if (k <= mid) update(s, mid, k, (node<<1)+1); else update(mid+1, e, k, (node<<1)+2); } 3. now ur query part start if left nodes value greater or equals to given value V then goto left node otherwise goto right node as soon as you reach the leaf node that will be your first fit bin and subtract value v from leaf node. and accordingly update value from leaf to root. if the algorithm is not clear. you are welcome to ask. -- 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.