# HORSES - Editorial

can anyone tell me whats wrong

#include
using namespace std;
void sort(int p,int n);
int min(int p,int n);
int m=100000010;
int main()
{
int t,i;
//cout<<“Enter the value of t:”;
cin>>t;
while(t)
{
int n;
//cout<<“Enter the value of n:”;
cin>>n;
int a[n],p=a;
for(i=0;i<n;i++)
{
//cout<<“Enter value at index “<<i<<”:”;
cin>>a[i];
}
sort(p,n);
cout<<min(p,n);
t–;
}
}
void sort(int p,int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(
(p+j)<
(p+j+1))
{
temp=
(p+j);
(p+j)=(p+j+1);
(p+j+1)=temp;
}
}
}
}
int min(int p,int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if((
(p+j)-
(p+j+1))<m)
{
m=(
(p+j)-*(p+j+1));
}
}
}
return m;
}

I too thought so? but i got wrong answer, why??

why is taking the difference between second and first element after sorting, wrong? can anyone tell me the test case where that fails?

can anyone please tell me what is wrong in my code:-

#include <stdio.h>

void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int partition(int a[], int start, int end) {
int pivot = a[end];
int i = start-1;
int j;

for(j=start; j<=end-1; j++) {
if(a[j]<=pivot) {
i=i+1;
swap(&a[i], &a[j]);
}
}
swap(&a[i+1], &a[end]);
return i+1;
}

void quicksort(int a[], int start, int end) {
if(start < end) {
int q = partition(a, start, end);
quicksort(a, start, q-1);
quicksort(a, q+1, end);
}
}

int main() {
int T, N ;
scanf("%d" , &T);
while(T–)
{
scanf("%d" , &N);
int i;
int a[N];
for(i = 0 ; i<N ; i++)
{
scanf("%d" , &a[i]);
}

``````    quicksort(a , 0 , N-1);

int min = a[1]-a[0];
for(i = 1 ; i<N-1 ; i++)
{
if(min>a[i+1]-a[i])
{
min = a[i+1]-a[i];
}
}

printf("%d" , min);
}

return 0;
``````

}

should be
i<n

My program gives the correct output to the sample input but gives a WA when I submit it. I used the inbuilt sort function of C++
Here is the link to my code
https://www.codechef.com/viewsolution/36180293

Not on my machine, it doesn’t

``````[simon@simon-laptop][15:28:01]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling snehalmahima-HORSES.cpp
Successful
[simon@simon-laptop][15:28:05]
[~/devel/hackerrank/otherpeoples]>echo "1
5
4 9 1 32 13" | ./a.out
0
``````

The reason is that you are not initialising the `pre` array, so its initial contents are undefined.

Then, in the loop:

``````         for(int i=1;i<n-1;i++)
pre[i-1]=arr[i]-arr[i-1];
``````

you are only filling in the first `n - 2` values of `pre`, leaving its final two values uninitialised.

The `sort(pre,pre+n-1);` sorts the first `n - 1` elements of `pre`, which includes one uninitialised element, leaving the sorted version of `pre` undefined.

2 Likes

I fixed the possible bugs. It works now

for(int i=1;i<=n-1;i++)
pre[i-1]=arr[i]-arr[i-1];

and sort(pre,pre+n-1)

https://www.codechef.com/viewsolution/36181662

Thanks for the suggestions

1 Like

Can someone please explain the proof in a bit more detail.

what is wrong with this code? I am getting Wrong Answer

``````#include <bits/stdc++.h>

using namespace std;

long long arr[5001];
int main() {
int t;
cin >> t;
while(t --) {
int n;
cin >> n;
for (int i = 0; i < n; i ++) {
cin >> arr[i];
}
sort(arr, arr+n);
long long ans = INFINITY;
for(int i = 1; i < n; i ++) {
ans = min(ans, arr[i] - arr[i - 1]);
}
cout << ans;
}
return 0;
}``````

Consider the test input:

``````2
5
4 9 1 32 13
5
4 9 1 32 13
``````
2 Likes

Output is:
3
3
Seems Correct to me.

On my machine, your code gives:

``````33
``````
3 Likes

Ohh “\n” Thanks mate, half an hour for this

2 Likes
`````` #include <iostream>
#include <algorithm>
using namespace std;

int main() {
int testcases;
cin>>testcases;
for(int i=0;i<testcases;i++){
int count;
cin>>count;
int arr[count];
for(int j=0;j<count;j++){
cin>>arr[j];
}
sort(arr,arr+count);
int minimum_dis =1000 ;
for(int j=1;j<count;j++){
int temp =arr[j]-arr[j-1];
if(temp<minimum_dis) minimum_dis =temp;
}
cout<<minimum_dis<<endl;
}
return 0;

}
``````

I cant find an error in this code and I am not able to submit it. Help me…

Can anyone tell what’s wrong in this code?

``````  #include<bits/stdc++.h>
using namespace std;
int main() {
int a;
int b,min=0;
cin>>a;
while(a--){
cin>>b;
int s[b];
for(int i=0;i<b;i++)
cin>>s[i];
sort(s,s+b);
min=s[1]-s[0];
for(int i=1;i<b;i++){
if((s[i+1]-s[i])<min)
min=s[i+1]-s[i];
}
cout<<min;
}
}
``````

Out of bounds error on the line `if((s[i+1]-s[i])<min)`:

``````[simon@simon-laptop][16:37:40]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling kirakami-HORSES.cpp
+ g++ -std=c++14 kirakami-HORSES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][16:37:44]
[~/devel/hackerrank/otherpeoples]>echo "1
> 5
> 4 9 1 32 13" | ./a.out
kirakami-HORSES.cpp:15:22: runtime error: index 5 out of bounds for type 'int [*]'
``````

Edit:

Also, consider the test input:

``````2
5
4 9 1 32 13
5
4 9 1 32 13

``````
1 Like

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int t;
scanf("%d",&t);
while(t–){
long long int n,x;
int y=10000000;
scanf("%lld",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%lld",&arr[i]);}

``````    for(int k=0;k<n;k++){
for(int j=k+1;j<n;j++){
x=abs(arr[k]-arr[j]);
if(y>x){
y=x;
}
}
}
``````

printf("%lld\n",y);
}
return 0;
}
why this is a wrong approach ,its give me right ans but when i am submitting it ,it shows that it is wrong .please help me.

``````1