[algogeeks] print spiral

2012-06-14 Thread Ashish Goel
given a matrix m*n print elements in spiral eg 1 2 3 4 5 6 => 1,2,4,6,5,3 1 2 3 =>1,2,3 i wrote following code but for the case 2 printing 1,2,3,2 I wish to avoid keeping a counter of how many elements have been print so far... void spiral(int a[], int m, int n) { while ( (rs<(m+1)/2) &&

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Navin Kumar
I corrected in function InsertAtBottom :"In my code isntead of (!IsEmptyStack) use IsEmptyStack" On Fri, Jun 15, 2012 at 10:49 AM, Navin Kumar wrote: > @all: > > In my code isntead of (!IsEmptyStack) use IsEmptyStack > > Logic : > > First pop the all element and then start putting element at bott

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Navin Kumar
@all: In my code isntead of (!IsEmptyStack) use IsEmptyStack Logic : First pop the all element and then start putting element at bottom in reverse order i.e. the element which is fetched last is put at the bottom and so on. ex: let our stack is: 1 2 3 4 (1 is at bottom). function Call is will

[algogeeks] Re: No of tri-angles

2012-06-14 Thread enchantress
My bad.. it should be this: num_triangles = 0 for i:2 to N-2 for j:i+1 to N-1 num_triangles += i+j<=N ? i-1:N-j This gives values in increasing order such that c>b>a (c,b,a being lengths of sides of the triangle) and sum of any two sides is greater than the third. Con

Re: [algogeeks] Re: No of tri-angles

2012-06-14 Thread payel roy
How come 1,2,4 gives a triangle ?? 1+2 < 4. On 14 June 2012 16:12, enchantress wrote: > Use two loops: > num_triangles = 0 > for i:1 to (N-1)/2 > for j:i+1 to i+jnum_triangles += N-(i+j) > > This gives values in increasing order such that c>b>a (c,b,a being length

Re: [algogeeks] Re: Book Feedback needed for book from Narasimha Karumanchi

2012-06-14 Thread saurabh singh
Kindly find some other group for requesting e-books<- www.squiffer.com This is a good reference for ebooks.Any more requests for uploading ebooks may result in a ban from the group. Saurabh Singh B.Tech (Computer Science) MNNIT blog:geekinessthecoolway.blogspot.com On Fri, Jun 8, 20

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Nishant Pandey
i think i already explained the logic initially :) On Thu, Jun 14, 2012 at 9:30 PM, Hassan Monfared wrote: > how can you pop from empty stack ? > " temp=pop(S); " > > Regards, > Hassan > > On Thu, Jun 14, 2012 at 8:25 PM, Ashish Goel wrote: > >> Navin: copy pastes not encouraged till the logic

Re: [algogeeks] Amazon Interview Question

2012-06-14 Thread megha agrawal
Hashmap can be used for effective retreival.. On Thu, Jun 14, 2012 at 4:23 PM, Mohit Rathi wrote: > arr1 = [abc,xyz,lmn,def] > arr2 = [3,6,2,8] > > if user enters "xyz" then 6 will be printed > else > if xyz doesn't exist in arr1 then ask for a number and add them in > respective arrays(name in

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Karthikeyan V.B
Hi all, Generate combinations (taken k out of n) of a given string Eg: Input : abcd Output: abc acd abd bcd -- 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

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Nishant Pandey
the steps would be like this : if i say stack is like this 1,2,3,4 then 1) pop each and every item from the stack till stack is empty the nodes will be ther in function call stack stil not pushed 2) now now take elements from function call stack like 4 and push it 3) when 3 comes next time first

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Nishant Pandey
stack means inbuild stack we cant use any DS from our program explicitly. On Thu, Jun 14, 2012 at 2:44 PM, rahul patil wrote: > to store items in stack you will need some DS. Do they mean you cant use > any auxillary DS than stack ? > > > On Thu, Jun 14, 2012 at 2:24 PM, Ashish Goel wrote: > >>

