#!/bin/python3 import math import os import random import re import sys # # Complete the 'taskOfPairing' function below. # # The function is expected to return a LONG_INTEGER. # The function accepts LONG_INTEGER_ARRAY freq as parameter. # def taskOfPairing(freq): # Initialize the start of the continuous subarray i_0 = 0 # Initialize the end of the continuous subarray i_1 = 1 n = len(freq) total = 0 while i_1 < n: # While not at the end of the subarray, check if the next value is non-zero if freq[i_1] == 0: # If value is zero, take the sum of the continuous subarray and integer divide by 2. That's the # number of pairs in this segment total += sum(freq[i_0: i_1]) // 2 # The new start of the continuous array is here i_0 = i_1 i_1 += 1 # Upon reaching the end, find the number of pairs of the last subarray total += sum(freq[i_0:i_1]) // 2 return total if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') freq_count = int(input().strip()) freq = [] for _ in range(freq_count): freq_item = int(input().strip()) freq.append(freq_item) result = taskOfPairing(freq) fptr.write(str(result) + '\n') fptr.close() |
No comments:
Post a Comment