Java has a built-in sort function for integers, just use that.
Small challenge: solve it without sorting!
Hi everyone!! Iâm a beginner programmer and curently Iâm stuck in my solution. Itâs writtent in C#. I have this error
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Solution.Main on line 29
Can anyone can help me why it shows up:(
class Solution
{
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
int[] horses = new int[N];
for (int i = 0; i < N; i++)
{
horses[i] = int.Parse(Console.ReadLine());
}
Array.Sort(horses);
int minDifference = horses[1] - horses[0];
int currentDifference =0;
for ( int i = 0; i< horses.Length; i++)
{
currentDifference = horses[i+1] - horses[i];
if (minDifference > currentDifference)
{
minDifference = currentDifference;
}
}
// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");
Console.WriteLine(minDifference);
}
}
Hi
for ( int i = 0; i< horses.Length; i++)
horses[i+1]
i+1 is outside for the last interation of the loop.
First, when Iâve submitted my code, they say that my algorithm fails with tie horses.
Ok, so Iâve managed to correct it and Iâve entered tie horses in the personalized test and failed.
Then Iâve submitted the algorithm anyway and the tie horses case is success.
Anyway I just want to wish good luck to those who havenât managed to resolve the puzzle yet.
A little advice : Itâs not because simplest solution isnât the best that youâll have to search way too far.
can you tell me how to do it please? iâm a newbie of java and i canât pass this
Hello everyone!
Iâm trying to resolve this puzzle in C++, but I have some problems.
In summary, the output of my code doesnât match with the output of the test case.
I think that the following image summarizes a little bit better the problem:
Thanks in advance!
Use cerr to display debug ouput like âThe difference betweenâŠâ. Use cout only for the expected answer (1)
I resolved
That was the problem
Now, I have to understand the fails in the validators panel !
Hi, I have found a little issue, I hope i am mistaking , when Iâve printed the numbers for input I have found out that the first pw data is missing. This issue is in JAVA. I have manage to pass 91% of validation.
Example of error data
input: 3 5 8 9
output: 5 8 9
code used:
int N = in.nextInt();
for (int i = 0; i < N; i++) {
int pi = in.nextInt();
System.err.println(pi);
}
Hi,
I have realized the bash code thanks for the previous replies which have helped me.
To help you to do it, here are some reminders and some tricks :
The best way to assign a value is ((diff=$x- $y)) instead of diff=expr $x - $y
To get an absolute value, you just have to do it : abs=${diff#-} instead of any if instruction.
Good luck.
Well, just got a bash achievement, took me a while.
The trick is, that if you extract (i+1)th element from array before (i)th element, it somehow takes extremely more time, so the program needs more time to work than given time limit.
Hello,
Iâm coding using python, and Iâm stuck at 90%, the test number 6 - horses in disorder - does not pass, while the same test in the IDE passes.
I read the hint but I donât know what to perform to optimize the algorithm. When I try to add some test the code become to slow with a lot of horses. My code is after the post
Any help appreciated to help me perform the 100%.
Thanks
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
numbers = []
difference = None
temp = int(raw_input())
if 1 < temp < 100000:
n = temp
for i in xrange(n):
pi = int(raw_input())
if 0 < pi <= 10000000:
numbers.append(pi)
del(n)
# Write an action using print
# To debug: print >> sys.stderr, "Debug messages..."
numbers.sort()
for i,n in enumerate(numbers):
difference_current = abs(numbers[i] - numbers[i-1])
if not difference or (difference_current < difference):
difference = difference_current
print difference
Bonjour tout le monde !
Je suis vraiment embĂȘtĂ© avec le test de âChevaux dans le dĂ©sordreâ aussi. ça passe parfaitement cotĂ© IDE mais le validateur me rejette et comme il nâest pas verbeux je ne sais pas si le problĂšme vient du fait que les chevaux ne sont pas triĂ©s ou que le temps de traitement est trop long (sachant que je passe les tests âNombreux chevauxâ).
Jâai testĂ© avec la fonction sort (en JS), tri fusion, tri rapide, tri par insertion Ă lâinitialisation du tableau, mais rien nây fait je suis toujours bloquĂ© sur ce test cotĂ© validation (il passe toujours cotĂ© IDE)
En vous remerciant !
[quote="baptistemm, post:235, topic:38"]
for i,n in enumerate(numbers):
difference_current = abs(numbers[i] - numbers[i-1])
[/quote]
what will be your first i? what do you hold in numbers[first i - 1]?
Bonjour,
JâĂ©choue au deuxiĂšme test de validation (plus petite dirffĂ©rence sur les deux premiers chevaux), et je nâarrive pas Ă voir pourquoi.
Est-ce quâil y a un moyen de voir les input output de ces tests?
Merci
Est-ce quâil y a un moyen de voir les input output de ces tests?
si tu veux voir du debug, le plus simple comme dĂ©taillĂ© dans le code câest dâĂ©crire sur la stderr.
Hello bvs23bkv33
actually I surrounded the instruction with a condition if i>0:
, I donât know why it disappeared. However even when I had back the condition the test #6 still fails.
Dear fellow Coders!
I really need your help with this puzzle. Logically, my code would work perfectly for all test cases, but for some reason it always prnts out a negative value!
My code is here.
By the way, I am using Python 3
I would really appreciate for any ideas on why my code is wrong