Russian

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2024

 Main Tasks Examples PT for MPI-2

 Overview Input-output operations Task groups Begin Integer Boolean If Case For While Series Proc Func Minmax Array Matrix String File Text Param Recur Dynamic Dynamic (obj) Tree Tree (obj)

# One-dimensional arrays

The condition "An array of N integers (or real numbers) is given" means that the actual size of the array (an integer N) and all its elements are given. The size of any array is assumed to be in the range 2 to 10, if the task does not specify it explicitly. The order number of the first element of array is assumed to be equal to 1.

If a task connected with array creation or array changing does not specify output data then the resulting array elements are supposed to be output in ascending order of their indices.

Since version 4.19, the taskbook includes two versions of this group. The new version, named ZArray, differs from the original one in that it associates with array elements not order numbers (starting with 1), but indices (starting with 0); the prefix Z in the group name can be interpreted as "zero-indexed". The ZArray group is convenient for languages in which elements of arrays and other collections are always indexed from 0. In the ZArray group, compared to the Array group, the tasks with the following numbers have been changed: 11–15, 19–23, 26–33, 44–45, 49, 55–57, 72–73, 90–91, 93–94, 101–102, 104–107, 115, 121–124. In some tasks, only the wording was corrected (the initial and resulting data sets remained unchanged): 5–6, 16–18, 58–61, 65, 69, 79–87, 112, 114, 131–140.

## One-dimensional arrays: creation

The size of a resulting array is assumed to be not greater than 10 in all tasks connected with the array creation.

Array1. Given an integer N (> 0), create and output an array of N integers that are the first positive odd numbers: 1, 3, 5, … .

Array2. Given an integer N (> 0), create and output an array of N integers that are the first positive integer powers of 2: 2, 4, 8, 16, … .

Array3. An integer N (> 1), the first term A and the common difference D of an arithmetic sequence are given (A and D are real numbers). Create and output an array of N real numbers that are the initial terms of this sequence:

A,    A + D,    A + 2·D,    A + 3·D,    … .

Array4°. An integer N (> 1), the first term A and the common ratio R of a geometric sequence are given (A and D are real numbers). Create and output an array of N real numbers that are the initial terms of this sequence:

A,    A·R,    A·R2,    A·R3,    … .

Array5. Given an integer N (> 2), create and output an array of N integers that are the initial terms of a sequence {FK} of the Fibonacci numbers:

F1 = 1,        F2 = 1,        FK = FK−2 + FK−1,    K = 3, 4, … .

Array6. Given three integers N (> 2), A, B, create and output an array of N integers such that the first element is equal to A, the second one is equal to B, and each subsequent element is equal to the sum of all previous ones.

## One-dimensional arrays: output of elements

Array7°. Given an array of N real numbers, output its elements in inverse order.

Array8. Given an array of N integers, output all odd numbers contained in the array in ascending order of their indices. Also output the amount K of odd numbers contained in the array.

Array9. Given an array of N integers, output all even numbers contained in the array in descending order of their indices. Also output the amount K of even numbers contained in the array.

Array10. Given an array of N integers, output all even numbers contained in the array in ascending order of their indices and then output all odd numbers contained in the array in descending order of their indices.

Array11. An array A of N real numbers and an integer K (1 ≤ K ≤ N) are given. Output array elements with order numbers that are multiples of K: AK, AK, AK, … . Do not use conditional statements.

Array12. An array A of N real numbers is given (N is an even number). Output array elements with even order numbers in ascending order of indices: A2, A4, A6, …, AN. Do not use conditional statements.

Array13. An array A of N real numbers is given (N is an odd number). Output array elements with odd order numbers in descending order of indices: AN, AN−2, AN−4, …, A1. Do not use conditional statements.

Array14. An array A of N real numbers is given. Output array elements with even order numbers (in ascending order of indices) and then output array elements with odd order numbers (in ascending order of indices too):

A2,    A4,    A6,    …,    A1,    A3,    A5,    … .

Do not use conditional statements.

Array15. An array A of N real numbers is given. Output array elements with odd order numbers in ascending order of indices and then output array elements with even order numbers in descending order of indices:

A1,    A3,    A5,    …,    A6,    A4,    A2.

Do not use conditional statements.

