Maximum Consecutive Ones II

Maximum consecutive ones II. Given an array which only consists of 0s and 1s. Write a code to find the maximum number of consecutive 1s in an array, if you can flip at most one 0.

For example:

Input: {1, 1, 0, 0, 1, 1, 1, 1, 1}

Output: 6

In this example, If we can flip at most one zero then the maximum number of consecutive 1s in an array is 6 {1, 1, 1, 1, 1, 1}.

Maximum Sum Subarray of Size K

Let’s discuss a interesting problem maximum sum subarray of size k

Given an array of positive integers and a positive number K. Write a code to find the maximum sum subarray of size k.

For example:

Let’s first understand what all subarrays we can form whose size is 3.

i) First subarray is {2, 1, 5} and it’s sum is 8.

ii) Second subarray is {1, 5, 1} and it’s sum is 7.

iii) Third subarray is {5, 1,3} and it’s sum is 9.

iv) Fourth subarray is {1, 3, 2} and it’s sum is 6.

Out of all these subarrays of size three, the maximum sum subarray is {5, 1, 3} and it’s sum is 9.

Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

For example –

Example 1:

Input: “abcabcbb” Output: 3

The output string is “abc”, with a length of 3.

Example 2:

Input: “bbbbb” Output: 1

The longest substring in this example is “b”. Its length is 1.

Example 3:

Input: “pwwkew” Output: 3

The answer is “wke”. Its length is 3.