Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int sum = sc.nextInt();
- int [] nums = new int[n];
- for(int i=0; i<n;i++){
- nums[i] =sc.nextInt();
- }
- sc.close();
- /*
- for(int i=0; i<n;i++){
- System.out.println(nums[i]);
- }
- */
- int currentSum = nums[0];
- int max = 0;
- int i= 0;
- int j = i+1;
- while(i<n-1 && j<n)
- {
- currentSum += nums[j];
- j++;
- if(currentSum > sum)
- {
- currentSum -= nums[i];
- i++;
- }
- else{
- max = Math.max(max,currentSum);
- }
- }
- System.out.println("Max Sum: "+ max);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement