there is an error it is looking for 47 but that would mean the 2 best horses for the dule would be “9999999” and “9999952” but there is no “9999952” in the list. I wrote code to test this. here it is::
li = []
w = 0
x = 0
y = 0
z = 0
run = 0
n = int(input())
for i in range(n):
pi = int(input())
li.append(pi)
w = “FALSE”
x = 0
y = 0
for i in li:
if i > x: x = i
y = y + 1
for i in li:
if i == x - 47: W = “TRUE”
z = n - y
print(“x=”,x,“y=”,y,“z=”,z,“IS THERE A 47 POINT SPREAD?”,w, file=sys.stderr)
print(x)
So either it is not explained properly as in they are not looking for “The difference D between the two closest strengths.” but rather they was them split by 2s and then to locate the closest set. This is either a mistake in the fin target witch is “111” by the way or a miss communication in the directions
AS PER PYTHON3
Asking for help. I pass every testcases except the “Horses in disorder”.
Why isn’t my code working on that single test? I got a difference of 2 instead of 1. Any ideas?
Thank you
const N = parseInt(readline());
let d=10000000;
let p=[]
for (let i = 0; i < N; i++) {
const pi = parseInt(readline());
p[i]=pi;
}
p.sort;
for (let j=0; j<N-1; j++) {
if (Math.abs(p[j+1]-p[j])<d) {
d=Math.abs(p[j+1]-p[j]);
}
}
console.log(d);
I passed test case 1 with this code below in Python. However, I can not pass 2 and 3. Does anyone can tell me what is the problem with the code below?
import sys
import math
n = int(input())
pi_list = [] #empty list for pi
for i in range(n):
pi = int(input())
pi_list.append(pi) #append all pi to pi_list
a = max(pi_list) # find the maximum value of pi in the list
pi_list.remove(a) # remove the maximum one
b = max(pi_list) # find the second maximum value in the list
diff = a - b # maximum - second maximum
print(diff) # the answer should be the smallest difference
Your code finds the difference between the two maximum values. But that’s not your task. You have to find the smallest difference between two values.
Example:
4
2
3
9
20
The smallest difference is between 2 and 3. So the answer is 1 because it’s the smallest difference. But your code prints 11, because it’s the difference between 9 and 20.
Does this answer your question?
Hello,
I solved using C++ but was not able to pass Many Horses Case.
Output of the Many Horses Case was = Process has timed out. This may mean that your solution is not optimized enough to handle some cases.
What am I doing Wrong?
Code :
int main()
{
int N, P[100000], i, d[100000],min ;
cin >> N;
for (i = 0; i < N; i++)
{
cin >> P[i]; cin.ignore();
}
for(i=0;i<N-1;i++)
{
for(int j=0;j<N-1-i;j++)
{
if(P[j]>P[j+1])
{
int temp;
temp=P[j+1];
P[j+1]=P[j];
P[j]=temp;
}
}
}
for(i=0;i<N-1;i++)
{
d[i]=P[i+1]-P[i];
}
for(i=1;i<N-1;i++)
{
if(d[0]>d[i])
{
min=d[i];
}
}
cout<<min<<endl;
}
If I understand your code right, you have implemented your own algorithm to sort P.
That’s not necessary, because C++ has already a sort-algorithm.
You can replace the for-loop for sorting P by this:
sort(P, P+N);
This sorts P and makes your code much faster. Now you don’t have the problem of timeout anymore. You still have a bug in your code, but try to find it on your own. I don’t want to do all the work for you
I hope this answers your question.