Find Maximum Subarray Sum (Kadane’s algorithm)

In this tutorial, I am going to discuss a very famous interview problem find maximum subarray sum (Kadane’s algorithm).

Given an integer array of N elements, find the maximum sum contiguous subarray (containing at least one element).

For example –

Example 1 –

Input: { 1, 2, -5, 4, 3, 8 , 5 }

Output: 20

The maximum sum of a subarray in this array is 20.  And the subarray is (4, 3, 8, 5).

Example 2 –

Input: {-2, -1}

Output: -1

Example 3 –

Input: { 1, -3, 2, -5, 7, 6, -1, -4, 11, -23}

Output: 19

The largest sum contiguous subarray  in this array is 19 ( 7, 6, -1, -4, 11).

Recursion vs Iteration – Difference between Recursion and Iteration

Recursion vs Iteration. What’s the difference between recursion and iteration.

Recursion and Iteration both are two different programming approaches. For some problems recursion is best suited and in some other cases iterative way of programming is good.

In programming, repeated set of instructions can be handled either by using recursive or iterative approach in our code. So which approach we choose and why? Let’s talk about the difference between iteration and recursion.

Find Duplicate Elements in an Array – Java Program

Write a java program to find duplicate elements in an array. Given an array of integers, We have to write a code to find all duplicate elements in an array.

For example :

Input : arr = { 2, 5, 3, 1, 8, 7, 5, 3, 2 }

Output: {5, 3, 2}

In this tutorial, I am going to explain three approaches to find duplicate elements in an array. This question is also very importantĀ forĀ technical interviews.