📌  相关文章
📜  计数通过执行给定操作可在数组中获得的1

📅  最后修改于: 2021-05-19 17:39:00             🧑  作者: Mango

给定大小为N的数组arr []最初仅由0 s组成,任务是计算N次执行以下操作可在数组中获得的1 s的数量。

例子:

天真的方法:解决此问题的最简单方法是使用变量i遍历[1,N]的范围并翻转索引为i的倍数的所有数组元素。最后,打印数组中存在的1秒总数的计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count total number of 1s in
// array by performing given operations
int cntOnesArrWithGivenOp(int arr[], int N)
{
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // Flip all array elements whose
        // index is multiple of i
        for (int j = i - 1; j < N;
             j += i) {
 
            // Update arr[i]
            arr[j] = !(arr[j]);
        }
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If current element is 1
        if (arr[i] == 1) {
 
            // Update cntOnes
            cntOnes += 1;
        }
    }
 
    return cntOnes;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 0, 0, 0, 0, 0 };
 
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << cntOnesArrWithGivenOp(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG
{
 
    // Function to count total number of 1s in
    // array by performing given operations
    static int cntOnesArrWithGivenOp(int arr[], int N)
    {
       
        // Stores count of 1s in the array
        // by performing the operations
        int cntOnes = 0;
 
        // Iterate over the range [1, N]
        for (int i = 1; i <= N; i++)
        {
 
            // Flip all array elements whose
            // index is multiple of i
            for (int j = i - 1; j < N; j += i)
            {
 
                // Update arr[i]
                arr[j] = arr[j] == 0 ? 1 : 0;
            }
        }
 
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
 
            // If current element is 1
            if (arr[i] == 1)
            {
 
                // Update cntOnes
                cntOnes += 1;
            }
        }
        return cntOnes;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 0, 0, 0, 0, 0 };
        int N = arr.length;
        System.out.print(cntOnesArrWithGivenOp(arr, N));
    }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to implement
# the above approach
 
# Function to count total number of 1s in
# array by performing given operations
def cntOnesArrWithGivenOp(arr, N):
     
    # Stores count of 1s in the array
    # by performing the operations
    cntOnes = 0
     
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
         
        # Flip all array elements whose
        # index is multiple of i
        for j in range(i - 1, N, i):
             
            # Update arr[i]
            arr[j] = 1 if arr[j] == 0 else 0
 
    # Traverse the array
    for i in range(N):
 
        # If current element is 1
        if (arr[i] == 1):
             
            # Update cntOnes
            cntOnes += 1
 
    return cntOnes
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 0, 0, 0, 0, 0 ]
    N = len(arr)
     
    print(cntOnesArrWithGivenOp(arr, N))
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
    // Function to count total number of 1s in
    // array by performing given operations
    static int cntOnesArrWithGivenOp(int []arr, int N)
    {
       
        // Stores count of 1s in the array
        // by performing the operations
        int cntOnes = 0;
 
        // Iterate over the range [1, N]
        for (int i = 1; i <= N; i++)
        {
 
            // Flip all array elements whose
            // index is multiple of i
            for (int j = i - 1; j < N; j += i)
            {
 
                // Update arr[i]
                arr[j] = arr[j] == 0 ? 1 : 0;
            }
        }
 
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
 
            // If current element is 1
            if (arr[i] == 1)
            {
 
                // Update cntOnes
                cntOnes += 1;
            }
        }
        return cntOnes;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int []arr = { 0, 0, 0, 0, 0 };
        int N = arr.Length;
        Console.Write(cntOnesArrWithGivenOp(arr, N));
    }
}
 
// This code contributed by shikhasingrajput


C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count total number of 1s in
// array by performing the given operations
int cntOnesArrWithGivenOp(int arr[], int N)
{
 
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Update cntOnes
    cntOnes = sqrt(N);
 
    return cntOnes;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 0, 0, 0, 0, 0 };
 
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << cntOnesArrWithGivenOp(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to count total number of 1s in
  // array by performing the given operations
  static int cntOnesArrWithGivenOp(int arr[], int N)
  {
 
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Update cntOnes
    cntOnes = (int)Math.sqrt(N);
    return cntOnes;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arr[] = { 0, 0, 0, 0, 0 };
    int N = arr.length;
    System.out.println(cntOnesArrWithGivenOp(arr, N));
  }
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
 
# Function to count total number of 1s in
# array by performing the given operations
def cntOnesArrWithGivenOp(arr, N) :
 
    # Stores count of 1s in the array
    # by performing the operations
    cntOnes = 0;
 
    # Update cntOnes
    cntOnes = int(N ** (1/2));
    return cntOnes;
 
# Driver Code
if __name__ == "__main__" :
    arr = [ 0, 0, 0, 0, 0 ];
    N = len(arr);
    print(cntOnesArrWithGivenOp(arr, N));
 
    # This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to count total number of 1s in
  // array by performing the given operations
  static int cntOnesArrWithGivenOp(int []arr, int N)
  {
 
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Update cntOnes
    cntOnes = (int)Math.Sqrt(N);
    return cntOnes;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []arr = { 0, 0, 0, 0, 0 };
    int N = arr.Length;
    Console.WriteLine(cntOnesArrWithGivenOp(arr, N));
  }
}
 
// This code is contributed by shikhasingrajput


输出:
2

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

高效方法:为了优化上述方法,该思想基于以下事实:只有完全平方包含奇数个因子。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntOnes ,通过执行操作将1的计数存储在数组中。
  • 更新cntOnes = sqrt(N)
  • 最后,打印cntOnes的值。

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count total number of 1s in
// array by performing the given operations
int cntOnesArrWithGivenOp(int arr[], int N)
{
 
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Update cntOnes
    cntOnes = sqrt(N);
 
    return cntOnes;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 0, 0, 0, 0, 0 };
 
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << cntOnesArrWithGivenOp(arr, N);
 
    return 0;
}

Java

// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to count total number of 1s in
  // array by performing the given operations
  static int cntOnesArrWithGivenOp(int arr[], int N)
  {
 
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Update cntOnes
    cntOnes = (int)Math.sqrt(N);
    return cntOnes;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arr[] = { 0, 0, 0, 0, 0 };
    int N = arr.length;
    System.out.println(cntOnesArrWithGivenOp(arr, N));
  }
}
 
// This code is contributed by susmitakundugoaldanga

Python3

# Python3 program to implement
# the above approach
 
# Function to count total number of 1s in
# array by performing the given operations
def cntOnesArrWithGivenOp(arr, N) :
 
    # Stores count of 1s in the array
    # by performing the operations
    cntOnes = 0;
 
    # Update cntOnes
    cntOnes = int(N ** (1/2));
    return cntOnes;
 
# Driver Code
if __name__ == "__main__" :
    arr = [ 0, 0, 0, 0, 0 ];
    N = len(arr);
    print(cntOnesArrWithGivenOp(arr, N));
 
    # This code is contributed by AnkThon

C#

// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to count total number of 1s in
  // array by performing the given operations
  static int cntOnesArrWithGivenOp(int []arr, int N)
  {
 
    // Stores count of 1s in the array
    // by performing the operations
    int cntOnes = 0;
 
    // Update cntOnes
    cntOnes = (int)Math.Sqrt(N);
    return cntOnes;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []arr = { 0, 0, 0, 0, 0 };
    int N = arr.Length;
    Console.WriteLine(cntOnesArrWithGivenOp(arr, N));
  }
}
 
// This code is contributed by shikhasingrajput
输出:
2

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