Re: [algogeeks] Microsoft Interview Question

2012-06-14 Thread nadeem khan
how to do it in space comp- O(1) . I mean without using additional arrays. Could it be done ? On Thu, Jun 14, 2012 at 3:46 PM, utsav sharma wrote: > @all :- by segregating 0 and 1 or by taking quicksort approach > we have to swap no.s resulting which array becomes unordered. > > my approach is s

Re: [algogeeks] Re: Book Feedback needed for book from Narasimha Karumanchi

2012-06-14 Thread s yogeesh
Suggest any book for basics of data structures in C. -- 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...@googleg

Re: [algogeeks] Please explain

2012-06-14 Thread Lomash Goyal
void pushreverse(int data) { int temp; if(top==0) { push(data); return; } else temp=pop(); pushreverse(data); push(temp); } int reversestack() { //static int arr[50]; int top2=0; int i; if(top==0) { return top2; } top2=pop(); reversestack(); pushreverse(top2); } On Thu, Jun 14, 2012 at 2:16 P

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Lomash Goyal
void pushreverse(int data) { int temp; if(top==0) { push(data); return; } else temp=pop(); pushreverse(data); push(temp); } int reversestack() { //static int arr[50]; int top2=0; int i; if(top==0) { return top2; } top2=pop(); reversestack(); pushreverse(top2); } On Thu, Jun 14, 2012 at 2:44 P

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Hassan Monfared
how can you pop from empty stack ? " temp=pop(S); " Regards, Hassan On Thu, Jun 14, 2012 at 8:25 PM, Ashish Goel wrote: > Navin: copy pastes not encouraged till the logic is also clarified ;) > > Best Regards > Ashish Goel > "Think positive and find fuel in failure" > +919985813081 > +919966006

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Ashish Goel
Navin: copy pastes not encouraged till the logic is also clarified ;) Best Regards Ashish Goel "Think positive and find fuel in failure" +919985813081 +919966006652 On Thu, Jun 14, 2012 at 7:25 PM, Sourabh Singh wrote: > @ Navin Kumar > > Nice . Solution. > But > your function also make a stac

Re: [algogeeks] LEFT MOST set bit in an unsigned integer.

