Sunday, December 28, 2014

Find range of a target number

Given a sorted array with duplicates and a number, find the range in the 
form of (startIndex, endIndex) of that number. For example, 

find_range({0 2 3 3 3 10 10}, 3) should return (2,4). 
find_range({0 2 3 3 3 10 10}, 6) should return (-1,-1). 
The array and the number of duplicates can be large.

Click here for the original problem.
Now that I realize when dealing with searching problem, I should try to get an O(log(n)) solution. Yeah, binary search.


public class FindRange {
 public int[] findBound(int[] num, int target) {
  if (num == null)
   throw new NullPointerException("Null array");
  int[] bound = {-1, -1};
  if (num.length == 0)
   return bound;
  findRange(0, num.length - 1, bound, num, target);
  return bound;
  
 }
 private void findRange(int start, int end, int[] bound, int[] num, int target) {
  if (start > end)
   return;
  int mid = (start + end) / 2;
  if (num[mid] == target) {
   if (start == mid || num[mid - 1] != num[mid]) {
    if (bound[0] == -1 || mid < bound[0])
     bound[0] = mid;
   }
    
   else {
    findRange(start, mid - 1, bound, num, target);
   }
   if (end == mid || num[mid + 1] != num[mid]) {
    if (mid > bound[1])
     bound[1] = mid;
   }
   else {
    findRange(mid + 1, end, bound, num, target);
   }
  }
  else if (num[mid] < target)
   findRange(mid + 1, end, bound, num, target);
  else
   findRange(start, mid - 1, bound, num, target);
 }

}

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete