An array is given and below method contains logic for solve method. This logic takes more time to execute. So try to minimize

ans =0; int n=a.length; for(int i=n;i>=1;i--){ int temp=0; for(int j=1;i>=n;j++){ for(int k=n;k>=1;k--){ if(k>=i&&i>=j&&a[k-1]<a[i-1]) temp++; } } ans+=temp/(i); }

7/22/2019 3:40:26 AM

praveen kasojjala

5 Answers

New Answer


praveen kasojjala Sorry then there is no point in trying to solve an incomplete code puzzle.


praveen kasojjala Whenever you have an issue with the logic of your code please try to post a working example. is this code even working? This loop for(int i=n; i<=1; i--) will execute for value of n = 0 or 1 only and once inside, you are practically in for infinite loop because i-- will make the value of i to 0 then negative, then i<=1 will remain true. For any value of n > 1, the loop is not going to execute Assuming you are inside the loop, then the exit condition of inner for loop for(int j=1; i>=n; j++) is controlled by the variable of outer for loop and this loop will execute at the most if n = 0 or 1, after that i value will be less than n Third for loop will only execute if n=1, and will execute only once. After that you are in practically infinte loop due to outermost for loop. Figure out properly what you want to do, how you want to control your loops.


~ swim ~ oops sorry . It's typing mistake. Please can you please check once


praveen kasojjala The purpose of code is not clear. what are you trying to achieve? why do you need 3 for loops for 1D array. Wirhout knowing that it's no point trying to optimize it. And if possible share the full code. I can give a shot, but there is no guarantee i'll do better.


Sorry for less info.. What will you do if question itself given like that asked you to optimize it.