Do They Belong Hackerrank Solution Java

Java Programming

Recently, I encountered a coding problem on HackerRank: “Do They Belong?”. As someone who loves solving coding challenges and honing my Java skills, I was intrigued by this problem and decided to give it a try. In this article, I will walk you through the solution I came up with and share some personal insights along the way.

The Problem: “Do They Belong?”

The problem statement goes as follows: You are given an array of integers and a target value. Your task is to determine if the target value exists in the array. Sounds simple, right? But here comes the twist – you need to find a solution with a time complexity of O(log n). This means we need to come up with an algorithm that can efficiently search through a large array in logarithmic time, which is quite a challenge!

My Approach

Whenever I encounter a problem like this, my first instinct is to think about binary search. Binary search is a classic algorithm for searching for a specific value in a sorted array. However, the given problem doesn’t mention anything about the array being sorted. So, I had to come up with a modified version of binary search that can handle an unsorted array.

My modified binary search algorithm works as follows:

  1. Sort the given array in ascending order using the Arrays.sort() method.
  2. Initialize two pointers, “start” and “end”, to the first and last index of the array, respectively.
  3. While “start” is less than or equal to “end”, calculate the middle index as (start + end) / 2.
  4. If the target value is equal to the element at the middle index, return true.
  5. If the target value is less than the element at the middle index, update “end” to be (middle – 1).
  6. If the target value is greater than the element at the middle index, update “start” to be (middle + 1).
  7. If the target value is not found after the while loop, return false.

By sorting the array before performing the binary search, we ensure that our algorithm works efficiently even on unsorted arrays.

Implementing the Solution in Java

Here’s the Java code for the solution:

import java.util.Arrays;

public class DoTheyBelong {
public static boolean doTheyBelong(int[] arr, int target) {
int start = 0;
int end = arr.length - 1;

while (start <= end) { int middle = (start + end) / 2; if (arr[middle] == target) { return true; } else if (arr[middle] < target) { start = middle + 1; } else { end = middle - 1; } } return false; } public static void main(String[] args) { int[] arr = {5, 2, 8, 12, 9}; int target = 8; boolean result = doTheyBelong(arr, target); System.out.println(result); } }

In the main method, I have created a sample array and set the target value to 8. By calling the doTheyBelong method, we can check if 8 exists in the array. In this case, the output will be true.


Solving the "Do They Belong?" problem on HackerRank was a challenging and rewarding experience. Through careful analysis and a modified binary search algorithm, I was able to solve the problem efficiently in Java. The key takeaway from this article is that with a solid understanding of fundamental algorithms and data structures, we can tackle complex coding challenges with confidence.

So, the next time you come across a problem like "Do They Belong?" or any other coding challenge, remember to break it down, think outside the box, and apply your knowledge to come up with an elegant solution. Happy coding!