break limitation

Question link:http://www.codechef.com/problems/APPROX2
To reduce time complexity,i have done like this:http://www.codechef.com/viewsolution/3942983 but still partially wrong answer:p Can anyone help? what is the problem with sorting?
It seems that there is a problem with break statement :stuck_out_tongue:
replacing break to continue(i.e. bruteforce) everything works fine…

No, there’s no problem with the break statement, You just don’t have to use it. You are coming out of the loop if the new |ai + aj - k| is more than the current min val. Just think a bit, all you need is to remove the “else” statement.

           for(int j=i+1;j<n;++j)
		    {
			   tmp=k-a[i]-a[j];tmp=mod(tmp);
			   if(tmp<min) { min=tmp; c=1;}
			   else if(tmp==min) c++;
			   //else break;  remove this 
		    }
1 Like

“You are coming out of the loop if the new |ai + aj - k| is more than the current min val.”
that is what i wanted to do:there are 2 loops in my code and i am coming out of 1 loop with break statement because after sorting the array there is no need of going further in the 2nd loop if tmp>min because on going further there will always be tmp>min (which is not of our use) because the array is already sorted in ascending order(that is why i have sorted my array)…
please help!!!

//