Dada una array arr[] que consta de N enteros positivos, la tarea es encontrar todos los j tales que a rr[j] = arr[i] y todos los números en el rango [min(j, i), max(j, i)] es menor o igual que arr[i] donde 1 ≤ i ≤ N .
Ejemplos:
Entrada: arr[] = {4, 7, 7, 9, 7}, N = 5
Salida: 1 2 2 1 1
Explicación:
Para i = 1, j = 1 es el único elemento tal que arr[i] = arr [j] y ningún elemento en el medio tiene un valor mayor que arr[1].
Para i = 2, j = 2 y 3 son los elementos tales que arr[i] = arr[j] y ningún elemento en el medio tiene un valor mayor que arr[2].
Para i = 3, j = 2 y 3 son los elementos tales que arr[i] = arr[j] y ningún elemento en el medio tiene un valor mayor que arr[3].
Para i = 4, j = 4 es el único elemento tal que arr[i] = arr[j] y ningún elemento en el medio tiene un valor mayor que arr[4].
Para i = 5, j = 5 es el único elemento tal que arr[i] = arr[j] y ningún elemento en el medio tiene un valor mayor que arr[5].Entrada: arr[] = {1, 2, 1, 2, 4}, N = 5
Salida: 1 2 1 2 1
Enfoque: la forma más sencilla de resolver el problema es usar dos bucles for anidados para recorrer la array y encontrar los pares tales que arr[i] = arr[j] y ningún elemento en el rango [i, j] es mayor que arr [yo] . Siga los pasos a continuación para resolver el problema:
- Inicialice una array , digamos ans[] que almacena la respuesta para todos los elementos en el rango [0, N-1] .
- Iterar en el rango [N-1, 0] usando la variable i y realizar los siguientes pasos:
- Iterar en el rango [i, 0] usando la variable j y realizar los siguientes pasos:
- Si arr[j] = arr[i] , aumente el valor de ans[i] en 1 .
- Si arr[j] > arr[i] , termine el ciclo.
- Iterar en el rango [i+1, N-1] usando la variable j y realizar los siguientes pasos:
- Si arr[j] = arr[i] , aumente el valor de ans[i] en 1 .
- Si arr[j] > arr[i] , termine el ciclo.
- Iterar en el rango [i, 0] usando la variable j y realizar los siguientes pasos:
- Después de completar los pasos anteriores, imprima la array ans[] como respuesta.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] void findElements(int arr[], int N) { // To Store answer int ans[N]; // Initialize the value of ans[i] as 0 for (int i = 0; i < N; i++) { ans[i] = 0; } // Traverse the array in reverse direction for (int i = N - 1; i >= 0; i--) { // Traverse in the range [i, 0] for (int j = i; j >= 0; j--) { if (arr[j] == arr[i]) { // Increment ans[i] if arr[j] =arr[i] ans[i]++; } else // Terminate the loop if (arr[j] > arr[i]) break; } // Traverse in the range [i+1, N-1] for (int j = i + 1; j < N; j++) { // Increment ans[i] if arr[i] = arr[j] if (arr[j] == arr[i]) ans[i]++; else if (arr[j] > arr[i]) { break; } } } for (int i = 0; i < N; i++) { cout << ans[i] << " "; } } // Driver Code int main() { // Given Input int arr[] = { 1, 2, 1, 2, 4 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call findElements(arr, N); }
Java
// Java program for the above approach public class GFG { // Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] static void findElements(int arr[], int N) { // To Store answer int ans[] = new int[N]; // Initialize the value of ans[i] as 0 for (int i = 0; i < N; i++) { ans[i] = 0; } // Traverse the array in reverse direction for (int i = N - 1; i >= 0; i--) { // Traverse in the range [i, 0] for (int j = i; j >= 0; j--) { if (arr[j] == arr[i]) { // Increment ans[i] if arr[j] =arr[i] ans[i]++; } else // Terminate the loop if (arr[j] > arr[i]) break; } // Traverse in the range [i+1, N-1] for (int j = i + 1; j < N; j++) { // Increment ans[i] if arr[i] = arr[j] if (arr[j] == arr[i]) ans[i]++; else if (arr[j] > arr[i]) { break; } } } for (int i = 0; i < N; i++) { System.out.print(ans[i] + " "); } } // Driver code public static void main(String[] args) { // Given Input int arr[] = { 1, 2, 1, 2, 4 }; int N = arr.length; // Function Call findElements(arr, N); } } // This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach # Function to find j such that arr[j]=arr[i] # and no element is greater than arr[i] in the # range [j, i] def findElements(arr, N): # Initialising a list to store ans # Initialize the value of ans[i] as 0 ans = [0 for i in range(N)] # Traverse the array in reverse direction for i in range(N - 1, -1, -1): # Traverse in the range [i, 0] for j in range(i, -1, -1): if arr[j] == arr[i]: # Increment ans[i] if arr[j] = arr[i] ans[i] += 1 else: # Terminate the loop if arr[j] > arr[i]: break # Traverse in the range [i+1, N-1] for j in range(i+1, N): # Increment ans[i] if arr[j] = arr[i] if arr[j] == arr[i]: ans[i] += 1 else: if arr[j] > arr[i]: break # Print the ans for i in range(N): print(ans[i], end = " ") return # Driver Code if __name__ == '__main__': # Given Input arr = [ 1, 2, 1, 2, 4 ] N = len(arr) # Function call findElements(arr, N) # This code is contributed by MuskanKalra1
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] static void findElements(int []arr, int N) { // To Store answer int []ans = new int[N]; // Initialize the value of ans[i] as 0 for(int i = 0; i < N; i++) { ans[i] = 0; } // Traverse the array in reverse direction for(int i = N - 1; i >= 0; i--) { // Traverse in the range [i, 0] for(int j = i; j >= 0; j--) { if (arr[j] == arr[i]) { // Increment ans[i] if arr[j] =arr[i] ans[i]++; } else // Terminate the loop if (arr[j] > arr[i]) break; } // Traverse in the range [i+1, N-1] for(int j = i + 1; j < N; j++) { // Increment ans[i] if arr[i] = arr[j] if (arr[j] == arr[i]) ans[i]++; else if (arr[j] > arr[i]) { break; } } } for(int i = 0; i < N; i++) { Console.Write(ans[i] + " "); } } // Driver Code public static void Main() { // Given Input int []arr = { 1, 2, 1, 2, 4 }; int N = arr.Length; // Function Call findElements(arr, N); } } // This code is contributed by SURENDRA_GANGWAR
Javascript
<script> // JavaScript program for the above approach // Function to find j such that arr[j]=arr[i] // and no element is greater than arr[i] in the // range [j, i] function findElements(arr, N) { // To Store answer let ans = new Array(N); // Initialize the value of ans[i] as 0 for (let i = 0; i < N; i++) { ans[i] = 0; } // Traverse the array in reverse direction for (let i = N - 1; i >= 0; i--) { // Traverse in the range [i, 0] for (let j = i; j >= 0; j--) { if (arr[j] == arr[i]) { // Increment ans[i] if arr[j] =arr[i] ans[i]++; } else // Terminate the loop if (arr[j] > arr[i]) break; } // Traverse in the range [i+1, N-1] for (let j = i + 1; j < N; j++) { // Increment ans[i] if arr[i] = arr[j] if (arr[j] == arr[i]) ans[i]++; else if (arr[j] > arr[i]) { break; } } } for (let i = 0; i < N; i++) { document.write(ans[i] + " "); } } // Driver Code // Given Input let arr = [1, 2, 1, 2, 4]; let N = arr.length // Function Call findElements(arr, N); </script>
1 2 1 2 1
Tiempo Complejidad: O(N 2 )
Espacio Auxiliar: O(N)