×

# QUALPREL : Not understanding the reason for TLE,.

 0 Why am I getting TLE when complexity is still O(nlogn) as mentioned in Editorial Solution and I used binary search in increasingly sorted array which is O(logn) instead of traversing elements in O(n) #include using namespace std; int main() { ios_base::sync_with_stdio(false); int tests; cin >> tests; while(tests--){ int n, k; cin >> n >> k; int arr[n]; for(int i=0; i> arr[i]; } sort(arr, arr+n); int x = arr[n-(k-1)-1]; int low = 0, high = n-1, mid = (low+high)/2; while(low<=mid){ if(arr[mid]

 0 Try out the test case 1 6 6 1 2 3 3 3 4  In this test case your code will stuck in infinite loop because here according to your code x will be 1 low will be 0 and high will be 5 and mid will be 2 in first pass So arr [2] will be 3 which is greater than x so high becomes 1 and mid becomes 0 Now arr [0] will be 1 which is not less than x so high becomes -1 and mid becomes 0 here again arr [0] is not less than x so high becomes -1 and mmid becomes 0 and this will run forever like this and you won't get your answer that's why it's giving TLE answered 27 Nov '18, 11:18 5★admin5 226●9 accept rate: 18%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×1,688
×801
×721
×102
×29

question asked: 27 Nov '18, 00:02

question was seen: 94 times

last updated: 27 Nov '18, 11:22