📌  相关文章
📜  N次移动后计算数组中1的个数

📅  最后修改于: 2021-06-26 16:22:37             🧑  作者: Mango

给定大小为N的数组,其中最初所有元素均为0(零)。任务是在数组上执行N次移动后,计算数组中1的个数,如下所述:
在每次移动(从1开始到N)中,位于移动次数倍数位置的元素从0变为1或从1变为0。
移动1 :在1、2、3,…的位置更改元素
移动2 :在2、4、6,…位置更改元素
移动3 :在3、6、9,…位置更改元素
执行N次移动后计算值为1的元素。

注意:考虑该数组是1索引的。

天真的方法:重复移动的次数,每次移动都将元素从“移动编号”遍历到N,并检查位置是否为移动编号的倍数。如果是移动次数的倍数,则在该位置更改元素,即,如果为0,则将其更改为1,如果为1,则将其更改为0。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include 
 
using namespace std;
 
// Function to count number of 1's in the
// array after performing N moves
int countOnes(int arr[], int N)
{
    for (int i = 1; i <= N; i++) {
        for (int j = i; j <= N; j++) {
 
            // If index is multiple of move number
            if (j % i == 0) {
                if (arr[j - 1] == 0)
                    arr[j - 1] = 1; // Convert 0 to 1
                else
                    arr[j - 1] = 0; // Convert 1 to 0
            }
        }
    }
 
    int count = 0;
 
    // Count number of 1's
    for (int i = 0; i < N; i++)
        if (arr[i] == 1)
            count++; // count number of 1's
 
    return count;
}
 
// Driver Code
int main()
{
    int N = 10; // Initialize array size
 
    // Initialize all elements to 0
    int arr[10] = { 0 };
 
    int ans = countOnes(arr, N);
 
    cout << ans;
 
    return 0;
}


Java
// Java implementation of the above approach
 
class GFG
{
 
