Dados dos enteros M y N , la tarea es encontrar el número de arrays de N longitudes posibles que tengan elementos adyacentes no iguales que se encuentren en el rango [1, M] que tengan elementos en el primer y último índice iguales.
Ejemplos:
Entrada: N = 3, M = 3
Salida: 6
Explicación:
Las arrays posibles son {1, 2, 1}, {1, 3, 1}, {2, 1, 2}, {2, 3, 2}, {3, 1, 3}, {3, 2, 3}.Entrada: N = 5, M = 4
Salida: 84
Enfoque: siga los pasos a continuación para resolver el problema:
- Primero fije arr[0] y arr[N-1] igual a 1.
- Ahora encuentre el número de arreglos posibles de tamaño i que termina en 1 (es decir, arr[i] = 1 ). Almacene este resultado en end_with_one[i] .
- Ahora, encuentra el número de arreglos posibles de tamaño i que no terminen en 1 ( arr[i] ≠ 1 ). Almacene este resultado en end_not_with_one[i] .
- Dado que, el número de formas de formar la array hasta el i -ésimo índice con arr[i] = 1 , es el mismo que el número de formas de formar la array hasta el (i – 1) ésimo índice con arr[i – 1] ≠ 1 , establecer end_with_one[i] = end_not_with_one[i – 1] .
- Ahora, el número de formas de formar el arreglo hasta el i -ésimo índice con arr[i] ≠ 1 es el siguiente:
- Si arr[i – 1]= 1, hay (M – 1) números para ser colocados en el i -ésimo índice.
- Si arr[i – 1] ≠ 1, entonces (M – 2) números pueden colocarse en el índice i , ya que arr[i] no puede ser 1 y arr[i] no puede ser igual a arr[i – 1] .
- Por lo tanto, establezca end_not_with_one[i] = end_with_one[i-1] * (M – 1) + end_not_with_one[i-1]* (M – 2) .
- Por lo tanto, el número de formas de formar arreglos de tamaño N con arr[0] y arr[N – 1] igual a 1 es end_with_one[N – 1] .
- De manera similar, arr[0] y arr[N – 1] se pueden establecer en cualquier elemento de 1 a M.
- Por lo tanto, el número total de arreglos posibles es M * end_with_one[N-1] .
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ program for the above approach #include <bits/stdc++.h> 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 print the 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; # Print answer 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
<script> // Javascript program for the above approach // Function to print the count of // arrays satisfying given condition function totalArrays(N, M) { var end_with_one = Array(N+1); var end_not_with_one = Array(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 (var 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 var N = 3, M = 3; // Stores the count of arrays // where arr[0] = arr[N - 1] = 1 var temp = totalArrays(N, M); // Since arr[0] and arr[N - 1] // can be any number from 1 to M var ans = M * temp; // Print answer document.write( ans + "<br>"); </script>
Producción:
6
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por swatijha0908 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA