Categories: Uncategorised

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];
        }
    }
}
mahendravarman.m@gmail.com

Recent Posts

Spring Webflux Functional Endpoint – File Upload

In this blog using the Spring WebFlux module, we are going to leverage the functional…

9 months ago

Serverless Functions with Spring Cloud Function, AWS Lambda

Spring Cloud Function is a project within the Spring ecosystem that allows developers to build…

9 months ago

Spring Boot + RabbitMQ – Decoupling Microservices Communication

RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…

9 months ago

Spring Integration – Sending files over SFTP

Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…

9 months ago

Spring Cloud Config Client

The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…

10 months ago

Handling CSV in Python

In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…

10 months ago