-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1502-Can_Make_Arithmetic_Progression_From_Sequence.cpp
More file actions
101 lines (91 loc) · 2.85 KB
/
1502-Can_Make_Arithmetic_Progression_From_Sequence.cpp
File metadata and controls
101 lines (91 loc) · 2.85 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
* 1502-Can_Make_Arithmetic_Progression_From_Sequence.cpp
* Billy.Ljm
* 06 June 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
*
* A sequence of numbers is called an arithmetic progression if the difference
* between any two consecutive elements is the same.
*
* Given an array of numbers arr, return true if the array can be rearranged to
* form an arithmetic progression. Otherwise, return false.
*
* ===========
* My Approach
* ===========
* Assuming the array can form an arithmetic progression, we can easily know the
* first term which will be the minimum, and the common difference via (max -
* min) / size`. Then, we just have to iterate again through the array, ensuring
* all terms adhere to the rules and that are no repeated numbers
*
* This has a time complexity of O(n) and space complexity of O(n), where n is
* the size of the array.
******************************************************************************/
#include <iostream>
#include <vector>
/**
* Solution
*/
class Solution {
public:
/**
* Determines if an array can be rearranged to form an arithmetic progression
*
* @param arr array to rearrange
*
* @return true if array can be rearranged into an arithmetic progression
*/
bool canMakeArithmeticProgression(std::vector<int>& arr) {
// find min and max, and common difference
int minn = INT_MAX;
int maxx = INT_MIN;
for (int ele : arr) {
if (ele < minn) minn = ele;
if (ele > maxx) maxx = ele;
}
int diff = (maxx - minn) / (arr.size() - 1);
if ((maxx - minn) % (arr.size() - 1) != 0) return false; // non-integer difference
else if (diff == 0) return true; // all terms are the same
// check if all terms are valid
std::vector<bool> found(arr.size(), false);
for (int ele : arr) {
if ((ele - minn) % diff != 0) return false; // not arithmetic progression
else if ((ele - minn) / diff >= arr.size()) return false; // out of bounds
else if (found[(ele - minn) / diff]) return false; // duplicate value
found[(ele - minn) / diff] = true;
}
return true;
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<int> arr;
// test case 1
arr = { 3, 5, 1 };
std::cout << "canMakeArithmeticProgression(" << arr << ") = ";
std::cout << std::boolalpha << sol.canMakeArithmeticProgression(arr) << std::endl;
// test case 2
arr = { 1, 2, 4 };
std::cout << "canMakeArithmeticProgression(" << arr << ") = ";
std::cout << std::boolalpha << sol.canMakeArithmeticProgression(arr) << std::endl;
return 0;
}