Count of 1’s after flipping the bits at multiples from 1 to N
Last Updated :
16 Oct, 2022
Given N sized binary array A[] containing all 0’s initially. The task is to find the final count of 1’s after flipping the bits at multiples from 1 to N.
Examples:
Input: A[] = [0, 0, 0, 0]
Output: 2
Explanation:
Flipping bits at multiples of 1 – [1, 1, 1, 1]
Flipping bits at multiples of 2 – [1, 0, 1, 0]
Flipping bits at multiples of 3 – [1, 0, 0, 0]
Flipping bits at multiples of 4 – [1, 0, 0, 1]
Therefore count of 1’s after final flipping is 2.
Input: A[] = [0, 0, 0, 0, 0, 0, 0]
Output: 2
Explanation:
Flipping bits at multiples of 1 – [1, 1, 1, 1, 1, 1, 1]
Flipping bits at multiples of 2 – [1, 0, 1, 0, 1, 0, 1]
Flipping bits at multiples of 3 – [1, 0, 0, 0, 1, 1, 1]
Flipping bits at multiples of 4 – [1, 0, 0, 1, 1, 1, 1]
Flipping bits at multiples of 5 – [1, 0, 0, 1, 0, 1, 1]
Flipping bits at multiples of 6 – [1, 0, 0, 1, 0, 0, 1]
Flipping bits at multiples of 7 – [1, 0, 0, 1, 0, 0, 0]
Therefore count of 1’s after final flipping is 2
Naive Approach: The basic idea of the solution is based on greedy approach.
For each element from 1 to N, flip all the elements at its multiples. The final count of 1 in the array is the answer.
Follow the steps below to implement the approach:
- Create an array of size N and fill it with 0.
- Run a loop i = 1 to i = N, and for each i,
- run a loop j = i – 1 to N with increment = i.
- Iterate over the array and calculate number of 1.
Implementation of the above discussed approach is given below.
C++
#include <iostream>
using namespace std;
int findOnes( int N, int arr[])
{
int count = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i - 1; j < N; j += i) {
if (arr[j] == 0)
arr[j] = 1;
else
arr[j] = 0;
}
}
for ( int i = 0; i < N; i++) {
if (arr[i] == 1)
count++;
}
return count;
}
int main()
{
int N = 4;
int arr[N];
for ( int i = 0; i < N; i++)
arr[i] = 0;
int count = findOnes(N, arr);
cout << count;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findOnes( int N, int arr[])
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++) {
for ( int j = i - 1 ; j < N; j += i) {
if (arr[j] == 0 )
arr[j] = 1 ;
else
arr[j] = 0 ;
}
}
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 1 )
count++;
}
return count;
}
public static void main (String[] args) {
int N = 4 ;
int [] arr = new int [N];
for ( int i = 0 ; i < N; i++)
arr[i] = 0 ;
int count = findOnes(N, arr);
System.out.println(count);
}
}
|
Python3
def findOnes(N, arr):
count = 0
for i in range ( 1 , N + 1 ):
for j in range (i - 1 , N, i):
if (arr[j] = = 0 ):
arr[j] = 1
else :
arr[j] = 0
for i in range (N):
if (arr[i] = = 1 ):
count + = 1
return count
N = 4
arr = [ 0 ] * N
for i in range (N):
arr[i] = 0
count = findOnes(N, arr)
print (count)
|
C#
using System;
class GFG {
static int findOnes( int N, int [] arr)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i - 1; j < N; j += i) {
if (arr[j] == 0)
arr[j] = 1;
else
arr[j] = 0;
}
}
for ( int i = 0; i < N; i++) {
if (arr[i] == 1)
count++;
}
return count;
}
public static void Main()
{
int N = 4;
int [] arr = new int [N];
for ( int i = 0; i < N; i++)
arr[i] = 0;
int count = findOnes(N, arr);
Console.WriteLine(count);
}
}
|
Javascript
<script>
function findOnes(N, arr)
{
let count = 0;
for (let i = 1; i <= N; i++) {
for (let j = i - 1; j < N; j += i) {
if (arr[j] == 0)
arr[j] = 1;
else
arr[j] = 0;
}
}
for (let i = 0; i < N; i++) {
if (arr[i] == 1)
count++;
}
return count;
}
let N = 4;
let arr = new Array(N);
for (let i = 0; i < N; i++)
arr[i] = 0;
let count = findOnes(N, arr);
document.write(count);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Efficient Approach: The efficient approach to solve this problem is based on following mathematical observation:
After flipping all the bits at multiples of numbers in range 1 to N, there will be only floor(?N) 1s left.
This relation can be proved as per shown below:
Initially, all the elements are 0.
Lemma 1: At any index i, the final element will be 1 if it is flipped odd number of times. This is trivial.
Lemma 2: For any index i, the number of times element at that index will be flipped equals number of factors of that coin.
Since we flip elements at multiples of 1, 2, 3, 4, … N. For each factor of any index i, it will be flipped.
Lemma 3: From Lemma 1 and Lemma 2, all the indices having odd number of factors will have 1 as its final element .
For any natural number N, we can write it in its prime factorization form:
- N = ?a x ?b x ?c x ?d ….
where ? < ? < ? < ? …. are prime numbers and a, b, c, d are whole numbers.
- Then, total number of factors of N = (a+1) x (b+1) x (c+1) x (d+1) x …
- Hence, we want ((a+1) x (b+1) x (c+1) x (d+1) x …) to be odd.
- => ((a+1) x (b+1) x (c+1) x (d+1) x …) is odd if (a+1) x (b+1) x (c+1) x (d+1) x … is odd
=> (a+1) x (b+1) x (c+1) x (d+1) x … is odd if (a+1), (b+1), (c+1), (d+1) … is odd
=> (a+1), (b+1), (c+1), (d+1) is odd if a, b, c, d, …. is even
- Hence, N = ?a x ?b x ?c x ?d …. should has a, b, c, d, … as even whole numbers. This is only possible if N is a perfect square.
Therefore, all indices which are perfect squares will have 1 as their final element.
Number of perfect squares below N = ??N?
Implementation of the above discussed observation is given below.
C++
#include <cmath>
#include <iostream>
using namespace std;
int findOnes( int N, int arr[])
{
return floor ( sqrt (N));
}
int main()
{
int N = 4;
int arr[] = { 0, 0, 0, 0 };
int count = findOnes(N, arr);
cout << count;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int findOnes( int N, int arr[])
{
return ( int )Math.floor(( int )Math.sqrt(N));
}
public static void main(String[] args)
{
int N = 4 ;
int arr[] = new int [] { 0 , 0 , 0 , 0 };
int count = findOnes(N, arr);
System.out.print(count);
}
}
|
C#
using System;
class GFG {
static int findOnes( int N, int [] arr)
{
return ( int )(Math.Floor(Math.Sqrt(N)));
}
public static void Main()
{
int N = 4;
int [] arr = { 0, 0, 0, 0 };
int count = findOnes(N, arr);
Console.Write(count);
}
}
|
Javascript
<script>
function findOnes(N, arr)
{
return Math.floor(Math.sqrt(N));
}
let N = 4;
let arr = [ 0, 0, 0, 0 ];
let count = findOnes(N, arr);
document.write(count);
</script>
|
Python3
import math
def findOnes(N, arr):
return math.floor(math.sqrt(N))
N = 4
arr = [ 0 , 0 , 0 , 0 ]
count = findOnes(N, arr)
print (count)
|
Time Complexity: O(logN) as it is using inbuilt sqrt function
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...