Coder Social home page Coder Social logo

array's Introduction

array

Binary Search

Iterative

        int[] input = {1, 2, 3 ,4 ,5 ,6 , 7, 8, 9};
       
        int left = 0;
        int right = input.length - 1;
        int target = 8;
        while (left <= right) {
            int mid = left + (right -left) / 2;
            if (input[mid] < target) {
                left = mid + 1;
            } else if (input[mid] > right) {
                right = mid - 1;
            } else {
                System.out.println(input[mid]);
                return;
            }
        }

Recursive

    public static void binarySearch(int[] input, int lower, int higher, int target) {
        if (lower >= higher) return;
        int mid = lower + (higher - lower) / 2;
        if (input[mid] < target) {
            binarySearch(input, mid + 1, higher, target);
        }
        else if (input[mid] > target) {
            binarySearch(input, lower, mid, target);
        } else {
            System.out.println(target);
        }
    } 
    

Two sum problem - two solutions

private static void haseSetSolution(int[] input, int sum) {
    Set<Integer> set = new HashSet<Integer>();
    
   
    for (int i: input) {
      int second = sum - i;
      if(set.contains(second)) {
        System.out.println(i + " , " + second);
      } else {
          set.add(i);
      }
    }
  }
  
  private static void pointerSolution(int[] input, int sum) {
  Arrays.sort(input);
    int min = 0;
    int max = input.length - 1;
    while (min < max) {
      int currentSum = input[min] + input[max];
      if (currentSum < sum) {
        min++;
      } else if(currentSum > sum) {
        max--;
      }
      else {
         System.out.println(input[min] + " , " + input[max]);
        max--;
      }
    }
  }

Find an element in a sorted rotated array - O(log n)

int[] input = {4 ,5 ,6 , 7, 0, 1, 2};
        int target = 1;
        int left = 0;
        int right = input.length - 1;

        // find pivot i.e min number - left will point to pivot element
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (input[mid] > input[right]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        int start = left;
        left = 0;
        right = input.length - 1;
        if (target >= input[start] && target <= input[right]) {
            left = start;
        } else {
            right = start;
        }
        System.out.println(left + ", " + right);

array's People

Contributors

shivarach avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.