📜  从 1 到 N 的倍数翻转位后 1 的计数

📅  最后修改于: 2022-05-13 01:56:05.198000             🧑  作者: Mango

从 1 到 N 的倍数翻转位后 1 的计数

给定最初包含所有 0 的N大小的二进制数组A[] 。任务是在从 1 到N的倍数翻转位后找到 1 的最终计数。

例子:

朴素方法:解决方案的基本思想是基于贪婪方法。

请按照以下步骤实施该方法:

  • 创建一个大小为N的数组并用 0 填充它。
  • 运行一个循环i = 1 到 i = N,对于每个i,
    • 运行循环j = i – 1 到 N ,增量 = i。
      • 翻转每个j的位。
  • 遍历数组并计算 1 的个数。

下面给出上述方法的实现。

C++
// C++ code to implement the approach
#include 
using namespace std;
 
// Function to calculate number of 1
// in the final array
int findOnes(int N, int arr[])
{
    int count = 0;
 
    // Loop to flip the elements
    // at multiples of i
    for (int i = 1; i <= N; i++) {
        for (int j = i - 1; j < N; j += i) {
            if (arr[j] == 0)
                arr[j] = 1;
            else
                arr[j] = 0;
        }
    }
 
    // Loop to determine 1s at final array
    for (int i = 0; i < N; i++) {
        if (arr[i] == 1)
            count++;
    }
 
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[N];
 
    for (int i = 0; i < N; i++)
        arr[i] = 0;
 
    int count = findOnes(N, arr);
    cout << count;
 
    return 0;
}


Java
// Java code to implement the approach
import java.io.*;
 
class GFG {
 
  // Function to calculate number of 1
  // in the final array
  static int findOnes(int N, int arr[])
  {
    int count = 0;
 
    // Loop to flip the elements
    // at multiples of i
    for (int i = 1; i <= N; i++) {
      for (int j = i - 1; j < N; j += i) {
        if (arr[j] == 0)
          arr[j] = 1;
        else
          arr[j] = 0;
      }
    }
 
    // Loop to determine 1s at final array
    for (int i = 0; i < N; i++) {
      if (arr[i] == 1)
        count++;
    }
 
    return count;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 4;
    int[] arr = new int[N];
 
    for (int i = 0; i < N; i++)
      arr[i] = 0;
 
    int count = findOnes(N, arr);
    System.out.println(count);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code to implement the approach
 
# Function to calculate number of 1
# in the final array
def findOnes(N, arr):
    count = 0
 
    # Loop to flip the elements
    # at multiples of i
    for i in range(1, N + 1):
        for j in range(i - 1, N, i):
            if (arr[j] == 0):
                arr[j] = 1
            else:
                arr[j] = 0
 
    # Loop to determine 1s at final array
    for i in range(N):
        if (arr[i] == 1):
            count += 1
 
    return count
 
# Driver Code
N = 4
arr = [0]*N
 
for i in range(N):
    arr[i] = 0
 
count = findOnes(N, arr)
print(count)
 
# This code is contributed by shinjanpatra


C#
// C# code to implement the approach
using System;
 
class GFG {
 
  // Function to calculate number of 1
  // in the final array
  static int findOnes(int N, int[] arr)
  {
    int count = 0;
 
    // Loop to flip the elements
    // at multiples of i
    for (int i = 1; i <= N; i++) {
      for (int j = i - 1; j < N; j += i) {
        if (arr[j] == 0)
          arr[j] = 1;
        else
          arr[j] = 0;
      }
    }
 
    // Loop to determine 1s at final array
    for (int i = 0; i < N; i++) {
      if (arr[i] == 1)
        count++;
    }
 
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 4;
    int[] arr = new int[N];
 
    for (int i = 0; i < N; i++)
      arr[i] = 0;
 
    int count = findOnes(N, arr);
    Console.WriteLine(count);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ code to implement the approach
#include 
#include 
using namespace std;
 
// Function to count number of 1's
// in final array
int findOnes(int N, int arr[])
{
    return floor(sqrt(N));
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[] = { 0, 0, 0, 0 };
 
    int count = findOnes(N, arr);
    cout << count;
 
    return 0;
}


Java
// JAVA code to implement the approach
import java.util.*;
class GFG
{
 
  // Function to count number of 1's
  // in final array
  public static int findOnes(int N, int arr[])
  {
    return (int)Math.floor((int)Math.sqrt(N));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 4;
    int arr[] = new int[] { 0, 0, 0, 0 };
 
    int count = findOnes(N, arr);
    System.out.print(count);
  }
}
 
// This code is contributed by Taranpreet


C#
// C# code to implement the approach
using System;
class GFG {
 
    // Function to count number of 1's
    // in final array
    static int findOnes(int N, int[] arr)
    {
        return (int)(Math.Floor(Math.Sqrt(N)));
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int[] arr = { 0, 0, 0, 0 };
 
        int count = findOnes(N, arr);
        Console.Write(count);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


Python3
# Python code to implement the approach
 
# Function to count number of 1's
# in final array
import math
 
 
def findOnes(N, arr):
 
    return math.floor(math.sqrt(N))
 
 
# Driver Code
N = 4
arr = [ 0, 0, 0, 0 ]
 
count = findOnes(N, arr)
print(count)
 
# This code is contributed by shinjanpatra



输出
2

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

有效方法:解决此问题的有效方法基于以下数学观察:

这个关系可以证明如下:

因此,所有完美平方的索引都将 1 作为其最终元素。

下面给出上述观察的实施。

C++

// C++ code to implement the approach
#include 
#include 
using namespace std;
 
// Function to count number of 1's
// in final array
int findOnes(int N, int arr[])
{
    return floor(sqrt(N));
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[] = { 0, 0, 0, 0 };
 
    int count = findOnes(N, arr);
    cout << count;
 
    return 0;
}

Java

// JAVA code to implement the approach
import java.util.*;
class GFG
{
 
  // Function to count number of 1's
  // in final array
  public static int findOnes(int N, int arr[])
  {
    return (int)Math.floor((int)Math.sqrt(N));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 4;
    int arr[] = new int[] { 0, 0, 0, 0 };
 
    int count = findOnes(N, arr);
    System.out.print(count);
  }
}
 
// This code is contributed by Taranpreet

C#

// C# code to implement the approach
using System;
class GFG {
 
    // Function to count number of 1's
    // in final array
    static int findOnes(int N, int[] arr)
    {
        return (int)(Math.Floor(Math.Sqrt(N)));
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int[] arr = { 0, 0, 0, 0 };
 
        int count = findOnes(N, arr);
        Console.Write(count);
    }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript


Python3

# Python code to implement the approach
 
# Function to count number of 1's
# in final array
import math
 
 
def findOnes(N, arr):
 
    return math.floor(math.sqrt(N))
 
 
# Driver Code
N = 4
arr = [ 0, 0, 0, 0 ]
 
count = findOnes(N, arr)
print(count)
 
# This code is contributed by shinjanpatra


输出
2

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