On my machine, your code gives:
33
On my machine, your code gives:
33
Ohh “\n” Thanks mate, half an hour for this
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
// your code goes here
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
#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.
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Edit:
Consider the test input:
1
2
1 1000000000
thanks its work; just increase the value of y .