The fourth problem of the "Programming Basics" Practical Exam includes a single loop with simple logic in it. Let's take a few examples.
There are n integers a1, a2, …, an given. Calculate the sums:
- sum1 = a1 + a4 + a7 + … (the numbers are summed, starting from the first one with step 3).
- sum2 = a2 + a5 + a8 + … (the numbers are summed, starting from the second one with step 3).
- sum3 = a3 + a6 + a9 + … (the numbers are summed, starting from the third one with step 3).
The input data is read from the console. The first row is an integer n (0 ≤ n ≤ 1000). On the next n rows are n integers in the range [-1000 … 1000]: a1, a2, …, an.
On the console should be printed 3 rows containing the 3 sums in a format such as in the example.
|sum1 = 3
sum2 = 5
sum3 = 0
|sum1 = 19
sum2 = -2
sum3 = 6
|sum1 = 10
sum2 = 13
sum3 = 2
We will take the count of numbers from the console and declare starting values of the three sums.
Since we do not know in advance how many numbers we will process, we will take them one at a time in a loop which will be repeated n times and we will process them in the body of the loop.
To find out in which of the three sums we need to add the number, we will divide its sequence number into three and we will use the remainder. We will use the variable
i, which tracks the number of spins per loop, to find out which sequence number we are. When the remainder of
i/3 is zero, it means we will add this number to the first sum when it is 1 to the second and when it is 2 to the third.
Finally, we will print the result on the console in the required format.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/516#6.
A number of n numbers are given: a1, a2, …, an. Calculate the length of the longest increasing sequence of consecutive elements in the row of numbers.
The input data is read from the console. The first row is an integer n (0 ≤ n ≤ 1000). On the following n lines are n integers in the range [-1000 … 1000]: a1, a2, …, an.
On the console must be printed one number - the length of the longest increasing sequence.
To solve this problem, we need to think a bit more algorithmically. A sequence of numbers is given to us, and we need to check whether each subsequent will be larger than the previous one, and if so we count how long is the sequence in which this condition is fulfilled. Then we have to find which one subsequence of these is the longest. To do this, let's make some variables that we will use during the problem.
n is the count of numbers we get from the console. In
countCurrentLongest, we will keep the number of elements in the increasing sequence we are currently counting. For example, in the sequence: 5, 6, 1, 2, 3
countCurrentLongest will be 2 when we reach the second element of the counting (5, 6, 1, 2, 3) and will become 3 when we reach the last element (5, 6, 1, 2, 3), because the increasing row 1, 2, 3 has 3 elements. We will use
countLongest to keep the longest increasing sequence. The other variables are
a - the number we are currently in, and
aPrev - the previous number which we will compare with
a to see if the row is growing.
We begin to rotate the numbers and check if the present number
a is larger than the previous
aPrev. If this is true, then the row is growing and we need to increase its number by 1. This is preserved in the variable that tracks the length of the sequence we are currently in -
countCurrentLongest. If the number
a is not greater than the previous one, it means that a new sequence starts and we have to start the count from 1. Finally, after all the checks,
aPrev becomes the number we are currently using and we start the loop from the beginning with the next entered
Here is a sample implementation of the algorithm described:
It remains to be seen which of all sequences is the longest. We will do this by checking in the loop if the sequence we are currently has become longer than the longest by now. The whole loop will look like this:
Finally, we print the length of the longest sequence found.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/516#7.