Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Bugs problem solution

YASH PAL, 31 July 202415 February 2026
In this HackerEarth Bugs problem solution, You are a developer at XYZ company. You like to call the bugs in your code as enemies. You maintain an array A of the list of enemies in decreasing order of their difficulty i.e., the most difficult bug will be the first element of the array. Initially, there are no bugs in the code. You are given N tasks. Each task contains one of the following two types of operations:
  1. P: Add a bug with difficulty P into the array A.
  2. Sort the array in decreasing order and print the difficulty of (n / 3)th bug in the sorted array, where n is the size of the array A. If the number of bugs is less than 3, print Not enough enemies.
 
 
HackerEarth Bugs problem solution

 

 

HackerEarth Bugs problem solution.

import java.util.*;
import java.io.*;

public class Problem {

static ArrayList<Integer> a = new ArrayList();
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int N = sc.nextInt();
int count=0;
while( N-->0 )
{

int type=sc.nextInt();
if( type==1 )
{
int P=sc.nextInt();
add_to_list(P);
count++;
}
else
{
if( count>=3 )
System.out.println(a.get(count/3-1));
else
System.out.println("Not enough enemies");
}
}
}





private static void add_to_list(int n)
{
int size = a.size();
int l=0,r=size-1,flag=0,mid,index,obs;
while(true)
{
if( l>r )
{
index = l;
break;
}

mid = (l+r)/2;
obs = a.get(mid);
if( obs==n )
{
index = mid;
break;
}
else if( obs>n )
{
l = mid+1;
flag=0;
}
else
{
r = mid-1;
flag=1;
}

}
if( index>=size )
a.add(n);
else if( index<0 )
a.add(0,n);
else
a.add(index,n);

}


}
 
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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