Counts 1s that can be obtained in an Array by performing given operations
Last Updated :
23 Jul, 2025
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: 2
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: 1
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>
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>
Time Complexity: O(log2(N))
Auxiliary Space: O(1)
Similar Reads
Count 1s present in a range of indices [L, R] in a given array Given an array arr[] consisting of a single element N (1 ⤠N ⤠106) and two integers L and R, ( 1 ⤠L ⤠R ⤠105), the task is to make all array elements either 0 or 1 using the following operations : Select an element P such that P > 1 from the array arr[].Replace P with three elements at the sam
10 min read
Number of array elements derivable from D after performing certain operations Given an array of N integers and 3 integers D, A and B. The task is to find the number of array elements that we can convert D into by performing the following operations on D: Add A (+A)Subtract A (-A)Add B (+B)Subtract B (-B) Note: It is allowed to perform any number of operations of any type. Exa
10 min read
Number of array elements derivable from D after performing certain operations Given an array of N integers and 3 integers D, A and B. The task is to find the number of array elements that we can convert D into by performing the following operations on D: Add A (+A)Subtract A (-A)Add B (+B)Subtract B (-B) Note: It is allowed to perform any number of operations of any type. Exa
10 min read
Number of array elements derivable from D after performing certain operations Given an array of N integers and 3 integers D, A and B. The task is to find the number of array elements that we can convert D into by performing the following operations on D: Add A (+A)Subtract A (-A)Add B (+B)Subtract B (-B) Note: It is allowed to perform any number of operations of any type. Exa
10 min read
Count pairs in given Array having sum of index and value at that index equal Given an array arr[] containing positive integers, count the total number of pairs for which arr[i]+i = arr[j]+j such that 0â¤i<jâ¤n-1. Examples: Input: arr[] = { 6, 1, 4, 3 }Output: 3Explanation: The elements at index 0, 2, 3 has same value of a[i]+i as all sum to 6 {(6+0), (4+2), (3+3)}. Input: a
8 min read
Count ways of creating Binary Array ending with 1 using Binary operators Given an array of characters arr[] of size N, whose each character is either '&' (Bitwise AND) or '|' (Bitwise OR). there are many possible ways of having a binary array of size N+1. Any of these arrays (say X[]) is transformed to another array Y[] by performing the following operations Y[0] = X
15+ min read