Microsoft Online Assessment (OA) - Particle Velocity

You are a programmer in a scientific team doing research into particles. As an experiment, you have measured the position of a single particle in N equally distributed moments of time. The measurement made in moment K is recorded in an array particles as particles[K].

Now, your job is to count all the periods of time when the movement of the particle was stable. Those are the periods during which the particle doesn't change its velocity: i.e., the difference between any two consecutive position measurements remains the same. Note that you need at least three measurements to be sure that the particle didn't change its velocity.

For Example

  • 1, 3, 5, 7, 9 is stable (velocity is 2)
  • 7, 7, 7, 7 is stable (particle stays in place)
  • 3, -1, -5, -9 is stable (velocity is 4)
  • 0, 1 is not stable (you need at least three measurements)
  • 1, 1, 2, 5, 7 is not stable (velocity changes between measurements)

More formally, your task is to find all the periods of time particles[P], particles[P+1], ....particles[Q] (of length at least 3) during which the movement of the particle is stable. Note that some periods of time might be contained in others (see below example).

Example:

Input: [-1, 1, 3, 3, 3, 2, 3, 2, 1, 0]
Output: 5
Explanation:

Possible periods of time for which velocity is stable are:

valueslocation(from, to)Velocity
[-1, 1, 3](0,2)2
[3, 3, 3](2,4)0
[3, 2, 1, 0](6,9)-1
[3, 2, 1](6,8)-1
[2, 1, 0](7,9)-1

Note: Last two periods are contained by (6,9)

Write a function:

1public static int particleVelocity(int[] particles)

that given array particles consisting of N integers representing the results of the measurements, returns the number of periods of time when the movement of the particle was stable. The function should return -1 if the result exceeds 10^9.

More examples:

Example 1:

Input: [1, 3, 5, 7, 9]
Output: 6
Explanation:

Possible periods of time for which velocity is stable are:

valueslocation(from, to)Velocity
[1, 3, 5](0,2)2
[3, 5, 7](1,3)2
[5, 7, 9](2,4)2
[1, 3, 5, 7, 9](0,4)2
[1, 3, 5, 7](0,3)2
[3, 5, 7, 9](1,4)2

Example 2:

Input: [7, 7, 7, 7]
Output: 3
Explanation:

Possible periods of time for which velocity is stable are:

valueslocation(from, to)Velocity
[7, 7, 7, 7](0,3)0
[7, 7, 7](1,3)0
[7, 7, 7](0,2)0

Try it yourself

Implementation

1def particleVelocity(particles):
2    numStable = 0
3    if len(particles) < 3:
4       return 0
5    for i in range(len(particles) - 2):
6        for j in range(i + 1, len(particles) - 1):
7            if particles[j + 1] - particles[j] == particles[i + 1] - particles[i]:
8                numStable += 1
9            else :
10                break
11    return numStable if numStable < 1000000000 else -1
12
13if __name__ == '__main__':
14    elevs = [int(x) for x in input().split()]
15    print(particleVelocity(elevs))
16
1import java.util.Arrays;
2import java.util.List;
3import java.util.Scanner;
4import java.util.stream.Collectors;
5
6class Solution {
7
8    public static int particleVelocity(int[] particles) {
9        int total_periods = 0, particles_size = particles.length;
10        for (int i = 0; i < particles_size; i++) {
11            for (int count = 0; i + 2 < particles_size && particles[i + 1] - particles[i] == particles[i + 2] - particles[i + 1]; i++) {
12                count++;
13                total_periods += count;
14            }
15        }
16        return total_periods < 1000000000 ? total_periods : -1;
17    }
18
19    public static void main(String[] args) {
20        Scanner scanner = new Scanner(System.in);
21        int[] particles = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
22        scanner.close();
23        System.out.println(particleVelocity(particles));
24    }
25
26}
27
1const readline = require('readline');
2
3function particleVelocity(particles) {
4    let total_periods = 0;
5    const particles_size = particles.length;
6
7    for (let i = 0; i < particles_size; i++) {
8        for (let count = 0; i + 2 < particles_size && particles[i + 1] - particles[i] === particles[i + 2] - particles[i + 1]; i++) {
9            count++;
10            total_periods += count;
11        }
12    }
13    return total_periods < 1000000000 ? total_periods : -1;
14}
15
16const rl = readline.createInterface(process.stdin);
17rl.on('line', (input) => {
18    rl.close();
19    const particles = input.split(' ').map(x => parseInt(x, 10));
20    console.log(particleVelocity(particles));
21});
22

Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