Kindergarten Adventures

Problem Statement :

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day!

The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other.

After letting the students draw for a certain period of time, Meera starts collecting their work to ensure she has time to review all the drawings before the end of the day. However, some of her students aren't finished drawing! Each student i needs ti extra minutes to complete their drawing.

Meera collects the drawings sequentially in the clockwise direction, starting with student ID x, and it takes her exactly 1 minute to review each drawing. This means that student x gets 0 extra minutes to complete their drawing, student x+1 gets 1 extra minute, student x+2 gets 2 extra minutes, and so on. Note that Meera will still spend 1 minute for each student even if the drawing isn't ready.

Given the values of t1, t2,  . . .,  tn , help Meera choose the best possible x to start collecting drawings from, such that the number of students able to complete their drawings is maximal. Then print x on a new line. If there are multiple such IDs, select the smallest one.

Input Format

The first line contains a single positive integer, n , denoting the number of students in the class.
The second line contains n space-separated integers describing the respective amounts of time that each student needs to finish their drawings (i.e .t1,  t2,  . . .,  tn ).


1  <=  n   <= 10^5
1  <=  ti  <= n

Output Format

Print an integer denoting the ID number, , where Meera should start collecting the drawings such that a maximal number of students can complete their drawings. If there are multiple such IDs, select the smallest one.

Solution :


                            Solution in C :

In  C++ :

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int n;
int t[125252];
int imos[125252];

int main(){
    int tt = t[i];
    int from = i+1;
    int to = (i-tt+n)%n;
  int ans = -1;
  int val = -1;
      ans = i;
      val = imos[i];
  return 0;

In  Java :

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        int n = sc.nextInt();
        int [] tab = new int[n];
        for(int i = 0; i < n; i++) {
            int k = sc.nextInt();
            if(k == 0) {
            tab[(i + 1) % n]++;
            int p = i - k + 1;
            if(p < 0) {
                p += n;
        int max = -100000;
        int res = 0;
        int sum = 0;
        for(int i = 0; i < n; i++) {
            sum += tab[i];
            if(sum > max) {
                max = sum;
                res = i;
        System.out.println(res + 1);

In C :

#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;
            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);
    return 0;

In Python3 :

import sys
n = int(sys.stdin.readline())
t = [int(x) for x in sys.stdin.readline().split()]

current_available = 0
become_unavailable = [0] * (3 * n)
best_index = 1
best_result = 0
for i in range(2*n):
    el = t[i % n]
    become_unavailable[i + (n - el)] += 1
    if current_available > best_result:
        best_result = current_available
        best_index = i % n + 1
    if current_available == best_result:
        best_index = min(i % n + 1, best_index)
    current_available += 1
    current_available -= become_unavailable[i]

View More Similar Problems

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →


This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →