Related Articles

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

• Difficulty Level : Basic
• Last Updated : 05 May, 2021

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 ``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);` `    ``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

 ``
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 ``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);` `    ``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

 ``
Output:
`2`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up