Array16°. An array A of N real numbers is given. Output array elements as follows:

A1,    AN,    A2,    AN−1,    A3,    AN−2,    … .

Array17. An array A of N real numbers is given. Output array elements as follows:

A1,    A2,    AN,    AN−1,    A3,    A4,    AN−2,    AN−3,    … .

## One-dimensional arrays: analysis of elements

Some tasks of this section may be solved without storing all input data simultaneously, so they do not need arrays for solving. However, using arrays usually leads to more simple and obvious algorithms.

Array18. An array A of 10 nonzero integers is given. Output the value of the first element AK that satisfies the following inequality: AK < A10. If the array does not contain such elements then output 0.

Array19. An array A of 10 integers is given. Output the order number of the last element AK that satisfies the following double inequality: A1 < AK < A10. If the array does not contain such elements then output 0.

Array20. An array of N real numbers and two integers K and L (1 ≤ K ≤ L ≤ N) are given. Find the sum of array elements with the order numbers in the range K to L inclusively.

Array21. An array of N real numbers and two integers K and L (1 ≤ K ≤ L ≤ N) are given. Find the average of array elements with the order numbers in the range K to L inclusively.

Array22. An array of N real numbers and two integers K and L (1 < K ≤ L ≤ N) are given. Find the sum of all array elements except ones with the order numbers in the range K to L inclusively.

Array23. An array of N real numbers and two integers K and L (1 < K ≤ L ≤ N) are given. Find the average of all array elements except ones with the order numbers in the range K to L inclusively.

Array24. An array of N distinct integers is given. If the array elements represent an arithmetic sequence (see Array3) then output its common difference, otherwise output 0.

Array25. An array of N nonzero integers is given. If the array elements represent a geometric sequence with integer common ratio (see Array4) then output its common ratio, otherwise output 0.

Array26. An array of N integers is given. If odd and even numbers are alternated in the array then output 0, otherwise output the order number of the first element that breaks the above condition.

Array27. An array of N nonzero integers is given. If positive and negative numbers are alternated in the array then output 0, otherwise output the order number of the first element that breaks the above condition.

Array28. Given an array A of N real numbers, find the minimum among elements with even order numbers: A2, A4, A6, … .

Array29. Given an array A of N real numbers, find the maximum among elements with odd order numbers: A1, A3, A5, … .

Array30. An array of N real numbers is given. Find the order numbers of array elements that are greater than their right neighbor. Output these order numbers in ascending order and also output the amount of such elements.

Array31. An array of N real numbers is given. Find the order numbers of array elements that are greater than their left neighbor. Output these order numbers in descending order and also output the amount of such elements.

Array32°. Given an array of N real numbers, find the order number of its first local minimum (an array element is called the local minimum if it is smaller than its neighbors).

Array33. Given an array of N real numbers, find the order number of its last local maximum (an array element is called the local maximum if it is greater than its neighbors).

Array34. Given an array of N real numbers, find the maximum among its local minimums (see the local minimum definition in Array32).

Array35. Given an array of N real numbers, find the minimum among its local maximums (see the local maximum definition in Array33).

Array36. Given an array of N real numbers, find the maximum among array elements that are neither local minimum nor local maximum (see the definitions of local minimum and local maximum in Array32 and Array33 respectively). If the array does not contain such elements then output 0 (as a real number).

Array37. Given an array of N real numbers, find the amount of its parts of ascending (that is, parts that contain elements whose values are in ascending order).

Array38. Given an array of N real numbers, find the amount of its parts of descending (that is, parts that contain elements whose values are in descending order).

Array39. Given an array of N real numbers, find the amount of its parts of monotonicity (that is, parts that contain elements whose values are in ascending or in descending order).

Array40. A real number R and an array A of N real numbers are given. Find the array element that is the nearest to the number R (i. e., an element AK such that the value |AK − R| is minimal).

Array41. Given an array of N real numbers, find two adjacent elements with the maximal sum of values and output these elements in ascending order of its indices.

Array42. A real number R and an array of N real numbers are given. Find two adjacent elements whose sum of values is the nearest to the number R and output these elements in ascending order of its indices (see the definition of two nearest numbers in Array40).

Array43. Given an array of N integers whose values are in ascending or in descending order, find the amount of its elements with distinct values.

