Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package codechef; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Codechef
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- // your code goes here
- Scanner sc = new Scanner(System.in);
- int t = sc.nextInt();
- while(t-- != 0){
- int N = sc.nextInt();
- int k = sc.nextInt();
- int[] a = new int[N];
- for(int i=0; i<N; i++){
- a[i] = sc.nextInt();
- // System.out.print(a[i] + " ");
- }
- int c = cnt(a,N,k);
- System.out.println(c);
- }
- }
- static int cnt(int[] a, int n, int k){
- int i=0;
- int c = 0;
- while(i < n){
- int sum = a[i];
- // System.out.println("i inside while loop is " + i);
- if(sum > k) {
- return -1;
- }
- else{
- if(sum <= k){
- c+=1;
- }
- }
- int f = i;
- for(int j = f+1; j<n; j++){
- // System.out.println(a[j] + " ");
- sum += a[j];
- // System.out.println("i is " + i);
- if(sum <= k){
- i = j+1;
- }
- else{
- if(j == n-1){
- i = n;
- if(a[j] <= k) c+=1;
- break;
- }
- else{
- i = j;
- break;
- }
- }
- // i = j;
- }
- }
- return(c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement