From ce9c5ca5d04851a4a905022d6708daadf6b4c7fc Mon Sep 17 00:00:00 2001 From: chayan das Date: Wed, 16 Jul 2025 22:12:28 +0530 Subject: [PATCH] Create 3201. Find the Maximum Length of Valid Subsequence I --- ... the Maximum Length of Valid Subsequence I | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 3201. Find the Maximum Length of Valid Subsequence I diff --git a/3201. Find the Maximum Length of Valid Subsequence I b/3201. Find the Maximum Length of Valid Subsequence I new file mode 100644 index 0000000..6e3dab9 --- /dev/null +++ b/3201. Find the Maximum Length of Valid Subsequence I @@ -0,0 +1,32 @@ +class Solution { +public: + int maximumLength(vector& nums) { + int allev = 0, allodd = 0, oddev = 0; + + // Count total even and odd numbers + for (int i = 0; i < nums.size(); i++) { + if (nums[i] % 2 == 0) { + allev++; + } else { + allodd++; + } + } + + // Count longest alternating odd-even subsequence + bool expectOdd = true; + bool expectEven = true; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] % 2 == 0 && expectEven) { + oddev++; + expectEven = false; + expectOdd = true; + } else if (nums[i] % 2 != 0 && expectOdd) { + oddev++; + expectOdd = false; + expectEven = true; + } + } + + return max({allev, allodd, oddev}); + } +};