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 numberOfOne = 0;
for (int i : tmpArr) {
if (i == 1) {
numberOfOne++;
} else {
minSwaps += numberOfOne;
}
}
return minSwaps;
}
 /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Test1 test1 = new Test1();
int[] minSwaps = {
1,1,1,0,0,0,1,0
};

// test1.printArray(minSwaps);
int minswap = test1.calculateMinSwaps(minSwaps);
System.out.println(minswap);
}

}


On Saturday, June 23, 2012 11:34:55 AM UTC+5:30, zerocool142 wrote:
>
> Given an array containing sequence of bits (0 or 1), you have to sort 
> this array in the ascending order i.e. all 0' in first part of array 
> followed by all 1's.   The constraints is that you can swap only the 
> adjacent elements in the array. Find the minimum number of swaps 
> required to sort the given input array. 
>
> Example:   Given the array (0,0,1,0,1,0,1,1) the minimum number of swaps 
> is 3. 
>

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

Reply via email to