2012-06-14 Thread Hassan Monfared
It is equal to counting leading zeros. previous codes run in O(32). this code ( http://codingways.blogspot.com/2012/06/count-leading-zero-bits-in-integer.html ) runs in O(4) Regards, Hassan On Thu, Jun 14, 2012 at 2:34 PM, utsav sharma wrote: > there are two approaches we can take > one is of an

[algogeeks] Re: Permutations of a string

2012-06-14 Thread algogeek
Johnson trotter algorithm is another way to generate all permutations.. On Saturday, May 5, 2012 4:08:13 PM UTC+5:30, Sairam wrote: > > I have written a code which gives all permutations of a string. I have > assumed that all the characters in a given string are distinct. > The main idea is

Re: [algogeeks] Amazon Interview Question

2012-06-14 Thread RB
Since the size of array is very less I think Hashmap is the best. Use name as the hash key and number as its value. On Thursday, June 14, 2012 6:46:34 PM UTC+5:30, utsav sharma wrote: > > it can be done using map in c++ > > On Thu, Jun 14, 2012 at 4:23 PM, Mohit Rathi wrote: > >> arr1 = [abc,xyz,

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Sourabh Singh
@ Navin Kumar Nice . Solution. But your function also make a stack only. so you are using a stack[internal] here. but may be this one is allowed:-) On Thu, Jun 14, 2012 at 5:23 AM, Navin Kumar wrote: > void Reverse(struct Stack *S) { > int data; > if(IsEmptyStack(S)) return; > d

Re: [algogeeks] Amazon Interview Question

2012-06-14 Thread utsav sharma
it can be done using map in c++ On Thu, Jun 14, 2012 at 4:23 PM, Mohit Rathi wrote: > arr1 = [abc,xyz,lmn,def] > arr2 = [3,6,2,8] > > if user enters "xyz" then 6 will be printed > else > if xyz doesn't exist in arr1 then ask for a number and add them in > respective arrays(name in arr1 and numbe

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Navin Kumar
void Reverse(struct Stack *S) { int data; if(IsEmptyStack(S)) return; data=pop(s); ReverseStack(S); InsertAtBottom(S, data); } void InsertAtBottom(struct stack *S, int data) { int temp; if(!IsEmptyStack(S)){ push(S,data); return; } temp=pop(S); Ins

[algogeeks] How are strings stored in memory??

2012-06-14 Thread deepikaanand
If a string is input via stdin char str[20]; cout<<"\n Enter string" ; cin>>str; //say xyz\npqr cout

Re: [algogeeks] Amazon Interview Question

2012-06-14 Thread Mohit Rathi
arr1 = [abc,xyz,lmn,def] arr2 = [3,6,2,8] if user enters "xyz" then 6 will be printed else if xyz doesn't exist in arr1 then ask for a number and add them in respective arrays(name in arr1 and number in arr2). Hope it helps On Thu, Jun 14, 2012 at 3:58 PM, utsav sharma wrote: > example pls... >

[algogeeks] Re: No of tri-angles

2012-06-14 Thread enchantress
Use two loops: num_triangles = 0 for i:1 to (N-1)/2 for j:i+1 to i+jb>a (c,b,a being lengths of sides of the triangle) and sum of any two sides is greater than the third. Consider : 1,2,3,4,5,6 Possible side combinations are: 1,2,4 1,2,5 1,2,6 1,3,5 1,3,6 1,4,6 2,3,6 On W

Re: [algogeeks] Amazon Interview Question

2012-06-14 Thread utsav sharma
example pls... On Thu, Jun 14, 2012 at 1:01 PM, Mohit Rathi wrote: > Hi, > > *There are two arrays of length 100 each. Each of these has initially n > (n<=100) > elements. First array contains names and the second array contains numbers > such that ith name in array1 corresponds to ith number in

Re: [algogeeks] Microsoft Interview Question

2012-06-14 Thread utsav sharma
@all :- by segregating 0 and 1 or by taking quicksort approach we have to swap no.s resulting which array becomes unordered. my approach is start from right and if a negative no. occurs insert it into another array temp[] this will shift all positive no.s to right and in 2nd pass start from left

Re: [algogeeks] LEFT MOST set bit in an unsigned integer.

2012-06-14 Thread utsav sharma
there are two approaches we can take one is of anika i.e. shifting a no. right till it becomes 0 and count no. of shifts int main() { int x=10; int cnt=-1; while(x) { x>>=1; cnt++; } printf("%d",cnt); } another is of aditya in which we didn't change the original no. instead take a new no. n=pow(2,

Re: [algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread rahul patil
to store items in stack you will need some DS. Do they mean you cant use any auxillary DS than stack ? On Thu, Jun 14, 2012 at 2:24 PM, Ashish Goel wrote: > > Best Regards > Ashish Goel > "Think positive and find fuel in failure" > +919985813081 > +919966006652 > > -- > You received this messag

[algogeeks] MS Question: Reverse stack using push, pop without any auxiliary data structure

2012-06-14 Thread Ashish Goel
Best Regards Ashish Goel "Think positive and find fuel in failure" +919985813081 +919966006652 -- 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, se

Re: [algogeeks] Please explain

2012-06-14 Thread sengar.mahi
sry i got confused its 3 then 2 then 1. i thought its 321 On Thu, Jun 14, 2012 at 2:13 PM, Mahendra Sengar wrote: > main() > > { > > static i=3; > > printf("%d",i--); > > return i>0 ? main():0; > > } > > > the above code gives output 321 > Please Explain how? > > -- > You

[algogeeks] Please explain

2012-06-14 Thread Mahendra Sengar
main() { static i=3; printf("%d",i--); return i>0 ? main():0; } the above code gives output 321 Please Explain how? -- 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

Re: [algogeeks] LEFT MOST set bit in an unsigned integer.

2012-06-14 Thread aditya kumar
for 16bit ..take a number n = 1000 let x be the number mentionf in a question pos = -1 while (1){ if(x & n){ pos++ exit} else n ยป=1 } pos is the answer if pos is greater than numbr of bits then x doesnt hv set bits > How to find the LEFT MOST set bit in an unsigned integer. > Example

Re: [algogeeks] Microsoft Interview Question

2012-06-14 Thread saurabh singh
Order may not be maintained necessarily by this solution Saurabh Singh B.Tech (Computer Science) MNNIT blog:geekinessthecoolway.blogspot.com On Thu, Jun 14, 2012 at 1:47 PM, Manikanta Babu wrote: > Check this out, it works in O(n); > > int i = 0; > int j = n-1; > > while

Re: [algogeeks] Re: Power(n^n)

2012-06-14 Thread Prakhar Jain
Typo in this problem statement. K is not less than or equal to 1000. Only N<=1000. K can be as big as 1000^1000,i.e. 1000 digits. -- Prakhar Jain IIIT Allahabad B.Tech IT 3rd Year Mob no: +91 9454992196 E-mail: rit2009...@iiita.ac.in jprakha...@gmail.com On Tue, Jun 12, 2012 at 12:

Re: [algogeeks] Microsoft Interview Question

2012-06-14 Thread Manikanta Babu
Check this out, it works in O(n); int i = 0; int j = n-1; while((i= 0)&&(i0 && a[j]<0) { swap(a[i],a[j]); i++; j--; } else { if(a[i]<0)

Re: [algogeeks] Microsoft Interview Question

2012-06-14 Thread Mad Coder
As nothing is written about space complexity, I am assuming that we can take extra space. Take a temporary array of length n. 1. Maintain a counter for the length of temporary array filled till now. 2. Traverse the given array. If value contained is negative insert it in new array and update cou

[algogeeks] Amazon Interview Question

2012-06-14 Thread Mohit Rathi
Hi, *There are two arrays of length 100 each. Each of these has initially n (n<=100) elements. First array contains names and the second array contains numbers such that ith name in array1 corresponds to ith number in array2. Write a program which asks the user to enter a name, finds it in array1,

Re: [algogeeks] LEFT MOST set bit in an unsigned integer.

2012-06-14 Thread Azhar Hussain
unsigned int v; // 32-bit word to find the log base 2 of unsigned int r = 0; // r will be lg(v) while (v >>= 1) // unroll for more speed... { r++; } http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious - Azhar. On Thu, Jun 14, 2012 at 12:48 PM, Anika Jain wrote: > sorry

Re: [algogeeks] LEFT MOST set bit in an unsigned integer.

2012-06-14 Thread Anika Jain
sorry note its right shifting in my above post instead of left shift! On Thu, Jun 14, 2012 at 12:47 PM, Anika Jain wrote: > keep on left shifting the number till it becomes 0. the moment it becomes > 0, the count of how many times you had to right shift the number to get 0 > is the answer. > > >

Re: [algogeeks] LEFT MOST set bit in an unsigned integer.

2012-06-14 Thread Anika Jain
keep on left shifting the number till it becomes 0. the moment it becomes 0, the count of how many times you had to right shift the number to get 0 is the answer. On Thu, Jun 14, 2012 at 11:45 AM, Krishna Kishore wrote: > How to find the LEFT MOST set bit in an unsigned integer. > Example: > "00*

Re: [algogeeks] c prog problem

2012-06-14 Thread Pradip Singh
102 102 -90 64 On Thu, Jun 14, 2012 at 10:48 AM, Anika Jain wrote: > in this by typecasting address of float a to char * u assign the address > of a to ptr. but as ptr is a character pointer when *ptr is printed only 1 > byte currently pointed by ptr is pointed. when ptr is incremented it points