diff --git a/SET Multiset/MaxSumSlideingWindow b/SET Multiset/MaxSumSlideingWindow new file mode 100644 index 0000000..b313191 Binary files /dev/null and b/SET Multiset/MaxSumSlideingWindow differ diff --git a/SET Multiset/MaxSumSlideingWindow.cpp b/SET Multiset/MaxSumSlideingWindow.cpp new file mode 100644 index 0000000..220e232 --- /dev/null +++ b/SET Multiset/MaxSumSlideingWindow.cpp @@ -0,0 +1,38 @@ +//SLideing window maximum sum of sub array +#include +#include +using namespace std; + +void maxSubarraySum(int arr[], int n, int k, int x) +{ + int sum = 0, ans = 0; + for (int i = 0; i < k; i++) + { + sum += arr[i]; + } + if (sum < x) + { + ans = sum; + } + for (int i = k; i < n; i++) + { + sum = sum - arr[i - k]; + sum = sum + arr[i]; + if (sum < x) + { + ans = max(ans, sum); + } + } + cout << ans << " : is msximum sub array" << endl; +} + +int main() +{ + int arr[] = {7, 5, 4, 6, 8, 9}; + int k = 3; + int x = 20; + int n = 6; + maxSubarraySum(arr, n, k, x); + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/allocateminimumPages b/SET Multiset/allocateminimumPages new file mode 100644 index 0000000..dd7cd84 Binary files /dev/null and b/SET Multiset/allocateminimumPages differ diff --git a/SET Multiset/allocateminimumPages.cpp b/SET Multiset/allocateminimumPages.cpp new file mode 100644 index 0000000..5fafd2d --- /dev/null +++ b/SET Multiset/allocateminimumPages.cpp @@ -0,0 +1,70 @@ +//Binary search tree challenge 2 +#include +#include +#include +#include +using namespace std; +bool isPossible(int arr[], int n, int m, int min) +{ + int studentsRequired = 1, sum = 0; + for (int i = 0; i < n; i++) + { + if (arr[i] > min) + { + + return false; + } + if (sum + arr[i] > min) + { + studentsRequired++; + sum = arr[i]; + } + + if (studentsRequired > m) + { + return false; + } + + else + { + sum += arr[i]; + } + } + return true; +} + +int allocateminimumPages(int arr[], int n, int m) +{ + int sum = 0; + if (n < m) + { + return -1; + } + for (int i = 0; i < n; i++) + { + sum += arr[i]; + } + int start = 0, end = sum, ans = INT_MAX; + while (start <= end) + { + int mid = (start + end) / 2; + if (isPossible(arr, n, m, mid)) + { + ans = min(ans, mid); + end = mid - 1; + } + else + { + start = mid + 1; + } + } + return ans; +} +int main() +{ + int arr[] = {12, 34, 67, 90}; + int n = 4; + int m = 2; + cout << "The minimum no of pages : " << allocateminimumPages(arr, n, m); + return 0; +} \ No newline at end of file diff --git a/SET Multiset/binarySearchChhallange1 b/SET Multiset/binarySearchChhallange1 new file mode 100644 index 0000000..5fef217 Binary files /dev/null and b/SET Multiset/binarySearchChhallange1 differ diff --git a/SET Multiset/binarySearchChhallange1.cpp b/SET Multiset/binarySearchChhallange1.cpp new file mode 100644 index 0000000..c97838e --- /dev/null +++ b/SET Multiset/binarySearchChhallange1.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; + +bool isFeasible(int mid, int arr[], int n, int k) +{ + int pos = arr[0], elements = 1; + for (int i = 1; i < n; i++) + { + if (arr[i] - pos >= mid) + { + pos = arr[i]; + elements++; + if (elements == k) + { + return true; + } + } + } + return false; +} + +int largestMinDistance(int arr[], int n, int k) +{ + sort(arr, arr + n); + int result = -1; + int left = 1, right = arr[n - 1]; + while (left < right) + { + int mid = (left + right) / 2; + if (isFeasible(mid, arr, n, k)) + { + result = max(result, mid); + left = mid + 1; + } + else + { + right = mid; + } + } + return result; +} + +int main() +{ + int arr[] = {1, 2, 8, 4, 9}; + int n = 5; + int k = 3; + cout << "largest min distance is : " << largestMinDistance(arr, n, k) << endl; + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/bstChallange3 b/SET Multiset/bstChallange3 new file mode 100644 index 0000000..f1be592 Binary files /dev/null and b/SET Multiset/bstChallange3 differ diff --git a/SET Multiset/bstChallange3.cpp b/SET Multiset/bstChallange3.cpp new file mode 100644 index 0000000..f491b6e --- /dev/null +++ b/SET Multiset/bstChallange3.cpp @@ -0,0 +1,53 @@ +//Binary search tree challenge 3 Painters partition problem +#include +#include +using namespace std; +int findFeasible(int boards[], int n, int limit) +{ + int sum = 0, painters = 1; + for (int i = 0; i < n; i++) + { + sum += boards[i]; + if (sum > limit) + { + sum = boards[i]; + painters++; + } + } + return painters; +} + +int paintersPartition(int boards[], int n, int m) +{ + int totalLength = 0, k = 0; + for (int i = 0; i < n; i++) + { + k = max(k, boards[i]); + totalLength += boards[i]; + } + int low = k, high = totalLength; + while (low < high) + { + int mid = (high + low) / 2; + int painters = findFeasible(boards, n, mid); + if (painters <= m) + { + high = mid; + } + else + { + low = mid + 1; + } + } + return low; +} + +int main() +{ + int arr[] = {10, 20, 30, 40}; + int n = 4; + int m = 2; + cout << "Minimum time to paint the boards : " << paintersPartition(arr, n, m) << endl; + cout << endl; + return 0; +} \ No newline at end of file diff --git a/SET Multiset/bstChallange5 b/SET Multiset/bstChallange5 new file mode 100644 index 0000000..225fdd4 Binary files /dev/null and b/SET Multiset/bstChallange5 differ diff --git a/SET Multiset/bstChallange5.cpp b/SET Multiset/bstChallange5.cpp new file mode 100644 index 0000000..3db589c --- /dev/null +++ b/SET Multiset/bstChallange5.cpp @@ -0,0 +1,31 @@ +//BST challange 5 find the peak element +#include +#include +using namespace std; + +int findPeakElement(int arr[], int low, int high, int n) +{ + int mid = low + (high - low) / 2; + if ((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid])) + { + return mid; + } + else if (mid > 0 && arr[mid - 1] > arr[mid]) + { + + return findPeakElement(arr, low, mid - 1, n); + } + else + { + return findPeakElement(arr, mid + 1, high, n); + } +} + +int main() +{ + int arr[] = {0, 6, 8, 5, 7, 9}; + int n = 6; + cout << "Peak element index is :" << findPeakElement(arr, 0, n - 1, n) << endl; + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/bstchallange4 b/SET Multiset/bstchallange4 new file mode 100644 index 0000000..94ee35f Binary files /dev/null and b/SET Multiset/bstchallange4 differ diff --git a/SET Multiset/bstchallange4.cpp b/SET Multiset/bstchallange4.cpp new file mode 100644 index 0000000..e25afb8 --- /dev/null +++ b/SET Multiset/bstchallange4.cpp @@ -0,0 +1,47 @@ +//bst challange 4 search and rotated array +#include +#include +using namespace std; + +int searchInRotatedArray(int arr[], int key, int left, int right) +{ + if (left > right) + { + return -1; + } + int mid = (left + right) / 2; + if (arr[mid] == key) + { + return mid; + } + if (arr[left] <= arr[mid]) + { + if (key >= arr[left] && key <= arr[mid]) + { + return searchInRotatedArray(arr, key, left, mid - 1); + } + return searchInRotatedArray(arr, key, mid + 1, right); + } + if (key >= arr[mid] && key <= arr[right]) + { + return searchInRotatedArray(arr, key, mid + 1, right); + } + return searchInRotatedArray(arr, key, left, mid - 1); +} +int main() +{ + int arr[] = {6, 7, 8, 9, 10, 1, 2, 5}; + int n = 8; + int key = 8; + int idx = searchInRotatedArray(arr, key, 0, n - 1); + if (idx == -1) + { + cout << "Key does not exist" << endl; + } + else + { + cout << "Key is present at idx : " << idx << endl; + } + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/minimumsubArraySlideingwindow b/SET Multiset/minimumsubArraySlideingwindow new file mode 100644 index 0000000..be364a2 Binary files /dev/null and b/SET Multiset/minimumsubArraySlideingwindow differ diff --git a/SET Multiset/minimumsubArraySlideingwindow.cpp b/SET Multiset/minimumsubArraySlideingwindow.cpp new file mode 100644 index 0000000..14429d0 --- /dev/null +++ b/SET Multiset/minimumsubArraySlideingwindow.cpp @@ -0,0 +1,44 @@ +// minimum sub array windos slideing window time complexity O(n) +#include +#include +#include +using namespace std; +int smallestSubArrayWithSum(int arr[], int n, int x) +{ + int sum = 0, minLength = n + 1, start = 0, end = 0; + while (end < n) + { + while (sum <= x && end < n) + { + sum += arr[end++]; + } + while (sum > x && start < n) + { + if (end - start < minLength) + { + minLength = end - start; + } + sum -= arr[start++]; + } + } + return minLength; +} + +int main() +{ + int arr[] = {1, 4, 45, 6, 10, 19}; + + int x = 51; + int n = 6; + int minLength = smallestSubArrayWithSum(arr, n, x); + if (minLength == n + 1) + { + cout << "No such array exist" << endl; + } + else + { + cout << "The Smallest Length is :" << minLength << endl; + } + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/multiSet b/SET Multiset/multiSet new file mode 100644 index 0000000..f08b4a4 Binary files /dev/null and b/SET Multiset/multiSet differ diff --git a/SET Multiset/multiSet.cpp b/SET Multiset/multiSet.cpp new file mode 100644 index 0000000..1d92ada --- /dev/null +++ b/SET Multiset/multiSet.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +using namespace std; +int main() +{ + multiset s; + s.insert(1); + s.insert(2); + s.insert(3); + s.insert(3); + s.insert(4); + s.insert(5); + // s.erase(3); + s.erase(s.find(3)); + for (auto i : s) + { + cout << i << endl; + } + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/numberformedfromSubArraydivisibleby3 b/SET Multiset/numberformedfromSubArraydivisibleby3 new file mode 100644 index 0000000..0a6a5a6 Binary files /dev/null and b/SET Multiset/numberformedfromSubArraydivisibleby3 differ diff --git a/SET Multiset/numberformedfromSubArraydivisibleby3.cpp b/SET Multiset/numberformedfromSubArraydivisibleby3.cpp new file mode 100644 index 0000000..dd8c677 --- /dev/null +++ b/SET Multiset/numberformedfromSubArraydivisibleby3.cpp @@ -0,0 +1,58 @@ +//number formed from Sub Array divisible by 3 slideing window challange 3 time complexity O(n) +#include +#include +#include +using namespace std; + +void computeNumberFromSubArray(vector arr, int k) +{ + pair ans; + int sum = 0; + + for (int i = 0; i < k; i++) + { + sum += arr[i]; + } + bool found = false; + if (sum % 3 == 0) + { + ans = make_pair(0, k - 1); + found = true; + } + for (int j = k; j < arr.size(); j++) + { + if (found) + { + break; + } + sum = sum + arr[j] - arr[j - k]; + if (sum % 3 == 0) + { + ans = make_pair(j - k + 1, j); + found = true; + } + } + if (!found) + { + ans = make_pair(-1, 0); + } + if (ans.first == -1) + { + cout << "No such element exists" << endl; + } + else + { + for (int i = ans.first; i <= ans.second; i++) + { + cout << arr[i] << " "; + } + cout << endl; + } +} +int main() +{ + vector arr = {84, 23, 45, 12, 56, 82}; + int k = 3; + computeNumberFromSubArray(arr, k); + return 0; +} \ No newline at end of file diff --git a/SET Multiset/palindromicaConcatenation b/SET Multiset/palindromicaConcatenation new file mode 100644 index 0000000..b31a556 Binary files /dev/null and b/SET Multiset/palindromicaConcatenation differ diff --git a/SET Multiset/palindromicaConcatenation.cpp b/SET Multiset/palindromicaConcatenation.cpp new file mode 100644 index 0000000..d891b7d --- /dev/null +++ b/SET Multiset/palindromicaConcatenation.cpp @@ -0,0 +1,67 @@ +//Sub array of size k with palindromic concatination slideing window challange 4 time complexity O(n^2) +#include +#include +#include +#include +using namespace std; +bool isPalindrome(int n) +{ + int temp = n; + int number = 0; + while (temp > 0) + { + number = number * 10 + temp % 10; + temp = temp / 10; + } + if (number == n) + { + return true; + } + else + { + return false; + } +} + +int findPalindromicSubArray(vector arr, int k) +{ + int num = 0; + for (int i = 0; i < k; i++) + { + num = num * 10 + arr[i]; + } + if (isPalindrome(num)) + { + return 0; + } + for (int j = k; j < arr.size(); j++) + { + num = (num % (int)pow(10, k - 1)) * 10 + arr[j]; + if (isPalindrome(num)) + { + return j - k + 1; + } + } + return -1; +} + +int main() +{ + vector arr = {2, 3, 5, 1, 1, 5}; + int k = 4; + int ans = findPalindromicSubArray(arr, k); + if (ans == -1) + { + cout << "Palindromic sub array dos't exist" << endl; + } + else + { + for (int i = ans; i < ans + k; i++) + { + cout << arr[i] << " "; + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/perfectNumberinSubArray b/SET Multiset/perfectNumberinSubArray new file mode 100644 index 0000000..b0737ba Binary files /dev/null and b/SET Multiset/perfectNumberinSubArray differ diff --git a/SET Multiset/perfectNumberinSubArray.cpp b/SET Multiset/perfectNumberinSubArray.cpp new file mode 100644 index 0000000..a60d4eb --- /dev/null +++ b/SET Multiset/perfectNumberinSubArray.cpp @@ -0,0 +1,74 @@ +// Perfect Number in sub array slideing window challange +#include +#include +using namespace std; +bool isNumberPerfect(int n) +{ + int sum = 1; + for (int i = 2; i < sqrt(n); i++) + { + if (n % i == 0) + { + if (i == n / i) + { + sum += i; + } + else + { + sum += i + n / i; + } + } + } + if (sum == n && n != 1) + { + return true; + } + + return false; +} +int maxSum(int arr[], int n, int k) +{ + if (n < k) + { + cout << "Invalid values" << endl; + return -1; + } + int res = 0; + for (int i = 0; i < k; i++) + { + res += arr[i]; + } + int sum = res; + for (int i = 0; i < k; i++) + { + sum += arr[i] - arr[i - k]; + res = max(res, sum); + } + return res; +} + +int maxNumberofPerfects(int arr[], int n, int k) +{ + for (int i = 0; i < n; i++) + { + if (isNumberPerfect(arr[i])) + { + arr[i] = 1; + } + else + { + arr[i] = 0; + } + } + return maxSum(arr, n, k); +} + +int main() +{ + int arr[] = {28, 2, 3, 6, 496, 99, 8128, 24}; + int k = 4; + int n = 8; + cout << "Maiximum number of perfects :" << maxNumberofPerfects(arr, n, k) << endl; + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/set b/SET Multiset/set new file mode 100644 index 0000000..a85493e Binary files /dev/null and b/SET Multiset/set differ diff --git a/SET Multiset/set.cpp b/SET Multiset/set.cpp new file mode 100644 index 0000000..792f0fb --- /dev/null +++ b/SET Multiset/set.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; +int main() +{ + // set s; + set> s; + s.insert(1); + s.insert(2); + s.insert(3); + s.insert(4); + // for (auto i : s) + // { + // cout << i << " "; + // } + // cout << "\n"; + //second method of printing in set + // for (auto i = s.begin(); i != s.end(); i++) + // { + // cout << *i << " \n"; + // } + // reverse + // for (auto i = s.rbegin(); i != s.rend(); i++) + // { + // cout << *i << " "; + // } + // cout << endl; + // size + // cout << s.size() << "\n"; + + //uppper and lower bound + + // cout << s.lower_bound(2) << endl; + // cout << s.lower_bound(3) << endl; + // cout << s.upper_bound(4) << endl; + // cout << s.upper_bound(4) == s.end() << " \n"; + //erase will erase something what you want + s.erase(1); + + for (auto i : s) + { + cout << i << " "; + } + cout << "\n"; + + return 0; +} \ No newline at end of file diff --git a/SET Multiset/tempCodeRunnerFile.cpp b/SET Multiset/tempCodeRunnerFile.cpp new file mode 100644 index 0000000..36c2af4 --- /dev/null +++ b/SET Multiset/tempCodeRunnerFile.cpp @@ -0,0 +1 @@ + s.ghar se bahar hai kon return 0; \ No newline at end of file diff --git a/SET Multiset/unorderset b/SET Multiset/unorderset new file mode 100644 index 0000000..28c2aa1 Binary files /dev/null and b/SET Multiset/unorderset differ diff --git a/SET Multiset/unorderset.cpp b/SET Multiset/unorderset.cpp new file mode 100644 index 0000000..f1276cc --- /dev/null +++ b/SET Multiset/unorderset.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +using namespace std; +int main() + +{ + unordered_set s; + s.insert(1); + s.insert(2); + s.insert(3); + s.insert(3); + s.insert(4); + s.erase(s.begin()); + for (auto i : s) + { + cout << i << endl; + } + + return 0; +} \ No newline at end of file