- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Alternating Subarray
The Count Alternating Subarrays are used to count the number of subarrays where no two adjacent elements are similar. we can also call these subarrays as alternating subarrays.
Problem Statement
Before understanding what is "Count Alternating Subarrays" let's see what is a sub-array, and alternating sub-arrays.
- A subarray is part of an array formed by removing some or no prefixes of the array and removing some or no suffix elements of the given array.
-
While dividing an array into multiple sub-arrays there might be a chance that two adjacent sub-arrays may have the same values. If no two adjacent sub-arrays have the same values then, these are known as alternating sub-arrays.
Example Scenario 1
Input: nums = [1, 1, 1 , 1]
Output:count = 4
The alternating subarrays = [1], [1], [1], [1]
Example Scenario 2
Input: nums = [1, 1, 1, 0]
Output:count = 5
The alternating subarrays = [1], [1], [1], [0], [1, 0]
Example Scenario 3
Input: nums = [1, 0, 1, 0, 1]
Output: count = 9
The alternating subarrays = [1], [0], [1], [0], [1], [1, 0], [0, 1], [1, 0, 1], [0, 1, 0].
Time complexity
The time complexity for counting alternating subarrays is O(n), where ( n ) is the length of the array. To solve this problem in various programming languages, use the following approaches.
- Brute-force Approach
- Dynamic Programming Approach
Brute-force Approach
This approach is a straightforward method for solving problems all possible solutions.
Example
The program counts the number of alternating subarrays in a given vector of integers by iterating through the vector and adding the lengths of all alternating subarrays.
#include <iostream> #include <vector> using namespace std; class Count { public: long long countAlternatingSubarrays ( vector<int>& nums ) { long long ans = 1, s = 1; for (int i = 1; i < nums.size(); ++i) { s = nums[i] != nums[i - 1] ? s + 1 : 1; ans += s; } return ans; } }; int main() { Count X; vector<int> nums = {1, 0, 1, 0, 1, 0}; cout << " Count of alternating subarrays = " << X.countAlternatingSubarrays(nums) << endl; return 0; }
Output
Count of alternating subarrays = 21
import java.util.*; public class Count { public long countAlternatingSubarrays(int[] nums) { long ans = 1, s = 1; for (int i = 1; i < nums.length; ++i) { s = (nums[i] != nums[i - 1]) ? s + 1 : 1; ans += s; } return ans; } public static void main(String[] args) { Count X = new Count(); int[] nums = {1, 0, 1, 0, 1, 0}; System.out.println("Count of alternating subarrays = " + X.countAlternatingSubarrays(nums)); } }
Output
Count of alternating subarrays = 21
def count_alternating_subarrays(nums): ans = s = 1 for i in range(1, len(nums)): s = s + 1 if nums[i] != nums[i - 1] else 1 ans += s return ans nums = [1, 0, 1, 0, 1, 0] print("Count of alternating subarrays =", count_alternating_subarrays(nums))
Output
Count of alternating subarrays = 21
Dynamic programming Approach
This method is used to solve complex problems by breaking them into simpler subproblems.
Example
Following is the program that counts the number of subarrays with alternating elements in a given vector of integers by checking every possible subarray.
#include <iostream> #include <vector> using namespace std; long long countAlternatingSubarrays(const vector<int>& nums) { long long count = 0; for (int i = 0; i < nums.size(); ++i) { int sign = 0; for (int j = i; j < nums.size(); ++j) { if (j == i || nums[j] != nums[j - 1]) { count++; sign = !sign; } else { break; } } } return count; } int main() { vector<int> nums = {1, 0, 1, 0, 1, 0}; cout << "Count of alternating subarrays = " << countAlternatingSubarrays(nums) << endl; return 0; }
Output
Count of alternating subarrays = 21
import java.util.*; public class Main { public static long countAlternatingSubarrays(List <Integer> nums) { long count = 0; for (int i = 0; i < nums.size(); ++i) { int sign = 0; for (int j = i; j < nums.size(); ++j) { if (j == i || !nums.get(j).equals(nums.get(j - 1))) { count++; sign = 1 - sign; } else { break; } } } return count; } public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 0, 1, 0, 1, 0); System.out.println("Count of alternating subarrays = " + countAlternatingSubarrays(nums)); } }
Output
Count of alternating subarrays = 21
def count_alternating_subarrays(nums): count = 0 for i in range(len(nums)): sign = 0 for j in range(i, len(nums)): if j == i or nums[j] != nums[j - 1]: count += 1 sign = not sign else: break return count nums = [1, 0, 1, 0, 1, 0] print("Count of alternating subarrays = ", count_alternating_subarrays(nums))
Output
Count of alternating subarrays = 21