package org.wesome.dsalgo;
public class BitonicSubarray {
public static int bitonicSubarray(int[] arr) {
int length = arr.length;
if (length == 0) {
return length;
}
int endIndex = 0, maxBitonicArrayLen = 1, indx = 0;
while (indx + 1 < length) {
int bitonicArrayLen = 1;
/* loop till array next is greater than current, increment bitonicArrayLen */
while (indx + 1 < length && arr[indx] < arr[indx + 1]) {
indx++;
bitonicArrayLen++;
}
/* loop till array next is smaller than current, increment bitonicArrayLen */
while (indx + 1 < length && arr[indx] > arr[indx + 1]) {
indx++;
bitonicArrayLen++;
}
while (indx + 1 < length && arr[indx] == arr[indx + 1]) {
indx++;
}
if (bitonicArrayLen > maxBitonicArrayLen) {
maxBitonicArrayLen = bitonicArrayLen;
endIndex = indx;
}
}
System.out.println("longest bitonic subArray is " + maxBitonicArrayLen + " from " + arr[(endIndex - maxBitonicArrayLen + 1)] + " to " + arr[endIndex]);
return maxBitonicArrayLen;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.wesome.dsalgo.BitonicSubarray.bitonicSubarray;
public class BitonicSubarrayTest {
@Test
void bitonicSubarrayTest1() {
int[] arr = {3, 5, 8, 4, 5, 9, 10, 8, 5, 3, 4};
Assertions.assertEquals(7, bitonicSubarray(arr));
}
@Test
void bitonicSubarrayTest2() {
int[] arr = {1, 2, 3, 4, 5, 4, 3, 2, 1};
Assertions.assertEquals(9, bitonicSubarray(arr));
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.2.0"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}