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++
#include <bits/stdc++.h>
using namespace std;
int cntOnesArrWithGivenOp( int arr[], int N)
{
int cntOnes = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i - 1; j < N;
j += i) {
arr[j] = !(arr[j]);
}
}
for ( int i = 0; i < N; i++) {
if (arr[i] == 1) {
cntOnes += 1;
}
}
return cntOnes;
}
int main()
{
int arr[] = { 0, 0, 0, 0, 0 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << cntOnesArrWithGivenOp(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int cntOnesArrWithGivenOp( int arr[], int N)
{
int cntOnes = 0 ;
for ( int i = 1 ; i <= N; i++)
{
for ( int j = i - 1 ; j < N; j += i)
{
arr[j] = arr[j] == 0 ? 1 : 0 ;
}
}
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 1 )
{
cntOnes += 1 ;
}
}
return cntOnes;
}
public static void main(String[] args)
{
int arr[] = { 0 , 0 , 0 , 0 , 0 };
int N = arr.length;
System.out.print(cntOnesArrWithGivenOp(arr, N));
}
}
|
Python3
def cntOnesArrWithGivenOp(arr, N):
cntOnes = 0
for i in range ( 1 , N + 1 ):
for j in range (i - 1 , N, i):
arr[j] = 1 if arr[j] = = 0 else 0
for i in range (N):
if (arr[i] = = 1 ):
cntOnes + = 1
return cntOnes
if __name__ = = '__main__' :
arr = [ 0 , 0 , 0 , 0 , 0 ]
N = len (arr)
print (cntOnesArrWithGivenOp(arr, N))
|
C#
using System;
class GFG
{
static int cntOnesArrWithGivenOp( int []arr, int N)
{
int cntOnes = 0;
for ( int i = 1; i <= N; i++)
{
for ( int j = i - 1; j < N; j += i)
{
arr[j] = arr[j] == 0 ? 1 : 0;
}
}
for ( int i = 0; i < N; i++)
{
if (arr[i] == 1)
{
cntOnes += 1;
}
}
return cntOnes;
}
public static void Main(String[] args)
{
int []arr = { 0, 0, 0, 0, 0 };
int N = arr.Length;
Console.Write(cntOnesArrWithGivenOp(arr, N));
}
}
|
Javascript
<script>
function cntOnesArrWithGivenOp(arr, N)
{
let cntOnes = 0;
for (let i = 1; i <= N; i++)
{
for (let j = i - 1; j < N; j += i)
{
arr[j] = arr[j] == 0 ? 1 : 0;
}
}
for (let i = 0; i < N; i++)
{
if (arr[i] == 1)
{
cntOnes += 1;
}
}
return cntOnes;
}
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++
#include <bits/stdc++.h>
using namespace std;
int cntOnesArrWithGivenOp( int arr[], int N)
{
int cntOnes = 0;
cntOnes = sqrt (N);
return cntOnes;
}
int main()
{
int arr[] = { 0, 0, 0, 0, 0 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << cntOnesArrWithGivenOp(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int cntOnesArrWithGivenOp( int arr[], int N)
{
int cntOnes = 0 ;
cntOnes = ( int )Math.sqrt(N);
return cntOnes;
}
public static void main(String[] args)
{
int arr[] = { 0 , 0 , 0 , 0 , 0 };
int N = arr.length;
System.out.println(cntOnesArrWithGivenOp(arr, N));
}
}
|
Python3
def cntOnesArrWithGivenOp(arr, N) :
cntOnes = 0 ;
cntOnes = int (N * * ( 1 / 2 ));
return cntOnes;
if __name__ = = "__main__" :
arr = [ 0 , 0 , 0 , 0 , 0 ];
N = len (arr);
print (cntOnesArrWithGivenOp(arr, N));
|
C#
using System;
class GFG
{
static int cntOnesArrWithGivenOp( int []arr, int N)
{
int cntOnes = 0;
cntOnes = ( int )Math.Sqrt(N);
return cntOnes;
}
public static void Main(String[] args)
{
int []arr = { 0, 0, 0, 0, 0 };
int N = arr.Length;
Console.WriteLine(cntOnesArrWithGivenOp(arr, N));
}
}
|
Javascript
<script>
function cntOnesArrWithGivenOp(arr , N) {
var cntOnes = 0;
cntOnes = parseInt( Math.sqrt(N));
return cntOnes;
}
var arr = [ 0, 0, 0, 0, 0 ];
var N = arr.length;
document.write(cntOnesArrWithGivenOp(arr, N));
</script>
|
Time Complexity: O(log2(N))
Auxiliary Space: O(1)