Maximum Consecutive Ones III

Maximum consecutive ones III. Find maximum consecutive ones in an array of 0s and 1s, If k flip is allowed.

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 we can flip k zeros.

For example:

Input – {1, 1, 0, 0, 0, 1, 1, 1, 1, 1}, k = 2 (We can flip two zeros)

Output: 7

Explanation:

In this example, If we can flip at most k (which is 2) zero. By flipping the zeros, here we can form following subarrays.

{1, 1, 1, 1} -> From index 0 to 3 (zero is present at index 2 and 3)

One subarray is from index 1 to 3 -> {1, 1, 1}

Another subarray is from index 2 to 3 (As both the element at this index is zero) -> {1, 1}

From index 3 to 9 -> {1, 1, 1, 1, 1, 1, 1}

Out of these, the maximum number of consecutive 1s in an array is 7 {1, 1, 1, 1, 1, 1, 1}.

Find Triplet with Given Sum in an Array

Find triplet with given sum in an array.

Given an array of unsorted integers and a value k. Write a code to determine whether or not there exist three elements in array whose sum is equal to k.

Return true if triplet exist else return false.

For example:

Example 1:

Input: {1, 4, 45, 6, 10, 8} k = 13 Output: true (1, 4, 8)

Example 2:

Input: {2, 7, 4, 0, 9, 5, 1, 3} k = 6
Output: true {(2, 4, 0), (5, 1, 0), (1, 2, 3)}

Reverse Level Order Traversal of a Binary Tree

Given a binary tree, write a code to print its reverse level order traversal.

For example:

The reverse or bottom-up level order traversal of this binary tree is 4, 3, 2, 1, 6, 5, 7.

In this example, first we printed last level then second last level and so on. So, we have to start printing the level from bottom-up.