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

📅  最后修改于: 2021-09-03 13:40:30             🧑  作者: Mango

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

例子:

朴素的方法:解决这个问题的最简单的方法是使用变量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


Javascript


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


Javascript


输出:
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

蟒蛇3

# 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

Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live