Determine the final state of each bulb after processing all the elements of number[]
Last Updated :
28 Dec, 2023
Given an array of bulbs[] initially in either 1 (i.e ‘on’) or 0 (i.e ‘off’) state and a list of numbers[], where each number represents a position in the bulb[] array. For each element in number[] you have to flip the state of bulbs at positions that are multiples of the prime factors of the chosen number. The task is to determine the final state (either ‘on’ or ‘off’) of each bulb in the array after processing all the elements of number[].
Note: This problem uses 1-based indices.
Examples:
Input: bulbs = [1, 1, 0, 0, 1, 1, 0, 1, 1, 1], number[] = [3, 8, 15]
Output: 1001000011
Explanation: Initial state=[1100110111]
numbers[0] = 3, there is one prime factor: {3}. After the states are changed, affected bulbs in bold:[1110100101]
numbers[1] = 4, there is one prime factor: (2). The states of the bulbs and the affected bulbs are[1011110000]
numbers[2] = 15, the prime factors are {3, 5). The states of the bulbs and the affected bulbs are[1001100010],[1001000011]
The final states are 1001000011.
Input: bulbs = [0,1,1,0,1,1,0,1,1,1], number[] = [3, 8, 6]
Output: 0110110111
Approach: The problem can be solved using the following approach:
The approach involves factorizing given numbers into their prime components, counting the occurrences of each prime factor, and toggling values in a binary array at positions that are multiples of prime factors with odd counts.
Step-by-step approach:
- Iterate through each number in the numbers vector and factorize it.
- Store prime factor counts in the unordered map unmap.
- Iterate over all prime factor that are stored in unmap having odd frequency (only odd frequency elements would be usefull in flipping the states):
- Toggle values in the given array bulbs at positions that are multiples of number[.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int n, unordered_map< int , int >& unmap)
{
set< int > s;
while (n % 2 == 0) {
s.insert(2);
n /= 2;
}
for ( int i = 3; i <= sqrt (n); i += 2) {
while (n % i == 0) {
s.insert(i);
n /= i;
}
}
if (n > 2) {
s.insert(n);
}
for ( int i : s) {
unmap[i]++;
}
}
void change(vector< int >& bulbs, int n)
{
for ( int i = n - 1; i < bulbs.size(); i += n) {
bulbs[i] = !bulbs[i];
}
}
int main()
{
vector< int > bulbs{ 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 };
vector< int > numbers{ 3, 8, 15 };
unordered_map< int , int > unmap;
for ( int i : numbers) {
solve(i, unmap);
}
for ( auto ele : unmap) {
if (ele.second & 1) {
change(bulbs, ele.first);
}
}
for ( int i : bulbs) {
cout << i << " " ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
static void solve( int n, HashMap<Integer, Integer> unmap) {
HashSet<Integer> s = new HashSet<>();
while (n % 2 == 0 ) {
s.add( 2 );
n /= 2 ;
}
for ( int i = 3 ; i <= Math.sqrt(n); i += 2 ) {
while (n % i == 0 ) {
s.add(i);
n /= i;
}
}
if (n > 2 ) {
s.add(n);
}
for ( int i : s) {
unmap.put(i, unmap.getOrDefault(i, 0 ) + 1 );
}
}
static void change(ArrayList<Integer> bulbs, int n) {
for ( int i = n - 1 ; i < bulbs.size(); i += n) {
bulbs.set(i, 1 - bulbs.get(i));
}
}
public static void main(String[] args) {
ArrayList<Integer> bulbs = new ArrayList<>(Arrays.asList( 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 ));
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList( 3 , 8 , 15 ));
HashMap<Integer, Integer> unmap = new HashMap<>();
for ( int i : numbers) {
solve(i, unmap);
}
for (Map.Entry<Integer, Integer> ele : unmap.entrySet()) {
if ((ele.getValue() & 1 ) == 1 ) {
change(bulbs, ele.getKey());
}
}
for ( int i : bulbs) {
System.out.print(i + " " );
}
}
}
|
Python3
from math import sqrt
def solve(n, unmap):
s = set ()
while n % 2 = = 0 :
s.add( 2 )
n / / = 2
for i in range ( 3 , int (sqrt(n)) + 1 , 2 ):
while n % i = = 0 :
s.add(i)
n / / = i
if n > 2 :
s.add(n)
for i in s:
unmap[i] = unmap.get(i, 0 ) + 1
def change(bulbs, n):
for i in range (n - 1 , len (bulbs), n):
bulbs[i] = not bulbs[i]
if __name__ = = "__main__" :
bulbs = [ 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 ]
numbers = [ 3 , 8 , 15 ]
unmap = {}
for i in numbers:
solve(i, unmap)
for ele in unmap.items():
if ele[ 1 ] & 1 :
change(bulbs, ele[ 0 ])
for i in bulbs:
print ( int (i), end = " " )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Solve( int n, Dictionary< int , int > unmap)
{
HashSet< int > s = new HashSet< int >();
while (n % 2 == 0)
{
s.Add(2);
n /= 2;
}
for ( int i = 3; i <= Math.Sqrt(n); i += 2)
{
while (n % i == 0)
{
s.Add(i);
n /= i;
}
}
if (n > 2)
{
s.Add(n);
}
foreach ( int i in s)
{
if (unmap.ContainsKey(i))
unmap[i]++;
else
unmap[i] = 1;
}
}
static void Change(List< int > bulbs, int n)
{
for ( int i = n - 1; i < bulbs.Count; i += n)
{
bulbs[i] = bulbs[i] == 0 ? 1 : 0;
}
}
static void Main()
{
List< int > bulbs = new List< int > { 1, 1, 0, 0, 1, 1, 0, 1, 1, 1 };
List< int > numbers = new List< int > { 3, 8, 15 };
Dictionary< int , int > unmap = new Dictionary< int , int >();
foreach ( int i in numbers)
{
Solve(i, unmap);
}
foreach ( var ele in unmap)
{
if ((ele.Value & 1) != 0)
{
Change(bulbs, ele.Key);
}
}
foreach ( int i in bulbs)
{
Console.Write(i + " " );
}
}
}
|
Javascript
function solve(n, unmap) {
let s = new Set();
while (n % 2 === 0) {
s.add(2);
n /= 2;
}
for (let i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i === 0) {
s.add(i);
n /= i;
}
}
if (n > 2) {
s.add(n);
}
for (let i of s) {
unmap[i] = (unmap[i] || 0) + 1;
}
}
function change(bulbs, n) {
for (let i = n - 1; i < bulbs.length; i += n) {
bulbs[i] = !bulbs[i];
}
}
let bulbs = [1, 1, 0, 0, 1, 1, 0, 1, 1, 1];
let numbers = [3, 8, 15];
let unmap = {};
for (let i of numbers) {
solve(i, unmap);
}
for (let ele in unmap) {
if (unmap[ele] % 2 === 1) {
change(bulbs, parseInt(ele));
}
}
console.log(bulbs.map(Number).join( ' ' ));
|
Output
1 0 0 1 0 0 0 0 1 1
Time Complexity: O(N * sqrt(M)).
Auxiliary Space: O(sqrt(M)) due to the unordered map storing prime factors in the solve function.
Share your thoughts in the comments
Please Login to comment...