给定两个整数M和N ,任务是找到具有不相等的相邻元素且位于范围[1,M]中且元素的首个索引和最后一个索引相等的N个长度数组的数目。
例子:
Input: N = 3, M = 3
Output: 6
Explanation:
The possible arrays are {1, 2, 1}, {1, 3, 1}, {2, 1, 2}, {2, 3, 2}, {3, 1, 3}, {3, 2, 3}.
Input: N = 5, M = 4
Output: 84
方法:请按照以下步骤解决问题:
- 首先将arr [0]和arr [N-1]固定为1。
- 现在找到大小为i且以1结尾的数组的数目(即arr [i] = 1 )。将此结果存储到end_with_one [i]中。
- 现在,找到大小可能为i且不以1结尾的数组的数目( arr [i]≠1 )。将此结果存储到end_not_with_one [i]中。
- 由于直到arr [i] = 1的第i个索引为止形成数组的方法的数量与直到arr [i-1]的第(i – 1)个索引为止形成数组的方法的数量相同≠1时,设置end_with_one [i] = end_not_with_one [i – 1] 。
- 现在,直到第一个具有arr [i]≠1的索引的数组的形成方法如下:
- 如果arr [i – 1] = 1,则将第( i – 1)个数字放置在第i个索引处。
- 如果arr [i – 1]≠1,则可以将(M – 2)个数字放在索引i处,因为arr [i]不能为1,并且arr [i]不能等于arr [i – 1] 。
- 因此,设置end_not_with_one [i] = end_with_one [i-1] *(M – 1)+ end_not_with_one [i-1] *(M – 2) 。
- 因此,形成arr [0]和arr [N – 1]等于1的大小为N的数组的方式为end_with_one [N – 1] 。
- 同样,可以将arr [0]和arr [N – 1]设置为从1到M的任何元素。
- 因此,可能的数组总数为M * end_with_one [N-1] 。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to print the count of
// arrays satisfying given condition
int totalArrays(int N, int M)
{
int end_with_one[N + 1];
int end_not_with_one[N + 1];
// First element of
// array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
// Since the first element
// of arr[] is 1, the
// second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
// Traverse the remaining indices
for (int i = 2; i < N; i++) {
// If arr[i] = 1
end_with_one[i]
= end_not_with_one[i - 1];
// If arr[i] ≠ 1
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
// Since last element needs to be 1
return end_with_one[N - 1];
}
// Driver Code
int main()
{
int N = 3, M = 3;
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
int temp = totalArrays(N, M);
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
int ans = M * temp;
// Print answer
cout << ans << "\n";
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to print the count of
// arrays satisfying given condition
static int totalArrays(int N, int M)
{
int []end_with_one = new int[N + 1];
int []end_not_with_one = new int[N + 1];
// First element of
// array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
// Since the first element
// of arr[] is 1, the
// second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
// Traverse the remaining indices
for (int i = 2; i < N; i++)
{
// If arr[i] = 1
end_with_one[i]
= end_not_with_one[i - 1];
// If arr[i] ≠ 1
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
// Since last element needs to be 1
return end_with_one[N - 1];
}
// Driver Code
public static void main(String[] args)
{
int N = 3, M = 3;
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
int temp = totalArrays(N, M);
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
int ans = M * temp;
// Print answer
System.out.print(ans+ "\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to prthe count of
# arrays satisfying given condition
def totalArrays(N, M):
end_with_one = [0] * (N + 1);
end_not_with_one = [0] * (N + 1);
# First element of
# array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
# Since the first element
# of arr is 1, the
# second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
# Traverse the remaining indices
for i in range(2, N):
# If arr[i] = 1
end_with_one[i] = end_not_with_one[i - 1];
# If arr[i] ≠ 1
end_not_with_one[i] = end_with_one[i - 1] * (M - 1) + end_not_with_one[i - 1] * (M - 2);
# Since last element needs to be 1
return end_with_one[N - 1];
# Driver Code
if __name__ == '__main__':
N = 3;
M = 3;
# Stores the count of arrays
# where arr[0] = arr[N - 1] = 1
temp = totalArrays(N, M);
# Since arr[0] and arr[N - 1]
# can be any number from 1 to M
ans = M * temp;
# Pranswer
print(ans);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to print the count of
// arrays satisfying given condition
static int totalArrays(int N, int M)
{
int[] end_with_one = new int[N + 1];
int[] end_not_with_one = new int[N + 1];
// First element of
// array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
// Since the first element
// of arr[] is 1, the
// second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
// Traverse the remaining indices
for (int i = 2; i < N; i++) {
// If arr[i] = 1
end_with_one[i]
= end_not_with_one[i - 1];
// If arr[i] ≠ 1
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
// Since last element needs to be 1
return end_with_one[N - 1];
}
// Driver code
static void Main()
{
int N = 3, M = 3;
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
int temp = totalArrays(N, M);
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
int ans = M * temp;
// Print answer
Console.WriteLine(ans);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出:
6
时间复杂度: O(N)
辅助空间: O(N)