Created
November 17, 2016 08:07
-
-
Save jianminchen/7ce7a7a0d55b4566448958549bbd2b79 to your computer and use it in GitHub Desktop.
HackerRank - kindergarten C solution - study code No. 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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