Longest Odd-Even Subsequence in Java10 Sept 2024 | 7 min read The longest odd-even subsequence in Java is a problem in which one has to find a subsequence in a non-negative array of size s, such that the subsequence contains alternate odd and even numbers in an alternate manner. Thus, one has to find the count of numbers present in the longest subsequence that has the alternative sequence of the odd and even numbers. Note that the subsequence can start either with an even or with an odd number. Let's understand it with the help of some examples. For example: Input: int arr[] = {15, 16, 19, 14, 17, 18, 21, 30, 78}, //Size s = 9 Output: 8 Explanation: The required longest subsequence is (15, 16, 19, 14, 17, 18, 21, 30) or (15, 16, 19, 14, 17, 18, 21, 78), and the count of element in the subsequence is 8. Input: int arr[] = {11, 112, 12, 122, 15, 130, 131, 140, 117, 111}, //Size s = 10 Output: 7 Explanation: The required longest subsequence is (11, 112, 15, 130, 131, 140, 117) or (11, 112, 15, 130, 131, 140, 111), or there can be another subsequence too, and the count of the elements in the subsequence is 7. ApproachThere are two approaches to find out the longest odd-even subsequence. One is the brute force approach, also known as the naïve approach. The other is the efficient approach using dynamic programming. Let's start with the naïve one. Naïve ApproachIn this approach, we will find out the all-possible subsequences of the given array. Then, for all the computed subsequences, we will filter out the longest odd-even subsequence. For better understanding, observe the following program. FileName: OddEvenSeq.java Output: For the input array: 15 16 19 14 17 18 21 30 78 The longest odd even subsequence is: 8 For the input array: 11 112 12 122 15 130 131 140 117 111 The longest odd even subsequence is: 7 Complexity Analysis: In the above program, there are two choices; one is to include the element, and the other is to exclude the element. Therefore, the time complexity of the above program is exponential, which is O(2n), where n is the total number of elements present in the array. The time complexity O(2n) is quite huge. Therefore, the above program is not suitable for the input array that contains a lot of elements. Also, the program is taking a lot of extra space, which is also exponential O(2n). Implementation: Dynamic ProgrammingLet longLen(j) be the length of the Longest Odd-Even Subsequence (LOES) that is ending at the index j so that the a[j] is the last element of the LOES. Thus, longLen(j) can be written recursively as: longLen(j) = 1 + max(longLen(i)), where 0 < i < j and (a[i] < a[j]) and (a[i]+a[j]) % 2 != 0; or longLen(j) = 1, if there is no such i exists. In order to compute the longest odd even subsequence for the given array, it is required to return the max(longLen(j)) where 0 < j < size. The following program implements the dynamic programming approach for the above-mentioned recursive relation. FileName: OddEvenSeq1.java Output: For the input array: 15 16 19 14 17 18 21 30 78 The longest odd even subsequence is: 8 Complexity Analysis: The above program uses the 2-level nesting of the for-loops. Therefore, the time complexity of the above program is O(n2), where n is the total number of elements present in the array. The program is taking an array as the extra memory, which is of the size n. Therefore, the space complexity of the above program is O(n). Next TopicJava Subtract Days from Current Date |
Java double keyword
In Java, double is a datatype. It is used for storing decimal numbers with high precision. It is a 64-bit IEEE 754 floating-point data type, meaning it can handle large values and fractions accurately. We often see it in scientific calculations, financial applications, and physics...
3 min read
DoubleConsumer Interface in Java with Examples
The java.util.function package, which was first released with Java 8, includes the DoubleConsumer Interface, which is used to do functional programming in Java. It is an example of a function that accepts a single double-valued argument but outputs nothing. In order to define its accept()...
4 min read
Static Object in Java
Static objects play a critical function in the world of Java programming. They provide a method to share data and functionality throughout a couple of instances of a class. In this context, we are able to discover the idea of static objects in Java, talk their...
4 min read
How to Update the Value of an Existing Key in a LinkedHashMap in Java
? A LinkedHashMap is similar to a HashMap in Java programming, but it has extra functionality. It maintains a record of the elements' addition order. There is no set order for the elements in a standard HashMap. To remember the order of inserted keys, LinkedHashMap employs...
5 min read
Number of Ways to Rearrange Sticks with K Sticks Visible in Java
Given n sticks labelled from 1 to n, we must arrange them in a line such that exactly k sticks are visible from the left. A stick is visible if it is taller than all ious sticks. The task is to count the number of valid...
6 min read
Access Specifiers vs Modifiers
Difference Between Access Specifiers and Modifiers In Java, the access modifiers are used for restricting the scope of a class and its data members, member function, and constructor. Access modifiers play an important in designing Java programs and Java applications. Java has the following access modifiers: private protected public default Default Access Modifiers When...
3 min read
Initializing a List in Java
One of Collection's derived interfaces is Java.util.List. It is an ordered group of objects that allows for the storing of duplicate values. List enables positional access and element insertion since it maintains the insertion order. The Vector, Stack, LinkedList, and ArrayList classes to implement the List...
6 min read
Java Interpreter
Java is a platform-independent programming language. It means that we can run Java on the platforms that have a Java interpreter. It is the reason that makes the Java platform-independent. The Java interpreter converts the Java bytecode (.class file) into the code understand by the operating...
3 min read
How to Pass an ArrayList to a Method in Java
? In Java, ArrayLists are commonly used to store and manipulate collections of data. At times, you may need to pass an ArrayList as an argument to a method to perform operations or modify its content. This article will guide you through the process of passing an...
3 min read
Architecture Neutral in Java
Java, a versatile and widely-used programming language, is renowned for its platform independence, thanks to its architecture-neutral nature. The term architecture neutrality refers to Java's ability to run on any device or platform without requiring modifications to its code. This unique feature has played a...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India