Quote:
I tried to find the duplicate other way
Your code is not related to the question, we can't guess where is your problem.
Quote:
it shows TLE error
TLE is 'Time Limit Exceeded', it is a message from challenges site, it says that your code takes too long to solve the problem, your algorithm is not enough efficient.
Show real code to get help fixing it.
[Update]
find duplicates in array and find complexity of the task are not the same thing.
This code can be simplified
public class TimeAndSpaceComplexity_DuplicateInArray {
public static int findDuplicate(int[] arr) {
int n = arr.length-2;
int sum = 0;
for(int i : arr){
sum+=i;
}
return (sum - ((n*(n+1))/2));
}
}
The complexity is how many comparisons are done depending on array size.