    // Function to count number of 1's in the
    // array after performing N moves
    static int countOnes(int arr[], int N)
    {
        for (int i = 1; i <= N; i++)
        {
            for (int j = i; j <= N; j++)
            {
 
                // If index is multiple of move number
                if (j % i == 0)
                {
                    if (arr[j - 1] == 0)
                    {
                        arr[j - 1] = 1; // Convert 0 to 1
                    }
                    else
                    {
                        arr[j - 1] = 0; // Convert 1 to 0
                    }
                }
            }
        }
 
        int count = 0;
 
        // Count number of 1's
        for (int i = 0; i < N; i++)
        {
            if (arr[i] == 1)
            {
                count++; // count number of 1's
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 10; // Initialize array size
 
        // Initialize all elements to 0
        int arr[] = new int[10];
 
        int ans = countOnes(arr, N);
 
        System.out.println(ans);
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the above approach
 
# Function to count number of 1's in the
# array after performing N moves
def countOnes(arr, N):
    for i in range(1, N + 1, 1):
        for j in range(i, N + 1, 1):
            # If index is multiple of move number
            if (j % i == 0):
                if (arr[j - 1] == 0):
                    arr[j - 1] = 1 # Convert 0 to 1
                else:
                    arr[j - 1] = 0 # Convert 1 to 0
 
    count = 0
 
    # Count number of 1's
    for i in range(N):
        if (arr[i] == 1):
            count += 1 # count number of 1's
 
    return count
 
# Driver Code
if __name__ == '__main__':
    N = 10 # Initialize array size
 
    # Initialize all elements to 0
    arr = [0 for i in range(10)]
 
    ans = countOnes(arr, N)
 
    print(ans)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
     
class GFG
{
 
    // Function to count number of 1's in the
    // array after performing N moves
    static int countOnes(int []arr, int N)
    {
        for (int i = 1; i <= N; i++)
        {
            for (int j = i; j <= N; j++)
            {
 
                // If index is multiple of move number
                if (j % i == 0)
                {
                    if (arr[j - 1] == 0)
                    {
                        arr[j - 1] = 1; // Convert 0 to 1
                    }
                    else
                    {
                        arr[j - 1] = 0; // Convert 1 to 0
                    }
                }
            }
        }
 
        int count = 0;
 
        // Count number of 1's
        for (int i = 0; i < N; i++)
        {
            if (arr[i] == 1)
            {
                count++; // count number of 1's
            }
        }
        return count;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 10; // Initialize array size
 
        // Initialize all elements to 0
        int []arr = new int[10];
 
        int ans = countOnes(arr, N);
 
        Console.WriteLine(ans);
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


C++
// C++ implementation of the above approach
 
#include 
 
using namespace std;
 
// Function to count number of perfect squres
int perfectSquares(int a, int b)
{
    // Counting number of perfect squares
    // between a and b
    return (floor(sqrt(b)) - ceil(sqrt(a)) + 1);
}
 
// Function to count number of 1s in
// array after N moves
int countOnes(int arr[], int n)
{
    return perfectSquares(1, n);
}
 
// Driver Code
int main()
{
    // Initialize array size
    int N = 10;
 
    // Initialize all elements to 0
    int arr[10] = { 0 };
 
    cout << countOnes(arr, N);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG {
 
    // Function to count number of perfect squres
    static double perfectSquares(int a, int b)
    {
        // Counting number of perfect squares
        // between a and b
        return (Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1);
    }
 
    // Function to count number of 1s in
    // array after N moves
    static double countOnes(int arr[], int n)
    {
        return perfectSquares(1, n);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Initialize array size
        int N = 10;
 
        // Initialize all elements to 0
        int arr[] = { 0 };
 
        System.out.println(countOnes(arr, N));
    }
}
 
// This code is contributed by jit_t.


Python3
# Python3 implementation of the above approach
from math import sqrt, ceil, floor;
 
# Function to count number of perfect squres
def perfectSquares(a, b) :
     
    # Counting number of perfect squares
    # between a and b
    return (floor(sqrt(b)) -
             ceil(sqrt(a)) + 1);
 
# Function to count number of 1s in
# array after N moves
def countOnes(arr, n) :
 
    return perfectSquares(1, n);
 
# Driver Code
if __name__ == "__main__" :
 
    # Initialize array size
    N = 10;
 
    # Initialize all elements to 0
    arr = [0] * 10;
 
    print(countOnes(arr, N));
 
# This code is contributed by Ankit Rai


C#
// C# implementation of the above approach
using System;
 
class GFG {
 
    // Function to count number of perfect squres
    static double perfectSquares(int a, int b)
    {
        // Counting number of perfect squares
        // between a and b
        return (Math.Floor(Math.Sqrt(b)) - Math.Ceiling(Math.Sqrt(a)) + 1);
    }
 
    // Function to count number of 1s in
    // array after N moves
    static double countOnes(int[] arr, int n)
    {
        return perfectSquares(1, n);
    }
 
    // Driver Code
    static public void Main()
    {
        // Initialize array size
        int N = 10;
 
        // Initialize all elements to 0
        int[] arr = { 0 };
 
        Console.WriteLine(countOnes(arr, N));
    }
}
 
// This code is contributed by JitSalal.


PHP


Javascript


输出:
3

时间复杂度: O(N 2 )

高效的方法:高效的方法是基于贪婪的方法。它基本上是基于以下模式。
当我们对N = 1、2、3、4、5进行此操作时,发现所需的答案是从1到n(包括两个端点)的完全平方的总数。
因此,答案=理想平方的总数(从1到N)

下面是上述方法的实现:

C++

// C++ implementation of the above approach
 
#include 
 
using namespace std;
 
// Function to count number of perfect squres
int perfectSquares(int a, int b)
{
    // Counting number of perfect squares
    // between a and b
    return (floor(sqrt(b)) - ceil(sqrt(a)) + 1);
}
 
// Function to count number of 1s in
// array after N moves
int countOnes(int arr[], int n)
{
    return perfectSquares(1, n);
}
 
// Driver Code
int main()
{
    // Initialize array size
    int N = 10;
 
    // Initialize all elements to 0
    int arr[10] = { 0 };
 
    cout << countOnes(arr, N);
 
    return 0;
}

Java

// Java implementation of the above approach
import java.io.*;
 
class GFG {
 
    // Function to count number of perfect squres
    static double perfectSquares(int a, int b)
    {
        // Counting number of perfect squares
        // between a and b
        return (Math.floor(Math.sqrt(b)) - Math.ceil(Math.sqrt(a)) + 1);
    }
 
    // Function to count number of 1s in
    // array after N moves
    static double countOnes(int arr[], int n)
    {
        return perfectSquares(1, n);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Initialize array size
        int N = 10;
 
        // Initialize all elements to 0
        int arr[] = { 0 };
 
        System.out.println(countOnes(arr, N));
    }
}
 
// This code is contributed by jit_t.

Python3

# Python3 implementation of the above approach
from math import sqrt, ceil, floor;
 
# Function to count number of perfect squres
def perfectSquares(a, b) :
     
    # Counting number of perfect squares
    # between a and b
    return (floor(sqrt(b)) -
             ceil(sqrt(a)) + 1);
 
# Function to count number of 1s in
# array after N moves
def countOnes(arr, n) :
 
    return perfectSquares(1, n);
 
# Driver Code
if __name__ == "__main__" :
 
    # Initialize array size
    N = 10;
 
    # Initialize all elements to 0
    arr = [0] * 10;
 
    print(countOnes(arr, N));
 
# This code is contributed by Ankit Rai

C#

// C# implementation of the above approach
using System;
 
class GFG {
 
    // Function to count number of perfect squres
    static double perfectSquares(int a, int b)
    {
        // Counting number of perfect squares
        // between a and b
        return (Math.Floor(Math.Sqrt(b)) - Math.Ceiling(Math.Sqrt(a)) + 1);
    }
 
    // Function to count number of 1s in
    // array after N moves
    static double countOnes(int[] arr, int n)
    {
        return perfectSquares(1, n);
    }
 
    // Driver Code
    static public void Main()
    {
        // Initialize array size
        int N = 10;
 
        // Initialize all elements to 0
        int[] arr = { 0 };
 
        Console.WriteLine(countOnes(arr, N));
    }
}
 
// This code is contributed by JitSalal.

的PHP


Java脚本


输出:
3

时间复杂度: O(log(log N))

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。