2023 Huawei od machine test real questions [calculate the closest number] C language

Table of contents

topic

train of thought

Code


topic

Given an array X and a positive integer K, please find the subscript i that makes the expression X[i] - x[i + 1] ... - X[i + K 1] the closest to the median of the array , if there are multiple i satisfying the condition, please 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
explanation:
1. The elements of the array X are all positive integers;
2. The length of X The value range of n: 2<= n <= 1000;
3. K is greater than 0 and smaller than the size of the array;
4. The value range of i: 0 <= i < 1000;
5. The sorted array X[N] of the title The median is X[N/2].
Example 1
input:

[50,50,2,3], 2
output:

1

Description:
1. The median is 50: [50,50,2,3] becomes [2,3,50,50] after sorting in ascending order, and the median is the element 50 with subscript 4/2=2;

2. The calculation result is 1: X[50,50,2,3] Calculate X[i] - ...- X[i + K- 1] according to the title to get three numbers

0 (X[0]-X[1]= 50 -50) 、

48 (X[1]-X

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/132006424