- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given three variables size, max_val, last_element as input. The goal is to find the count of different arrays that can be formed in a manner such that they have size elements, have elements between 1 and max_val and the first element is always 1 and the last element is always max_val. Also make sure that no two consecutive elements are the same.

**For Example**

**Input -** size = 5, max_val = 3, last_element = 3

**Output -** Count of arrays having consecutive element with different values are: 5

**Explanation -** The arrays will be:-

[ 1, 2, 3, 1, 3 ], [ 1, 2, 3, 2, 3 ], [ 1, 2, 1, 2, 3 ], [ 1, 3, 1, 2, 3 ], [ 1, 3, 2, 1, 3 ].

**Input -** size = 3 max_val = 2 last_element = 2

**Output -** Count of arrays having consecutive element with different values are: 0

**Explanation -** No array possible with 3 elements and having [ 1, _, 2 ] as consecutive elements as we cannot fill anything except 1 or 2 for the middle element and this will violate consecutive different elements' condition.

- In this approach we will use dynamic programming and combinatorics for finding such arrays count.
- The first and last elements will be fixed as 1 and last_element. For any size of arrays the ways to fill it will be for size-2 elements only.
- For filling [1 to max_val] elements to be filled in size-2 places. Ways will be ways(max_val)= ways(size) / (max_val - 1)
- For each range 1 to i, ways will be ways(i)=ways(size) / (max_val - 1) [ ways(size) = number of ways last element can be filled with numbers 2 to max_val ).
- If last_element is 1 then ways will be ways(size-1) as last element can only be 1.
- The second last element can always be between 1 and max_val.
- If second last element is not 1 then ways will be (max_val-2)*ways(i-1) as arr
_{i </sub>cannot be 1 or arri-1} - If second last element is 1 then ways will be (max_val-1)*ways(i-1) as arr
_{i-1 }is 1 and arr_{i-2}is not 1. - Ways(i) will be :- (max_val - 2)*ways(i-2) + (max_val-2)*ways(i-1)
- Take variables size, max_val and last_element as input.
- Function diff_val(int size, int max_val, int last_element) takes all inputs and returns the count of arrays having consecutive elements with different values.
- Take the initial count as 0.
- Take array arr[Max_N] = { 0 } storing count of ways to fill arrays. Initialize arr[0] with 0 and arr[1] with 1.
- Traverse from i=2 to i<size.
- Take temp_1 = (max_val - 2) * arr[i - 1] and temp_2 = (max_val - 1) * arr[i - 2]
- Set arr[i] = temp_1 + temp_2.
- In case last_element == 1 then set count = (max_val - 1) * arr[size - 2].
- Otherwise return arr[size - 1].
- At the end return count as result.

#include <bits/stdc++.h> using namespace std; #define Max_N 109 int diff_val(int size, int max_val, int last_element) { int count = 0; int arr[Max_N] = { 0 }; arr[0] = 0; arr[1] = 1; for (int i = 2; i < size; i++) { int temp_1 = (max_val - 2) * arr[i - 1]; int temp_2 = (max_val - 1) * arr[i - 2]; arr[i] = temp_1 + temp_2; } if (last_element == 1) { count = (max_val - 1) * arr[size - 2]; } else { return arr[size - 1]; } return count; } int main() { int size = 5; int max_val = 3; int last_element = 3; cout << "Count of arrays having consecutive element with different values are: " << diff_val(size, max_val, last_element); return 0; }

If we run the above code it will generate the following output −

Count of arrays having consecutive element with different values are: 5

- Related Questions & Answers
- Count distinct pairs from two arrays having same sum of digits in C++
- Count of only repeated element in a sorted array of consecutive elements in C++
- Count Pairs of Consecutive Zeros in C++
- Count pairs from two arrays having sum equal to K in C++
- Count all sub-arrays having sum divisible by k
- Find the element having different frequency than other array elements in C++
- Count pairs formed by distinct element sub-arrays in C++
- Maximum OR sum of sub-arrays of two different arrays in C++
- Count Subarrays with Consecutive elements differing by 1 in C++
- Count of subsequences having maximum distinct elements in C++
- Product of all pairwise consecutive elements in an Arrays in C++
- Sum of special triplets having elements from 3 arrays in C++
- Merge k sorted arrays of different sizes in C++
- Count of matrices (of different orders) with given number of elements in C++
- Plotting two different arrays of different lengths in matplotlib

Advertisements