w3resource

Java Array Exercises: Find and print one continuous subarray to sort an entire array

Java Array: Exercise-72 with Solution

Write a Java program to find and print one continuous subarray (from a given array of integers) that if you only sort the said subarray in ascending order then the entire array will be sorted in ascending order.

Example:
Input :
nums1 = {1, 2, 3, 0, 4, 6}
nums2 = { 1, 3, 2, 7, 5, 6, 4, 8}
Output:
Continuous subarray:
1 2 3 0
Continuous subarray:
3 2 7 5 6 4

Sample Solution:

Java Code:

import java.util.Arrays;
public class solution {
public static int[] findUnsortedSubarray(int[] nums) {
			int[] result = new int[3];
            int n = nums.length;
            int start = -1;
            int end = -2;
            int min = nums[n - 1];
            int max = nums[0];
            for (int i = 1; i < n; i++) {
                max = Math.max(max, nums[i]);
                min = Math.min(min, nums[n - 1 - i]);
                if (nums[i] < max) {
                    end = i;
                }
                if (nums[n - 1 - i] > min) {
                    start = n - 1 - i;
                }
            }
           		result[0] = start;
		    result[1] = end;

			return result;
        }
    
  public static void main(String[] args)
  {
    int[] nums1 = {1, 2, 3, 0, 4, 6};
	System.out.printf("\nOriginal array: "+Arrays.toString(nums1));	

	int[] result1 = findUnsortedSubarray(nums1);
	System.out.printf("\nContinuous subarray:\n");
	for(int i=result1[0]; i<=result1[1]; i++){
        System.out.print(nums1[i] +" ");
        }
	
	int[] nums2 = { 1, 3, 2, 7, 5, 6, 4, 8};
	System.out.printf("\n\nOriginal array: "+Arrays.toString(nums2));	
    System.out.printf("\nContinuous subarray:\n"); 
	int[] result2 = findUnsortedSubarray(nums2);
	
	for(int i=result2[0]; i<=result2[1]; i++){
        System.out.print(nums2[i] +" ");
        }
   }  
}

Sample Output:

Original array: [1, 2, 3, 0, 4, 6]
Continuous subarray:
1 2 3 0 

Original array: [1, 3, 2, 7, 5, 6, 4, 8]
Continuous subarray:
3 2 7 5 6 4 

Flowchart:

Flowchart: Find and print one continuous subarray to sort an entire array.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to form the largest number from a given list of non negative integers.
Next: Write a Java program to sort a given array of distinct integers where all its numbers are sorted except two numbers.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH