Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 17, 2016 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jianminchen/4f7f9a3cfd9686e031cdf84396e63847 to your computer and use it in GitHub Desktop.
Save jianminchen/4f7f9a3cfd9686e031cdf84396e63847 to your computer and use it in GitHub Desktop.
HackerRank university codesprint - kindergarten adventure - study code - score 30 (Maximum score 30) - segment tree
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int n = int.Parse(Console.ReadLine());
int[] A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
int[] w = new int[n];
for (int i = 0; i < n; i++) {
w[(n + i - A[i]) % n]++;
}
int k = 0, ans = 0;
int bad = 0;
for (int i = 0; i < n; i++) {
if ((n + i - k) % n < A[i]) bad++;
}
int badans = bad;
for (int i = 0; i < n; i++) {
bad = bad - 1 + w[i];
if (bad < badans) {
badans = bad;
ans = i+1;
}
}
Console.WriteLine(ans + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment