VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-of-ways-array-can-be-partitioned-such-that-same-numbers-are-present-in-same-subarray/

⇱ Count number of ways array can be partitioned such that same numbers are present in same subarray - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count number of ways array can be partitioned such that same numbers are present in same subarray

Last Updated : 23 Jul, 2025

Given an array A[] of size N. The Task for this problem is to count several ways to partition the given array A[] into continuous subarrays such that the same integers are not present in different subarrays. Print the answer modulo 109 + 7.

Examples:

Input: A[] = {1, 2, 1, 3}
Output: 2
Explanation: There are two ways to break above array such that it follows above conditions:

  • First way is {{1, 2, 1} + {3}}
  • Second way is {{1, 2, 1, 3}}

{{1, 2} + {1, 3}} is not possible since 1 is present in different subarrays.

Input: A[] = {1, 1, 1, 1}
Output: 1
Explanation: There is only one way to break above array such that it follows above conditions:

  • {1, 1, 1, 1} is the only way.

{{1, 1} + {1, 1}} is not possible since same element repeats in two different subarrays.

Approach: The problem can be solved using the below approach:

The problem can be solved by finding the first and last occurrence of every element. We will treat it as segment and merge all overlapping segments. Suppose we are left with M segments after merging the overlapping segments. Now, for every segment we have the choice to include it with the previous segment's subarray or make a new subarray from it. This is same as Stars and Bars where we have (M-1) blanks between M partitions and we have 2choices for each partition, that is either place a bar in the blank to start a new subarray or do not place a bar and continue with the previous subarray. So, if we have M segments after merging the overlapping segments, we have 2(M-1) ways to make valid partition of the array.

For eg: In the first example {1, 2, 1, 3}

  • First and last occurrence of 1 : {first occurrence = 1, last occurrence = 3}
  • First and last occurrence of 2: {first occurrence = 2, last occurrence = 2}
  • First and last occurrence of 3: {first occurrence = 4, last occurrence = 4}

if it is represented as segments there are three segments currently {1, 3}, {2, 2} and {4, 4}

Let's unite overlapping segments we will end up with {1, 3} and {4, 4}

now M = 2 so by above formula 2M - 1 = 2 (there will be two possible partitions which follows required conditions)

Step-by-step algorithm:

  • Declare two Hash Maps firstOcc[] and lastOcc[] to record the first and last occurence of every element.
  • Declare array of pairs segments[] for storing segments.
  • Sort the array segments[] and merge all the overlapping segments.
  • Count the number of segments left after merging the overlapping segments, say M.
  • Return the answer as 2(M - 1) mod 1000000007 using binary exponentiation.

Implementation of the above approach:


Output
2

Time Complexity: O(N), where N is the size of input array A[].
Auxiliary Space: O(N)

Comment