ldCizPjVm8

import java.util.Arrays;

class Main {

  public static void main(String[] args){
    int[] i = new int[]{1, 2, 3, 4, 5, 6, 7};
    
    int easyMid = i.length/2;
    int mid = (i.length % 2)==0 ? (i[easyMid] + i[easyMid-1])/2 : easyMid;
    int target = 1;
    System.out.println(SearchForIndex(i, target, mid ));
  }
  
  public static String SearchForIndex(int[] list, int target, int startingIndex){
    int index = startingIndex;
    System.out.println("Index: "+index);
    int value = list[index];
    int listLength = list.length;
    int topMedianMath = index+(listLength-(index+1));
    int lowerMedianMath = index-(listLength-(index+1));
    int topMedian = topMedianMath!=index && topMedianMath<listLength? (index+(listLength-(index+1))) : index+1;
    int lowerMedian = lowerMedianMath!=index && lowerMedianMath>-1? (index-(listLength-(index+1))) : index-1;
    if (value == target) {
      System.out.println("Target found: "+index);
      return "Target found: "+index;
    }
    else if (index==listLength-1) {
      if (list[index-1] == target){
        System.out.println("Target found: "+(index-1));
        return "Target found: "+(index-1);
      }
    } else if (index==0) {
      if (list[1] == target){
        System.out.println("Target found: 1");
        return "Target found: 1";
      }
    } else if (index > 0 && index < listLength-1) {
      if (list[index+1]==target){
        System.out.println("Target found: "+(index+1));
        return "Target found: "+(index+1);
      } else if (list[index-1]==target){
        System.out.println("Target found: "+(index-1));
        return "Target found: "+(index-1);
      }
    }
    if ((target>value) && (index != (listLength-1)) && (list[listLength-1] >= target)) {
      System.out.println("Searching upper");
      SearchForIndex(list, target, topMedian);
    }
    else if ((target<value) && (index != 0) && (list[0] <= target)) {
      System.out.println("Searching lower");
      SearchForIndex(list, target, lowerMedian);
    }
    return "Not found";