Q.) Given an array containing lower case and upper case alphabets and numbers, how can you sort/arrange the array in one single pass using just one variable for swapping such that the resultant array should put the input elements into 3 buckets in the following fashion - Input - aA1B23Cbc4 Output - abcABC1234 Note - ordering doesn't matter the output could be - ABC1234abc or 1234abcABC You just have to arrange the data into 3 buckets in single pass using just one temp variable for swapping. Expected runtime - O(n) and O(1) extra space.
Solution:
#include"stdio.h"
int main()
{
int Size,i;
scanf("%d",&Size);
char arr[Size+1];
scanf("%s",arr);
int low=0,mid=1,high=Size-1;
while(mid<=high)
{
if(arr[mid]>='0' && arr[mid]<='9')
{
arr[mid]=arr[low]+arr[mid]-(arr[low]=arr[mid]);
low++;
mid++;
}
else if(arr[mid]>='a' && arr[mid]<='z')
mid++;
else if(arr[mid]>='A' && arr[mid]<='Z')
{
arr[mid]=arr[high]+arr[mid]-(arr[high]=arr[mid]);
high--;
}
}
printf("%s",arr);
return 0;
}