Array44. An array of N integers is given, the array contains two elements with equal values. Find these elements and output their order numbers in ascending order.

Array45. Given an array of N real numbers, find two nearest array elements (i. e., two different elements AK and AL such that the value |AK − AL| is minimal) and output their order numbers in ascending order.

Array46. A real number R and an array of N real numbers are given. Find two different elements such that the sum of their values is the nearest to the number R, and output these elements in ascending order of indices (see the definition of two nearest numbers in Array40).

Array47°. Given an array of N integers, find the amount of its elements with distinct values.

Array48. Given an array of N integers, find the maximal amount of its elements with equal values.

Array49. An array of N integers is given. If the array is a permutation (i. e., if the array contains all integers in the range 1 to N) then output 0, otherwise output the order number of the first inadmissible element.

Array50. An array A of N integers is given, the array is a permutation (see the permutation definition in Array49). Find the amount of inversions in this permutation (i. e., the amount of pairs of elements AI and AJ such that I < J and AI > AJ).

## Work with several one-dimensional arrays

Array51. Two arrays A and B of N real numbers are given. Exchange the contents of the given arrays and output elements of the changed array A and then output elements of the changed array B.

Array52. Given an array A of N real numbers, create a new array B of the same size with elements defined as:

 BK = 2·AK, if AK < 5, AK/2 otherwise.

Array53. Two arrays A and B of N real numbers are given. Create a new array C of the same size; each element CK must be equal to the largest of elements of A and B with the same index K.

Array54°. An array A of N integers is given. Write elements of A whose values are even numbers to a new array B (in the same order) and output the size of array B and all its elements.

Array55. An array A of N (≤ 15) integers is given. Write elements of A with odd order numbers (1, 3, …) to a new array B and output the size of array B and all its elements. Do not use conditional statements.

Array56. An array A of N (≤ 15) integers is given. Write elements of A with order numbers that are multiples of 3 (3, 6, …) to a new array B and output the size of array B and all its elements. Do not use conditional statements.

Array57. An array A of N integers is given. Write elements of A with even order numbers to a new array B and then write elements of A with odd order numbers to the same array B, so the array B will contain the following elements:

A2,    A4,    A6,    …,    A1,    A3,    A5,    … .

Do not use conditional statements.

Array58. An array A of N real numbers is given. Create a new array B of the same size; each array element BK must be equal to the sum of elements of A with the order numbers in the range 1 to K.

Array59. An array A of N real numbers is given. Create a new array B of the same size; each array element BK must be equal to the average of elements of A with the order numbers in the range 1 to K.

Array60. An array A of N real numbers is given. Create a new array B of the same size; each array element BK must be equal to the sum of elements of A with the order numbers in the range K to N.

Array61. An array A of N real numbers is given. Create a new array B of the same size; each array element BK must be equal to the average of elements of A with the order numbers in the range K to N.

Array62. Given an array A of N real numbers, create two new arrays B and C and write all positive elements of A to the array B and all negative elements of A to the array C (in the same order). Output the size of the array B and all its elements and then output the size of the array C and all its elements.

Array63°. Two arrays A and B of 5 real numbers are given. Values of elements of each array are in ascending order. Write all elements of A and B to a new array C (of size 10) so that values of all elements of C were in ascending order.

Array64. Three arrays A, BC of NA, NBNC integers are given. Values of elements of each array are in descending order. Write all elements of A, BC to a new array D (of size NA + NB + NC) so that values of all elements of D were in descending order.

## One-dimensional arrays: changing

All tasks of this subsection should be solved without using additional arrays.

Array65. An array A of N real numbers and an integer K (1 ≤ K ≤ N) are given. Increase the value of each element of A by the initial value of AK.

Array66. An array of N integers is given. Increase all even numbers contained in the array by the initial value of the first even number in the array. If the array does not contain even numbers then do not change it.

Array67. An array of N integers is given. Increase all odd numbers contained in the array by the initial value of the last odd number in the array. If the array does not contain odd numbers then do not change it.

Array68. Given an array of N real numbers, exchange values of its minimal and maximal element.

Array69. Given an array of N real numbers (N is an even number), exchange values of its first and second element, its third and fourth element, and so on.

Array70. Given an array of N real numbers (N is an even number), exchange values of the first half and the second half of its elements.

