📌  相关文章
📜  计算由第一个和最后一个元素相等的不同连续元素组成的所有N长度数组

📅  最后修改于: 2021-05-14 08:21:35             🧑  作者: Mango

给定两个整数MN ,任务是找到具有不相等的相邻元素的,位于[1,M]范围内的元素的第一个索引和最后一个索引相等的N个长度数组的数目。

例子:

方法:请按照以下步骤解决问题:

  • 首先将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]
  • 现在,形成数组直到第i具有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]设置为从1M的任何元素。
  • 因此,可能的数组总数为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


输出:
6

时间复杂度: O(N)
辅助空间: O(N)