Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OddEvenArraySimplified
- {
- public static void main(String args[])
- {
- int oddInd=0;
- int evenInd=1;
- char[] str = "01101010111001011".toCharArray();
- int strLen = str.length-1;
- while(true)
- {
- while(oddInd<strLen && str[oddInd]=='1')
- {
- oddInd+=2;//here
- }
- if(oddInd>=strLen)
- break;
- while(evenInd<strLen &&str[evenInd]=='0')
- {
- evenInd+=2;//here
- }
- if(evenInd>=strLen)
- break;
- swap(str, evenInd, oddInd);
- }
- for(int i = 0 ;i< strLen; i++)
- System.out.print(str[i] + ",");
- }
- private static void swap(char[] str, int a, int b)
- {
- char tmp = str[a];
- str[a] = str[b];
- str[b]= tmp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement