Peak Element

import 'package:test/test.dart';

int findPeakUtil(List<int> arr, int low, int high, int n) {
  var x = (low + high) / 2;
  int mid = x.toInt();
  if ((mid == 0 || arr[mid - 1] <= arr[mid]) &&
      (mid == n - 1 || arr[mid + 1] <= arr[mid]))
    return mid;
  else if (mid > 0 && arr[mid - 1] > arr[mid])
    return findPeakUtil(arr, low, (mid - 1), n);
  else
    return findPeakUtil(arr, (mid + 1), high, n);
}

int findPeak(List<int> arr, int n) {
  return findPeakUtil(arr, 0, n - 1, n);
}

void main() {
  test("findPeak returns 2 for [1, 3, 20, 6, 1, 2]", () {
    List<int> lst = [1, 3, 20, 6, 1, 2];
    expect(findPeak(lst, lst.length), equals(2));
  });

  test("findPeak returns 3 for [1, 3, 20, 32, 1, 2]", () {
    List<int> lst = [1, 3, 20, 32, 1, 2];
    expect(findPeak(lst, lst.length), equals(3));
  });

  test("findPeak returns 2 for [321, 4353, 22320, 232, 23, 223]", () {
    List<int> lst = [321, 4353, 22320, 232, 23, 223];
    expect(findPeak(lst, lst.length), equals(2));
  });

  test("findPeak returns 2 for [121, 54, 2100, 36, 155, 90]", () {
    List<int> lst = [121, 54, 2100, 36, 155, 90];
    expect(findPeak(lst, lst.length), equals(2));
  });

  test("findPeak returns 2 for [5, 10, 20, 15]", () {
    List<int> lst = [5, 10, 20, 15];
    expect(findPeak(lst, lst.length), equals(2));
  });

  test("findPeak returns 1 for [10, 20, 15, 2, 23, 90, 67]", () {
    List<int> lst = [10, 20, 15, 2, 23, 90, 67];
    expect(findPeak(lst, lst.length), equals(1));
  });
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.