[Huawei OD machine test real questions python] Calculate the closest number [2023 Q1 | 100 points]

Description of topic

[Calculate the closest number]

Given an array X and a positive integer K, please find the subscript i that makes the result of the expression X[i] – X[i + 1] – … – X[i + K – 1] closest to the median of the array, If more than one i satisfy the condition, return the largest i.

Among them, the median of the array: an array with a length of N, arranged in ascending order according to the value of the elements, and the subscript is the value of the N/2 element

Supplementary note:

1. The elements of the array X are all positive integers;

2. The value range of the length n of X: 2<= n <= 1000;

3. K is greater than 0 and less than the size of the array;

4. The value range of i: 0 <= i < 1000;

5. The median of the sorted array X[N] of the title is X[N/2].

enter description

output description

Example 1 The input and output examples are only for debugging, and the background judgment data generally does not include examples

enter

[50,50,2,3],2

output

1

Python code implementation:

Guess you like

Origin blog.csdn.net/xiao_pengjy/article/details/131715225