java array program to identify sub arrays with element sum equal to Zero

Core Java Array interview questions on the identification of multiple sub arrays within in the Array Input whose array elements sum is Zero.

Program Logic:

– Java Array provided with the list of array elements.
* Traverse the elements to identify if the value of that element is zero.
* Traverse the elements to identify if the value of multiple array elements sums up to Zero
* Print each such occurrence as an sub-array

package core.java.oracleappshelp.array;
public class identifySubArrayInArrayWithSumZero {
	// Function to print all sub-arrays with 0 sum present
		// in the given array
		public static void identifySubArrayToDisplay(int[] ArrayWithAllElements)
		{
			// Traverse for the sub- array through the outerLoop
			for (int outerLoop = 0; outerLoop < ArrayWithAllElements.length; outerLoop++)
			{
				int sum = 0;
				// Traverse for the sub- array through the innerLoop
				for (int innerLoop = outerLoop; innerLoop < ArrayWithAllElements.length; innerLoop++)
				{
					// adding value for innerLoop and outerLoop to get total sum value.
					sum += ArrayWithAllElements[innerLoop];
					// validate if the array index for innerLoop and OuterLoop sums up to 0
					if (sum == 0) {
						System.out.println("Subarray [" + outerLoop + ".." + innerLoop + "]");
					}
				}
			}
		}
			public static void main (String[] args)
		{
			int[] A = { -2, 2, 1, 5, -6,  3, 1, -4,  };
			identifySubArrayToDisplay(A);
		}
}

java array program to sort list in binary of 0s and 1s

The  below java program executes the java method – sortArrayElementsInBinaryFormat( ) to sort the received list into the Binary format of 0s and 1s in sequence.

Program Logic:

1. Pass the Array Element List to the method – sortArrayElementsInBinaryFormat( )

2. Traverse and identify the elements with 0 value

3. Move the elements with 0 value forward and elements with value 1 thereafter

4. Print the sorted binary array

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

Array Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]

/*
 * Java Array Program to sort the received Array Binary Input of 0 and 1
 * identify the elements with 0 value
 * Move the elements with 0 value forward in the Array 
 * elements with 1 value to be placed after all 0s 
 * Input : { 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 }
 * Output: {0,0,0,0,0,1,1,1,1,1,1}
 */
package core.java.oracleappshelp.array;
import java.util.Arrays;
public class GenerateBinaryArray {
/*  Method - sortArrayElementsInBinaryFormat
 *  Java Method to sort the received list into the Binary format of 0 and 1 in sequence.
 */
		public static void sortArrayElementsInBinaryFormat(int[] arrayElements)
		{
			// identify the elements with 0 value
			int elementWithZeroValue = 0;
			for (int elementValue : arrayElements) {
				if (elementValue == 0) {
					elementWithZeroValue++;
				}
			}
			// move the elements with 0 value forward and elements with value 1 thereafter
			int elementLoop = 0;
			while (elementWithZeroValue-- != 0) {
				arrayElements[elementLoop++] = 0;
			}
			// fill all remaining elements by 1
			while (elementLoop < arrayElements.length) {
				arrayElements[elementLoop++] = 1;
			}
		}
		// Sort binary array in linear time
		public static void main (String[] args)
		{
			int[] arrayElementList = { 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0 };
			sortArrayElementsInBinaryFormat(arrayElementList);
			// print the sorted binary array
			System.out.println(Arrays.toString(arrayElementList));
		}
}
Java Array Program Output: 
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]