Skip to content

Instantly share code, notes, and snippets.

@salva
Last active September 17, 2021 18:47
Show Gist options
  • Save salva/1899d5e97818d134145825596c2b1865 to your computer and use it in GitHub Desktop.
Save salva/1899d5e97818d134145825596c2b1865 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int n = atoi(argv[1]);
int half = n / 2;
int i, j;
int *a = malloc(n * sizeof(int));
for (j = 0, i = half; i < n ; i++, j++) a[j] = i;
for ( i = 0 ; i < half; i++, j++) a[j] = i;
int start = 0;
int end = n;
for (i = n; i > 1; i--) {
// for (j = start; j < end; j++) printf(" %d", a[j % n]); printf("\n");
start++;
if (i & 1)
a[end++ % n] = a[start++ % n];
}
printf("%d\n", a[start % n] + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment