I was solving a question on GeeksforGeeks regarding finding the total number of triangles possible using the three different array elements as sides of the triangle from an unsorted array arr[] of size n. This is the first implementation for the above question. ...
int findNumberOfTriangles(int arr[], int n)
{
// code here
sort(arr,arr+n);
int i,j,k,count=0;
for(i=0;i<n-2;i++)//first side of triangle
{
k=i+2; // third side of triangle , ie. rightmost element initialized
for(j=i+1;j<n;j++) //second side of triangle
{
// search for all elements less than sum of the
// first and second side of the triangle
while(k<n)
{
if(arr[i]+arr[j]>arr[k])
k++;
}
count=count+ k-j-1; //adds no of triangle possible for each pair of sides
}
}
return count;
}
... The above implementation is correct and should have O(N^2) time complexity. For the above code, I am getiing a TLE(Time Limit exceeded) from GeeksforGeeks platform with expected time limit=1.02 sec.
Now compare the following implementation to the above implementation.
int findNumberOfTriangles(int arr[], int n)
{
// code here
sort(arr,arr+n);
int i,j,k,count=0;
for(i=0;i<n-2;i++)//first side of triangle
{
k=i+2; // third side of triangle , ie. rightmost element initialized
for(j=i+1;j<n;j++) //second side of triangle
{
// search for all elements less than sum of the
// first and second side of the triangle
while(k<n && arr[i]+arr[j]>arr[k])
k++;
count=count+ k-j-1; //adds no of triangle possible for each pair of sides
}
}
return count;
}
The implementation differs only in the while loop inside second for loop.I moved the if condition from inside of the while body to the condition declaration part by using the Logical AND operation. The time complexity for this is same as the first implementation :O(N^2).
But , this implementation was accepted by GeeksforGeeks with time taken = (0/1.0).
Can someone tell me why is there so much performance difference between two equivalent pieces of code. Is this due to GeeksforGeeks platform or due to the features of the C++ language. GeeksforGeeks uses g++5.4 compiler
Link : https://practice.geeksforgeeks.org/problems/count-possible-triangles-1587115620/1