EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. EaglePB2's Special

Cycle Prefix Averages

Difficulty: Easy

PreviousHong Kong Identity cardNextWord Squares

Last updated 2 months ago

Question

Prefix Averages Algorithm is used to calculate average of the first i elements in an array. That algorithm is widely used in financial analysis. Peter, which is a financial analyst, is now wondering if he keeps repeating the prefix averages, how many times it repeats until all the numbers are same as first element. To make things simple, he will omit any trailing decimals during finding the averages instead.

Input Format

The first line consists of an integer N, which is the number of arrays to input. Starting from second line, there 1D array which consist of various numbers with various size, given all integers.

Constraints

1≤N≤1001 \le N \le 1001≤N≤100
1≤i≤1051 \le i \le 10^51≤i≤105
1≤arr[i]≤1091 \le arr[i] \le 10^91≤arr[i]≤109

Output Format

Output the total count of cycle per line, which makes all numbers in an array into 1. If it is impossible to make all numbers in an array into 1, print -1 instead.

Sample Inputs:

Input

4
11 23 5 27 33 1 45 18
101325 999999 8763 1599 20
2 3 5 7 11 13 17 19 23 29
12333 33455 101325 114514 1919810

Output

5
22
4
16

Explanation

Taking first line as example:

In first cycle:

  • [0]: 11

  • ...

The new array becomes:

Repeat the cycle again until the array becomes:

And it took 5 cycles to complete it.

Input

2
7401 5067 60577 54677 80678 49658 7959 89948 2217 27686
78905 53234 39515 43375 83724 98992 66158 79755 70123 44449

Output

-1
-1

Explanation

Those series will never converge to same element; therefore, they output as -1.

Solution

Sample solution as follows:

def cycle_average(arr):
    final_value = sum(arr) // len(arr)
    counter = 0
    prev_arr = arr[:]

    while len(set(arr)) > 1:
        counter += 1
        prefix_sum = 0
        new_arr = arr[:]

        for i in range(len(arr)):
            prefix_sum += arr[i]  
            arr[i] = prefix_sum // (i + 1)
        
        if arr == new_arr:
            return -1
    
        if all(x == final_value for x in arr):
            break

    return counter

# Read input
N = int(input())
for _ in range(N):
    arr = list(map(int, input().strip().split()))
    print(cycle_average(arr))
[11,23,5,27,33,1,45,18][11,23,5,27,33,1,45,18][11,23,5,27,33,1,45,18]

[1]: 11+132=17\frac{11+13}{2}=17211+13​=17

[2]: 11+23+53=13\frac{11+23+5}{3}=13311+23+5​=13

[7]: 11+23+5+27+33+1+45+188=20\frac{11+23+5+27+33+1+45+18}{8}= 20811+23+5+27+33+1+45+18​=20

[11,17,13,16,19,16,20,20][11, 17, 13, 16, 19, 16, 20, 20] [11,17,13,16,19,16,20,20]
[11,11,11,11,11,11,11,11][11,11,11,11,11,11,11,11][11,11,11,11,11,11,11,11]