Número de 0 y 1 en posiciones principales en la array dada

Dada una array arr[] de tamaño  N  donde cada elemento es  0  o  1 . La tarea es encontrar el conteo de 0s y 1s que están en índices primos.

Ejemplos:  

Entrada: arr[] = {1, 0, 1, 0, 1} 
Salida: 
Número de 0 = 1 
Número de 1 = 1

Entrada: arr[] = {1, 0, 1, 1} 
Salida: 
Número de 0 = 0 
Número de 1 = 2  

Enfoque: recorra la array y, por cada 0 encontrado, actualice la cuenta de 0 si el índice actual es primo y actualice la cuenta de 1 para todos los 1 que están en índices primos.

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 that returns true
// if n is prime
bool isPrime(int n)
{
    if (n <= 1)
        return false;
         
    // Check from 2 to n
    for(int i = 2; i < n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Function to find the count
// of 0s and 1s at prime indices
void countPrimePosition(int arr[], int n)
{
     
    // To store the count of 0s and 1s
    int c0 = 0, c1 = 0;
    for(int i = 0; i < n; i++)
    {
         
        // If current 0 is at
        // prime position
        if (arr[i] == 0 && isPrime(i))
            c0++;
             
        // If current 1 is at
        // prime position
        if (arr[i] == 1 && isPrime(i))
            c1++;
    }
    cout << "Number of 0s = " << c0 << endl;
    cout << "Number of 1s = " << c1;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 0, 1, 0, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
     
    countPrimePosition(arr, n);
    return 0;
}
 
// This code is contributed by noob2000

Java

// Java implementation of the approach
class GFG {
 
    // Function that returns true
    // if n is prime
    static boolean isPrime(int n)
    {
        if (n <= 1)
            return false;
 
        // Check from 2 to n
        for (int i = 2; i < n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
 
    // Function to find the count
    // of 0s and 1s at prime indices
    static void countPrimePosition(int arr[])
    {
 
        // To store the count of 0s and 1s
        int c0 = 0, c1 = 0;
        int n = arr.length;
        for (int i = 0; i < n; i++) {
 
            // If current 0 is at
            // prime position
            if (arr[i] == 0 && isPrime(i))
                c0++;
 
            // If current 1 is at
            // prime position
            if (arr[i] == 1 && isPrime(i))
                c1++;
        }
        System.out.println("Number of 0s = " + c0);
        System.out.println("Number of 1s = " + c1);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 1, 0, 1, 0, 1 };
        countPrimePosition(arr);
    }
}

Python3

# Python3 implementation of the approach
 
# Function that returns true
# if n is prime
def isPrime(n) :
     
    if (n <= 1) :
        return False;
 
    # Check from 2 to n
    for i in range(2, n) :
        if (n % i == 0) :
            return False;
         
    return True;
 
# Function to find the count
# of 0s and 1s at prime indices
def countPrimePosition(arr) :
 
    # To store the count of 0s and 1s
    c0 = 0; c1 = 0;
    n = len(arr);
     
    for i in range(n) :
     
        # If current 0 is at
        # prime position
        if (arr[i] == 0 and isPrime(i)) :
            c0 += 1;
     
        # If current 1 is at
        # prime position
        if (arr[i] == 1 and isPrime(i)) :
            c1 += 1;
             
    print("Number of 0s =", c0);
    print("Number of 1s =", c1);
         
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 0, 1, 0, 1 ];
    countPrimePosition(arr);
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function that returns true
    // if n is prime
    static bool isPrime(int n)
    {
        if (n <= 1)
            return false;
 
        // Check from 2 to n
        for (int i = 2; i < n; i++)
        {
            if ((n % i) == 0)
                return false;
        }
        return true;
    }
 
    // Function to find the count
    // of 0s and 1s at prime indices
    static void countPrimePosition(int []arr)
    {
 
        // To store the count of 0s and 1s
        int c0 = 0, c1 = 0;
        int n = arr.Length;
        for (int i = 0; i < n; i++)
        {
 
            // If current 0 is at
            // prime position
            if ((arr[i] == 0) && (isPrime(i)))
                c0++;
 
            // If current 1 is at
            // prime position
            if ((arr[i] == 1) && (isPrime(i)))
                c1++;
        }
        Console.WriteLine("Number of 0s = " + c0);
        Console.WriteLine("Number of 1s = " + c1);
    }
 
    // Driver code
    static public void Main ()
    {
         
        int[] arr = { 1, 0, 1, 0, 1 };
        countPrimePosition(arr);
    }
}
 
// This code is contributed by ajit.

Javascript

<script>
// Javascript implementation of the approach
 
// Function that returns true
// if n is prime
function isPrime(n) {
    if (n <= 1)
        return false;
 
    // Check from 2 to n
    for (let i = 2; i < n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}
 
// Function to find the count
// of 0s and 1s at prime indices
function countPrimePosition(arr, n) {
 
    // To store the count of 0s and 1s
    let c0 = 0, c1 = 0;
 
    for (let i = 0; i < n; i++) {
 
        // If current 0 is at
        // prime position
        if (arr[i] == 0 && isPrime(i))
            c0++;
 
        // If current 1 is at
        // prime position
        if (arr[i] == 1 && isPrime(i))
            c1++;
    }
    document.write("Number of 0s = " + c0 + "<br>");
    document.write("Number of 1s = " + c1);
}
 
// Driver code
let arr = [1, 0, 1, 0, 1];
 
let n = arr.length;
countPrimePosition(arr, n);
 
// This code is contributed by _saurabh_jaiswal
</script>
Producción: 

Number of 0s = 1
Number of 1s = 1

 

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por facebookruppal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *