diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2023-01-04 09:44:53 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2023-01-04 09:44:53 +0330 |
| commit | dc6e6dc4252027b60ebd935abb1a53ce46145989 (patch) | |
| tree | dba808345b6296ac45e7e53f7ee7046d503965d4 /challenge-198/deadmarshal/cpp/ch-1.cpp | |
| parent | ee249218f373166edca2b95144a9b0b59e200e05 (diff) | |
| download | perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.tar.gz perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.tar.bz2 perlweeklychallenge-club-dc6e6dc4252027b60ebd935abb1a53ce46145989.zip | |
TWC198
Diffstat (limited to 'challenge-198/deadmarshal/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-198/deadmarshal/cpp/ch-1.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-198/deadmarshal/cpp/ch-1.cpp b/challenge-198/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..2065745b5f --- /dev/null +++ b/challenge-198/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,30 @@ +#include<iostream> +#include<vector> +#include<algorithm> + +template<typename T> +int max_gap(std::vector<T> &vec) +{ + if(vec.size() < 2) return 0; + std::sort(vec.begin(),vec.end()); + size_t i{}; + int max{},count{},temp{}; + while(i < vec.size()) + { + temp = abs(vec[i] - vec[i+1]); + if(temp > max) max = temp; + i += 2; + } + for(i = 0; i < vec.size()-1; ++i) + if(abs(vec[i] - vec[i+1]) == max) count++; + return count; +} + +int main() +{ + std::vector<int> vec1{2,5,8,1},vec2{3}; + std::cout << max_gap<int>(vec1) << '\n'; + std::cout << max_gap<int>(vec2) << '\n'; + return 0; +} + |
