@satveer: Yes, maintaining another stack is a solution. But we need to be careful about duplicate elements. If the current value is equal to the top of the " minimum stack" then also we need to push the element in both the stacks, i.e. the "main stack" and the "minimum stack".
Or may be we may store the count of the elements in the "minimum stack" with the elements. In that way, we may save space.
This is a simple recursive solution. You can optimize it by storing the solution to the sub-problems (dynamic programming).
void print_zero(int a, int n, int indx, int sum, int r, int r_indx)
for(i=indx; i<n; i++)
for(j=0; j<r_indx; j++)
print_zero(a, n, i+1, sum+a[i], r, r_indx+1);
int n = sizeof(a)/sizeof(a);
Above code has a compilation error in the statement mid = (low+high)>>;1;
an extra semicolon is there. The rest is fine. The complexity of this code in O(logn). However if you want to return the exact square root then you need to define some error margin, say "e". In that case the complexity will be O(log(n/e)).
/*This code properly maintains overflow by returning the number of 1's in the answer */
int f(int num)
int a = 111;
if(a%num == 0 ) return length;
a = a*10 + 1;
int length = f(23);
for(i=1; i<=length; i++)
Miguel Oliveira : The problem statement has been updated. The continuous part was not mentioned before. It was only " subsequence "- rajdeepgupta20 August 13, 2013