break limitation

Question link:APPROX2 Problem - CodeChef
To reduce time complexity,i have done like this:CodeChef: Practical coding for everyone 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!!!