fork download
  1. /* package codechef; // don't place package name! */
  2.  
  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 Codechef
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int t = sc.nextInt();
  15. while(t-- != 0){
  16.  
  17. int N = sc.nextInt();
  18. int k = sc.nextInt();
  19. int[] a = new int[N];
  20. for(int i=0; i<N; i++){
  21.  
  22. a[i] = sc.nextInt();
  23. // System.out.print(a[i] + " ");
  24. }
  25.  
  26. int c = cnt(a,N,k);
  27. System.out.println(c);
  28. }
  29. }
  30. static int cnt(int[] a, int n, int k){
  31.  
  32. int i=0;
  33. int c = 0;
  34. while(i < n){
  35.  
  36. int sum = a[i];
  37. // System.out.println("i inside while loop is " + i);
  38. if(sum > k) {
  39. return -1;
  40. }
  41. else{
  42. if(sum <= k){
  43. c+=1;
  44. }
  45. }
  46. int f = i;
  47. for(int j = f+1; j<n; j++){
  48. // System.out.println(a[j] + " ");
  49. sum += a[j];
  50. // System.out.println("i is " + i);
  51. if(sum <= k){
  52. i = j+1;
  53. }
  54. else{
  55. if(j == n-1){
  56. i = n;
  57. if(a[j] <= k) c+=1;
  58. break;
  59. }
  60. else{
  61. i = j;
  62. break;
  63. }
  64. }
  65. // i = j;
  66. }
  67.  
  68. }
  69. return(c);
  70. }
  71. }
Time limit exceeded #stdin #stdout 5s 36300KB
stdin
1
1 5
1
stdout
Standard output is empty