Count Increasing Subarrays in Python

💻 Solved a great problem today: Count Increasing Subarrays 🚀 Given an array, the task was to count all strictly increasing subarrays of size ≥ 2. 🔍 Approach I used: Broke the array into continuous increasing segments For each segment of length k, calculated subarrays using the formula: 👉 k(k-1)/2 Summed all results to get the final answer ⚡ Time Complexity: O(n) 📦 Space Complexity: O(n) 💡 This problem helped me understand how breaking a problem into patterns can simplify complex logic. Here’s my Python solution 👇 class Solution: def countIncreasing(self, arr): list2 = [] list2.append(arr[0]) list3 = [] for i in range(1, len(arr)): if arr[i-1] < arr[i] and arr[i] > 2: list2.append(arr[i]) else: list3.append(list2) list2 = [] list2.append(arr[i]) list3.append(list2) ans = 0 for i in list3: if len(i) > 1: ans += (len(i) * (len(i) - 1)) // 2 return ans 🔥 Always learning, always improving. #python #dsa #coding #problemSolving #programming #developers #learning

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories