[algogeeks] Re: Question asked in Amazon online test

2012-07-24 Thread Amit
Let's define a term RANDOMNESS of array as... summation of position of each 1's for eg RANDOMNESS for (0,0,1,0,1,0,1,1) will be 23 now calculate max possible RANDOMNESS for the given array (each 1 on max possible right position) here it will be 26 so ans will be-- MAX RANDOMNESS of given array

[algogeeks] Re: Question asked in Amazon Online Test

2012-07-01 Thread Ashu
count=0; Start parsing from left to right If operator push in stack. In number add in queue and increment count by one, if count == 2 then pop from stack add in queue, decrements the count by one. On Friday, 29 June 2012 19:46:43 UTC+5:30, zerocool142 wrote: Given an integer expression in a

[algogeeks] Re: Question asked in Amazon Online Test

2012-07-01 Thread neelpulse
I think this algo is correct. On Sunday, 1 July 2012 13:06:17 UTC+5:30, Ashu wrote: count=0; Start parsing from left to right If operator push in stack. In number add in queue and increment count by one, if count == 2 then pop from stack add in queue, decrements the count by one. On

Re: [algogeeks] Re: Question asked in Amazon Online Test

2012-07-01 Thread Akshat Sapra
*Using Stack : * j = 0; for ( int i = 0; i prefix.len(); i++ ) { if ( isOperator(prefix[i]) ) stk.push(prefix[i]); else { postfix[j] = prefix[i]; j++; while ( !stk.empty() stk.top == '#' ) {

[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] Re: Question asked in Amazon online test

2012-06-28 Thread ANKIT BHARDWAJ
get the right most zero and left most one if index of right most zero is less than the index of left most one ,the problem is solved other wise swap 0 and 1 and so on... i argue this will give minimum swaps... -- You received this message because you are subscribed to the Google Groups

[algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread deepikaanand
will bubble sort give number of swaps?? I tried the (bubble sort) code of 1,0,0,1,1,0,1 swapcount = 5 and for the array (0,0,1,0,1,0,1,1)swapcount = 3 -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread Sourabh Singh
@deepika yes, it's giving number of swaps. still Linear time solution would be better :-) On Fri, Jun 22, 2012 at 11:38 PM, deepikaanand swinyanand...@gmail.com wrote: will bubble sort give number of swaps?? I tried the (bubble sort) code of 1,0,0,1,1,0,1 swapcount = 5 and for the array

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread Darpan Baweja
i think it can be done in O(n) for each 1, calculate no. of zeros in right and adding all no. of zeros in right of all 1's will give no. of swaps. correct me if i am wrong On Sat, Jun 23, 2012 at 12:19 PM, Sourabh Singh singhsourab...@gmail.comwrote: @deepika yes, it's giving number of

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread Sourabh Singh
@ALL see if this work's : #includeiostream using namespace std; int a[8]={0,0,1,0,1,0,1,1}; int count_zero(int size) { int i,count =0; for(i=0;isize;i++) if(!a[i]) count++; return count; } int solve(int size) { int num_zero=count_zero(size); int p_zero,p_one,i; int count=0;

[algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread deepikaanand
@saurabh..wat array r u getting finallyis it all 1's or in sorted order for the input int a[8]={1,1,1,0,1,0,1,1}; -- 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

[algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread Anurag Gupta
if we just need to determine number of swaps, it can be Done in O(n) Ex : 11100010 start counting number of zeros from the end so we have zeroCount = 1 whenever we encounter a 1 we add current zeroCount to numberOfSwaps so numberOfSwaps = 1 and so on the final value of numberOsSwaps is the

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread rajesh pandey
start scanning the array from last .. maintain two pointers. one for last encountered zero. second for moving backward. whenever you encounter the one just swap it with the last zero. enhance the pointer till next zero encounters. at last you will have the number of swaps. I think my solution

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread Guruprasad Sridharan
It will work because we need to swap only adjacent elements. So we do a sweep from left to right finding the number of ones encountered to the left of a zero when we find a zero. We keep adding the number as we sweep the entire length. On Sat, Jun 23, 2012 at 1:14 PM, deepikaanand

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread ashish jain
@all yaa.. For getting number of swaps.. we have to calculate total number of zeroes on the right for each '1' and on adding them we will get the number of swaps. and in O(n) time. On Sat, Jun 23, 2012 at 1:16 PM, Guruprasad Sridharan sridharan.mi...@gmail.com wrote: It will work because we

Re: [algogeeks] Re: Question asked in Amazon online test

2012-06-23 Thread Sourabh Singh
@deepika anand solution given by me is for getting number of swap's in O(n) as far as sorting goes any O(n lgn) algo can be used . if count sort is allowed then O(n) for sorting also.[constant extra space.. ] On Sat, Jun 23, 2012 at 12:49 AM, ashish jain ashishjainco...@gmail.com wrote: @all