Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
  • Work with US
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

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes