Leetcode Problem : Merge Sorted Array

Solved below leet code problem using Java 8 Streams.

https://leetcode.com/problems/merge-sorted-array/

Solution:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        
        int[] nums3 = Arrays.copyOf(nums1,m);
		int[] nums4 = Arrays.copyOf(nums2,n);
     
        Stream<int[]> stream1 =Stream.of(nums3);
		Stream<int[]>  stream2 =Stream.of(nums4);
        
        Stream<int[]>  resultingStream = Stream.concat(stream1, stream2);
     
        int[] nums5 = resultingStream.flatMapToInt(y -> Arrays.stream(y)).sorted().toArray();
        
        for(int i=0;i<nums5.length;i++){
            nums1[i] = nums5[i];
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *