Day 37 — Merge Intervals

Day 37 — Merge Intervals

Given a collection of intervals, merge all overlapping intervals.

Out of Free Stories? Here is my Friend Link.

Introduction🛹

Hey Guys, Today is day 37 *of the challenge that I took. Wherein I will be solving every day for 100 days the *programming questions that have been asked in** previous interviews.**

You have a bonus at the end if you keep reading. You can find out the companies that have asked these questions in real interviews.

All these problems are taken from the following e-book. 🎓

This is completely free 🆓 if you have an amazon kindle subscription.

This** e-book contains 100 coding problems** that have been asked in top tech interview questions. It also has a guide to solving *all the problems in **200+ ways. *These problems I assure you** has been asked in previous interviews.

You have to decide whether you want to go** unprepared *for a tech interview or go ahead and **quick search for this guide *to solve the 100 problems.

Begin Your ascent to greatness🚀

Note :_ this e-book only contains the _links __to the__solutions.

Day 37 — Merge Intervals 🏁

AIM🏹

Given a collection of intervals, merge *all *overlapping intervals.

Example🕶

Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].

Code👇

class Solution {
        public int[][] merge(int[][] intervals) {
             if (intervals.length == 0) {
                return new int[0][0];
            }

            Arrays.sort(intervals, (i1, i2) -> i1[0] - i2[0]);
            List<int[]> list = new ArrayList<>();
            list.add(new int[] {intervals[0][0], intervals[0][1]});
            for (int i = 1; i < intervals.length; i++) {
                int[] lastItem = list.get(list.size() - 1);

                if (intervals[i][0] > lastItem[1]) {
                    list.add(new int[] {intervals[i][0], intervals[i][1]});
                } else if (intervals[i][1] > lastItem[1]) {
                    list.remove(list.size()-1);
                    list.add(new int[] {lastItem[0], intervals[i][1]});
                }
            }

            int ans[][]= new int[list.size()][2];

            for(int i=0; i< list.size(); i++)
            {

                int arr[] = list.get(i);
                ans[i][0] = arr[0];
                ans[i][1] = arr[1];

            }
            // int ans[] = new in[2][2];
            return ans;
        }
    }

interview programming coding software-development java

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

3 Best Programming Languages for Java Developers

In this article, I’ll share 3 JVM Programming languages Java programmers can learn and why you should learn them. Being a Polyglot developer is of good quality and often valued highly in interviews.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

How to Prepare for a Coding Interview in 8 Weeks

As of this writing, the market is tough. We’ve been hit hard with a deadly pandemic that left thousands of people unemployed. It’s layoffs everywhere and the companies are being conservative when it comes to hiring.