Array71°. Given an array of N real numbers, change the order of its elements to inverse one.

Array72. An array A of N real numbers and two integers K and L (1 ≤ K < L ≤ N) are given. Change the order of the array elements between AK and AL (including these elements) to inverse one.

Array73. An array A of N real numbers and two integers K and L (1 ≤ K < L ≤ N) are given. Change the order of the array elements between AK and AL (not including these elements) to inverse one.

Array74. An array of N real numbers is given. Assign zero value to the array elements between the minimal element and the maximal element (not including these elements).

Array75. An array of N real numbers is given. Change the order of the array elements between the minimal element and the maximal element (including these elements) to inverse one.

Array76. Given an array of N real numbers, assign zero value to all its local maximums (an array element is called the local maximum if it is greater than its neighbors).

Array77. Given an array of N real numbers, replace each local minimum with its square (an array element is called the local minimum if it is smaller than its neighbors).

Array78. Given an array of N real numbers, replace each array element with the average of this element and its neighbors.

Array79°. Given an array of N real numbers, perform the right shift of array elements on one position by assigning initial values of A1, A2, …, AN−1 to elements A2, A3, …, AN respectively (an initial value of the last element will be lost). Assign zero value to the first element of the changed array.

Array80. Given an array of N real numbers, perform the left shift of array elements on one position by assigning initial values of AN, AN−1, …, A2 to elements AN−1, AN−2, …, A1 respectively (an initial value of the first element will be lost). Assign zero value to the last element of the changed array.

Array81. Given an array of N real numbers and an integer K (1 ≤ K < N), perform the right shift of array elements on K positions by assigning initial values of A1, A2, …, ANK to elements AK+1, AK+2, …, AN respectively (an initial value of the last K elements will be lost). Assign zero value to the first K elements of the changed array.

Array82. Given an array of N real numbers and an integer K (1 ≤ K < N), perform the left shift of array elements on K positions by assigning initial values of AN, AN−1, …, AK+1 to elements ANK, ANK−1, …, A1 respectively (an initial value of the first K elements will be lost). Assign zero value to the last K elements of the changed array.

Array83. Given an array of N real numbers, perform the right cyclic shift of array elements on one position by assigning initial values of A1, A2, …, AN−1, AN to elements A2, A3, …, AN, A1 respectively.

Array84. Given an array of N real numbers, perform the left cyclic shift of array elements on one position by assigning initial values of AN, AN−1, …, A2, A1 to elements AN−1, AN−2, …, A1, AN respectively.

Array85. Given an array of N real numbers and an integer K (1 ≤ K ≤ 4, K < N), perform the right cyclic shift of array elements on K positions by assigning initial values of A1, A2, …, AN to elements AK+1, AK+2, …, AK respectively. An additional array of 4 elements may be used for performing the required shift.

Array86. Given an array of N real numbers and an integer K (1 ≤ K ≤ 4, K < N), perform the left cyclic shift of array elements on K positions by assigning initial values of AN, AN−1, …, A1 to elements ANK, ANK−1, …, ANK+1 respectively. An additional array of 4 elements may be used for performing the required shift.

Array87. An array of N real numbers is given. The values of all array elements, except the first one, are in ascending order. Arrange all array elements in ascending order by moving the first element to a new position.

Array88. An array of N real numbers is given. The values of all array elements, except the last one, are in ascending order. Arrange all array elements in ascending order by moving the last element to a new position.

Array89°. An array of N real numbers is given. The values of all array elements, except one element, are in descending order. Arrange all array elements in descending order by moving the element, that violates ordering, to a new position.

Array90. An array of N real numbers and an integer K (1 ≤ K ≤ N) are given. Remove an element with the order number K from the array.

Array91. An array of N real numbers and two integers K and L (1 ≤ K < L ≤ N) are given. Remove elements with the order numbers in the range K to L inclusively from the array and output the size of the changed array and all its elements.

Array92°. Given an array of N integers, remove all odd numbers from the array and output the size of the changed array and all its elements.

Array93. Given an array of N (> 2) integers, remove all elements with even order numbers (2, 4, …) from the array. Do not use conditional statements.

Array94. Given an array of N (> 2) integers, remove all elements with odd order numbers (1, 3, …) from the array. Do not use conditional statements.

