Why runtime error?
Your code is running on other ide https://ide.geeksforgeeks.org/Srjy51L3ls
There may some issue with cb ide.
It is running on cb ide too but while submitting the code it is passing test case 1 and giving run error on next two??
You have complicated your code a bit. Throwing run error for large values of n.Either the loop is not terminating or you are trying to access an array element that’s is invalid (most probably this).
Also answer may be large so take mod value of it as mentioned in the question.
Use the algorithm :
1.Find the longest subarray starting from 0 with all elements distinct. Use mathematical formula to calculate all possible subarrays within it(They would definately contain all distinct elements)
2. keep removing from the start and adding infront an element and repeat above step.
Click here for better understanding .
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.