PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: Jeevan Jyot Singh
Testers: Utkarsh Gupta, Hriday
Editorialist: Nishank Suresh
DIFFICULTY:
1371
PREREQUISITES:
None
PROBLEM:
You are given two integers X and Y. Find three integers A, B, C in the range [-1000, 1000] whose mean is X and median is Y.
EXPLANATION:
The median is Y, so one of the three numbers should definitely be Y.
The mean is X, which means that \frac{A+B+C}{3} = X, or in other words, A+B+C = 3X.
After noticing this, several constructions are possible.
One solution is to print the three numbers Y, Y, 3X - 2Y. Their sum is clearly 3X, and since there are two occurrences of Y, one of them is guaranteed to be the middle element regardless of the value of 3X-2Y, hence the median is Y.
TIME COMPLEXITY
\mathcal{O}(1) per test case.
CODE:
Editorialist's code (Python)
for _ in range(int(input())):
x, y = map(int, input().split())
print(y, y, 3*x - 2*y)
2 Likes
Solution Failed on following Input
input
1
4 5
output
-295 5 302
Why is it wrong?
2 Likes
I understand this explanation
But in this solution, why are we not bothering the value of A and C
Since we can only tell the median if A, B, and C are printed in sorted order
I know Y will always be in the middle position
But the no’s have to be printed in sorted order to find median
What was the input in task#1?
My initial solution was :
b = y
c = b+1
a = 3x - b - c
it didn’t pass.
Then took:
a = -600
b = y and
c = 3x - a - b
and it passed the testcases.
I think you’ve misunderstood the definition of median a bit.
The median is defined for any set of integers, irrespective of whether they are sorted or not. The median of \{1, 2, 3\}, \{2, 3, 1\}, \{3, 1, 2\} is all 2, because in each of those sets, 2 is the middle number if you write its elements in sorted order.
So, you don’t really need to print the numbers in sorted order.
1 Like
Consider a case like y = 0 and x = 100. Your initial solution computes
b = 0
c = 1
a = 299
The median of this set isn’t 0.
1 Like
https://www.codechef.com/viewsolution/73432127
Here is my solution.
I don’t understand why i get wa. Can anyone please explain what’s wrong in my solution?
1 Like
can u please tell me why I am getting WA
here is my solution
https://www.codechef.com/viewsolution/73393767
Check for mean=2,median=1, your output is 3,1,2 and the median of your output is 2 and not 1
oh ok now i got it thanks
MY SOLUTION
Can anyone please explain at what point does my code go wrong?
https://www.codechef.com/viewsolution/73442869
It is printing correct output for every test case still i am getting WA.
I dont know python but i can take a guess:
Take mean =1, median=2
Your output is [0,2,1] whose median is 1 and not 2
Ill give you a test case
X=0 and y=-100
Your output is [0,-100,100] whose median is 0 and not -100
Take this test case
N=0, m=-100
Your output:[0,-100,100]
the median of your output is 0 and not -100
why isn’t this working???
t=int(input())
for _ in range(t):
x,y=map(int,input().split())
if ((3x)-y)%2==0:
f=t=((3x)-y)/2
else:
f=int(((3x)-y)/2)
t=(3x)-y-f
print(int(f),int(y),int(t))
Some test cases are not being able to pass :
here is the code:
int main() {
int t;
cin>>t;
while(t>0){
int x;
int y;
cin>>x>>y;
if(x<=100&&x>=-100&&y<=100&&y>=-100){
int a,b,c;
if(x==y){
a=x;
b=x;
c=x;;
cout<<a<<" “<<b<<” “<<c<<endl;
}
else if(y<0)
{
b=y;
c=0;
a=3x-b;
cout<<a<<" “<<b<<” "<<c<<endl;
}
else
{
a=0;
b=y;
c=3x-b;
cout<<a<<” “<<b<<” "<<c<<endl;
}
}
t–;
}
return 0;
}
Why this one is wrong can some one explain?
ll t;cin>>t;
while(t--){
ll x,y; cin>>x>>y;
if(y>0){
int a=0, b = y, c=3*x -y;
cout<<a<<" "<<b<<" "<<c<<endl;
}
else if(y<0){
int a=3*x+y, b = y, c=0;
cout<<a<<" "<<b<<" "<<c<<endl;
}
else if(y==0){
int a=3*x-1, b = y, c=3*x - a;
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
return 0;
}