sir , logic is not so clear and i’m unable to decode the logic , can you please help me how exactly the test case is running , and what exactly we have to do.
The logic is not clear
i have coded ,can you please tell me the error , due to which all test cases are not getting passed.
please reply ma’am as im not able to recognize the mistake i have made
@1999atrijsharma
initalise ur sum with maximum value of array,
ans with e
you can refer to this code
i have used your code still all test cases is not passed, can you recheck please
this is my code , can you please improvise it to get a correct answer as im geeting right answer in test case
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); long result[]=new long[t]; int j=0; for(int i=0;i<t;i++) { long n=sc.nextInt(); long m=sc.nextInt(); long ar[]=new long[1000008]; long sum=0; for(int f=0;f<n;f++) { ar[i]=sc.nextLong(); sum=sum+ar[i]; } long res=min(ar, n, m); result[j]=sum-res; j++; } for(int i=0;i<j;i++) System.out.println(result[i]); } public static long min(long[] arr, long n, long k) { long lo = arr[arr.length - 1]; long totalpages = 0; long hi = 0; for (int i = 0; i < n; i++) { totalpages += arr[i]; } hi = totalpages; long ans = 0; while (lo <= hi) { long mid = (lo + hi) / 2; boolean isvalid = isValid(arr, k, mid); if (isvalid) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static boolean isValid(long[] arr, long k, long mid) { long sum = 0; int noOfstudents = 1; for (int i = 0; i < arr.length; i++) { sum += arr[i]; if (sum > mid) { noOfstudents++; sum = arr[i]; if (noOfstudents > k) { return false; } } } return true; } }
this is my code please correct it
maam i have uploaded the corrected code but still i got 0 as all test case failed in backend can you please check the code that you have provided to me
