-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
411 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool isValid(int arr[], int n, int k, int mx) | ||
{ | ||
int stud = 1; | ||
int sum = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
sum += arr[i]; | ||
if (sum > mx) | ||
{ | ||
stud++; | ||
sum = arr[i]; | ||
} | ||
if (stud > k) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int allocateMinNumberOfPage(int arr[], int n, int k) | ||
{ | ||
if (n < k) | ||
return -1; | ||
int start = *max_element(arr, arr + n); | ||
int end = accumulate(arr, arr + n, 0); | ||
int result = -1; | ||
while (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (isValid(arr, n, k, mid)) | ||
{ | ||
result = mid; | ||
end = mid - 1; | ||
} | ||
else | ||
{ | ||
start = mid + 1; | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
//same as finding peak element | ||
int peakElement(int arr[], int n) | ||
{ | ||
int start = 0; | ||
int end = n - 1; | ||
while (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (mid > 0 && mid < n - 1) | ||
{ | ||
if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) | ||
{ | ||
return mid; | ||
} | ||
else if (arr[mid - 1] > arr[mid]) | ||
{ | ||
end = mid - 1; | ||
} | ||
else | ||
{ | ||
start = mid + 1; | ||
} | ||
} | ||
else if (mid == 0) | ||
{ | ||
if (arr[mid + 1] < arr[mid]) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
return mid + 1; | ||
} | ||
} | ||
else if (mid == n - 1) | ||
{ | ||
if (arr[mid] > arr[mid - 1]) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
return mid - 1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int peakElement(int arr[], int n) | ||
{ | ||
int start = 0; | ||
int end = n - 1; | ||
while (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (mid > 0 && mid < n - 1) | ||
{ | ||
if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) | ||
{ | ||
return mid; | ||
} | ||
else if (arr[mid - 1] > arr[mid]) | ||
{ | ||
end = mid - 1; | ||
} | ||
else | ||
{ | ||
start = mid + 1; | ||
} | ||
} | ||
else if (mid == 0) | ||
{ | ||
if (arr[mid + 1] < arr[mid]) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
return mid + 1; | ||
} | ||
} | ||
else if (mid == n - 1) | ||
{ | ||
if (arr[mid] > arr[mid - 1]) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
return mid - 1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int ascBinarySearch(vector<int> &arr, int n, int start, int end, int el) | ||
{ | ||
if (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (arr[mid] == el) | ||
{ | ||
return mid; | ||
} | ||
else if (arr[mid] < el) | ||
{ | ||
return ascBinarySearch(arr, n, mid + 1, end, el); | ||
} | ||
else | ||
{ | ||
return ascBinarySearch(arr, n, start, mid - 1, el); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
int descBinarySearch(vector<int> &arr, int n, int start, int end, int el) | ||
{ | ||
if (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (arr[mid] == el) | ||
{ | ||
return mid; | ||
} | ||
else if (arr[mid] > el) | ||
{ | ||
return descBinarySearch(arr, n, mid + 1, end, el); | ||
} | ||
else | ||
{ | ||
return descBinarySearch(arr, n, start, mid - 1, el); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
//same as finding peak element | ||
int peakElement(vector<int> &arr, int n) | ||
{ | ||
int start = 0; | ||
int end = n - 1; | ||
while (start <= end) | ||
{ | ||
int mid = start + (end - start) / 2; | ||
if (mid > 0 && mid < n - 1) | ||
{ | ||
if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) | ||
{ | ||
return mid; | ||
} | ||
else if (arr[mid - 1] > arr[mid]) | ||
{ | ||
end = mid - 1; | ||
} | ||
else | ||
{ | ||
start = mid + 1; | ||
} | ||
} | ||
else if (mid == 0) | ||
{ | ||
if (arr[mid + 1] < arr[mid]) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
return mid + 1; | ||
} | ||
} | ||
else if (mid == n - 1) | ||
{ | ||
if (arr[mid] > arr[mid - 1]) | ||
{ | ||
return mid; | ||
} | ||
else | ||
{ | ||
return mid - 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int solve(vector<int> &arr, int el) | ||
{ | ||
int n = arr.size(); | ||
int pdx = peakElement(arr, n); | ||
int idx1 = ascBinarySearch(arr, n, 0, pdx - 1, el); | ||
int idx2 = descBinarySearch(arr, n, pdx, n - 1, el); | ||
if (idx1 == -1 && idx2 == -1) | ||
return -1; | ||
else | ||
{ | ||
if (idx1 == -1) | ||
{ | ||
return idx2; | ||
} | ||
if (idx2 == -1) | ||
{ | ||
return idx1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
pair<int, int> index(vector<vector<int>> &arr, int n, int m, int el) | ||
{ | ||
int i = 0; | ||
int j = m - 1; | ||
while ((i >= 0 && i < n) && (j >= 0 && j < m)) | ||
{ | ||
if (arr[i][j] == el) | ||
{ | ||
return {i, j}; | ||
} | ||
else if (arr[i][j] > el) | ||
{ | ||
j--; | ||
} | ||
else | ||
{ | ||
i++; | ||
} | ||
} | ||
return {-1, -1}; | ||
} |
Oops, something went wrong.