Java simple with complete explanation
Approach\r\nThe provided solution employs a sliding window technique to solve the problem. Let\'s break down the approach step by step:\r\n\r\n- Initialize vari
# Approach\r\nThe provided solution employs a sliding window technique to solve the problem. Let\'s break down the approach step by step:\r\n\r\n- Initialize variables left, right, count, max, and temp. Set len as the length of the input array nums. Here, left and right represent the left and right pointers of the sliding window, count keeps track of the number of odd numbers encountered, max stores the total count of nice subarrays, and temp is a temporary variable to count the number of nice subarrays at each position.\r\n\r\n- Start iterating through the array using the right pointer until it reaches the end of the array.\r\n\r\n- At each iteration, check if the current number at position right is odd or even. If it is odd\r\n-       (nums[right] % 2 == 1)\r\n-  increment the count by 1 to indicate the presence of an odd number in the current subarray. Reset temp to 0 to start counting the number of nice subarrays at the current position.\r\n\r\n- Inside the while loop, check if the condition of having exactly k odd numbers is satisfied \r\n-      (count == k). \r\n-   If this condition is met, it means we have found a nice subarray.\r\n\r\n- Increment temp by 1 to count the number of nice subarrays starting from the current position right.\r\n\r\n- Move the left pointer inside a nested while loop to shrink the window and find more nice subarrays. We keep moving the left pointer and decrementing the count whenever we encounter an odd number. This step ensures that we maintain exactly k odd numbers in the subarray.\r\n\r\n- After finding all possible nice subarrays starting from the current right position, add temp to max to update the total count of nice subarrays.\r\n\r\n- Finally, increment the right pointer to expand the window and continue the process until we reach the end of the array.\r\n\r\n- Return max, which represents the total number of nice subarrays.- \r\n\r\n# Complexity\r\n- **Time complexity:** \r\n\r\n-     O(n)\r\nThe time complexity of this approach is O(N), where N is the length of the input array nums. The algorithm only iterates through the array once using the sliding window technique.\r\n<hr>\r\n\r\n- **Space complexity:**\r\nslid \r\n-     O(1)\r\nThe space complexity is O(1) because no extra space is used apart from the input array and a few variables to track indices and counts.\r\n\r\n# Code\r\n```\r\nclass Solution {\r\n    public int numberOfSubarrays(int[] nums, int k) {\r\n        int len=nums.length;\r\n        int left=0,right=0,count=0,max=0,temp=0;\r\n        while(right<len)\r\n        {\r\n            if(nums[right]%2==1)\r\n            {\r\n                count++;\r\n                temp=0;\r\n            }\r\n           \r\n            while(count==k)\r\n            {\r\n                temp++;\r\n                    if(nums[left]%2==1)\r\n                    {\r\n                        count--;\r\n                    }\r\n                   \r\n                    left++;\r\n            }\r\n            max=max+temp;\r\n            \r\n            right++;\r\n        }\r\n        return max;\r\n        \r\n    }\r\n}\r\n```
['Array', 'Two Pointers', 'Sliding Window', 'Java']