Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Vipin_Kumar_SET B_Thinkbridge

FUll Name: VIpin Kumar


Mobie no: 9451043980
Email: vk0809853@gmail.com
Set : B
Location : Lucknow (U.P) (willing to relocate)

Problem Statement

Problem 1: Find the longest consecutive sequence in the


given array

Problem Understanding

Here an unsorted array is given where we have to find the


longest consecutive
sequence

Algorithm

-> Using the sort STL which is an in build library we can


first sort them.
-> We will get the ascending ordered array.
-> Using only a for loop will do our.
-> we'll decare an int cnt variable which will count the
maximum number of
continous occuring numbers.

code and pseudo code

->initialise - int cnt=0


-> size of array- int n=arr.size()
-> sorting the array using sort function- sort(nums,nums+n)
-> increment cnt while iterating in for loop-

for(int i=0;i<n-1;i++){
if(arr[i]<arr[i+1] && abs(arr[i+1]-arr[i]==1))
cnt++;
}
-> we will cnt variable which will be having the count of
consecutive integers.

code output

Input -> nums=[200,300,4,5,6]


output -> 3

Time complexity= o(n) // Traversing


Linearly
Space complexity=o(1)
Problem Statement

Problem 2 -> We have to merge the overlapping interval which are


redundant or we
can say they could be taken in one timestamp.

Problem Understanding

-> for example = (1,3) (2,8) (4,9) then we can write it as


(1,9)

Algorithm

-> By looking at it i got instution that we should sort the


intervals
if their start times are same then it will be sorted
according to their
end time. (sort function works like this).

-> we will compare the end time of first index with its
next index's start point
-> if it is greater or equal to the it and also it greater
then or equal to end point
of the its next index then we will take its ending
point and will create an new
updated time stamps.
-> we create an answer array to store new intervals.
-> we are also comparing the end point of next index from
start index's end point
in order to tackle an edge case, for example =
let an intervals are (15,19) and (17,18)
here if we don't comapre the end
point
of next index also then we will an error
-> we will return the ans array or vector.

code and pseudo code

-> Take size of array as int n=intervals.size() and create


an vector of pair
vector<pair<int,int>>ans
-> sort the array
-> Run an for loop till n-1 index in order to compare till
last interval
for(int i=0;i<n-1;i++) // Not using curly
braces intentionally
auto interval=arr[i];
int
start=interval.first
int end = interval.second

-> Here we will compare the end time of index with its next
interval's start and end time.
-> we will inser them the updated
interval in our ans vector as ans.push_back({start,end})
-> After loop's termination we will return our ans vector.

Code Output

-> Input = intervals= (1,5) (2,7) (8,10) (11,15) (12,


16)
-> output = (1,7) (8,10) (11,16)

Time complexity = o(n)


Space complexity = o(n) // using vector for
storing answers.

Problem statement

-> According to the given coordinate and locations of


building we have to create
an output with key-value pairs of vector or array with
soarted starting point.

Problem understanding

We will have to generate a key-value paired list which


is soarted with their start point
and also apply the similar logic that we had applied in
the merge overlapping interval so
that we could assimilate the redundant similar
horizontal lines.
Algo

-> first we will sort the each bulding index according to


their x-coordinate
using an STL comparator for comapring a perticular
coordinate
-> Using the merge interval logic we will merge the similar
the heights intervals
-> Using the merge interval logic with slight change we
could merge redundant data

// due to run out of time i can't able to write whole


pseudo code

Code output

input= (1,2) (5,7) (6,7) (8,9)


output= (1,2) (8,9)

TC= o(nlogn) + o(n) //comparator and


linear search
SC= 0(n)

You might also like