### Remove nth Node from Last - Leet Code Solution

Problem Statement Given a linked list, remove the n-th node from the end of list…

December 23, 2020

You are given an array of integers with size N, and a number K. Find the maximum sum of continuous subarray of size K.

```
Input: [1, 2, 6, 2, 4, 1], k=3
Output: 12
Explanation: Subarray with maximum sum is [6, 2, 4].
```

You need to keep sum of the subarray of size=k all the time, and keep on iterating.

```
public int findMaxSum(int[] arr, int k) {
int max=0;
for (int i=0; i<=arr.length-k; i++) {
int sum_k = 0;
for (int j=i; j<i+k; j++) {
sum_k += arr[j];
}
max = Math.max(max, sum_k);
}
return max;
}
```

There are two loops

- n-k times != n
- k times

Total of *O(n*k)*

If you observe problem and above solution closely,

- you need to keep continuous array
- size should be K

When you first calculate sum of the first window size of K. You already have the sum of previous window. To get the sum of next window. You need to remove first number of previous window, and add next number. So effectively you are using the sum of previous window sum. You need not to go to calculate whole sum again.

```
1 2 3 4 5
First window = 1 2 3, sum=6
For next window, subtract 1 from sum, and add 4 to remaining sum.
Second window = 2 3 4
i.e. 6 - 1 + 4 = 9
```

In this way, we are effectively moving our window from begining to end and we need to keep the max sum available till each window.

Lets look at the code.

```
public int findMaxSum(int[] arr, int k) {
int max = 0;
int left = 0;
int right = 0;
int window_sum = 0;
while (right < arr.length) {
window_sum += arr[right];
if (right >= k) {
max = Math.max(max, window_sum);
//subtract first number of previous widow
window_sum -= arr[left];
//move left pointer to next number
left ++;
}
}
return max;
}
```

We are keeping two pointers for window left and right. On completing the window size, we subtract first number of last window and keep a tab of the max sum available.

We are iterating over array just once.
Its *O(n)*

Problem Statement Given a linked list, remove the n-th node from the end of list…

Problem Statement Given a signed integer, reverse digits of an integer. Return…

Problem Statement Given an array, rotate the array to the right by k steps…

Here are some tips while giving your coding interviews. 1. Never try to jump to…

Problem Statement Given a string, determine if it is a palindrome, considering…

Its every software engineer’s dream to work with the big FAANG companies…

Introduction Strapi is a backend system provides basic crud operations with…

Introduction I had to create many repositories in an Github organization. I…

Introduction I was trying to download some youtube videos for my kids. As I have…

Introduction In this post, we will explore some useful command line options for…

Introduction In this post, we will see how we can apply a patch to Python and…

Introduction We will introduce a Package Manager for Windows: . In automations…