[algogeeks] Re: Question asked in Amazon online test

2012-06-29 Thread Prateek Khurana
Hi, This one is quite easy. You have to calculate the number of one's before every zero and add them up. That's it. public class Test1 { public void printArray(int[] tmpArr) { for (int i : tmpArr) { System.out.println(i); } } public int calculateMinSwaps(int[] tmpArr) { int minSwaps = 0; int

[algogeeks] A programming competition question

2012-06-29 Thread Vrashabh Irde
http://stackoverflow.com/q/11240022/1081340 -- 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.

[algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread raghavan M
Hi Question as in subject *No extra spaceĀ (can use one extra space)-O(1) max *No order change allowed example: input : 1,-5,2,10,-100,-2 output: -5,-10,-100,1,2 input : -1,-5,10,11,15,-500,200,-10 output : -1,-5,-10,-500,-10,10,11,15 Thanks Raghavn -- You received this message because you

Re: [algogeeks] MS Question :implement read write lock class

2012-06-29 Thread Mithun Kumar
This link should help http://stackoverflow.com/questions/5667793/how-does-a-read-write-mutex-lock-work?rq=1 -mithun On Fri, Jun 29, 2012 at 7:30 AM, bharat b bagana.bharatku...@gmail.comwrote: class lock { enum{read, write,free}status; }; By default, make status value as free. Based

Re: [algogeeks] trie display

2012-06-29 Thread atul anand
first search for node where abc ends i.e say func Search() will return node whose node-ch='c';(last character of input abc) then pass this node to following algo :- this is just a code sketchso you can add boundary conditions to it..(if i miss it) print(node *root,int j) { if(!root)

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread saurabh singh
duplicate of a previous post.Kindly refer to that post. Saurabh Singh B.Tech (Computer Science) MNNIT blog:geekinessthecoolway.blogspot.com On Fri, Jun 29, 2012 at 10:41 AM, raghavan M peacelover1987...@yahoo.co.inwrote: Hi Question as in subject *No extra space (can use one extra

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread Ravi Ranjan
one can modify dutch national flag algo for two colors(2 types positive n negative) -- 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

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread adarsh kumar
Quick sort partition routine variation. On Fri, Jun 29, 2012 at 3:06 PM, Ravi Ranjan ravi.cool2...@gmail.comwrote: one can modify dutch national flag algo for two colors(2 types positive n negative) -- You received this message because you are subscribed to the Google Groups Algorithm

[algogeeks] Switch doubt in C

2012-06-29 Thread adarsh kumar
Doubt, very trivial though: #includestdio.h int main() { int x=3; switch(x) { case 1: x=1; break; case 2: x=2; break; case 3: x=3; break; default: x=0; break; case

Re: [algogeeks] Switch doubt in C

2012-06-29 Thread saurabh singh
the cases are simple lables they have nothing to do with the flow of program. Saurabh Singh B.Tech (Computer Science) MNNIT blog:geekinessthecoolway.blogspot.com On Fri, Jun 29, 2012 at 3:14 PM, adarsh kumar algog...@gmail.com wrote: Doubt, very trivial though: #includestdio.h int main() {

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread raghavan M
The main idea of this question is *not to change order of occurrence.Dutch National flag other swapping like quick sort will change the order of occurrence of number try yourself with simple example for proof. From: Ravi Ranjan ravi.cool2...@gmail.com To:

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread raghavan M
This is a variant of that one From: saurabh singh saurab...@gmail.com To: algogeeks@googlegroups.com Sent: Friday, 29 June 2012 3:05 PM Subject: Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order duplicate of a

Re: [algogeeks] Switch doubt in C

2012-06-29 Thread Praveen
your program enter at case 3: after that it kept falling in next cases as well hence x was assigned values x=0, x=0, and x=4 at the end.. and then exit the switch case... use print after every assignment u'll see that... On Fri, Jun 29, 2012 at 3:16 PM, saurabh singh saurab...@gmail.com

Re: [algogeeks] Switch doubt in C

2012-06-29 Thread atul anand
case 1: , case 2 : , case 3 , case 4 ...etc etc are just labels... so switch(x) just jumps to that case x and then move downward. so if you dont use break..it will keep checking following cases. On Fri, Jun 29, 2012 at 3:14 PM, adarsh kumar algog...@gmail.com wrote: Doubt, very trivial

Re: [algogeeks] Switch doubt in C

2012-06-29 Thread Sharad Dixit
+1 atul If a matching label is found, execution proceeds from there. Control then passes down through all remaining labels within the switch statement. On Fri, Jun 29, 2012 at 3:28 PM, atul anand atul.87fri...@gmail.com wrote: case 1: , case 2 : , case 3 , case 4 ...etc etc are just

[algogeeks]

2012-06-29 Thread vindhya chhabra
please someone explain lvalue and rvalue with example... for llvalue..please explain ++x++ also.. thanks. -- Vindhya Chhabra -- 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

Re: [algogeeks]

2012-06-29 Thread adarsh kumar
l-values(left, literal meaning) appear on the lhs of a statement, and r-values vice versa. Essentially, l-values are identifiers. The memory location that will be thereby allocated can vary for r-values. Put in short, all l-values are r-values but not all r-values are l-values. And ++x++, will

Re: [algogeeks]

2012-06-29 Thread Atul Singh
R-value - Contents of memory location ( ie value stored in that memory location ) L-Value - Name of that memory location suppose , int i=9; than here* r-value is 9* and* l-value is i* . Taking *++x++ ..* As Postfix ( ++ ) is having higher precendence over Prefix( ++ ) so Postfix will be

Re: [algogeeks] Fixing Up The Binary Search Tree

2012-06-29 Thread Karthikeyan V.B
calculate the balance factor for all nodes and find any node with factor 1 or factor -1 and perform AVL rotation from that node -- 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.

Re: [algogeeks]

2012-06-29 Thread praveen raj
l value - address r value - content of variable(value) ex- x=2 x has value and address but 2 has only value cout++x++ I think this will result into l error . PRAVEEN RAJ DELHI COLLEGE OF ENGINEERING -- You received this message because you are subscribed to the Google Groups Algorithm

Re: [algogeeks] adobe

2012-06-29 Thread sarthak rai
2 malloc(): int **a=(int **)malloc(sizeof(int *)*nrows); for(i=0;inrows;i++) a[i]=(int *)malloc(sizeof(int)*ncols); using only 1 malloc: int **a=(int **)malloc(sizeof(int *)*nrows+(nrows*ncols)*(sizeof(int))); for(i=0;inrows;i++) { a[i]=(int*)(a+nrows+i*ncols); } On Fri, Jun 29, 2012 at 4:46 PM,

[algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread Gobind Kumar Hembram
Given an integer expression in a prefix format (i.e. the operator precedes the number it is operating on) , print the expression in the post fix format . Example: If the integer expression is in the prefix format is *+56-78, the postfix format expression is 56+78-*. Both of these correspond to

Re: [algogeeks]

2012-06-29 Thread vindhya chhabra
@all thanks On Fri, Jun 29, 2012 at 6:33 PM, Atul Singh atulsingh7...@gmail.com wrote: R-value - Contents of memory location ( ie value stored in that memory location ) L-Value - Name of that memory location suppose , int i=9; than here* r-value is 9* and* l-value is i* . Taking

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread Bhaskar Kushwaha
If the order is important then I think we can use any stable sorting algorithm with the following comparison function int compare (int a ,int b) { if((a0b0)||(a0b0)) return 0; else return ab; } On Fri, Jun 29, 2012 at 3:37 PM, raghavan M peacelover1987...@yahoo.co.inwrote: This is a variant of

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread Bhaskar Kushwaha
@saurabh please provide the link to the post you are mentioning On Fri, Jun 29, 2012 at 8:38 PM, Bhaskar Kushwaha bhaskar.kushwaha2...@gmail.com wrote: If the order is important then I think we can use any stable sorting algorithm with the following comparison function int compare (int a

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread utsav sharma
@bhaskar:- please explain stable sorting algorithm you would use(as mainly all of them require extra space) @sourabh:- that previous post discussion does't lead to any correct soln On Fri, Jun 29, 2012 at 8:39 PM, Bhaskar Kushwaha bhaskar.kushwaha2...@gmail.com wrote: @saurabh please provide

[algogeeks] Re: Microsoft Interview Question

2012-06-29 Thread mohit
+1 naveen On Thursday, June 28, 2012 8:29:26 PM UTC+5:30, Navin Gupta wrote: Keep two pointers - one at start of the array and other at end of the array Now current points to start of the array If current is negative , increment current If current is positive , swap it with the element

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread Hassan Monfared
Hi, this can be done by simple modification in bubble sort : void inplace_pos_neg(int pdata[],int sz) { bool changed=false; do { changed=false; for(int i=1;isz;i++) if(pdata[i-1]0 pdata[i]0) { swap(pdata[i-1], pdata[i]); changed=true; } }while(changed); } void test_inplace_pos_neg() { int

Re: [algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread Bhaskar Kushwaha
I think just reversing the prefix notation converts it to postfix notation On Fri, Jun 29, 2012 at 7:46 PM, Gobind Kumar Hembram gobind@gmail.comwrote: Given an integer expression in a prefix format (i.e. the operator precedes the number it is operating on) , print the expression in the

Re: [algogeeks] MS Question: Segregrate positive and negative nos in array without changing order

2012-06-29 Thread Bhaskar Kushwaha
@Hassan I think your algo will take time O(n^2) in worst case which occurs when all elements are negative except the last one @everyone Can we solve this problem in linear time? On Fri, Jun 29, 2012 at 9:10 PM, Hassan Monfared hmonfa...@gmail.comwrote: Hi, this can be done by simple

[algogeeks] Re: Microsoft Interview Question

2012-06-29 Thread Dave
@Navin: Try this with {1,-1,2}. current points to the 1 and end points to the 2. Since 1 is positive, the algorithm swaps the 1 and the 2, giving {2,-1,1}. Then it decrements current to point outside the array and end to point to the -1. How can this be right? Dave On Thursday, June 28, 2012

Re: [algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread amrit harry
@bhaskar ur algo fails on this case (5+3)-(2+(3/6)) -+53+2/36 63/2+35-+ showing that 6/3 but actually it is 3/6 so i think it could be done by folowing algo make a binary tree of given expression in O(n) then do postorder traversal take O(n) so problem can be solved in O(n). and take O(2*n+1)

Re: [algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread rahul ranjan
oh bhai mere. kewal preorder use karke kaise tree bana dega??? On Fri, Jun 29, 2012 at 11:23 PM, amrit harry dabbcomput...@gmail.comwrote: @bhaskar ur algo fails on this case (5+3)-(2+(3/6)) -+53+2/36 63/2+35-+ showing that 6/3 but actually it is 3/6 so i think it could be done by

Re: [algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread Abhishek Sharma
convert prefix to infix,then convert infix to postfix.Now, to convert prefix to infix, push numbers in one stack and operators in other.Then use thishttp://www.velocityreviews.com/forums/t445633-prefix-to-infix-conversion.html algo to perform this.Then do the same for infix to postfix.It works

Re: [algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread Bhaskar Kushwaha
reverse the prefix notation and then reverse each continuous occurence of operands On Sat, Jun 30, 2012 at 12:50 AM, Abhishek Sharma abhi120...@gmail.comwrote: convert prefix to infix,then convert infix to postfix.Now, to convert prefix to infix, push numbers in one stack and operators in

Re: [algogeeks] Question asked in Amazon Online Test

2012-06-29 Thread Bhaskar Kushwaha
example -+53+2/36 step 1: 63/2+35+- step 2: 36/2+53+- On Sat, Jun 30, 2012 at 1:55 AM, Bhaskar Kushwaha bhaskar.kushwaha2...@gmail.com wrote: reverse the prefix notation and then reverse each continuous occurence of operands On Sat, Jun 30, 2012 at 12:50 AM, Abhishek Sharma