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

Add file for the problem aggressive cows and element in rotated sorted array… #367

Open
wants to merge 1 commit into
base: main
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
63 changes: 63 additions & 0 deletions OST_contri/cows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Function to check if it is possible to place cows with minimum distance 'mid'
bool canPlaceCows(const vector<int>& stalls, int cows, int mid) {
int count = 1; // Place the first cow in the first stall
int last_position = stalls[0];

for (int i = 1; i < stalls.size(); i++) {
if (stalls[i] - last_position >= mid) {
// Place the next cow
count++;
last_position = stalls[i];
if (count == cows) return true;
}
}
return false;
}

int aggressiveCows(const vector<int>& stalls, int cows) {
int n = stalls.size();

// Sort the stalls to apply binary search
vector<int> sorted_stalls = stalls;
sort(sorted_stalls.begin(), sorted_stalls.end());

// Set the bounds for binary search
int low = 1; // Minimum possible distance
int high = sorted_stalls[n-1] - sorted_stalls[0]; // Maximum possible distance
int result = 0;

while (low <= high) {
int mid = low + (high - low) / 2;

if (canPlaceCows(sorted_stalls, cows, mid)) {
result = mid; // Mid is a valid solution, try for a larger minimum distance
low = mid + 1;
} else {
high = mid - 1; // Mid is too large, try for a smaller minimum distance
}
}

return result;
}

int main() {
int n;
cout<<"Enter the no. of stalls:"<<endl;
cin>>n;
vector<int> stalls (n);
int cows;
cout<<"Enter the no. of cows :"<<endl;
cin>>cows;
cout<<"Enter the positions of the cows:"<<endl;
for(int i=0;i<n;i++)
cin>>stalls[i];
int maxDistance = aggressiveCows(stalls, cows);
cout << "The largest minimum distance is: " << maxDistance << endl;

return 0;
}
Binary file added OST_contri/cows.exe
Binary file not shown.
59 changes: 59 additions & 0 deletions OST_contri/ele_rotated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
using namespace std;

// Function to find the index of target in rotated sorted array
int searchInRotatedArray(const vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

// If target is found, return the index
if (nums[mid] == target)
return mid;

// Determine which part is sorted
if (nums[left] <= nums[mid]) { // Left side is sorted
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1; // Search in the left half
} else {
left = mid + 1; // Search in the right half
}
} else { // Right side is sorted
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
}

// If not found, return -1
return -1;
}

int main() {
int n, target;
cout << "Enter the number of elements in the array: ";
cin >> n;
vector<int> nums(n);

cout << "Enter the elements of the rotated sorted array: ";
for (int i = 0; i < n; i++) {
cin >> nums[i];
}

cout << "Enter the target element to search: ";
cin >> target;

int result = searchInRotatedArray(nums, target);

if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

return 0;
}
Binary file added OST_contri/ele_rotated.exe
Binary file not shown.