Move All Zeroes to End of Array

Given an array of integers, write a code to move all zeroes to end of array. The order of all other elements of an array should be same.

Now, In this question we have to write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

NOTE : We have to solve this problem inplace without making a copy of the array.

For example –

Example 1:

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

Output: {1, 2, 4, 5, 3, 8, 0, 0}

In this example, All zeros are moved to the end of array and the relative order of non-zero elements is also maintained.

Example 2:

Input : {1, 0, 2, 3, 0, 0, 0, 2}

Output : {1, 2, 3, 2, 0, 0, 0, 0}

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).

Find Common Elements in Two Arrays – Intersection of two Arrays

Write a c program to find common elements in two arrays.

Given two positive integer arrays arr1 and arr2 of lengths len1 and len2. We have to write a c code to find intersection of these two arrays. Both the arrays are sorted.

Let’s assume we have following two sorted arrays arr1 and arr2.

int arr1[] = {2,  3,  4,  5,  6};
int arr2[] = {4,  6,  7,  8,  9};

So the common elements in these two arrays is 4 and 6.

Find Common Elements in Two Arrays