Creating a " triangle row" using arrays

How do you make a program in which you must submit a number N which can vary from 3 to 100 or N Є [3 ;100 ] And you make an array submitting elements inside it. If the subtraction of the first and second number equals the subtraction of the last and second to last element . Then if the subtraction of the second and third number equals the subtraction of second last and third last and so on...
By the way absolute values also work |9 - 8| = |7 - 8|
Example:
Entry:
5
3 3 1 3 3 - yes

7
1 3 5 6 1 5 1 - no
Last edited on
Is this something like what you're after (not thoroughly tested):

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
#include <vector>
#include <iostream>
#include <cmath>

int main()
{
	size_t n;
	std::cout << "How many numbers to enter: ";
	std::cin >> n;

	std::vector<int> vi (n);

	for (auto& v : vi)
		std::cin >> v;

	const size_t mid {n / 2 + 1};

	auto it {vi.begin()};
	const auto ie {it + mid};
	auto itr {vi.rbegin()};
	const auto itre {itr + mid};

	bool ok {true};

	for (; ok && it < ie && itr < itre; ++it, ++itr)
		if (std::abs(*it - *(it + 1)) != std::abs(*itr - *(itr + 1)))
			ok = false;

	std::cout << std::boolalpha << ok << '\n';
}



How many numbers to enter: 5
3 3 1 3 3
true

How many numbers to enter: 7
1 3 5 6 1 5 1
false


it works well enough , thank you !
Topic archived. No new replies allowed.