Here is the question_link
Here is my solution.
please guide me why my solution giving runtime error?
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | Radewoosh | 3415 |
| 8 | Um_nik | 3376 |
| 9 | maroonrk | 3361 |
| 10 | XVIII | 3345 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 162 |
| 2 | adamant | 148 |
| 3 | Um_nik | 145 |
| 4 | Dominater069 | 143 |
| 5 | errorgorn | 141 |
| 6 | cry | 138 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 10 | soullless | 132 |
Here is the question_link
Here is my solution.
please guide me why my solution giving runtime error?
| Name |
|---|



Interesting. Running gdb with a random input of n=10k
The issue is in your comparator. Removing it cures the program of the SIGSEGV. What also works is changing
a1[1] > a2[1]toa1[1] >= a[2]. This stackoverflow answer explains the reason.Edited to Add:
https://en.cppreference.com/w/cpp/algorithm/sort mentions less than for comparison function. You need a strict partial order.
You've done it wrong:)
There must be that classic link.
mach_vm_map(size=1152921513196781568) failed (error code=3) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_allocWhen i changed some parts of your code (not drastically) I've got this exception. To simplify it means that you ran out of memory at some stage. By doing some further research i came up to a conclusion that an error pops up at sorting with comparator stage.
Its a simple memory leak. You have to change your comparator and it'll most likely solve the problem. The above links will guide you.