void arrange(int arr[], int n, int i)

{

if(i == 1)

{

arr[1] = arr[n];

arr[2] = arr[n << 1];

return;

}

int j = i - 1;

int c = arr[(n << 1) + j];

int b = arr[n + j];

int a = arr[j];

arrange(arr, n, j);

int x = 3 * j;

arr[x] = a;

arr[x + 1] = b;;

arr[x + 2] = c;

}

int main()

{

int n;

scanf("%d", &n);

int a[n], i;

for(i = 0; i < n; ++i)

scanf("%d", a + i);

if(n > 0 && n % 3 == 0)

arrange(a, n/3, n/3);

for(int i = 0; i < n; ++i)

printf("%d ", a[i]);

printf("\n");

}

### Like this:

Like Loading...

*Related*

Have u actually run this code.(This is not working at all) And also dont know what r u trying to do

This is c99 code. Use gcc -std=c99 for compilation

in ur code int c = arr[3 * n – 1]; int b = arr[2 * n – 1]; int a = arr[n – 1]; there should be i inplace of n

Thanks for pointing out this

will be int c = arr[2 * n + i-1]; int b = arr[n + i-1]; int a = arr[i-1];

This comment has been removed by the author.

That's not inplace. You are storing the entire array in the stack of program. This is even worse that copying the entire array into and then putting it into their right position(not inplace).

Hi all,I was testing this program. The below program worked………….#include #define SIZE 12 //multiple of 3void arrange(int arr[], int n, int i){ if(i == 1) { arr[1] = arr[n]; arr[2] = arr[n << 1]; return; } int a = arr[i – 1]; int b = arr[n + i – 1]; int c = arr[2*n + i – 1]; arrange(arr, n, i – 1); int x = 3 * (i – 1); arr[x] = a; arr[x + 1] = b; arr[x + 2] = c; }int main(){ int n = SIZE; int a[SIZE], i; printf("\nEnter %d numbers", SIZE); for(i = 0; i < n; ++i) scanf("%d", a + i); if(n != 0 && n % 3 == 0)arrange(a, n/3, n/3); for(i = 0; i < n; ++i)printf("%d ", a[i]); printf("\n");}

@Sambasiva Can You Please Through Some Light , Explain The Algorithm & Logic , Approach . Please help me to Understood what u r trying to do ?Thanks

In each of the recursion call , the programs saves each set of AxBxCx in function variables and uses this to rearrange the array while returning back.

Hi. I have a doubt as how you are rearranging this??

Good logic …Thanks…..!

But in recursion function each time 3 variables are created in the stack n/3 times .i.e 'n' extra space is required minimum.Correct me If I am wrong…???

nah.. this is not O(n) space

oops … meant O(1) space

Good logic , thanks 🙂