Error
|
|
---|---|
@Ibrahim Nash | 5761 |
@blackshadows | 5715 |
@akhayrutdinov | 5111 |
@mb1973 | 4989 |
@Quandray | 4944 |
@saiujwal13083 | 4506 |
@sanjay05 | 3762 |
@marius_valentin_dragoi | 3516 |
@sushant_a | 3459 |
@verma_ji | 3341 |
@KshamaGupta | 3318 |
Complete Leaderboard | |
|
|
@aroranayan999 | 1115 |
@bt8816103042 | 739 |
@SherlockHolmes3 | 447 |
@codeantik | 441 |
@SHOAIBVIJAPURE | 430 |
@shalinibhataniya1097 | 408 |
@ShamaKhan1 | 392 |
@neverevergiveup | 381 |
@amrutakashikar2 | 355 |
@mahlawatep | 353 |
@murarry3625 | 352 |
Complete Leaderboard |
Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Example 1:
Input n= 6 arr = {1, 1, 2, 2, 2, 1} Output 5 Explanation arr[] = {1, 1, 2, 2, 2, 1} Max Distance: 5 Distance for 1 is: 5-0 = 5 Distance for 2 is : 4-2 = 2 Max Distance 5
Example 2:
Input n = 12 arr = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2} Output 10 Explanation arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2} Max Distance 10 maximum distance for 2 is 11-1 = 10 maximum distance for 1 is 4-2 = 2 maximum distance for 4 is 10-5 = 5
Your Task:
Complete maxDistance() function which takes both the given array and their size as function arguments and returns the maximum distance between 2 same elemenrs.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(N)
Constraints:
1<=T<=100
1<=N<=1000
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes