- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Program to find length of longest increasing subsequence with at least k odd values in Python

Suppose we have a list of numbers called nums and another value k, we have to find the size of the longest increasing subsequence with at least k odd elements.

So, if the input is like nums = [12, 14, 16, 5, 7, 8] k = 2, then the output will be 3, as the longest increasing subsequence with at least 2 odd values is [5, 7, 8].

To solve this, we will follow these steps −

best := 0

Define a function dp() . This will take i, j, odd, taken

if odd >= k, then

best := maximum of best and taken

if j is same as size of nums , then

return

if nums[j] > nums[i], then

dp(j, j + 1, odd +(nums[j] AND 1) , taken + 1)

dp(i, j + 1, odd, taken)

From the main method do the following −

for i in range 0 to size of nums, do

dp(i, i + 1, nums[i] AND 1, 1)

return best

## Example

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums, k): best = 0 def dp(i, j, odd, taken): nonlocal best if odd >= k: best = max(best, taken) if j == len(nums): return if nums[j] > nums[i]: dp(j, j + 1, odd + (nums[j] & 1), taken + 1) dp(i, j + 1, odd, taken) for i in range(len(nums)): dp(i, i + 1, nums[i] & 1, 1) return best ob = Solution() nums = [12, 14, 16, 5, 7, 8] k = 2 print(ob.solve(nums, k))

## Input

[12, 14, 16, 5, 7, 8], 2

## Output

3

- Related Articles
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest circular increasing subsequence in python
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest palindromic subsequence in Python
- Program to find length of longest fibonacci subsequence in Python
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Longest Increasing Subsequence in Python
- Program to find length of longest consecutively increasing substring in Python
- Program to find length of longest common subsequence of three strings in Python
- Maximum sum subsequence with at-least k distant elements in C++ program
- Java Program for Longest Increasing Subsequence
- Program to find length of longest bitonic subsequence in C++
- Program to find length of longest common subsequence in C++