Showing posts with label Dynamic Programming. Show all posts
Showing posts with label Dynamic Programming. Show all posts

Thursday, June 11, 2009

print 1 to N

Write a C function that will print 1 to N one per each line on the
stdout where N is a int parameter to the function. The function should
not use while, for, do-while loops, goto statement, recursion, and
switch statement.

please do not post solutions using the ASM keyword and JUMP instructions inside it.

Solutions So Far:

Solution1:
Use setjmp and longjmp

Solution2:
they are used to invalidate the stack context, and return to the main function directly if inbetween more than 1 nested calls to other functions has been made. How will you use it for the desired purpose?


Solution3:
Is This Correct???

void nprint(int n)
{
char output[] = "1\n2\n3\n ... 32765\n32766\n32767\n";
char strn[5], *cp;

sprintf(strn, "%d", n);
cp = strstr(output, strn);
*(cp + strlen(strn) + 1) = '\0';

fputs(output, stdout);
}


Solution4:
I don't think this is a good question, i solved it using threads, but that's not really different of a recursion, changing the stack is just like a goto and the solution above use strstr that uses a loop, so I wouldn't spend much time trying to solve this shit question because all solutions will be crap.


Solution5:

Try This:
int i=1; //global

void a()
{
if(i==N) { printf("%d",i); exit(); }
else { printf("%d",i); i++; }
b();
}

void b()
{
if(i==N) { printf("%d",i); exit(); }
else { printf("%d",i); i++; }
a();
}


Solution6:
How about This:

#include
#include
#include
void main(int a,char **b)
{
int no,i,j;
char c[100]="ccprog ";
char d[100];
if(a==1)
{
printf("enter the value \n");
scanf("%d",&no);;
printf("%d\n",no);
no--;
if(no>0)
{
itoa(no,d,10);
strcat(c,d);
system(c);
}
}
else
{
no=atoi(b[1]);
printf("%d\n",no);
no--;
if(no>0)
{
itoa(no,d,10);
strcat(c,d);
system(c);
}
}
}



Do You Have Still Good Solution?
Please Share Here..

Saturday, May 30, 2009

monotonically increasing subsquence..

Give an o(n^2) time algo to find the longest monotonically increasing subsequence of a sequence of n numbers

Answers:
Check the dynamic programming section on Cormen's book.

its very simple for O(n^2)
can anyone tell me O(n lg n) solution. This is asked in cormen to do in O(n lg n) but i've failed to do so. An algorithm of O(n lg n) is given in dasgupta but i fail to understand how it can actually be implemented in O(n lg n)


O(nlogn)
DP coupled with binary search.
at each stage we get sorted sub-sequence, so for next element just searching(binary) that sequence to find position of that element in sub-sequence would do it.
It is basically O(nlogk) where k is length of longest MIS.



Friday, May 29, 2009

c + c++ = ?

C + C++=?
Different Answers for the Question c + c++=?
Answer1:
c + c++ = 2c
c + c++ = 2c+2

Answer2:
undefined behaviour


Answer3:
how can it give undefined behavior
lets give any value to c
suppose
Int c=1
then 
c + c++ = 2
This is not an undefined behavior.


Answer4:
I think,
the ans is 2c where c =....(means any positive number)



Now Tell Me Which Answer is Correct????

Monday, May 18, 2009

next palindrome number

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input
The first line contains integer t, the number of test cases. Followed by t lines containing integers K.

Output
For each K, output the smallest palindrome larger than K.
Example

Input:
2
808
2133

Output:
818
2222



Response1:
If ( (number already a palindrome) or (right half <> left half)
___ Copy reverse of left half to right half.


Response2:
perfect..above soln always gives correct answer.

for even no.of digits join n/2,n/2+1 th digit as one digit(so that u've to increment both these digits for 1st case).
ex.1441
ans->> 1551(if i'm not wrong)
here both the digits(2nd ,3rd).
this may also be done using naive approach like

while(!ispalindrome(++n))
;
output(n)

but this consumes a lot computing time.u can verify this
by giving large nos as i/p.


Response3:
the solution is not correct...
consider the case say 323421
now the right half ie 421 is greater than left 323
so according to your algo the answer wud be 323323
which is wrong...

the correct algo is
If ( (number already a palindrome) or (right half <> left half)
___increment the last digit of left half by 1 and copy the reverse of it to right half.



Response4:
>> In case of even length.. middle element is the rightmost element of left half.
>> otherwise middle element is separate from left half.

So, the correct algo goes like..

If( reverse left half> right half )
___ Copy reverse left half to right half
else
___ Increment middle element by 1 and then Copy reverse left half to right half.



Response5:
Modified algo

For even digits
increment left half until left half
copy left half to right half

Wednesday, April 1, 2009

Dynamic Programming: what is the space and time complexity of a dynamic programing algo for calulating the binomial coefficient C(n,k)

The Answer Is Simple
Space=O(n*k) Time=O(n*k)
space complexity is just the extra space you need.

To compute the binomial coefficient we use a 2-D matrix of size n*k..
Thus we use extra space of order n*k
and then use recursive definition of binomial coefficients
ie C(n,k)=C(n-1,k)+C(n-1,k-1)