Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Mathison and the funny subarray problem solution

YASH PAL, 31 July 2024
In this HackerEarth Mathison and the funny subarray problem solution 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.
HackerEarth Mathison and the funny subarray problem solution

HackerEarth Mathison and the funny subarray problem solution.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

using namespace std;

int main(){
ios_base::sync_with_stdio(false);

int N;
cin >> N;

vector<int> A(N);
for (int &x: A)
cin >> x;

map<int, vector<int>> mp;
for (int i = 0; i < N; i++)
mp[A[i]].push_back(i);

int best = 0;

for (auto p: mp){
if (!p.second.empty())
best = max(best, p.second.back() - p.second[0] + 1);
}

cout << best << endl;

return 0;
}

Second solution

import java.io.*;
import java.util.*;
import java.math.*;
import java.util.concurrent.*;

public final class a
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static FastScanner sc=new FastScanner(br);
static PrintWriter out=new PrintWriter(System.out);
static Random rnd=new Random();
static int maxn=(int)(1e5+5);

public static void main(String args[]) throws Exception
{
int n=sc.nextInt();int[] a=new int[n],first=new int[maxn],last=new int[maxn];

Arrays.fill(first,-1);Arrays.fill(last,-1);

if(n<1 || n>1000000)
{
throw new Exception("Wrong!");
}

for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();

if(a[i]<1 || a[i]>100000)
{
throw new Exception("Wrong!");
}

if(first[a[i]]==-1)
{
first[a[i]]=i;
}

last[a[i]]=i;
}

int max=0;

for(int i=0;i<maxn;i++)
{
if(first[i]!=-1)
{
max=Math.max(max,last[i]-first[i]+1);
}
}

out.println(max);out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;

public FastScanner(BufferedReader in) {
this.in = in;
}

public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}

public String next() throws Exception {
return nextToken().toString();
}

public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes