Solution 1 for Scaler Topics Fortnightly Contest - 11

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

This article is part of the Scaler Topics Fortnightly Contest - 11

Solution Approach

  • We can find the position of each of the values from 0 to N-1
  • Then we can traverse i from 0 to N-2 and check the leftmost and rightmost boundary of the position of the values considered so far. If the length equals to the number of elements considered, then it is a good subarray. We will just have to find the good subarray of maxmium length.

Time complexity: O(N)
Space complexity: O(N)

C++ Implementation

Java Implementation

Python Implementation