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++ 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 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 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# 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 |
<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> |
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++ 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 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 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# 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 |
<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> |
2
Time Complexity: O(log2(N))
Auxiliary Space: O(1)