×

# KRYP6 - Editorial

Author: Shivam Garg
Tester: Shiv Dhingra

EASY

# PREREQUISITES

Basic Knowledge of Sets (STL)

# PROBLEM

Given a list of integers, for each of them find the maximum number less than the given number preceding it.

# EXPLANATION

The brute approach will turn out to be of complexity $O(N ^2)$, and hence is bound to time out.

We can maintain a sorted set of numbers by iterating over the array. In other words, if we have a set of numbers preceding the given number, we can easily find out the largest number smaller than the given number.

We can simply perform binary search in this set to find the number just less than the given number.
This can be done by using lower_bound command of the set in C++, or equivalent in other languages.

set<long long>::iterator it = s.lower_bound(val);
it--;
return (*it);


This will fulfill our task. The complexity of accessing/inserting elements in the set is $O(Log(N))$.
So, the overall complexity turns out to be $O(N \hspace{1 mm} Log(N))$.

# SOLUTION

Setter's Solution - Code

6211210
accept rate: 0%

 0 your explanation is not clear bro(#no offence) if u can then add little more details for the approach thx in advance : ) answered 31 May '18, 00:22 358●6 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,682
×3,766
×1,038
×278
×160
×15

question asked: 13 Feb '18, 23:58

question was seen: 457 times

last updated: 31 May '18, 00:22