aboutsummaryrefslogtreecommitdiff
path: root/challenge-200/paulo-custodio/cpp/ch-1.cpp
blob: 46948327c7087f6b2750345b4ddefcbfb5b9e246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Challenge 200

Task 1: Arithmetic Slices
Submitted by: Mohammad S Anwar
You are given an array of integers.

Write a script to find out all Arithmetic Slices for the given array of integers.

An integer array is called arithmetic if it has at least 3 elements and the
differences between any three consecutive elements are the same.


Example 1
Input: @array = (1,2,3,4)
Output: (1,2,3), (2,3,4), (1,2,3,4)
Example 2
Input: @array = (2)
Output: () as no slice found.
*/

#include <iostream>
#include <vector>

bool is_arithmetic(int nums[], int nums_size) {
    if (nums_size < 3)
        return false;
    int step = nums[1] - nums[0];
    for (int i = 2; i < nums_size; i++) {
        if (nums[i] - nums[i-1] != step)
            return false;
    }
    return true;
}

void print_slices(std::vector<int> nums) {
    const char* sep = "";
    int nums_size = static_cast<int>(nums.size());

    std::cout << "(";
    for (int i = 0; i < nums_size-2; i++) {
        for (int j = i+2; j < nums_size; j++) {
            if (is_arithmetic(&nums[i], j-i+1)) {
                std::cout << sep;
                sep = "), (";
                for (int k = i; k <= j; k++) {
                    std::cout << nums[k];
                    if (k != j)
                        std::cout << ",";
                }
            }
        }
    }
    std::cout << ")" << std::endl;
}

int main(int argc, char* argv[]) {
    argv++; argc--;
    if (argc == 0) {
        std::cerr << "usage: ch-1 nums..." << std::endl;
        exit(EXIT_FAILURE);
    }

    std::vector<int> nums;
    for (int i = 0; i < argc; i++)
        nums.push_back(atoi(argv[i]));
    print_slices(nums);
}