You are not logged in. Please login at www.codechef.com to post your questions!

×

[Codeforces]- Help needed in Puzzles(337A) problem.

Here is my code. I have understood how to solve this using tutorial approach, but I want to know why my code is giving a WA and what changes should I make to get it accepted.

YOU GUYS ARE A CHARM. THANKS IN ADVANCE.....

// JUST LIKE ANIMALS !!!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
int n,m;cin>>n>>m;vector<int> v(n+2);
int i,ans=INT_MAX,mini,maxi;
for(i=0;i<m;i++)cin>>v[i];
for(i=0;(m-n?i<m-n:i<1);i++){
    maxi=*max_element(&v[i],&v[i+n]);
    mini=*min_element(&v[i],&v[i+n]);
    ans=min(maxi-mini,ans);
}
cout<<ans;
return 0;
}

The accepted solution after corrections as suggested by @mukul_chandel.

// JUST LIKE ANIMALS !!!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
int n,m;cin>>n>>m;vector<int> v(m+2);
int i,ans=1001,mini,maxi;
for(i=0;i<m;i++)cin>>v[i];
sort(&v[0],&v[m]);
for(i=0;i<=m-n;i++){
    maxi=*max_element(&v[i],&v[i+n]);
    mini=*min_element(&v[i],&v[i+n]);
    ans=min(maxi-mini,ans);
}
cout<<ans;
return 0;
}

asked 22 Oct '15, 16:34

shubham99's gravatar image

2★shubham99
2403932
accept rate: 5%

edited 22 Oct '15, 21:39


  • size of vector should be m.
  • no point of applying this algorithm without sorting. After sorting you can also find min and max without even using max_element() and min_element() which will also give your solution a better time complexity.
  • Here is your corrected solution.
link

answered 22 Oct '15, 20:01

mukul_chandel's gravatar image

2★mukul_chandel
561
accept rate: 42%

1

@mukul_chandel , Thanks for helping me out but your solution is done using the tutorial approach and I wanted to done it using mine. I was just doing a boo-boo which you pointed out. Thanks :) :) I will be adding my accepted solution too . :)

(22 Oct '15, 21:38) shubham992★

include<stdio.h>

void bubbleSort(int numbers[], int array_size) { int i, j, temp;

for (i = (array_size - 1); i >= 0; i--) { for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) { temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; } } } }

int main() { int a,b,x; scanf("%d %d",&a,&b); int ar[b]; for(x=0;x<b;x++) { scanf("%d",&ar[x]); };

 bubbleSort(ar,b);

int min=ar[a-1]-ar[0];
for(x=0;x<b-a+1;x++)
{
   if ((ar[a-1+x]-ar[x])<min) min=(ar[a-1+x]-ar[x]);
};

printf("%d",min);


return 0;

}

My solution for the avove problem in c

link
This answer is marked "community wiki".

answered 22 Oct '16, 20:13

harrypotter0's gravatar image

4★harrypotter0
318110
accept rate: 1%

Here is my solution to this problem.

link

answered 24 Oct '16, 04:35

nishant279's gravatar image

2★nishant279
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,718
×1,912
×1,477
×1,070
×682

question asked: 22 Oct '15, 16:34

question was seen: 3,321 times

last updated: 24 Oct '16, 04:35