Array95. Given an array of N integers, remove all successive equal elements (except for their first occurrence) from the array.

Array96. Given an array of N integers, remove all equal elements (except for their first occurrence) from the array.

Array97. Given an array of N integers, remove all equal elements (except for their last occurrence) from the array.

Array98. Given an array of N integers, remove all elements whose values appear less than three times in the array. Output the size of the changed array and all its elements.

Array99. Given an array of N integers, remove all elements whose values appear more than twice in the array. Output the size of the changed array and all its elements.

Array100. Given an array of N integers, remove all elements whose values appear exactly twice in the array. Output the size of the changed array and all its elements.

Array101. An array of N real numbers and an integer K (1 ≤ K ≤ N) are given. Insert a new element with zero value before an element with the order number K.

Array102. An array of N real numbers and an integer K (1 ≤ K ≤ N) are given. Insert a new element with zero value after an element with the order number K.

Array103. An array of N real numbers is given. Insert new elements with zero value before an element with the minimal value and after an element with the maximal value.

Array104. An array of N real numbers and two integers K and M (1 ≤ K ≤ N, 1 ≤ M ≤ 10) are given. Insert M new elements with zero value before an element with the order number K.

Array105. An array of N real numbers and two integers K and M (1 ≤ K ≤ N, 1 ≤ M ≤ 10) are given. Insert M new elements with zero value after an element with the order number K.

Array106. Given an array of N real numbers, double occurrences of elements with even order numbers (2, 4, …). Do not use conditional statements in loops.

Array107. Given an array of N real numbers, triple occurrences of elements with odd order numbers (1, 3, …). Do not use conditional statements in loops.

Array108°. Given an array of N real numbers, insert an element with zero value before each element with positive value.

Array109. Given an array of N real numbers, insert an element with zero value after each element with negative value.

Array110. Given an array of N integers, double occurrences of all elements whose values are even numbers.

Array111. Given an array of N integers, triple occurrences of all elements whose values are odd numbers.

Array112°. An array A of N (≤ 6) real numbers is given. Sort the array in ascending order by using the exchange sort method (the bubble sorting): compare each pair of two adjacent elements (A1 and A2, A2 and A3, and so on) and exchange their values in case the left element is greater than the right one; repeat N − 1 times such array pass. Output all array elements after each pass for checking results. Note that the amount of analyzed pairs of the array elements can be reduced by 1 after each pass.

Array113. An array A of N (≤ 6) real numbers is given. Sort the array in ascending order by using the selection sort method: find the greatest element in the array and exchange the values of the greatest element and the last element (with the order number N); repeat N − 1 times this process, reducing the amount of analyzed elements by 1 after each array pass. Output all array elements after each pass for checking results.

Array114. An array A of N (≤ 6) real numbers is given. Sort the array in ascending order by using the insertion sort method: compare an element A2 with the first element A1 and exchange their values, if necessary, so that these elements were in ascending order; then move an element A3 to the left (sorted) part of the array, so that three elements were in ascending order; repeat this process for other array elements. Output all array elements after processing of each element (from A2 to AN). Note that it is convenient to assign the array element being processed to an additional array element A0 (the barrier element).

Array115. An array A of N real numbers is given. Without changing the array A, output the order numbers that correspond to array elements in ascending order of their values. For solving the task create an additional index array I, which contains order numbers in the range 1 to N, and use the bubble sorting (see Array112) as follows: compare elements of array A (with the order numbers I1 and I2, I2 and I3, and so on) and exchange, if necessary, values of corresponding elements of index array I. After repeating N − 1 times of such array pass the required sequence of order numbers will be contained in the array I.

## One-dimensional arrays: series of equal numbers

Array116°. An array A of N integers is given. A group of successive array elements with equal values is called a series of equal numbers, the amount of its elements is called a length of series (a length of series may be equal to 1), the value of its elements is called a value of series. Create and output two new integer-valued arrays B and C containing respectively lengths and values of all series of equal numbers of the array A.

Array117. Given an array of N integers, insert an element with zero value before each series of equal numbers of the array (see the series definition in Array116).

Array118. Given an array of N integers, insert an element with zero value after each series of equal numbers of the array (see the series definition in Array116).

Array119. Given an array of N integers, increase each series of equal numbers of the array by one element (see the series definition in Array116).

