Skip to content

Commit

Permalink
new commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 committed Feb 6, 2024
1 parent b7a4ea3 commit f658f27
Show file tree
Hide file tree
Showing 76 changed files with 4,143 additions and 4,163 deletions.
64 changes: 32 additions & 32 deletions KokoEatingBananas.cpp → Arrays/KokoEatingBananas.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
class solution{
public:
bool solveKoko(vector<int> &piles,int H,int mid){
int hours=0;
for(auto i:piles){
int d=i/mid;
hours+=d;
if(i%mid!=0){
hours++;
}
}
if(hours<=H){
return true;
}
else{
return false;
}
}
int Solve(int N, vector<int>& piles, int H) {
// Code here
int s=1,e=INT_MAX;
while(s<=e){
int mid=s+(e-s)/2;
if(solveKoko(piles,H,mid)){
e=mid-1;
}
else{
s=mid+1;
}
}
return s;
}
class solution{
public:
bool solveKoko(vector<int> &piles,int H,int mid){
int hours=0;
for(auto i:piles){
int d=i/mid;
hours+=d;
if(i%mid!=0){
hours++;
}
}
if(hours<=H){
return true;
}
else{
return false;
}
}
int Solve(int N, vector<int>& piles, int H) {
// Code here
int s=1,e=INT_MAX;
while(s<=e){
int mid=s+(e-s)/2;
if(solveKoko(piles,H,mid)){
e=mid-1;
}
else{
s=mid+1;
}
}
return s;
}
};
28 changes: 14 additions & 14 deletions KthPositiveNumber.cpp → Arrays/KthPositiveNumber.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class Solution {
public:
int findKthPositive(vector<int>& arr, int k) {
int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] - mid - 1 < k) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left + k;
}
class Solution {
public:
int findKthPositive(vector<int>& arr, int k) {
int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] - mid - 1 < k) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left + k;
}
};
62 changes: 31 additions & 31 deletions LinearSearch.cpp → Arrays/LinearSearch.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#include <iostream>
using namespace std;

bool search(int arr[], int size, int key)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == key)
{
return 1;
}
}
return 0;
}
int main()
{

int arr[10] = {10, 2, 245, 7, 56, 56, 542, 3, 12, 45};
// Whether 1 is present or not?
cout << "Enter the key" << endl;
int key;
cin >> key;
bool found = search(arr, 10, key);
if (found)
{
cout << "Key is present" << endl;
}
else
{
cout << "Key is absent" << endl;
}
#include <iostream>
using namespace std;

bool search(int arr[], int size, int key)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == key)
{
return 1;
}
}
return 0;
}
int main()
{

int arr[10] = {10, 2, 245, 7, 56, 56, 542, 3, 12, 45};
// Whether 1 is present or not?
cout << "Enter the key" << endl;
int key;
cin >> key;
bool found = search(arr, 10, key);
if (found)
{
cout << "Key is present" << endl;
}
else
{
cout << "Key is absent" << endl;
}
52 changes: 26 additions & 26 deletions MaximumProductSubarray.cpp → Arrays/MaximumProductSubarray.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
class Solution {
public:
int fun(int mid, vector<int>& nums){
int ans = 0;
for(int i=0;i<nums.size();i++){
ans += (nums[i]/mid) + ((nums[i] % mid) != 0);
}

return ans;
}
int smallestDivisor(vector<int>& nums, int threshold) {
int lo = 1, hi = 1e9, ans = 0;

while(lo <= hi){
int mid = (lo + hi)/2;
if(fun(mid, nums) <= threshold){
ans = mid;
hi = mid - 1;
}
else{
lo = mid + 1;
}
}

return ans;
}
class Solution {
public:
int fun(int mid, vector<int>& nums){
int ans = 0;
for(int i=0;i<nums.size();i++){
ans += (nums[i]/mid) + ((nums[i] % mid) != 0);
}

return ans;
}
int smallestDivisor(vector<int>& nums, int threshold) {
int lo = 1, hi = 1e9, ans = 0;

while(lo <= hi){
int mid = (lo + hi)/2;
if(fun(mid, nums) <= threshold){
ans = mid;
hi = mid - 1;
}
else{
lo = mid + 1;
}
}

return ans;
}
};
100 changes: 50 additions & 50 deletions MedianOf2SortedArray.java → Arrays/MedianOf2SortedArray.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
class GFG

{

static double medianOfArrays(int n, int m, int a[], int b[])

{

int c[] = new int[m +n];

int s=0;

for(int i=0;i<n;i++) {

c[s] = a[i];

s++;

}

for(int i=0;i<m;i++) {

c[s] = b[i];

s++;

}

Arrays.sort(c);

if((m+n)%2==0) {

int d = (m+n)/2;

int e =((m+n)-1)/2;

double j = ((double)c[d] + c[e])/2;

return j;

}

else

return c[(int )((m+n)-1)/2];



}

class GFG

{

static double medianOfArrays(int n, int m, int a[], int b[])

{

int c[] = new int[m +n];

int s=0;

for(int i=0;i<n;i++) {

c[s] = a[i];

s++;

}

for(int i=0;i<m;i++) {

c[s] = b[i];

s++;

}

Arrays.sort(c);

if((m+n)%2==0) {

int d = (m+n)/2;

int e =((m+n)-1)/2;

double j = ((double)c[d] + c[e])/2;

return j;

}

else

return c[(int )((m+n)-1)/2];



}

}
46 changes: 23 additions & 23 deletions MedianOf2SortedArrays.cpp → Arrays/MedianOf2SortedArrays.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
vector<int>ans;
for(int i=0; i<nums1.size(); i++){
ans.push_back(nums1[i]);
}
for(int i=0; i<nums2.size(); i++){
ans.push_back(nums2[i]);
}
if(ans.size()==1)return ans[0];
int n=ans.size();
sort(ans.begin(), ans.end());

if(n%2!=0){
return ans[(n)/2];
}
else{

return double(ans[(n/2)-1]+ans[(n/2)])/2;
}

}
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
vector<int>ans;
for(int i=0; i<nums1.size(); i++){
ans.push_back(nums1[i]);
}
for(int i=0; i<nums2.size(); i++){
ans.push_back(nums2[i]);
}
if(ans.size()==1)return ans[0];
int n=ans.size();
sort(ans.begin(), ans.end());

if(n%2!=0){
return ans[(n)/2];
}
else{

return double(ans[(n/2)-1]+ans[(n/2)])/2;
}

}
};
Loading

0 comments on commit f658f27

Please sign in to comment.