0

How to solve time complexity and memory issue?

In solving some problems my code is taking more time than given target time. I am unable to solve that issue. Even though code is correct ,there are some changes required to optimise it. But I don't know how to do that. At the end it is incomplete program only. So can any help me what to do for these kind of problems. Give your suggestion to grow further in this programming world. Thank you

20th Jul 2019, 5:14 AM
praveen kasojjala
praveen kasojjala - avatar
6 Antworten
+ 2
Do you use uncounted loop then return to infinite loop? You need to break; them
20th Jul 2019, 5:22 AM
Dwi Novianto Nugroho
Dwi Novianto Nugroho - avatar
+ 1
is your code correct ? because it works only with length=1 and goes to infinity loop for(int i=n;i<=1;i--) { // is correct i>=1 ? for(int j=1;i>=n;j++) { // is correct j>=n instead i>=n (J instead I )? n=1 i=1 n<=1 j=1 i>=n j++ j=2 i>=n j++ ... infinity
23rd Jul 2019, 3:24 PM
zemiak
0
better if we can see the code
20th Jul 2019, 11:40 AM
zemiak
0
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); }
22nd Jul 2019, 3:28 AM
praveen kasojjala
praveen kasojjala - avatar
0
Sorry. Mistake.. in hurry i wrote like that.. can you please check this once
23rd Jul 2019, 3:41 PM
praveen kasojjala
praveen kasojjala - avatar
0
If I correct it with way above, I can create array a[1300] and run it under SL limit 10 sec int[] a=new int[1300]; int n=a.length-1; for (int i=0; i<=n; i++) a[i]=i;
23rd Jul 2019, 4:46 PM
zemiak