Day 11 - Java

Problem Statement: You are given two arrays of integers, arr1 and arr2. Your task is to merge these arrays into a single sorted array. However, there are two conditions for sorting:

The elements of the first array (arr1) should be sorted in descending order.

The elements of the second array (arr2) should be sorted in ascending order.

Write a Java program that accomplishes the following:

Read two integers n1 and n2 from the input, denoting the sizes of arr1 and arr2 respectively.

Read n1 space-separated integers representing the elements of arr1.

Read n2 space-separated integers representing the elements of arr2.

Merge arr1 and arr2 into a single sorted array adhering to the given conditions.

Print the merged array elements separated by space.

Your task is to complete the code by implementing the mergeAndSortArrays method. You should use lambda expressions and the Java Stream API to perform the sorting.

Input Format:

The first line contains two space-separated integers n1 and n2 (1 ≤ n1, n2 ≤ 10^5).

The second line contains n1 space-separated integers representing the elements of arr1.

The third line contains n2 space-separated integers representing the elements of arr2.

Output Format:

Print a single line containing the space-separated integers of the merged array.

Sample Input:

3 4

9 7 18

6 3 8 2024

Sample Output:

18 9 7 3 6 8 2024


SOLUTION:

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import java.util.Scanner;

import java.util.stream.Collectors;


public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String[] n = sc.nextLine().split(" ");

        int n1 = Integer.parseInt(n[0]);

        int n2 = Integer.parseInt(n[1]);


        int[] arr1 = Arrays.stream(sc.nextLine().split("\\s+"))

                           .mapToInt(Integer::parseInt)

                           .toArray();

        int[] arr2 = Arrays.stream(sc.nextLine().split("\\s+"))

                           .mapToInt(Integer::parseInt)

                           .toArray();

        

        List<Integer> res = mergeAndSortArrays(arr1, arr2);

        for (Integer i: res) {

          System.out.print(i+ " ");

          }

    }


    public static List<Integer> mergeAndSortArrays(int[] arr1, int[] arr2) {

        List<Integer> a1 = Arrays.stream(arr1)

                                         .boxed()

                                         .sorted(Comparator.reverseOrder())

                                         .collect(Collectors.toList());

        

        List<Integer> a2 = Arrays.stream(arr2)

                                         .boxed()

                                         .sorted()

                                         .collect(Collectors.toList());

        

        List<Integer> res = new ArrayList<>(a1);

        res.addAll(a2);

        return res;

    }

}


Insights:

  • The input elements are stored as strings and then converted to integer arrays (arr1 and arr2) using Java streams.
  • The mergeAndSortArrays method sorts the arrays using Java streams. It converts the primitive integer arrays to List<Integer> using streams' boxed() method.
  • For arr1, it sorts in descending order using sorted(Comparator.reverseOrder()). For arr2, it sorts in ascending order using the default sorting order.
  • After sorting both arrays, the method merges them by adding all elements of a2 (sorted in ascending order) to the end of a1 (sorted in descending order).
  • The code demonstrates chaining of stream operations, making it concise and readable.
  • The code utilizes Java 8 features such as streams, lambda expressions, and method references to achieve the desired functionality in a concise manner.
Sort and Enjoy with Arrays and Lists!! 
Explore more about Streams and Lambda!
Enjoy Coding!! :)

Comments