Re: [algogeeks] Re: Directi question-centre of the tree

2012-06-21 Thread Ashish Goel
farthest from

2: Find a vertex v1 | the farthest form r.
3: Find a vertex v2 | the farthest form v1.



won't v2 be farthest from  r? or we are talking about alternate pats also



Best Regards
Ashish Goel
Think positive and find fuel in failure
+919985813081
+919966006652


On Wed, Jun 20, 2012 at 6:25 PM, adarsh kumar algog...@gmail.com wrote:

 As you traverse along and find the diameter of the tree, keep track of the
 number of nodes thereby traversed. Let that be equal to n.
 Now, centre is the node corresponding to floor((n+1)/2).


 On Wed, Jun 20, 2012 at 5:19 PM, Nishant Pandey 
 nishant.bits.me...@gmail.com wrote:

 I am asking again .. can any one please suggest better method for getting
 the median on the longest path of the tree ..
 efficient method .


 On Tue, Jun 19, 2012 at 5:08 PM, Nishant Pandey 
 nishant.bits.me...@gmail.com wrote:


 for getting diameter we can simply add the max height of left subtree
 and max height of the right sub tree .

 the main issue is how efficiently we find median on that longest path to
 get the center of the tree .
 can any bdy sugest optimized algo for that ?


 On Mon, Jun 18, 2012 at 6:08 PM, rajesh pandey 
 rajesh.pandey.i...@gmail.com wrote:

 I found it in some paper ;)

  Diameter and center
 De nition 4. The diameter of tree is the length of the longest path.
 De nition 5. A center is a vertex v such that the longest path from v
 to a leaf is minimal
 over all vertices in the tree.Tree center(s) can be found using simple
 algorithm.
 Algorithm 1. (Centers of tree)
 1: Choose a random root r.
 2: Find a vertex v1 | the farthest form r.
 3: Find a vertex v2 | the farthest form v1.
 4: Diameter is a length of path from v1 to v2.
 5: Center is a median element(s) of path from v1 to v2.

 This is O(n) algorithm. It is clear that we can't determine tree
 isomorphism faster
 than O(n). So, if we nd a O(f(n)) algorithm for rooted trees
 isomorphism we can also
 obtain O(f(n)) algorithm for ordinary trees.

 On Saturday, June 16, 2012 12:04:32 PM UTC+5:30, achala sharma wrote:

 I think this algorithm is used for calculating poset in graph.

 On Sat, Jun 16, 2012 at 3:04 AM, Hemesh Singh 
 hemesh.mn...@gmail.comwrote:

 + 1 for DK's solution. Is that a standard algorithm coz I feel like I
 have heard it somewhere ??


 On Mon, Aug 8, 2011 at 1:37 AM, DK divyekap...@gmail.com wrote:

 @KK: DFS and BFS are O(N) and Floyd Warshall is O(N^3).
 Could you please state how you can use the traversals directly to
 get the center? (And prove your correctness too?)

 The solution given by Wladimir ( expanded upon by me) is O(N) and
 uses (somewhat) the inverse of a BFS as a traversal.

 --
 DK

 http://twitter.com/divyekapoor
 http://gplus.to/divyekapoor
 http://www.divye.in

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/**msg/algogeeks/-/HnMOZtOrkqwJhttps://groups.google.com/d/msg/algogeeks/-/HnMOZtOrkqwJ.


 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to algogeeks+unsubscribe@
 **googlegroups.com algogeeks%2bunsubscr...@googlegroups.com.
 For more options, visit this group at http://groups.google.com/**
 group/algogeeks?hl=enhttp://groups.google.com/group/algogeeks?hl=en
 .




 --
 Hemesh singh

 --
 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+unsubscribe@*
 *googlegroups.com algogeeks%2bunsubscr...@googlegroups.com.
 For more options, visit this group at http://groups.google.com/**
 group/algogeeks?hl=enhttp://groups.google.com/group/algogeeks?hl=en
 .


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/BWplK7bCatMJ.

 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.




 --
 Cheers,

 Nishant Pandey |Specialist Tools Development  |  
 npan...@google.comgvib...@google.com |
 +91-9911258345





 --
 Cheers,

 Nishant Pandey |Specialist Tools Development  |  
 npan...@google.comgvib...@google.com |
 +91-9911258345


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

Re: [algogeeks] Re: Microsoft question

2012-06-21 Thread Abhishek Sharma
refer to this link http://www.ics.uci.edu/~eppstein/161/960130.html.
or Using quicksort , it can be done in O(n).
One more way to do this is to make min heap of size-k. Then insert elements
from the original array.If element is greater than root of the heap,swap
them.In the Last, root of the heap would be the kth smallest element

On Wed, Jun 20, 2012 at 8:47 PM, ajeet ajeet.sing...@gmail.com wrote:

 Hi,

 It looks like, The interviewer is expecting MinHeap from you,

 If modification to array is permitted just build the heap in place (from
 end bubbleUp  n-1 time) and extract K elements in sorted order
 Otherwise you need minimum O(K) memory

 Again if you want to use the quick-sort, I would prefer only using
 partition part of quick sort..
 1. Select any pivot 'P'
 2. Partition the array..
 3. position of the pivot p
 4. if p  k ( kth min lies on left part) repeat again for k
 5 if p  k ( kth min lies on right part) repeat again for k-p
 6 if p = k ( You are lucky) exit

 Again in worst case it is o(N2)

 -Ajeet

 Thanks
 -A


 On Wednesday, 20 June 2012 00:56:36 UTC+5:30, adarsh kumar wrote:

 This can be done using the concept of median of medians, wherein we can
 achieve linear time complexity in the worst case.
 This is a concept used in parallel algorithms too, check it out.

 On Mon, Jun 18, 2012 at 5:38 PM, Prem Nagarajan 
 prem.cmna...@gmail.comwrote:

 @enchantress : This problem can be solved using quicksort in O(nlogn).
 No need to go for selection sort.
 But O(n) solution is needed.


 On Mon, Jun 18, 2012 at 2:50 PM, enchantress elaenjoy...@gmail.comwrote:


 Hi all,
 A variation of selection sort can be used which takes O(nk) time.

 for i 1 to k
   min_index = i
   for j i+1 to n
  if a[j]  a[min_index]
 min_index = j
   swap(a[i],a[min_index])

 required element : a[k]

 On Sunday, 17 June 2012 08:13:18 UTC+5:30, Prem Nagarajan wrote:

 Give an array of unsorted elements, find the kth smallest element in
 the array.

 The expected time complexity is O(n) and no extra memory space should
 be used.

 Quickselect algorithm can be used to obtain the solution. Can anyone
 explain how quickselect works?

  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit https://groups.google.com/d/**
 msg/algogeeks/-/FABBRabzVqMJhttps://groups.google.com/d/msg/algogeeks/-/FABBRabzVqMJ
 .

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to algogeeks+unsubscribe@**
 googlegroups.com algogeeks%2bunsubscr...@googlegroups.com.
 For more options, visit this group at http://groups.google.com/**
 group/algogeeks?hl=en 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+unsubscribe@**
 googlegroups.com algogeeks%2bunsubscr...@googlegroups.com.
 For more options, visit this group at http://groups.google.com/**
 group/algogeeks?hl=en http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/sBvT2ztJpoUJ.

 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.




-- 
Abhishek Sharma
Under-Graduate Student,
PEC University of Technology

-- 
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] Microsoft Interview Question

2012-06-21 Thread Abhishek Sharma
find the element nearest to zero.make it pivot element.then apply one pass
of quicksort over the array.this is O(n)
On Thu, Jun 21, 2012 at 12:27 AM, Akshat Sapra sapraaks...@gmail.comwrote:

 void make_group( int a[], int size) {
   int j = 0;

  for ( int i = 0; i  size; i++ ) {
  if ( a[i]  0 ) {
 swap(a[i],a[j]);
 j++;
  }
 }
 }


 --


 Akshat Sapra
 Under Graduation(B.Tech)
 IIIT-Allahabad(Amethi Campus)
 *--*
 sapraaks...@gmail.com
 akshatsapr...@gmail.com
 rit20009008@ rit20009...@gmail.comiiita.ac.in

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




-- 
Abhishek Sharma
Under-Graduate Student,
PEC University of Technology

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

2012-06-21 Thread Abhishek Sharma
@umer
it's not random,but biased


On Wed, Jun 20, 2012 at 11:16 AM, Umer Farooq the.um...@gmail.com wrote:

 Hmmm, true. That's what I expected in this solution. Similarly, we can get
 3 by (3,3) (1,2)

 However, did you take a look at the other solution I proposed? I guess
 that solves the problem to some extent.


 On Tue, Jun 19, 2012 at 7:18 PM, Sourabh Singh 
 singhsourab...@gmail.comwrote:

 @Umer and Navin :
 1 is generated by (1,3) only.
 2 is generated by (1,1) and (2,3).

 so given solution is wrong


 On Tue, Jun 19, 2012 at 5:17 AM, Sourabh Singh 
 singhsourab...@gmail.comwrote:

 @ *ALL*

 please. post along with your method .
 proof than it make's equal distribution over the given range.

 On Tue, Jun 19, 2012 at 4:47 AM, Navin Kumar 
 algorithm.i...@gmail.comwrote:

 @Umer:

 rand(5) + (rand(5)%2): = it will never give 6 because for rand(7)
 range will be 0-6.
 So better try this: rand(5) + (rand(5)%3).


 On Tue, Jun 19, 2012 at 2:45 PM, Umer Farooq the.um...@gmail.comwrote:

 rand(5) + (rand(5)%2);


 On Tue, Jun 19, 2012 at 12:30 PM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ sry
 condition should be:

 if(20*prob = 500/7) :-)


 On Tue, Jun 19, 2012 at 12:26 AM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ALL

 Given a random number generator say r(5) generates number between
 1-5 uniformly at random , use it to in r(7) which should generate a 
 random
 number between 1-7 uniformly at random

 i have seen this on many site's but not a single correct solution.
 all solution's posted got rejected by someone else.:
 plz.. suggest some algo :

 my aprroach:

 let's assume a rectangle :

 100  |___
 |___|__
 500/7   |  ||
 |  ||
 |___|__|
 0 1  2  3 4  5 67
 now :

 let : num  = rand(5);
prob = rand(5);

if(prob = rand(5))
 print  num
else
 print  5 + num*(2/5)


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




 --
 Umer

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




 --
 Umer

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




-- 
Abhishek Sharma
Under-Graduate Student,
PEC University of Technology

-- 
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] SPOJ problem : CANDY

2012-06-21 Thread Bhavesh agrawal
in this prob also
for i=0, i to k
read cand
 rem=(rem+cand)%k

   if (rem) then no
else yes

-- 
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] SPOJ problem : CANDY

2012-06-21 Thread Mayank Singh
my code is running perfectly well but giving WA in spoj..
#includestdio.h
#includestdlib.h

int main()
{
int cand,sum;
int T,N,i,j,temp[10];
scanf(%d,T);

sum = 0;
for(i=0;iT;i++)
{
scanf(%d,N);
for(j=0;jN;j++)
{
scanf(%d,cand);
sum = (sum+cand)%N;
}
if(sum == 0)
temp[i]=1;
else
temp[i]=0;
}
for(i=0;iT;i++)
{
if(temp[i]==1)
printf(YES\n);
else
printf(NO\n);
}
return 0;
}

-- 
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] SPOJ problem : CANDY

2012-06-21 Thread Bhavesh agrawal
i think you should try to give output for each test case rather to use any
temp array

-- 
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] Microsoft Interview Question

2012-06-21 Thread Rishabh Agarwal
@Abhi: if you apply quick sort then again the order will will not be intact

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/ic2CXJXSGuoJ.
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: Microsoft Interview Question

2012-06-21 Thread Anchal Gupta
@akshat ur code doesn't give intact output, so i modified ur code and
here is the code :

int j=0,k=0;
   for (i = 0; i  n; ++i)
   {
 if(a[i]0)
 {
  a[j] = a[i];
  j++;
 }
 else
 {
  temp[k] = a[i];
  k++;
 }

   }

   k=0;
   for (i = j; i  n; ++i)
   {
 a[i] = temp[k];
 k++;
   }

On Jun 21, 1:47 pm, Rishabh Agarwal rishabh...@gmail.com wrote:
 @Abhi: if you apply quick sort then again the order will will not be intact

-- 
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] Double Linked List Represented With Single Linked List

2012-06-21 Thread Atul Singh
These posts will clear ur questions
http://www.geeksforgeeks.org/archives/12367
http://www.geeksforgeeks.org/archives/12615

-- 
ATul Singh | Computer Science  Engineering| 2008-12 Batch | NIT Jalandhar
 | 9530739855

-- 
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] Double Linked List Represented With Single Linked List

2012-06-21 Thread Amitesh Singh
Krishna,
  how can you store a address pointing to k bits value into k/2 bits? XOR
is the way to do this.


-- 
Amitesh




On Thu, Jun 21, 2012 at 1:20 AM, Krishna Kishore
kknarenkris...@gmail.comwrote:

 *np *is nothing but *next_pointer. np[x] *does mean *x-np *which is the
 next pointer to node. Actually I am thinking in this way about *np, *that
 it stores the *XOR* value of *next* and *prev* pointers.Or Since *np *is
 a k-bit integer, the first k/2 bits wil be used for *next*, and other k/2
 bits wil be used for *prev *pointer.

 On Wednesday, 20 June 2012 18:28:07 UTC+5:30, adarsh kumar wrote:

 Simple!
 Just traverse the doubly linked list and keep track of next and previous
 of each node, and do XOR and save the result in a new pointer, what
 according to you is np.
 Be careful about boundary cases, i.e head and tail, though.

 On Wed, Jun 20, 2012 at 5:16 PM, Krishna Kishore 
 kknarenkris...@gmail.com wrote:

 Explain how to implement doubly linked lists using only one pointer value
 * np[x*] per item instead of the usual two (next and prev). Assume that
 all pointer values can be interpreted as
 k-bit integers, and define* np[x] to be np[x] = next[x] XOR prev[x],*the 
 k-bit “exclusive-or” of next[x] and prev[x]. (The value NIL is
 represented by 0.) Be sure to describe what information is needed to
 access the head of the list. Show how to implement the SEARCH, INSERT, and
 DELETE operations on such a list. Also show how to reverse such a list in
 O(1) time.

 This is the Question in the Book *Introduction To Algorithms *By
 CORMEN ( MIT Press ) Page Number : 209 , Problem No: 10.2-8.

 Thanks in Advance.

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit https://groups.google.com/d/**
 msg/algogeeks/-/Uj1E8KXljAQJhttps://groups.google.com/d/msg/algogeeks/-/Uj1E8KXljAQJ
 .
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to algogeeks+unsubscribe@**
 googlegroups.com algogeeks%2bunsubscr...@googlegroups.com.
 For more options, visit this group at http://groups.google.com/**
 group/algogeeks?hl=en http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/JlNBCRsj9kUJ.

 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] SPOJ problem : CANDY III

2012-06-21 Thread Mayank Singh
here is my code :

#includestdio.h
#includestdlib.h

int main()
{
long long cand,sum;
int T,N,i,j,*temp;
scanf(%d,T);
temp= (int*)calloc(T, sizeof( int));
sum = 0;
for(i=0;iT;i++)
{
scanf(%d,N);
for(j=0;jN;j++)
{
scanf(%lld,cand);
sum = (sum+cand)%N;
}
if(sum == 0)
temp[i]=1;
else
temp[i]=0;
}
for(i=0;iT;i++)
{
if(temp[i]==1)
printf(YES\n);
else
printf(NO\n);
}
return 0;
}

it is giving WA in spoj. i am unable to find what is wrong with it..plz
help me.

-- 
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: Microsoft Interview Question

2012-06-21 Thread rusty
guys this is my solution to the problem, it's a bit sloppy but as far as I 
checked it was working please have a go at it?


#include stdio.h
#include stdlib.h

int main() {
int arr1[] = {0,-5,3,0,4,-6,-9};
int arr2[7];
int j = 0;
for ( int i = 0 ; i  7 ; i++ ) {
//loop for -ve numbers
if ( arr1[i]  0 )
arr2[j++] = arr1[i];

}

//accomodate all the zeros if any
for ( int i = 0 ; i  7 ; i++ ) {
if ( arr1[i] == 0 )
arr2[j++] = arr1[i];
}
for ( int i = 0 ; i  7 ; i++ ) {
//loop for +ve numbers
if ( arr1[i]  0 )
arr2[j++] = arr1[i];

}
//print arr1 for reference
printf(\nInitially the array was);
for ( int i = 0 ; i  7 ; i++ )
printf(\narr1[%d]: %d,i,arr1[i]);
printf(\n\n);
//print arr2
for ( int i = 0 ; i  7 ; i++ )
printf(\narr2[%d]: %d,i,arr2[i]);

return 0;
}

On Wednesday, June 13, 2012 9:49:49 PM UTC+5:30, Krishna Kishore wrote:

 Given a array of integers both positive and negative and you need to shift 
 positive numbers to one side 
 and negative numbers to other side with the order intact. 
 ex. {-1,5,3,-8,4,-6,9} to {-1,-8,-6,5,3,4,9}. This should be done in O(n).


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/H8ANlbL0dmEJ.
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: Microsoft Interview Question

2012-06-21 Thread Nishant Pandey
can this be done in single pass in O(n) .

On Thu, Jun 21, 2012 at 8:10 PM, rusty dafc1...@gmail.com wrote:

 guys this is my solution to the problem, it's a bit sloppy but as far as I
 checked it was working please have a go at it?


 #include stdio.h
 #include stdlib.h

 int main() {
 int arr1[] = {0,-5,3,0,4,-6,-9};
 int arr2[7];
 int j = 0;
 for ( int i = 0 ; i  7 ; i++ ) {
 //loop for -ve numbers
 if ( arr1[i]  0 )
 arr2[j++] = arr1[i];

 }

 //accomodate all the zeros if any
 for ( int i = 0 ; i  7 ; i++ ) {
 if ( arr1[i] == 0 )
 arr2[j++] = arr1[i];
 }
 for ( int i = 0 ; i  7 ; i++ ) {
 //loop for +ve numbers
 if ( arr1[i]  0 )
 arr2[j++] = arr1[i];

 }
 //print arr1 for reference
 printf(\nInitially the array was);
 for ( int i = 0 ; i  7 ; i++ )
 printf(\narr1[%d]: %d,i,arr1[i]);
 printf(\n\n);
 //print arr2
 for ( int i = 0 ; i  7 ; i++ )
 printf(\narr2[%d]: %d,i,arr2[i]);

 return 0;

 }

 On Wednesday, June 13, 2012 9:49:49 PM UTC+5:30, Krishna Kishore wrote:

 Given a array of integers both positive and negative and you need to
 shift positive numbers to one side
 and negative numbers to other side with the order intact.
 ex. {-1,5,3,-8,4,-6,9} to {-1,-8,-6,5,3,4,9}. This should be done in O(n).

  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/H8ANlbL0dmEJ.

 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.




-- 
Cheers,

Nishant Pandey |Specialist Tools Development  |
npan...@google.comgvib...@google.com |
+91-9911258345

-- 
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] SPOJ problem : CANDY III

2012-06-21 Thread romil bansal
Initialize sum as zero for all test cases ie inside 1st for loop.
On Jun 21, 2012 5:22 PM, Mayank Singh singh13490may...@gmail.com wrote:

 here is my code :

 #includestdio.h
 #includestdlib.h

 int main()
 {
 long long cand,sum;
 int T,N,i,j,*temp;
 scanf(%d,T);
 temp= (int*)calloc(T, sizeof( int));
 sum = 0;
 for(i=0;iT;i++)
 {
 scanf(%d,N);
 for(j=0;jN;j++)
 {
 scanf(%lld,cand);
 sum = (sum+cand)%N;
 }
 if(sum == 0)
 temp[i]=1;
 else
 temp[i]=0;
 }
 for(i=0;iT;i++)
 {
 if(temp[i]==1)
 printf(YES\n);
 else
 printf(NO\n);
 }
 return 0;
 }

 it is giving WA in spoj. i am unable to find what is wrong with it..plz
 help me.

 --
 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: Microsoft question

2012-06-21 Thread romil bansal
Can't we use k iterations of bubble sort ?
On Jun 18, 2012 2:11 PM, Ramindar Singh ramin...@gmail.com wrote:

 We can use Median of medians


 http://en.wikipedia.org/wiki/Selection_algorithm#Linear_general_selection_algorithm_-_Median_of_Medians_algorithm


 On Sunday, 17 June 2012 08:13:18 UTC+5:30, Prem Nagarajan wrote:

 Give an array of unsorted elements, find the kth smallest element in the
 array.

 The expected time complexity is O(n) and no extra memory space should be
 used.

 Quickselect algorithm can be used to obtain the solution. Can anyone
 explain how quickselect works?

  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/4lQsacmUPYUJ.
 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] SPOJ problem : CANDY III

2012-06-21 Thread amrit harry
@Mayank coding style not seems gud.. try this...

int main()
{
int testCases;
scanf(%d,testCases);
while(testCases--0)
   {
//perform ur logic
//print output for each test case here instead of storing it into
an array.
}
}
return 0;
}

On Thu, Jun 21, 2012 at 10:35 PM, Mayank Singh
singh13490may...@gmail.comwrote:



 thanx it worked

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




-- 
Thanks  Regards
Amritpal singh

-- 
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] Reverse Queue

2012-06-21 Thread sanjay pandey
it seems @hassan sol is correctcan nybody knw d flaw in it???

-- 
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: Directi question-centre of the tree

2012-06-21 Thread sanjay pandey
@ashish it wont be...first we r finding one end from any node dat is r n
den frm dat end we r traversing to other deepest end...
it is possible dat r b d intermediate node...n distance from r to v2 is
smaller than from r to v1

-- 
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: Microsoft Interview Question

2012-06-21 Thread sanjay pandey
single traversal n O(n) are 2 diff things...plz specify???

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

2012-06-21 Thread Anant Sharma
The reason why finding a solution to this question is difficult is because
the combinations of rand5() function which we use ( eg. rand5() + rand5()%2
) leads to some numbers being generated more frequently than others. So the
problem would be solved if we could find a function which leads to each
output being generated an equal number of times ( once, or maybe more. )

5 * rand5() + rand5()

Looking at this function, it is easy to see that each value from 0 to 24
will be generated only once for every respective value that first and
second rand5() returns.

Suppose the first rand5() returns 0, then the second rand5() can return any
value from 0 - 4. Now see that no value from 0-4 could be generated by any
other combination. When the value returned by the second rand5() is 1,
corresponding to the value returned by second rand5(), we could get 5 - 9.


Now we have each number from 0-24 appearing once. But simply returning the
mod of this value with 7 wouldn't lead to equal probability distribution as
the values 0 - 3 would be returned more times than 4-6. So to fix this
inequality, we ignore when the value of the function is more than 20. Now
doing mod with 7 will result in every number 0 - 6 being returned with
equal probability.

We could even have ignored the values above 6, or the values above 13, the
only difference being that it would take more number of calls to return a
rand7() result.

This way is non-deterministic i.e. we cannot say how many times the
function will be called before we get the rand7()  result, but we can be
sure that whenever the function returns a value, it would have been as
probable as any other value from 0 - 6. Taking the mod of the result of the
function, there would be 3 ways in which each digit 0 through 6 can be
generated.


Implementation:

int rand7 ( ) {
int num = 5 * rand5 ( ) + rand ( ) ;
if ( num  20 )
 return rand7 (  ) ;
else
retutrn num % 7 ;
}

On Thu, Jun 21, 2012 at 12:44 PM, Abhishek Sharma abhi120...@gmail.comwrote:

 @umer
 it's not random,but biased


 On Wed, Jun 20, 2012 at 11:16 AM, Umer Farooq the.um...@gmail.com wrote:

 Hmmm, true. That's what I expected in this solution. Similarly, we can
 get 3 by (3,3) (1,2)

 However, did you take a look at the other solution I proposed? I guess
 that solves the problem to some extent.


 On Tue, Jun 19, 2012 at 7:18 PM, Sourabh Singh 
 singhsourab...@gmail.comwrote:

 @Umer and Navin :
 1 is generated by (1,3) only.
 2 is generated by (1,1) and (2,3).

 so given solution is wrong


 On Tue, Jun 19, 2012 at 5:17 AM, Sourabh Singh singhsourab...@gmail.com
  wrote:

 @ *ALL*

 please. post along with your method .
 proof than it make's equal distribution over the given range.

 On Tue, Jun 19, 2012 at 4:47 AM, Navin Kumar 
 algorithm.i...@gmail.comwrote:

 @Umer:

 rand(5) + (rand(5)%2): = it will never give 6 because for rand(7)
 range will be 0-6.
 So better try this: rand(5) + (rand(5)%3).


 On Tue, Jun 19, 2012 at 2:45 PM, Umer Farooq the.um...@gmail.comwrote:

 rand(5) + (rand(5)%2);


 On Tue, Jun 19, 2012 at 12:30 PM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ sry
 condition should be:

 if(20*prob = 500/7) :-)


 On Tue, Jun 19, 2012 at 12:26 AM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ALL

 Given a random number generator say r(5) generates number between
 1-5 uniformly at random , use it to in r(7) which should generate a 
 random
 number between 1-7 uniformly at random

 i have seen this on many site's but not a single correct solution.
 all solution's posted got rejected by someone else.:
 plz.. suggest some algo :

 my aprroach:

 let's assume a rectangle :

 100  |___
 |___|__
 500/7   |  ||
 |  ||
 |___|__|
 0 1  2  3 4  5 67
 now :

 let : num  = rand(5);
prob = rand(5);

if(prob = rand(5))
 print  num
else
 print  5 + num*(2/5)


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




 --
 Umer

  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to 

Re: [algogeeks]

2012-06-21 Thread Sourabh Singh
@ Anant Sharma

Good one. it should work .

On Thu, Jun 21, 2012 at 9:43 PM, Anant Sharma black.b...@gmail.com wrote:

 The reason why finding a solution to this question is difficult is because
 the combinations of rand5() function which we use ( eg. rand5() + rand5()%2
 ) leads to some numbers being generated more frequently than others. So the
 problem would be solved if we could find a function which leads to each
 output being generated an equal number of times ( once, or maybe more. )

 5 * rand5() + rand5()

 Looking at this function, it is easy to see that each value from 0 to 24
 will be generated only once for every respective value that first and second
 rand5() returns.

 Suppose the first rand5() returns 0, then the second rand5() can return any
 value from 0 - 4. Now see that no value from 0-4 could be generated by any
 other combination. When the value returned by the second rand5() is 1,
 corresponding to the value returned by second rand5(), we could get 5 - 9.


 Now we have each number from 0-24 appearing once. But simply returning the
 mod of this value with 7 wouldn't lead to equal probability distribution as
 the values 0 - 3 would be returned more times than 4-6. So to fix this
 inequality, we ignore when the value of the function is more than 20. Now
 doing mod with 7 will result in every number 0 - 6 being returned with equal
 probability.

 We could even have ignored the values above 6, or the values above 13, the
 only difference being that it would take more number of calls to return a
 rand7() result.

 This way is non-deterministic i.e. we cannot say how many times the function
 will be called before we get the rand7()  result, but we can be sure that
 whenever the function returns a value, it would have been as probable as any
 other value from 0 - 6. Taking the mod of the result of the function, there
 would be 3 ways in which each digit 0 through 6 can be generated.


 Implementation:

 int rand7 ( ) {
 int num = 5 * rand5 ( ) + rand ( ) ;
 if ( num  20 )
 return rand7 (  ) ;
 else
 retutrn num % 7 ;
 }

 On Thu, Jun 21, 2012 at 12:44 PM, Abhishek Sharma abhi120...@gmail.com
 wrote:

 @umer
 it's not random,but biased


 On Wed, Jun 20, 2012 at 11:16 AM, Umer Farooq the.um...@gmail.com wrote:

 Hmmm, true. That's what I expected in this solution. Similarly, we can
 get 3 by (3,3) (1,2)

 However, did you take a look at the other solution I proposed? I guess
 that solves the problem to some extent.


 On Tue, Jun 19, 2012 at 7:18 PM, Sourabh Singh singhsourab...@gmail.com
 wrote:

 @Umer and Navin :
 1 is generated by (1,3) only.
 2 is generated by (1,1) and (2,3).

 so given solution is wrong


 On Tue, Jun 19, 2012 at 5:17 AM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @ ALL

     please. post along with your method .
     proof than it make's equal distribution over the given range.

 On Tue, Jun 19, 2012 at 4:47 AM, Navin Kumar algorithm.i...@gmail.com
 wrote:

 @Umer:

 rand(5) + (rand(5)%2): = it will never give 6 because for rand(7)
 range will be 0-6.
 So better try this: rand(5) + (rand(5)%3).


 On Tue, Jun 19, 2012 at 2:45 PM, Umer Farooq the.um...@gmail.com
 wrote:

 rand(5) + (rand(5)%2);


 On Tue, Jun 19, 2012 at 12:30 PM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @ sry
 condition should be:

 if(20*prob = 500/7) :-)


 On Tue, Jun 19, 2012 at 12:26 AM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @ALL

 Given a random number generator say r(5) generates number between
 1-5 uniformly at random , use it to in r(7) which should generate a 
 random
 number between 1-7 uniformly at random

 i have seen this on many site's but not a single correct solution.
 all solution's posted got rejected by someone else.:
 plz.. suggest some algo :

 my aprroach:

 let's assume a rectangle :

 100      |___
             |___|__
 500/7   |                                      |            |
             |                                      |            |
             |___|__|
             0     1      2      3     4      5     6    7
 now :

 let : num  = rand(5);
        prob = rand(5);

        if(prob = rand(5))
                         print  num
        else
                         print  5 + num*(2/5)


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




 --
 Umer


Re: [algogeeks] Re: Microsoft Interview Question

2012-06-21 Thread rusty
Well they are the same you're going over an array once. As long as they are 
not nested they are still counted as O(n) because leading constants are 
dropped, at least that's what my acumen says. Need inputs on this guys!

On Friday, June 22, 2012 12:53:02 AM UTC+5:30, suzi wrote:

 single traversal n O(n) are 2 diff things...plz specify??? 

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/2cjTXWrkv7wJ.
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]

2012-06-21 Thread sohinee basak
i have a doubt ... is the range of rand7 from 0 to 6 or from 1 to 7

On Fri, Jun 22, 2012 at 10:13 AM, Anant Sharma black.b...@gmail.com wrote:


 The reason why finding a solution to this question is difficult is because
 the combinations of rand5() function which we use ( eg. rand5() + rand5()%2
 ) leads to some numbers being generated more frequently than others. So the
 problem would be solved if we could find a function which leads to each
 output being generated an equal number of times ( once, or maybe more. )

 5 * rand5() + rand5()

 Looking at this function, it is easy to see that each value from 0 to 24
 will be generated only once for every respective value that first and
 second rand5() returns.

 Suppose the first rand5() returns 0, then the second rand5() can return
 any value from 0 - 4. Now see that no value from 0-4 could be generated by
 any other combination. When the value returned by the second rand5() is 1,
 corresponding to the value returned by second rand5(), we could get 5 - 9.


 Now we have each number from 0-24 appearing once. But simply returning the
 mod of this value with 7 wouldn't lead to equal probability distribution as
 the values 0 - 3 would be returned more times than 4-6. So to fix this
 inequality, we ignore when the value of the function is more than 20. Now
 doing mod with 7 will result in every number 0 - 6 being returned with
 equal probability.

 We could even have ignored the values above 6, or the values above 13, the
 only difference being that it would take more number of calls to return a
 rand7() result.

 This way is non-deterministic i.e. we cannot say how many times the
 function will be called before we get the rand7()  result, but we can be
 sure that whenever the function returns a value, it would have been as
 probable as any other value from 0 - 6. Taking the mod of the result of the
 function, there would be 3 ways in which each digit 0 through 6 can be
 generated.


 Implementation:

 int rand7 ( ) {
 int num = 5 * rand5 ( ) + rand ( ) ;
 if ( num  20 )
  return rand7 (  ) ;
 else
 retutrn num % 7 ;
 }

 On Thu, Jun 21, 2012 at 12:44 PM, Abhishek Sharma abhi120...@gmail.comwrote:

 @umer
 it's not random,but biased


 On Wed, Jun 20, 2012 at 11:16 AM, Umer Farooq the.um...@gmail.comwrote:

 Hmmm, true. That's what I expected in this solution. Similarly, we can
 get 3 by (3,3) (1,2)

 However, did you take a look at the other solution I proposed? I guess
 that solves the problem to some extent.


 On Tue, Jun 19, 2012 at 7:18 PM, Sourabh Singh singhsourab...@gmail.com
  wrote:

 @Umer and Navin :
 1 is generated by (1,3) only.
 2 is generated by (1,1) and (2,3).

 so given solution is wrong


 On Tue, Jun 19, 2012 at 5:17 AM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ *ALL*

 please. post along with your method .
 proof than it make's equal distribution over the given range.

 On Tue, Jun 19, 2012 at 4:47 AM, Navin Kumar algorithm.i...@gmail.com
  wrote:

 @Umer:

 rand(5) + (rand(5)%2): = it will never give 6 because for rand(7)
 range will be 0-6.
 So better try this: rand(5) + (rand(5)%3).


 On Tue, Jun 19, 2012 at 2:45 PM, Umer Farooq the.um...@gmail.comwrote:

 rand(5) + (rand(5)%2);


 On Tue, Jun 19, 2012 at 12:30 PM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ sry
 condition should be:

 if(20*prob = 500/7) :-)


 On Tue, Jun 19, 2012 at 12:26 AM, Sourabh Singh 
 singhsourab...@gmail.com wrote:

 @ALL

 Given a random number generator say r(5) generates number between
 1-5 uniformly at random , use it to in r(7) which should generate a 
 random
 number between 1-7 uniformly at random

 i have seen this on many site's but not a single correct solution.
 all solution's posted got rejected by someone else.:
 plz.. suggest some algo :

 my aprroach:

 let's assume a rectangle :

 100  |___
 |___|__
 500/7   |  ||
 |  ||
 |___|__|
 0 1  2  3 4  5 67
 now :

 let : num  = rand(5);
prob = rand(5);

if(prob = rand(5))
 print  num
else
 print  5 + num*(2/5)


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

Re: [algogeeks] Re: Microsoft Interview Question

2012-06-21 Thread Nishant Pandey
i mean o(n) in single traversal .

On Fri, Jun 22, 2012 at 12:53 AM, sanjay pandey
sanjaypandey...@gmail.comwrote:

 single traversal n O(n) are 2 diff things...plz specify???

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




-- 
Cheers,

Nishant Pandey |Specialist Tools Development  |
npan...@google.comgvib...@google.com |
+91-9911258345

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

2012-06-21 Thread Sourabh Singh
@ sohinee basak
it won't matter much . +1 or -1 will get u right range.. : ...

On Thu, Jun 21, 2012 at 9:54 PM, sohinee basak sohine...@gmail.com wrote:
 i have a doubt ... is the range of rand7 from 0 to 6 or from 1 to 7

 On Fri, Jun 22, 2012 at 10:13 AM, Anant Sharma black.b...@gmail.com wrote:


 The reason why finding a solution to this question is difficult is because
 the combinations of rand5() function which we use ( eg. rand5() + rand5()%2
 ) leads to some numbers being generated more frequently than others. So the
 problem would be solved if we could find a function which leads to each
 output being generated an equal number of times ( once, or maybe more. )

 5 * rand5() + rand5()

 Looking at this function, it is easy to see that each value from 0 to 24
 will be generated only once for every respective value that first and second
 rand5() returns.

 Suppose the first rand5() returns 0, then the second rand5() can return
 any value from 0 - 4. Now see that no value from 0-4 could be generated by
 any other combination. When the value returned by the second rand5() is 1,
 corresponding to the value returned by second rand5(), we could get 5 - 9.


 Now we have each number from 0-24 appearing once. But simply returning the
 mod of this value with 7 wouldn't lead to equal probability distribution as
 the values 0 - 3 would be returned more times than 4-6. So to fix this
 inequality, we ignore when the value of the function is more than 20. Now
 doing mod with 7 will result in every number 0 - 6 being returned with equal
 probability.

 We could even have ignored the values above 6, or the values above 13, the
 only difference being that it would take more number of calls to return a
 rand7() result.

 This way is non-deterministic i.e. we cannot say how many times the
 function will be called before we get the rand7()  result, but we can be
 sure that whenever the function returns a value, it would have been as
 probable as any other value from 0 - 6. Taking the mod of the result of the
 function, there would be 3 ways in which each digit 0 through 6 can be
 generated.


 Implementation:

 int rand7 ( ) {
 int num = 5 * rand5 ( ) + rand ( ) ;
 if ( num  20 )
 return rand7 (  ) ;
 else
 retutrn num % 7 ;
 }

 On Thu, Jun 21, 2012 at 12:44 PM, Abhishek Sharma abhi120...@gmail.com
 wrote:

 @umer
 it's not random,but biased


 On Wed, Jun 20, 2012 at 11:16 AM, Umer Farooq the.um...@gmail.com
 wrote:

 Hmmm, true. That's what I expected in this solution. Similarly, we can
 get 3 by (3,3) (1,2)

 However, did you take a look at the other solution I proposed? I guess
 that solves the problem to some extent.


 On Tue, Jun 19, 2012 at 7:18 PM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @Umer and Navin :
 1 is generated by (1,3) only.
 2 is generated by (1,1) and (2,3).

 so given solution is wrong


 On Tue, Jun 19, 2012 at 5:17 AM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @ ALL

     please. post along with your method .
     proof than it make's equal distribution over the given range.

 On Tue, Jun 19, 2012 at 4:47 AM, Navin Kumar
 algorithm.i...@gmail.com wrote:

 @Umer:

 rand(5) + (rand(5)%2): = it will never give 6 because for rand(7)
 range will be 0-6.
 So better try this: rand(5) + (rand(5)%3).


 On Tue, Jun 19, 2012 at 2:45 PM, Umer Farooq the.um...@gmail.com
 wrote:

 rand(5) + (rand(5)%2);


 On Tue, Jun 19, 2012 at 12:30 PM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @ sry
 condition should be:

 if(20*prob = 500/7) :-)


 On Tue, Jun 19, 2012 at 12:26 AM, Sourabh Singh
 singhsourab...@gmail.com wrote:

 @ALL

 Given a random number generator say r(5) generates number between
 1-5 uniformly at random , use it to in r(7) which should generate a 
 random
 number between 1-7 uniformly at random

 i have seen this on many site's but not a single correct solution.
 all solution's posted got rejected by someone else.:
 plz.. suggest some algo :

 my aprroach:

 let's assume a rectangle :

 100      |___
             |___|__
 500/7   |                                      |            |
             |                                      |            |
             |___|__|
             0     1      2      3     4      5     6    7
 now :

 let : num  = rand(5);
        prob = rand(5);

        if(prob = rand(5))
                         print  num
        else
                         print  5 + num*(2/5)


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