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 ).

Constraints

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 :



title-img


                            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(){
  scanf("%d",&n);
  REP(i,n)scanf("%d",t+i);
  REP(i,n){
    int tt = t[i];
    int from = i+1;
    int to = (i-tt+n)%n;
    if(from<=to){
      imos[from]++;
      imos[to+1]--;
    }else{
      imos[0]++;
      imos[to+1]--;
      imos[from]++;
      imos[n]--;
    }
  }
  REP(i,n+1)imos[i+1]+=imos[i];
  int ans = -1;
  int val = -1;
  REP(i,n){
    if(imos[i]>val){
      ans = i;
      val = imos[i];
    }
  }
  printf("%d\n",ans+1);
  return 0;
}








In  Java :






import java.io.*;
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(System.in);
        int n = sc.nextInt();
        int [] tab = new int[n];
        
        for(int i = 0; i < n; i++) {
            int k = sc.nextInt();
            
            if(k == 0) {
                continue;
            }
            
            tab[(i + 1) % n]++;
            int p = i - k + 1;
            if(p < 0) {
                p += n;
            }
            
            tab[p]--;
        }
        
        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;
            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;
}









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]
       
print(best_index)
                        








View More Similar Problems

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →

Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →

Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →

Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →