Rick is really fond of fruit juices, but he is bored of their traditional flavours. Therefore, he has decided to mix as many of them as possible to obtain something entirely new as a result.
He has N glasses, numbered from 0 to N-1, each containing a different kind of juice. The J-th glass has capacity[J] units of capacity and contains juice[J] units of juice. In each glass there is at least one unit of juice.
Rick want to create a multivitamin mix in one of the glasses. He is going to do it by pouring juice from several other glasses into the chosen one. Each of the used glasses must be empty at the end (all of the juice from each glass has to be poured out).
What is the maximum number of flavours that Rick can mix?
Write a function:
int solution(vector<int> &juice, vector<int> &capacity);
that, given arrays juice and capacity, both of size N, returns the maximum number of flavours that Rick can mix in a single glass.
Examples:
1. Given juice = [10, 2, 1, 1] and capacity = [10, 3, 2, 2], your function should return 2. Rick can pour juice from the 3rd glass into the 2nd one.
2. Given juice = [1, 2, 3, 4] and capacity = [3, 6, 4, 4], your function should return 3. Rick can pour juice from the 0th and 2nd glasses into the 1st one.
3. Given juice = [2, 3] and capacity = [3, 4], your function should return 1. No matter which glass he chooses, Rick cannot pour juice from the other one into it. The maximum number of flavours in the chosen glass is 1.
4. Given juice = [1, 1, 5] and capacity = [6, 5, 8], your function should return 3. Rick can mix all juices in the 2nd glass.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of arrays juice and capacity is an integer within the range [1..1,000,000,000];
- arrays juice and capacity have the same length, equal to N;
- for each J juice[J] ≤ capacity[J].
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
diff = capacity[i]-juice[i];
index = i;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
//cout<<diff;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
diff = capacity[i]-juice[i];
index = i;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if(capacity[i]-juice[i]>nextdiff) && (capacity[i]-)
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if(capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<)
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
{
v.push_back(juice[i]);
}
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-diff[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
func.cpp: In function 'int solution(std::vector<int>&, std::vector<int>&)': func.cpp:22:42: error: invalid types 'long long int[long long int]' for array subscript nextdiff = capacity[i]-diff[i]; ^
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(maxcap<capacity[i])
maxcap = capacity[i];
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
for(i=0;i<n;i++)
{
if(i!=)
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
for(i=0;i<n;i++)
{
if(i!=index)
{
if()
}
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
for(i=0;i<n;i++)
{
if(i!=index)
{
if(juice[i]+juice[index]<=capacity[i])
r=2;
}
}
if(con<con1)
con = con1;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
for(i=0;i<n;i++)
{
if(i!=index)
{
if(juice[i]+juice[index]<=capacity[i])
r=2;
}
}
if(con<con1)
con = con1;
if(con<r)
con=r;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
for(i=0;i<n;i++)
{
if(i!=index)
{
if(juice[i]+juice[index]<=capacity[i])
r=2;
}
}
if(con<con1)
con = con1;
if(con<r)
con=r;
if(con>n)
return n;
return con;
}
// you can use includes, for example:
// #include <algorithm>
#include<bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &juice, vector<int> &capacity) {
// write your code in C++14 (g++ 6.2.0)
long long int n=juice.size(),i,diff=0,index=-1,sum=0,con=1,con1=1,maxcap=-1,empty=0,nextdiff=0,ind1=-1;
vector<long long int>v,v1;
for(i=0;i<n;i++)
{
if(capacity[i]-juice[i]>diff)
{
nextdiff = diff;
ind1 = index;
diff = capacity[i]-juice[i];
index = i;
}
else if((capacity[i]-juice[i]>nextdiff) && (capacity[i]-juice[i]<diff))
{
nextdiff = capacity[i]-juice[i];
ind1= i ;
}
if(capacity[i]>juice[i])
empty++;
sum=sum+juice[i];
if(maxcap<capacity[i])
maxcap = capacity[i];
}
if(empty==0)
return 1;
if(sum<=maxcap)
return n;
sum = 0;
for(i=0;i<n;i++)
{
if(i!=index)
v.push_back(juice[i]);
if(i!=ind1)
v1.push_back(juice[i]);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(sum+v[i]<=diff)
{
sum=sum+v[i];
con++;
if(sum == diff)
break;
}
else if(sum+v[i]>diff)
break;
}
sum=0;
sort(v1.begin(),v1.end());
for(i=0;i<n-1;i++)
{
if(sum+v1[i]<=nextdiff)
{
sum=sum+v1[i];
con1++;
if(sum == nextdiff)
break;
}
else if(sum+v1[i]>diff)
break;
}
long long int r = 1;
for(i=0;i<n;i++)
{
if(i!=index)
{
if(juice[i]+juice[index]<=capacity[i])
r=2;
}
}
if(con<con1)
con = con1;
if(con<r)
con=r;
if(con>n)
return n;
return con;
}
The solution obtained perfect score.
Tests in which the glass with the biggest capacity is not the one that should be picked as the base.
Tests in which the glass with the largest empty space is not the picked as the base.