APPROX2: HOW TO GET 100 MARKS IN MY CODE ?

This is my code. This got AC but with 31 marks. Please help me by telling how can I get the remaining 69 marks too with this code. What additions should I do ?I have provided the code here or you can check my submitted solution here - CodeChef: Practical coding for everyone

#include<bits/stdc++.h>
#define fr(a,n) for(long long i=a;i<n;i++)
using namespace std;

int main(){
ios::sync_with_stdio(0);
int t;long long int c,n,k,j,min,count;
cin>>t;
while(t--){
    count = 0;
	cin>>n>>k;
	vector<long long int> a(2*(n+5));
	fr(0,n)
	cin>>a[i];
    min=abs(a[0]+a[1]-k);
	fr(0,n-1){
		for(j=i+1;j<n;j++){
			c=abs(a[i]+a[j]-k);
			if(min>=c){
				min=c;
				count++;
			}	
		}
	}
	cout<<min<<" "<<count<<endl;
}
return 0;
}

You need to reset the count whenever a new minimum is encountered. Break your if condition into two (see the linked solution). And btw, why declare a vector of size 2*(n+5)? This is your code with AC: CodeChef: Practical coding for everyone

1 Like

You need to update the count to 0 as soon as you get a new minimum because you need to output the number of pairs that can produce the minimum.You can see your corrected code here

2 Likes

@shubham99

  • if(min>=c){
    min=c;
    count++;
    }
    *this is simply wrong…first check min than calculate how many pair equal to this min…hope u get it…happy coding
1 Like

I was trying something with that sized vector and thank you :slight_smile: