Re: [algogeeks] Re: i cannot solve this written test question

2011-11-13 Thread Ramakant Sharma
start from rightmost digit
find:i th positionsuch that ele[i]!=9
find j th position i ele[j]!=0
decrement jth position element and increment i th position element.
.
.
ex:ele=134
i=0,---no j found
i=1,j=0
==143
ele=23998
i=0,---no j found
i=1,---ele[i]=9
i=2,---ele[i]=9
i=3,j=0
==24997
ele 8000100
i=2 --no j found
i=3 j=2;
8001000

-- 
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.



Re: [algogeeks] Re: i cannot solve this written test question

2011-11-08 Thread sagar sindwani
if ( ten's place is != 9)
Decrement the unit place by 1 and increment the ten's place by 1.
else
Increment MSD(Most significant digit) by 1 and decrement next digit by 1.


example:-

712   721

897   987

On Sun, Oct 9, 2011 at 6:51 PM, wujin chen wujinchen...@gmail.com wrote:

 @Navneet, i mean the length of N is less than 1000, not the value of N .
 *so, N maybe cannot be represented in 32bit . 10^1000-1 is huge number~~
 *

 2011/10/9 Navneet navneetn...@gmail.com

 sumOfDigits(i) - simply sums all the digits and returns the value.
 sortDigits() - takes a number and return the lowest possible number
 possible with digits of param passed.
 flag = false;

 for(int i=n+1, i  1000; i++) //mention to go upto 1000 in problem
 {
 if(sumOfDigits(i) == sumOfDigits(n))
 {
 //a candidate
 int sorted i = sortDigits(i);
 int sorted n = sortDigits(n);
 if(i == n)
 {
 //we found one
 cout\nDesired number is iendl; //may abort or continue to find
 more
 flag = true;
 }
 }
 if(!flag)
  cout\nNo such number foundendl;
 }

 On Oct 9, 5:04 pm, wujin chen wujinchen...@gmail.com wrote:
  @Aamir , yes, for some N, it will be no ans, then return -1.
 
  2011/10/9 Aamir Khan ak4u2...@gmail.com
 
 
 
 
 
 
 
   Answer won't be possible in for each N. What would be answer for
 N=999 ?
 
   On Sun, Oct 9, 2011 at 4:22 PM, Ankur Garg ankurga...@gmail.com
 wrote:
 
   Is it sum of bits or sum of digits ?
 
   On Sun, Oct 9, 2011 at 1:39 PM, wujin chen wujinchen...@gmail.com
 wrote:
 
   Given a positive number N, find a minimum number M greater than N,
 M  has
   the same length with N and the sum of the bits are equal.
 
   example:
   N=134 , M=143,  // 1+3+4=1+4+3
   N=020, M = 101, //2=1+1
 
   the length of N is less than 1000.
 
--
   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.
 
--
   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.
 
   --
   Aamir Khan | 3rd Year  | Computer Science  Engineering | IIT Roorkee
 
--
   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.

 --
 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.


  --
 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.


-- 
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.



[algogeeks] Re: i cannot solve this written test question

2011-11-08 Thread dee
@sagar, your method is correct for three digits, but since the
question says the length could be upto 1000, the MSD in those cases
will change
Eg. 23998  24997 and not 32998
So in your algo, MSD should be replaced by while moving away from
tens, hundreds, thousands... and so on, the first digit which is not
9
Also, the algo should return -1 if the above while loop doesn't
end(that is no non-9 digit is encountered) for cases like 999, 998,
7 etc.



On Nov 8, 1:52 pm, sagar sindwani sindwani.sa...@gmail.com wrote:
 if ( ten's place is != 9)
 Decrement the unit place by 1 and increment the ten's place by 1.
 else
 Increment MSD(Most significant digit) by 1 and decrement next digit by 1.

 example:-

 712   721

 897   987







 On Sun, Oct 9, 2011 at 6:51 PM, wujin chen wujinchen...@gmail.com wrote:
  @Navneet, i mean the length of N is less than 1000, not the value of N .
  *so, N maybe cannot be represented in 32bit . 10^1000-1 is huge number~~
  *

  2011/10/9 Navneet navneetn...@gmail.com

  sumOfDigits(i) - simply sums all the digits and returns the value.
  sortDigits() - takes a number and return the lowest possible number
  possible with digits of param passed.
  flag = false;

  for(int i=n+1, i  1000; i++) //mention to go upto 1000 in problem
  {
  if(sumOfDigits(i) == sumOfDigits(n))
  {
  //a candidate
  int sorted i = sortDigits(i);
  int sorted n = sortDigits(n);
  if(i == n)
  {
  //we found one
  cout\nDesired number is iendl; //may abort or continue to find
  more
  flag = true;
  }
  }
  if(!flag)
   cout\nNo such number foundendl;
  }

  On Oct 9, 5:04 pm, wujin chen wujinchen...@gmail.com wrote:
   @Aamir , yes, for some N, it will be no ans, then return -1.

   2011/10/9 Aamir Khan ak4u2...@gmail.com

Answer won't be possible in for each N. What would be answer for
  N=999 ?

On Sun, Oct 9, 2011 at 4:22 PM, Ankur Garg ankurga...@gmail.com
  wrote:

Is it sum of bits or sum of digits ?

On Sun, Oct 9, 2011 at 1:39 PM, wujin chen wujinchen...@gmail.com
  wrote:

Given a positive number N, find a minimum number M greater than N,
  M  has
the same length with N and the sum of the bits are equal.

example:
N=134 , M=143,  // 1+3+4=1+4+3
N=020, M = 101, //2=1+1

the length of N is less than 1000.

 --
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.

 --
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.

--
Aamir Khan | 3rd Year  | Computer Science  Engineering | IIT Roorkee

 --
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.

  --
  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.

   --
  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.

-- 
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.



[algogeeks] Re: i cannot solve this written test question

2011-10-09 Thread shady
for N=020, M = 101, //2=1+1
why are you adding 0 before 20, just give -1 as the answer.
if adding 0 is allowed then we can give answer for all inputs

for N=999
make it as N=0999, M=1899

On Oct 9, 5:04 pm, wujin chen wujinchen...@gmail.com wrote:
 @Aamir , yes, for some N, it will be no ans, then return -1.

 2011/10/9 Aamir Khan ak4u2...@gmail.com







  Answer won't be possible in for each N. What would be answer for N=999 ?

  On Sun, Oct 9, 2011 at 4:22 PM, Ankur Garg ankurga...@gmail.com wrote:

  Is it sum of bits or sum of digits ?

  On Sun, Oct 9, 2011 at 1:39 PM, wujin chen wujinchen...@gmail.comwrote:

  Given a positive number N, find a minimum number M greater than N, M  has
  the same length with N and the sum of the bits are equal.

  example:
  N=134 , M=143,  // 1+3+4=1+4+3
  N=020, M = 101, //2=1+1

  the length of N is less than 1000.

   --
  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.

   --
  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.

  --
  Aamir Khan | 3rd Year  | Computer Science  Engineering | IIT Roorkee

   --
  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.

-- 
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.



[algogeeks] Re: i cannot solve this written test question

2011-10-09 Thread Navneet
sumOfDigits(i) - simply sums all the digits and returns the value.
sortDigits() - takes a number and return the lowest possible number
possible with digits of param passed.
flag = false;

for(int i=n+1, i  1000; i++) //mention to go upto 1000 in problem
{
if(sumOfDigits(i) == sumOfDigits(n))
{
//a candidate
int sorted i = sortDigits(i);
int sorted n = sortDigits(n);
if(i == n)
{
//we found one
cout\nDesired number is iendl; //may abort or continue to find
more
flag = true;
}
}
if(!flag)
 cout\nNo such number foundendl;
}

On Oct 9, 5:04 pm, wujin chen wujinchen...@gmail.com wrote:
 @Aamir , yes, for some N, it will be no ans, then return -1.

 2011/10/9 Aamir Khan ak4u2...@gmail.com







  Answer won't be possible in for each N. What would be answer for N=999 ?

  On Sun, Oct 9, 2011 at 4:22 PM, Ankur Garg ankurga...@gmail.com wrote:

  Is it sum of bits or sum of digits ?

  On Sun, Oct 9, 2011 at 1:39 PM, wujin chen wujinchen...@gmail.comwrote:

  Given a positive number N, find a minimum number M greater than N, M  has
  the same length with N and the sum of the bits are equal.

  example:
  N=134 , M=143,  // 1+3+4=1+4+3
  N=020, M = 101, //2=1+1

  the length of N is less than 1000.

   --
  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.

   --
  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.

  --
  Aamir Khan | 3rd Year  | Computer Science  Engineering | IIT Roorkee

   --
  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.

-- 
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.



Re: [algogeeks] Re: i cannot solve this written test question

2011-10-09 Thread wujin chen
@Navneet, i mean the length of N is less than 1000, not the value of N .
*so, N maybe cannot be represented in 32bit . 10^1000-1 is huge number~~
*
2011/10/9 Navneet navneetn...@gmail.com

 sumOfDigits(i) - simply sums all the digits and returns the value.
 sortDigits() - takes a number and return the lowest possible number
 possible with digits of param passed.
 flag = false;

 for(int i=n+1, i  1000; i++) //mention to go upto 1000 in problem
 {
 if(sumOfDigits(i) == sumOfDigits(n))
 {
 //a candidate
 int sorted i = sortDigits(i);
 int sorted n = sortDigits(n);
 if(i == n)
 {
 //we found one
 cout\nDesired number is iendl; //may abort or continue to find
 more
 flag = true;
 }
 }
 if(!flag)
  cout\nNo such number foundendl;
 }

 On Oct 9, 5:04 pm, wujin chen wujinchen...@gmail.com wrote:
  @Aamir , yes, for some N, it will be no ans, then return -1.
 
  2011/10/9 Aamir Khan ak4u2...@gmail.com
 
 
 
 
 
 
 
   Answer won't be possible in for each N. What would be answer for N=999
 ?
 
   On Sun, Oct 9, 2011 at 4:22 PM, Ankur Garg ankurga...@gmail.com
 wrote:
 
   Is it sum of bits or sum of digits ?
 
   On Sun, Oct 9, 2011 at 1:39 PM, wujin chen wujinchen...@gmail.com
 wrote:
 
   Given a positive number N, find a minimum number M greater than N, M
  has
   the same length with N and the sum of the bits are equal.
 
   example:
   N=134 , M=143,  // 1+3+4=1+4+3
   N=020, M = 101, //2=1+1
 
   the length of N is less than 1000.
 
--
   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.
 
--
   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.
 
   --
   Aamir Khan | 3rd Year  | Computer Science  Engineering | IIT Roorkee
 
--
   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.

 --
 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.



-- 
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.