Re: [algogeeks] Re: Number problem
@yellow your code turns 1000,100,10,2270,12130 to 1,1,1,27,123 repectively simply it removes all trailing zeros ... On Wed, Sep 22, 2010 at 8:10 PM, Yellow Sapphire pukhraj7...@gmail.comwrote: My Solution. Almost same as above but the flag is set by using bits. #define SETFLAG(flag, pos) (flag=flag | (1pos-1)) #define IS_FLAG_SET(flag,pos) (flag (1pos-1)) int remove_dup(int n) { int temp=0, temp2=0; unsigned int flag=0; /* * Reverse the number */ while (n0 (temp=temp*10 + n%10), n=n/10); /* * Find duplicates by setting the bits in the flag */ n=temp; temp=0; while (n0) { temp2=n%10; if(!IS_FLAG_SET(flag, temp2)) { temp=temp*10 + temp2; SETFLAG(flag, temp2); } n=n/10; } return temp; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- :-) * Nishant Agarwal Computer Science and Engineering NIT Allahabad * -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.
Re: [algogeeks] Re: Number problem
Here's the code int rem_dup(int n) { int *a,i,c,t,d,count[10]={0},arr[20],j=0,sum=0; a=(int *)malloc(sizeof(a)); for(i=0;n0;i++)//storing each digit of number in an array { a[i]=n%10; n=n/10; } c=d=i-1; i=0; while(ic)//reversing the array { t=a[i]; a[i++]=a[c]; a[c--]=t; } for(i=0;i=d;i++)//setting repeated digits to -1 {if(count[a[i]]==0) count[a[i]]++; else a[i]=-1; } for(i=0,j=0;i=d;i++) //storing all non repeating digits in anothor array arr[] { if(a[i]!=-1) { arr[j]=a[i]; j++; } } for(i=0;ij;i++) sum=sum+arr[i]*power(10,j-i-1); return sum; } On Thu, Sep 23, 2010 at 12:39 AM, Dave dave_and_da...@juno.com wrote: @Saurabh: Doesn't this turn 10 into 1? You need to count the digits in the number as you are reversing it, and replace the second while loop with a for loop with that many iterations. Dave On Sep 21, 11:28 pm, saurabh agrawal saurabh...@gmail.com wrote: @dave: your code is producing 4526 for input=24526 instead of 2456 Here's corrected code :) /CODE/// int function(int n){ int a[10]={0},temp=0,result =0; while(n){ //reverse the number.. temp=10*temp+n%10; n/=10; } n=temp; while(n){ ///remove duplicate digits... if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10; } return result;} ///END/ On Wed, Sep 22, 2010 at 8:51 AM, Dave dave_and_da...@juno.com wrote: @Saurabh: Doesn't your code turn 123 into 321? Try this: int function(int n) { int a[10]={0}; int result=0; int place=1; while(n){ if(a[n%10]==0){ a[n%10]=1; result+=(n%10)*place; place*=10; } n/=10; } return result; } Dave On Sep 21, 3:12 pm, saurabh agrawal saurabh...@gmail.com wrote: int function(int n){ int a[10]={0}; int result =0; while(n){ if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10;} return result;` } On Wed, Sep 22, 2010 at 12:39 AM, Albert alberttheb...@gmail.com wrote: Given a number find the number by eliminating the duplicate digits in the number.. for eg: 24526 ans is 2456 . int function(int n) { . . . } Give all sort of solutions to this problem. Efficiency in the code is important -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com algogeeks%2bunsubscr...@googlegroups.com algogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.-Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com algogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- :-) * Nishant Agarwal Computer Science and Engineering NIT Allahabad * -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.
Re: [algogeeks] Re: Number problem
My code failed because it was reversing the digits and thus 0 was getting added in the front which resulted in nothing. If allowed we can use a char array else will have to find a solution which does not reverse the digits of the number. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.
[algogeeks] Re: Number problem
My solution. int f(int n) { if (n = 0) return n; int digits = (int)log10(n) + 1; int m = 0; int flag[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int i = digits - 1; i = 0; --i) { int p = (int)pow(10.0, i); int d = n / p; n %= p; if (flag[d] 0) continue; flag[d] = 1; m = 10 * m + d; } return m; } -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.
[algogeeks] Re: Number problem
@Saurabh: Doesn't this turn 10 into 1? You need to count the digits in the number as you are reversing it, and replace the second while loop with a for loop with that many iterations. Dave On Sep 21, 11:28 pm, saurabh agrawal saurabh...@gmail.com wrote: @dave: your code is producing 4526 for input=24526 instead of 2456 Here's corrected code :) /CODE/// int function(int n){ int a[10]={0},temp=0,result =0; while(n){ //reverse the number.. temp=10*temp+n%10; n/=10; } n=temp; while(n){ ///remove duplicate digits... if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10; } return result;} ///END/ On Wed, Sep 22, 2010 at 8:51 AM, Dave dave_and_da...@juno.com wrote: @Saurabh: Doesn't your code turn 123 into 321? Try this: int function(int n) { int a[10]={0}; int result=0; int place=1; while(n){ if(a[n%10]==0){ a[n%10]=1; result+=(n%10)*place; place*=10; } n/=10; } return result; } Dave On Sep 21, 3:12 pm, saurabh agrawal saurabh...@gmail.com wrote: int function(int n){ int a[10]={0}; int result =0; while(n){ if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10;} return result;` } On Wed, Sep 22, 2010 at 12:39 AM, Albert alberttheb...@gmail.com wrote: Given a number find the number by eliminating the duplicate digits in the number.. for eg: 24526 ans is 2456 . int function(int n) { . . . } Give all sort of solutions to this problem. Efficiency in the code is important -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com algogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.-Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.
[algogeeks] Re: Number problem
@Saurabh: Doesn't your code turn 123 into 321? Try this: int function(int n) { int a[10]={0}; int result=0; int place=1; while(n){ if(a[n%10]==0){ a[n%10]=1; result+=(n%10)*place; place*=10; } n/=10; } return result; } Dave On Sep 21, 3:12 pm, saurabh agrawal saurabh...@gmail.com wrote: int function(int n){ int a[10]={0}; int result =0; while(n){ if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10;} return result;` } On Wed, Sep 22, 2010 at 12:39 AM, Albert alberttheb...@gmail.com wrote: Given a number find the number by eliminating the duplicate digits in the number.. for eg: 24526 ans is 2456 . int function(int n) { . . . } Give all sort of solutions to this problem. Efficiency in the code is important -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.
Re: [algogeeks] Re: Number problem
@dave: your code is producing 4526 for input=24526 instead of 2456 Here's corrected code :) /CODE/// int function(int n){ int a[10]={0},temp=0,result =0; while(n){ //reverse the number.. temp=10*temp+n%10; n/=10; } n=temp; while(n){ ///remove duplicate digits... if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10; } return result; } ///END/ On Wed, Sep 22, 2010 at 8:51 AM, Dave dave_and_da...@juno.com wrote: @Saurabh: Doesn't your code turn 123 into 321? Try this: int function(int n) { int a[10]={0}; int result=0; int place=1; while(n){ if(a[n%10]==0){ a[n%10]=1; result+=(n%10)*place; place*=10; } n/=10; } return result; } Dave On Sep 21, 3:12 pm, saurabh agrawal saurabh...@gmail.com wrote: int function(int n){ int a[10]={0}; int result =0; while(n){ if(a[n%10]==0){ a[n%10]=1; result=10*result+n%10; } n/=10;} return result;` } On Wed, Sep 22, 2010 at 12:39 AM, Albert alberttheb...@gmail.com wrote: Given a number find the number by eliminating the duplicate digits in the number.. for eg: 24526 ans is 2456 . int function(int n) { . . . } Give all sort of solutions to this problem. Efficiency in the code is important -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com algogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algoge...@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.