Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 17, 2016 08:07
Show Gist options
  • Save jianminchen/7ce7a7a0d55b4566448958549bbd2b79 to your computer and use it in GitHub Desktop.
Save jianminchen/7ce7a7a0d55b4566448958549bbd2b79 to your computer and use it in GitHub Desktop.
HackerRank - kindergarten C solution - study code No. 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
int *dc = calloc(n, sizeof(int));
int count = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
if (x < n) {
int start = (i+1)%n;
int end = (start-x+n)%n;
dc[start]++;
dc[end]--;
if (end <= start) count++;
}
}
int id = 0;
int val = 0;
for (int i = 0; i < n; i++) {
count += dc[i];
if (count > val) {
val = count;
id = i;
}
}
printf("%d", id+1);
free(dc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment