Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Set and Multi Set and Bit Manipulation #612

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added SET Multiset/MaxSumSlideingWindow
Binary file not shown.
38 changes: 38 additions & 0 deletions SET Multiset/MaxSumSlideingWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//SLideing window maximum sum of sub array
#include <bits/stdc++.h>
#include <iostream>
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;
}
Binary file added SET Multiset/allocateminimumPages
Binary file not shown.
70 changes: 70 additions & 0 deletions SET Multiset/allocateminimumPages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//Binary search tree challenge 2
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <climits>
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;
}
Binary file added SET Multiset/binarySearchChhallange1
Binary file not shown.
53 changes: 53 additions & 0 deletions SET Multiset/binarySearchChhallange1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
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;
}
Binary file added SET Multiset/bstChallange3
Binary file not shown.
53 changes: 53 additions & 0 deletions SET Multiset/bstChallange3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Binary search tree challenge 3 Painters partition problem
#include <bits/stdc++.h>
#include <iostream>
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;
}
Binary file added SET Multiset/bstChallange5
Binary file not shown.
31 changes: 31 additions & 0 deletions SET Multiset/bstChallange5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//BST challange 5 find the peak element
#include <bits/stdc++.h>
#include <iostream>
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;
}
Binary file added SET Multiset/bstchallange4
Binary file not shown.
47 changes: 47 additions & 0 deletions SET Multiset/bstchallange4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//bst challange 4 search and rotated array
#include <bits/stdc++.h>
#include <iostream>
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;
}
Binary file added SET Multiset/minimumsubArraySlideingwindow
Binary file not shown.
44 changes: 44 additions & 0 deletions SET Multiset/minimumsubArraySlideingwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// minimum sub array windos slideing window time complexity O(n)
#include <bits/stdc++.h>
#include <iostream>
#include <climits>
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;
}
Binary file added SET Multiset/multiSet
Binary file not shown.
22 changes: 22 additions & 0 deletions SET Multiset/multiSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <bits/stdc++.h>
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> 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;
}
Binary file added SET Multiset/numberformedfromSubArraydivisibleby3
Binary file not shown.
Loading