From d6e6b0328ce2a4fe228262713e89e391421f5ab3 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 17 Nov 2021 09:26:15 +0200 Subject: Added my solutions to Sem. 06 exercises --- .../Solutions Sem. 06/exercise04.cpp | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Year 1/Programming introduction/Solutions Sem. 06/exercise04.cpp (limited to 'Year 1/Programming introduction/Solutions Sem. 06/exercise04.cpp') diff --git a/Year 1/Programming introduction/Solutions Sem. 06/exercise04.cpp b/Year 1/Programming introduction/Solutions Sem. 06/exercise04.cpp new file mode 100644 index 0000000..fa31c84 --- /dev/null +++ b/Year 1/Programming introduction/Solutions Sem. 06/exercise04.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +bool isPalindromeArray(int nums[], int len, int start) { + int end = (len - start) / 2; + + for (int i = start; i < end; i++) { + if (nums[i] != nums[len - 1 - i]) + return false; + } + + return true; +} + +int isConcatOfTwoPalindromes(int nums[], int len) { + int border = len / 2 + 1; + + return isPalindromeArray(nums, border, 0) && isPalindromeArray(nums, len, border); +} + +int main() { + int numbers1[9] = { 1, 2, 3, 2, 1, 4, 5, 5, 4 }; + cout << isConcatOfTwoPalindromes(numbers1, 9) << endl; + + int numbers2[5] = { 1, 2, 3, 4, 5 }; + cout << isConcatOfTwoPalindromes(numbers2, 5) << endl; + + int numbers3[5] = { 1, 2, 3, 2, 1 }; + cout << isConcatOfTwoPalindromes(numbers3, 5) << endl; +} -- cgit v1.3.1-26-gb76a