All Tracks Algorithms Greedy Algorithms Basics of Greedy Algorithms Problem

Mathison and the funny subarray
Tag(s):

Easy, Greedy algorithm

Problem
Editorial
Analytics

Mathison has been playing quite a lot with some array A of length N. A subarray of the given array is called funny if it starts and ends with the same number.

Your task is to find the length of the longest funny subarray of the given array.

Input
The first line of the input file contains an integer, N, representing the number of integers in the given array.
The second line of the input file contains N space-separated integers, where the \(i^{th}\) integer represents \(A[i]\) .

Output
The output file should contain only one integer, the length of the longest funny subarray.

Constraints

  • \( 1 \le N \le 10^6 \)
  • \( 1 \le A[i] \le 10^5 \)
  • A subarray is a subset of consecutive elements of an array.
SAMPLE INPUT
9
2 2 3 1 2 2 3 1 1
SAMPLE OUTPUT
6
Explanation

The longest funny subarray has a length of 6. There are two possible choices: \([2, 2, 3, 1, 2, 2]\) and \([1, 2, 2, 3, 1, 1]\).

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

November Easy '17

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications

?