Segregate 0s and 1s in an Array – Java Code

How to Segregate 0s and 1s in an Array.

Given an array of 0s and 1s in a random order. We have to write a code to segregate 0s on the left side and 1s on the right side of the array.

Basically, we have to sort an array of 0s and 1s.

For example –

Input array   =  [0, 1, 0, 1, 0, 0, 1]
Output array = [0, 0, 0, 0, 1, 1, 1]

In an input array 0s and 1s are in an unsorted order. The output is the sorted order of 0s and 1s.

Java Program to Add Two Matrices

Write a java program to add two matrices.

For example : Given two matrices A and B the sum of these two matrices is A + B.

{1,  5,  1}                   {1,  1,  9}              {2,  6,  10}
{2,  8,  2}      +         {2,  2,  3}    =       {4,  10,  5}
{3,  7,  1}                  {5,  6,  4}              {8,  13,  5}