Count ways to form N sized Strings with at most two adjacent different pair
Given a value N, the task is to find the number of ways a binary string of size N can be formed such that at most 2 adjacent pairs have different values.
Examples:
Input: N = 1
Output: 2
Explanation: Possible ways to form string are “1”, “0”. So the answer is 2Input: N = 2
Output: 4
Explanation: Possible ways are “10”, “01”, “11”, “00”. So the answer is 4.Input: N = 4
Output: 14
Explanation: Possible ways are “1111”, “1110”, “1101”, “1011”, “0111”, “1100”, “1001”, “0110”, “0011”, “1000”, “0100”, “0010”, “0001”, “0000”. So the answer is 14.
Approach: This problem can be solved by following the below idea:
Consider all possible cases and add them up. Since maximum, we can have 2 adjacent pairs of different values, we count the number of ways for all 3 cases individually. The 3 ways possible are as follows:
- No adjacent pair with different values: We can have all the same bits i.e all bits are 0 or 1. So can form 2 strings in this case.
- Only one adjacent pair of different values: In this case, we will have all 1s together and 0s together so that only one pair of adjacent values is different. If the 1 is the first bit then the other are 0s. The total number of ways to do so is N-1(as there can be one ‘1’ + (N-1) ‘0’, 2 ‘1’ + (N-2) ‘0’, . . ., (N-1) ‘1’ + one ‘0’). Similarly, there can be 0s first and then 1s. So (N-1) more ways. Therefore total ways in this case, is 2*(N-1).
- Two adjacent pairs with different values: In this case, we keep bits in two patterns to make exactly two such positions where adjacents are different:
- Set of 1s then set of 0s and then again set of 1s – In this case, the set of 0s can be starting from the 2nd position and end maximum at the (N-1)th position (because at least first and last bit should be 1 for two adjacent different bits). So the number of ways, in this case, can be counted as the sum of the following:
- Number of ways if the set of 0s from 2nd position = N-2 ways (As blue color balls can end from 2nd to the (N-1)th position if it starts from 2nd position.)
- Number of ways if the set of 0s starts from 3rd position = N-3 ways.
- Similarly, till 0s start at (N-1)th position.
- Therefore the total number of ways, in this case, is (N – 2) + (N – 3)+ . . . + 1 = (N – 2) * (N – 1)/2.
- Set of 1s then set of 0s and then again set of 0s – This case is similar to the previous one. So the total number of ways, in this case, is also (N-1) * (N-2)/2.
- So the total number of ways is 2*{(N-1)*(N-2)/2} = (N-1)*(N-2) ways.
And hence total ways possible are the combination of these 3 cases i.e., 2 + 2*(N-1) + (N-2)*(N-1).
Below is the implementation of the above approach.
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to count total number of ways long long noOfWays(long long N) { // Adding all 3 cases // possible and returning it return 2 + 2 * (N - 1) + (N - 2) * (N - 1); } // Driver code int main() { int N = 4; // Function call cout << noOfWays(N); return 0; }
Java
// Java code to implement the approach import java.io.*; class GFG { // Function to count total number of ways static int noOfWays(int N) { // Adding all 3 cases // possible and returning it return 2 + 2 * (N - 1) + (N - 2) * (N - 1); } // Driver code public static void main (String[] args) { int N = 4; // Function call System.out.println(noOfWays(N)); } } // This code is contributed by Aman Kumar.
Python3
# Python3 code to implement the above approach # Function to count total number of ways def noOfWays(N) : # Adding all 3 cases # possible and returning it return 2 + 2 * (N - 1) + (N - 2) * (N - 1); # Driver code if __name__ == "__main__" : N = 4; # Function call print(noOfWays(N)); # This code is contributed by AnkThon
C#
// C# code to implement the approach using System; public class GFG { // Function to count total number of ways static int noOfWays(int N) { // Adding all 3 cases // possible and returning it return 2 + 2 * (N - 1) + (N - 2) * (N - 1); } static public void Main() { // Code int N = 4; // Function call Console.WriteLine(noOfWays(N)); } } // This code is contributed by lokeshmvs21.
Javascript
// JavaScript code to implement the approach // Function to count total number of ways const noOfWays = (N) => { // Adding all 3 cases // possible and returning it return 2 + 2 * (N - 1) + (N - 2) * (N - 1); } // Driver code let N = 4; // Function call console.log(noOfWays(N)); // This code is contributed by rakeshsahni
14
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...