703840. Rearrange the array
Easy
Rearrange the array
Slug: rearrange-the-array5802
Difficulty: Easy
Id: 703840
Topic Tags: Arrays, Data Structures
Company Tags: None
Summary
Given an array of integers, rearrange it such that all elements are in ascending order. This problem is related to arrays and data structures.
Detailed Explanation
To solve this problem, we will first sort the given array in ascending order. We can use a simple sorting algorithm like bubble sort or insertion sort for this purpose. Once the array is sorted, we can print it as the output.
Here's a step-by-step breakdown of the solution:
- Read the input array.
- Sort the array in ascending order using a suitable sorting algorithm.
- Print the sorted array.
The time complexity of this solution will be O(n log n) due to the sorting operation, where n is the number of elements in the array. The space complexity will be O(1) as we are not using any extra space that scales with the input size.
Optimized Solutions
Java
import java.util.Arrays;
public class RearrangeArray {
public static void main(String[] args) {
int[] arr = {4, 2, 9, 6, 23, 12, 34, 0, 1};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}