I have the same problem
Same here. I think @ankur_10 was just trying to make point with example. There will be cases where even after changing values in B it will hold valid status.
X can do =<0 in your solution, but the problem say it cannot
Thanks for the reply but
Sorry, I couldn’t understand what are you saying.
if x can do negative numbers then the test case which is given in the problem itself is wrong
3
4
1 4 3 8
15 8 11
2
4 8
10
2
2 4
3
the last one output will be -1 because it is minimum according to you
Please correct me if I am wrong
Yes please some one tell what is the problem in this approach or tell what is the test case 2 for which it is showing WA so i can figure out my mistake.
This is making me crazy, if anyone can help then please.
1 2 5 7 13
6 7 10 12
you can try this test case where X must be 5
but the algorithm gives 2.
I get it. yes for this test case my approach is giving the wrong answer but can u explain why? Continuing the discussion from My solution
Please tell me why my code is giving the wrong output.
I have used the algorithm that - if we have taken only n-1 elements from A and left kth element of A (Ak) and added X to each then -
Sum of elements (B) = (Sum of elements (A) - Ak) + X * (n-1)
using namespace std;
int main() {
long long int T,N,temp,sumB=0,sumA=0;
cin>>T;
while(T--){
sumA=0;sumB=0;
cin>>N;
long long int A[N],B[N-1];
for(long long int i=0;i<N;i++){
cin>>A[i];
sumA+=A[i];
}
for(long long int i=0;i<N-1;i++){
cin>>B[i];
sumB+=B[i];
}
long long int X=sumB;
for(long long int i=0;i<N;i++){
temp = sumA-A[i];
temp = sumB-temp;
if(temp%(N-1)==0 and temp/(N-1)>0){
if(X>temp/(N-1))X=temp/(N-1);
// break;
}
}
cout<<X<<endl;
}
return 0;
}
In the problem it was mentioned In case there are multiple possible values of X, print the smallest of them.
Even i did the same approach, but sadly someone posted the same approach as the editorial on youtube and it had more than 1k views during the contest.
Yes the aim is to minimize the value of X but the minimized value should also satisfy the conditions given in question.
In the above example if you get X=2 then how will you create the second array using the first array & adding X to it’s elements.
May anyone HELP me with this Logic
int n;
cin >> n;
int arr1[n], arr2[n - 1];
for (int i = 0; i < n; i++) {
cin >> arr1[i];
}
for (int i = 0; i < n - 1; i++) {
cin >> arr2[i];
}
int max1 = *max_element(arr1, arr1 + n);
int max2 = *max_element(arr2, arr2 + (n - 1));
int maxdiff = max2 - max1;
sort (arr1, arr1 + n);
sort (arr2, arr2 + (n - 1));
if (n >= 3) {
if ((arr1[0] + maxdiff) == arr2[0] || (arr1[1] + maxdiff) == arr2[0]) {
cout << maxdiff << "\n";
}
else {
int res = arr2[n - 2] - arr1[n - 1];
cout << res << "\n";
}
}
else if (n < 3) {
if (maxdiff <= 0) {
cout << arr2[0] - arr1[0] << "\n";
}
else {
cout << maxdiff << "\n";
}
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl “\n”
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
ll a[n],b[n-1];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n-1;i++)
{
cin>>b[i];
}
sort(a,a+n);
sort(b,b+n-1);
if(n==2)
{
if(b[0]-a[1]<0)
{
cout<<(b[0]-a[0])<<endl;
}
else if(b[0]-a[0]<0)
{
cout<<(b[0]-a[1])<<endl;
}
else
cout<<min((b[0]-a[0]),(b[0]-a[1]))<<endl;
}
else if(b[0]-a[0]==(b[1]-a[1]))
{
cout<<b[0]-a[0]<<endl;
}
else if(b[n-2]-a[n-1]==(b[n-3]-a[n-2]))
{
cout<<b[n-2]-a[n-1]<<endl;
}
else
{
cout<<(b[0]-a[0])<<endl;
}
}
}
can someone explain which test case is going wrong
same doubt
Consider this test case
1 3 5 7 9
5 7 9 11
Here both 2 and 4 are possible values of x. So the output should be the minimum of these two, i.e., 2 but your program is printing 4.
hey i solved problem using same algorithm but checked if bi = ai+x or not. i got AC. code but yeah this is not optimal obviously
To anyone wondering on which test case the sum approach fails
1 3 4
3 6
ans from algo : 1
correct ans : 2
1 3 4
3 6
ans from algo: 1
correct ans : 2