fork download
  1. /* package whatever; // don't place package name! */
  2. //Largest subarray with sum <=k
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int k = 5;
  13. int p =0;
  14. int arr[] = {1,2,1,3,4,5};
  15. int n = arr.length;
  16. int len=0;
  17. int sum = 0;
  18.  
  19. for(int i =0,j=0;j<n;j++){
  20. sum = sum + arr[j];
  21. while(sum>k) {
  22. sum -= arr[i];
  23. i++;
  24. }
  25. len = j-i+1;
  26. p = Math.max(p,len);
  27. }
  28.  
  29. System.out.println(p);
  30. }
  31. }
Success #stdin #stdout 0.09s 54540KB
stdin
Standard input is empty
stdout
3