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

Container with Most water #787

Open
wants to merge 2 commits 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
44 changes: 44 additions & 0 deletions C++/Chocolate Distribution Problem
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//Chocolaate Distribution Problem
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution{
public:
long long findMinDiff(vector<long long> a, long long n, long long m){
int mini=INT_MAX;
sort(a.begin(),a.end());
for (int i = 0; i + m - 1 < n; i++) {
int diff = a[i + m - 1] - a[i];
if (diff < mini)
mini = diff;
}
return mini;
}


};

// { Driver Code Starts.
int main() {
long long t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
vector<long long> a;
long long x;
for(long long i=0;i<n;i++)
{
cin>>x;
a.push_back(x);
}

long long m;
cin>>m;
Solution ob;
cout<<ob.findMinDiff(a,n,m)<<endl;
}
return 0;
} // } Driver Code Ends
49 changes: 49 additions & 0 deletions C++/Container with most water
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//Initial template for C++

#include<iostream>
using namespace std;
int maxArea(int A[], int len);

// } Driver Code Ends
//User function template for C++

long long maxArea(long long A[], int len)
{
int l = 0;
int r = len -1;
int area = 0;

while (l < r)
{
// Calculating the max area
int mini=min(A[l], A[r]);
area = max(area, (mini * (r - l)));

if (A[l] < A[r])
l += 1;

else
r -= 1;
}
return area;
}

// { Driver Code Starts.

// Driver code
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long long arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<maxArea(arr,n)<<endl;
}
return 0;
}
// } Driver Code Ends