Array120. An array of N integers is given. The array contains at least one series of two or more equal numbers. Decrease each series of equal numbers of the array by one element (see the series definition in Array116).

Array121. An integer K (> 0) and an array of N integers are given. Double the length of the K-th series of equal numbers of the array (see the series definition in Array116). If the array contains less than K series then do not change it.

Array122. An integer K (> 1) and an array of N integers are given. Remove the K-th series of equal numbers from the array (see the series definition in Array116). If the array contains less than K series then do not change it.

Array123. An integer K (> 1) and an array of N integers are given. Exchange the first and the K-th series of equal numbers of the array (see the series definition in Array116). If the array contains less than K series then do not change it.

Array124. An integer K (> 1) and an array of N integers are given. Exchange the last and the K-th series of equal numbers of the array (see the series definition in Array116). If the array contains less than K series then do not change it.

Array125. An integer L (> 1) and an array of N integers are given. Replace each series, whose length is less than L, by one element with zero value (see the series definition in Array116).

Array126. An integer L (> 0) and an array of N integers are given. Replace each series, whose length is equal to L, by one element with zero value (see the series definition in Array116).

Array127. An integer L (> 0) and an array of N integers are given. Replace each series, whose length is greater than L, by one element with zero value (see the series definition in Array116).

Array128. Given an array of N integers, increase its first series of the maximal length by one element (see the series definition in Array116).

Array129. Given an array of N integers, increase its last series of the maximal length by one element (see the series definition in Array116).

Array130. Given an array of N integers, increase all its series of the maximal length by one element (see the series definition in Array116).

## One-dimensional arrays: sets of points

Each set of points must be stored either in two arrays of numbers (the first array stores x-coordinates, the second one stores y-coordinates) or in one array of records with two fields x and y.

Array131. A set A of N points in the coordinate plane and a point B are given (all points are determined by their coordinates xy). Find the point of A that is the nearest to the point B. The distance R between two points with the coordinates (x1y1) and (x2y2) may be found by formula:

R = ((x2 − x1)2 + (y2 − y1)2)1/2.

Array132. A set A of N points in the coordinate plane is given (points are determined by their coordinates xy). Find the point of A that lies in the second coordinate quarter and is the farthest from the origin. If the set A does not contain such points then output the origin (0, 0).

Array133. A set A of N points in the coordinate plane is given (points are determined by their coordinates xy). Find the point of A that lies in the first or the third coordinate quarter and is the nearest to the origin. If the set A does not contain such points then output the origin (0, 0).

Array134°. A set A of N points in the coordinate plane is given (points are determined by their coordinates xy). Find two points of A with the maximal distance between them and output these points (in ascending order of their indices in the set A) and the value of the distance.

Array135. Two sets A and B of N1 and N2 points respectively are given (points are determined by their coordinates xy). Find the point of A and the point of B with the minimal distance between them. Output the value of the distance and then the point of A and the point of B.

Array136. A set A of N points in the coordinate plane is given (N > 2, points are determined by their coordinates xy). Find the point of A such that the sum of distances between this point and other points of A is minimal and output this point and the corresponding sum.

Array137. A set A of N points in the coordinate plane is given (N > 2, points are determined by their coordinates xy). Find the maximal perimeter of a triangle with vertices belonging to A. Output this perimeter and the corresponding vertices (in ascending order of their indices in the set A).

Array138. A set A of N points in the coordinate plane is given (N > 2, points are determined by their coordinates xy). Find the minimal perimeter of a triangle with vertices belonging to A. Output this perimeter and the corresponding vertices (in ascending order of their indices in the set A).

Array139. A set A of N points with integer-valued coordinates xy is given. The order in the plane is defined as follows:

(x1y1) < (x2y2),    if either x1 < x2 or x1 = x2 and y1 < y2.

Using this order definition, rearrange points of A in ascending order.

Array140. A set A of N points with integer-valued coordinates xy is given. The order in the plane is defined as follows:

(x1y1) < (x2y2),    if either x1 + y1 < x2 + y2 or x1 + y1 = x2 + y2 and x1 < x2.

Using this order definition, rearrange points of A in descending order.

 Designed byM. E. Abramyan and V. N. Braguilevsky Last revised:01.01.2024