Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class BitonicSubsequences {
- private static final int MOD = 1000000007; // 1e9+7
- public static int countBitonicSubsequences(int[] arr) {
- int n = arr.length;
- int[] increasing = new int[n];
- int[] decreasing = new int[n];
- Arrays.fill(increasing, 0);
- Arrays.fill(decreasing, 0);
- // Count increasing subsequences
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < i; j++) {
- if (arr[j] < arr[i]) {
- increasing[i] = (increasing[i] + increasing[j] + 1) % MOD;
- }
- }
- }
- // Count decreasing subsequences
- for (int i = n - 1; i >= 0; i--) {
- for (int j = i + 1; j < n; j++) {
- if (arr[j] < arr[i]) {
- decreasing[i] = (decreasing[i] + decreasing[j] + 1) % MOD;
- }
- }
- }
- // Count bitonic subsequences
- long count = 0;
- for (int i = 1; i < n - 1; i++) {
- count = (count + (long)increasing[i] * decreasing[i]) % MOD;
- }
- return (int)count;
- }
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 2, 1};
- int result = countBitonicSubsequences(arr);
- System.out.println("The number of bitonic subsequences is: " + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement