Open In App
Related Articles

Counts 1s that can be obtained in an Array by performing given operations

Improve Article
Improve
Save Article
Save
Like Article
Like

Given an array arr[] of size N consisting of only of 0s initially, the task is to count the number of 1s that can be obtained in the array by performing the following operation N times.

In i th operation, flip all the array elements whose index ( 1-based indexing ) is a multiple of i.

Examples:

Input: arr[] = { 0, 0, 0, 0, 0 } 
Output:
Explanation: 
Flipping array elements whose index is multiple of 1 modifies arr[] to { 1, 1, 1, 1, 1 } 
Flipping array elements whose index is multiple of 2 modifies arr[] to { 1, 0, 1, 0, 1 } 
Flipping array elements whose index is multiple of 3 modifies arr[] to { 1, 0, 0, 0, 1 } 
Flipping array elements whose index is multiple of 4 modifies arr[] to { 1, 0, 0, 1, 1 } 
Flipping array elements whose index is multiple of 5 modifies arr[] to { 1, 0, 0, 1, 0 } 
Therefore, the required output is 2.

Input: arr[] = { 0, 0 } 
Output:
 

Naive Approach: The simplest approach to solve this problem is to iterate over the range [1, N] using variable i and flip all the array elements whose index is a multiple of i. Finally, print the count of total number of 1s present in the array.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
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
import java.io.*;
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




<script>
 
// javascript program for the above approach
 
    // Function to count total number of 1s in
    // array by performing given operations
    function cntOnesArrWithGivenOp(arr, N)
    {
       
        // Stores count of 1s in the array
        // by performing the operations
        let cntOnes = 0;
 
        // Iterate over the range [1, N]
        for (let i = 1; i <= N; i++)
        {
 
            // Flip all array elements whose
            // index is multiple of i
            for (let j = i - 1; j < N; j += i)
            {
 
                // Update arr[i]
                arr[j] = arr[j] == 0 ? 1 : 0;
            }
        }
 
        // Traverse the array
        for (let i = 0; i < N; i++)
        {
 
            // If current element is 1
            if (arr[i] == 1)
            {
 
                // Update cntOnes
                cntOnes += 1;
            }
        }
        return cntOnes;
    }
 
// Driver Code
     
        let arr = [ 0, 0, 0, 0, 0 ];
        let N = arr.length;
        document.write(cntOnesArrWithGivenOp(arr, N));
     
</script>


Output: 

2

 

Time Complexity: O(N2) 
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is based on the fact that only perfect squares contain odd number of factors. Follow the steps below to solve the problem:

  • Initialize a variable, say cntOnes, to store the count of 1s in the array by performing the operations.
  • Update cntOnes = sqrt(N)
  • Finally, print the value of cntOnes.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
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.io.*;
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




<script>
// javascript program to implement
// the above approach
 
    // Function to count total number of 1s in
    // array by performing the given operations
    function cntOnesArrWithGivenOp(arr , N) {
 
        // Stores count of 1s in the array
        // by performing the operations
        var cntOnes = 0;
 
        // Update cntOnes
        cntOnes = parseInt( Math.sqrt(N));
        return cntOnes;
    }
 
    // Driver code
     
        var arr = [ 0, 0, 0, 0, 0 ];
        var N = arr.length;
        document.write(cntOnesArrWithGivenOp(arr, N));
 
// This code contributed by Rajput-Ji
</script>


Output: 

2

 

Time Complexity: O(log2(N)) 
Auxiliary Space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 13 Dec, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials