From 136c2788f042e3e94aa4a8bb9d321e6fc08a4526 Mon Sep 17 00:00:00 2001 From: Craig Lionel Roberts <80633545+CR1502@users.noreply.github.com> Date: Tue, 16 Mar 2021 14:39:55 +0530 Subject: [PATCH 1/3] Create Problem 1 day 2.cpp --- CR1502/Problem 1 day 2.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 CR1502/Problem 1 day 2.cpp diff --git a/CR1502/Problem 1 day 2.cpp b/CR1502/Problem 1 day 2.cpp new file mode 100644 index 0000000..2235b5e --- /dev/null +++ b/CR1502/Problem 1 day 2.cpp @@ -0,0 +1,21 @@ +/* + Time Complexity = O(N * maxElement) + Space Complexity = O(1) + + Where N is the length of the array. + Where maxElement is the maximum element of the array. +*/ + +#include + +bool isPresent(vector &arr, int n, int val) { + + for (int i = 0; i < n; i++) { + if (arr[i] == val) { + return true; + } + } + + return false; + +} From c87df22d09541945f53718dd001e64408600eec0 Mon Sep 17 00:00:00 2001 From: Craig Lionel Roberts <80633545+CR1502@users.noreply.github.com> Date: Tue, 16 Mar 2021 14:40:33 +0530 Subject: [PATCH 2/3] Create Program 2 of day 2.cpp --- CR1502/Program 2 of day 2.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 CR1502/Program 2 of day 2.cpp diff --git a/CR1502/Program 2 of day 2.cpp b/CR1502/Program 2 of day 2.cpp new file mode 100644 index 0000000..be66196 --- /dev/null +++ b/CR1502/Program 2 of day 2.cpp @@ -0,0 +1,29 @@ +bool isConsecutive(vector &arr, int n) { + + // Get minimum element + int minEle = INT_MAX; + + for (int i = 0; i < n; i++) { + minEle = min(minEle, arr[i]); + } + + // Get maximum element + int maxEle = INT_MIN; + + for (int i = 0; i < n; i++) { + maxEle = max(maxEle, arr[i]); + } + + if (maxEle - minEle + 1 != n) { + return false; + } + + for (int val = minEle; val <= maxEle; val++) { + if (!isPresent(arr, n, val)) { + return false; + } + } + + return true; + +} From 6f0f7301ca605fd3e297f788ac54f7b9caee3c53 Mon Sep 17 00:00:00 2001 From: Craig Lionel Roberts <80633545+CR1502@users.noreply.github.com> Date: Tue, 16 Mar 2021 14:45:03 +0530 Subject: [PATCH 3/3] Create Program 3 of day 2.cpp --- CR1502/Program 3 of day 2.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 CR1502/Program 3 of day 2.cpp diff --git a/CR1502/Program 3 of day 2.cpp b/CR1502/Program 3 of day 2.cpp new file mode 100644 index 0000000..db0c0a1 --- /dev/null +++ b/CR1502/Program 3 of day 2.cpp @@ -0,0 +1,28 @@ +/* + Time Complexity: O(N) + Space complexity: O(1) + + Where 'N' is the missing of elements in the given sequence. +*/ + +int missingNumber(vector &arr, int n) { + /* + The first term and (n+1)th term of the AP will be b[0] and b[n-1]. + So using the formula the common difference d will be given by + d = (b[n-1] - b[0]) / n + */ + + int d = (arr[n - 1] - arr[0]) / n; + int missing = -1; + // Traverse on each element to find position of missing number + for (int i = 0; i + 1 < n; ++i) { + int diff = arr[i + 1] - arr[i]; + // If the difference is not same as the common difference of the AP, then missing number will lie between the range b[i] to b[i+1] + if (diff != d) { + missing = arr[i] + d; + break; + } + } + // return the missing number + return missing; +}