Open In App

Determine the final state of each bulb after processing all the elements of number[]

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
 
// Function to factorize the given number n and update the
// prime factor counts in the unordered map unmap
void solve(int n, unordered_map<int, int>& unmap)
{
    set<int> s; // Set to store prime factors
    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]++;
    }
}
 
// Function to toggle values in the array at positions that
// are multiples of n
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;
 
    // Factorize each element in vector numbers and update
    // the prime factor counts in the unordered map unmap
    for (int i : numbers) {
        solve(i, unmap);
    }
 
    // Modify the binary array based on the parity of prime
    // factor counts
    for (auto ele : unmap) {
        if (ele.second & 1) {
            change(bulbs, ele.first);
        }
    }
 
    // Output the final modified array
    for (int i : bulbs) {
        cout << i << " ";
    }
 
    return 0;
}


Java




/*code by flutterfly */
import java.util.*;
 
public class Main {
     
    // Function to factorize the given number n and update the
    // prime factor counts in the HashMap unmap
    static void solve(int n, HashMap<Integer, Integer> unmap) {
        HashSet<Integer> s = new HashSet<>(); // Set to store prime factors
        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);
        }
    }
 
    // Function to toggle values in the array at positions that
    // are multiples of n
    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<>();
 
        // Factorize each element in the vector numbers and update
        // the prime factor counts in the HashMap unmap
        for (int i : numbers) {
            solve(i, unmap);
        }
 
        // Modify the binary array based on the parity of prime
        // factor counts
        for (Map.Entry<Integer, Integer> ele : unmap.entrySet()) {
            if ((ele.getValue() & 1) == 1) {
                change(bulbs, ele.getKey());
            }
        }
 
        // Output the final modified array
        for (int i : bulbs) {
            System.out.print(i + " ");
        }
    }
}


Python3




# code by Flutterfly
from math import sqrt
 
# Function to factorize the given number n and update the
# prime factor counts in the dictionary unmap
def solve(n, unmap):
    s = set()  # Set to store prime factors
    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
 
# Function to toggle values in the array at positions that
# are multiples of n
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 = {}
 
    # Factorize each element in list numbers and update
    # the prime factor counts in the dictionary unmap
    for i in numbers:
        solve(i, unmap)
 
    # Modify the binary array based on the parity of prime
    # factor counts
    for ele in unmap.items():
        if ele[1] & 1:
            change(bulbs, ele[0])
 
    # Output the final modified array
    for i in bulbs:
        print(int(i), end=" "# Convert boolean to integer for matching format


C#




// code by flutterfly
using System;
using System.Collections.Generic;
 
class Program
{
    // Function to factorize the given number n and update the
    // prime factor counts in the dictionary unmap
    static void Solve(int n, Dictionary<int, int> unmap)
    {
        HashSet<int> s = new HashSet<int>(); // Set to store prime factors
        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;
        }
    }
 
    // Function to toggle values in the array at positions that
    // are multiples of n
    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>();
 
        // Factorize each element in list numbers and update
        // the prime factor counts in the dictionary unmap
        foreach (int i in numbers)
        {
            Solve(i, unmap);
        }
 
        // Modify the binary array based on the parity of prime
        // factor counts
        foreach (var ele in unmap)
        {
            if ((ele.Value & 1) != 0)
            {
                Change(bulbs, ele.Key);
            }
        }
 
        // Output the final modified array
        foreach (int i in bulbs)
        {
            Console.Write(i + " ");
        }
    }
}


Javascript




// code by flutterfly
// Function to factorize the given number n and update the
// prime factor counts in the map unmap
function solve(n, unmap) {
    let s = new Set(); // Set to store prime factors
    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 to toggle values in the array at positions that
// are multiples of n
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 = {};
 
// Factorize each element in array numbers and update
// the prime factor counts in the map unmap
for (let i of numbers) {
    solve(i, unmap);
}
 
// Modify the binary array based on the parity of prime
// factor counts
for (let ele in unmap) {
    if (unmap[ele] % 2 === 1) {
        change(bulbs, parseInt(ele));
    }
}
 
// Output the final modified array
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads