Proporcione una array de enteros arr[] que consta de elementos del conjunto {0, 1} . La tarea es imprimir el número de formas en que la array se puede dividir en sub-arrays, de modo que cada sub-array contenga exactamente un 1 .
Ejemplos:
Entrada: arr[] = {1, 0, 1, 0, 1}
Salida: 4
A continuación se muestran las formas posibles:
- {1, 0}, {1, 0}, {1}
- {1}, {0, 1, 0}, {1}
- {1, 0}, {1}, {0, 1}
- {1}, {0, 1}, {0, 1}
Entrada: arr[] = {0, 0, 0}
Salida: 0
Acercarse:
- Cuando todos los elementos de la array son 0 , el resultado será cero.
- De lo contrario, entre dos adyacentes, debe haber una sola separación. Entonces, la respuesta es igual al producto de los valores pos i + 1 – pos i (para todos los pares válidos) donde pos i es la posición de i th 1 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition int countWays(int arr[], int n) { int pos[n], p = 0, i; // for loop for saving the positions of all 1s for (i = 0; i < n; i++) { if (arr[i] == 1) { pos[p] = i + 1; p++; } } // If array contains only 0s if (p == 0) return 0; int ways = 1; for (i = 0; i < p - 1; i++) { ways *= pos[i + 1] - pos[i]; } // Return the total ways return ways; } // Driver code int main() { int arr[] = { 1, 0, 1, 0, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << countWays(arr, n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition static int countWays(int arr[], int n) { int pos[] = new int[n]; int p = 0, i; // for loop for saving the // positions of all 1s for (i = 0; i < n; i++) { if (arr[i] == 1) { pos[p] = i + 1; p++; } } // If array contains only 0s if (p == 0) return 0; int ways = 1; for (i = 0; i < p - 1; i++) { ways *= pos[i + 1] - pos[i]; } // Return the total ways return ways; } // Driver code public static void main(String args[]) { int[] arr = { 1, 0, 1, 0, 1 }; int n = arr.length; System.out.println(countWays(arr, n)); } } // This code is contributed // by Akanksha Rai
Python3
# Python 3 implementation of the approach # Function to return the number of ways # the array can be divided into sub-arrays # satisfying the given condition def countWays(are, n): pos = [0 for i in range(n)] p = 0 # for loop for saving the positions # of all 1s for i in range(n): if (arr[i] == 1): pos[p] = i + 1 p += 1 # If array contains only 0s if (p == 0): return 0 ways = 1 for i in range(p - 1): ways *= pos[i + 1] - pos[i] # Return the total ways return ways # Driver code if __name__ == '__main__': arr = [1, 0, 1, 0, 1] n = len(arr) print(countWays(arr, n)) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition static int countWays(int[] arr, int n) { int[] pos = new int[n]; int p = 0, i; // for loop for saving the positions // of all 1s for (i = 0; i < n; i++) { if (arr[i] == 1) { pos[p] = i + 1; p++; } } // If array contains only 0s if (p == 0) return 0; int ways = 1; for (i = 0; i < p - 1; i++) { ways *= pos[i + 1] - pos[i]; } // Return the total ways return ways; } // Driver code public static void Main() { int[] arr = { 1, 0, 1, 0, 1 }; int n = arr.Length; Console.Write(countWays(arr, n)); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP implementation of the approach // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition function countWays($arr, $n) { $pos = array_fill(0, $n, 0); $p = 0 ; // for loop for saving the positions // of all 1s for ($i = 0; $i < $n; $i++) { if ($arr[$i] == 1) { $pos[$p] = $i + 1; $p++; } } // If array contains only 0s if ($p == 0) return 0; $ways = 1; for ($i = 0; $i < $p - 1; $i++) { $ways *= $pos[$i + 1] - $pos[$i]; } // Return the total ways return $ways; } // Driver code $arr = array(1, 0, 1, 0, 1); $n = sizeof($arr); echo countWays($arr, $n); // This code is contributed by Ryuga ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the number of ways // the array can be divided into sub-arrays // satisfying the given condition function countWays(arr, n) { var pos = new Array(n).fill(0); var p = 0, i; // for loop for saving the positions // of all 1s for (i = 0; i < n; i++) { if (arr[i] === 1) { pos[p] = i + 1; p++; } } // If array contains only 0s if (p === 0) return 0; var ways = 1; for (i = 0; i < p - 1; i++) { ways *= pos[i + 1] - pos[i]; } // Return the total ways return ways; } // Driver code var arr = [1, 0, 1, 0, 1]; var n = arr.length; document.write(countWays(arr, n)); </script>
Producción:
4
Complejidad de tiempo: O(n), donde n es el tamaño de la array dada
Espacio auxiliar: O(n), ya que se usó un espacio adicional de tamaño n