Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_SI / Pract. 09 / task7.cpp
diff options
context:
space:
mode:
Commit97b4d62734acf7533a96b08bcc6044eb94847931 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2022-01-13 21:15:47 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2022-01-13 21:15:47 +0200
Files19329717e5479af4bdacf34e3ca5ee8364f0f078 /pract_SI/Pract. 09/task7.cpp
Introduction_to_programming_FMI-97b4d62734acf7533a96b08bcc6044eb94847931.tar
Introduction_to_programming_FMI-97b4d62734acf7533a96b08bcc6044eb94847931.tar.gz
Introduction_to_programming_FMI-97b4d62734acf7533a96b08bcc6044eb94847931.zip
Parent7b3b0c12934abad49aeeea12901627355ab53f9e (diff)
Add Pract. solutions.
1 files changed, 32 insertions, 0 deletions
-rw-r--r--pract_SI/Pract. 09/task7.cpp32
diff --git a/pract_SI/Pract. 09/task7.cpp b/pract_SI/Pract. 09/task7.cpp
new file mode 100644
index 0000000..5065a6d
--- /dev/null
+++ b/pract_SI/Pract. 09/task7.cpp
@@ -0,0 +1,32 @@
+#include <vector>
+
+bool checkIsPalindrom(const vector<int>& v)
+{
+ //if (v.size() < 2) - not needed as for-loop covers this case.
+ // return true;
+
+ for (size_t i = 0; i < v.size() / 2; i++)
+ if (v[i] != v[v.size() - 1 - i])
+ return false;
+
+ return true;
+}
+
+bool isPalindrom(const std::vector<int>& v, int operations)
+{
+ if(v.empty())
+ return false;
+
+ if (operations < 0)
+ return false;
+
+ if (checkIsPalindrom(v))
+ return true;
+
+ std::vector<int> left = v, right = v;
+ left.erase(left.begin()); //remove first
+ right.pop_back(); //remove last
+ operations--;
+
+ return isPalindrom(left, operations) || isPalindrom(right, operations);
+} \ No newline at end of file