#include <iostream>
using namespace std;

// Define LED class here
class LED {
private:
    int pin;   // GPIO pin number
    int state; // LED state (0 = OFF, 1 = ON)

public:
    // Constructor: Initializes pin and sets state to 0 (OFF)
    LED(int p) {
        pin = p;
        state = 0;
    }

    // Sets the LED state to 1
    void on() {
        state = 1;
    }

    // Sets the LED state to 0
    void off() {
        state = 0;
    }

    // Flips the LED state (0 to 1 or 1 to 0)
    void toggle() {
        if (state == 1) {
            state = 0;
        } else {
            state = 1;
        }
    }

    // Returns the current LED state
    int status() {
        return state;
    }
};

int main() {
    int pin, initialState, toggleCount;
    // Reading input: pin, starting state, and how many times to toggle
    if (!(cin >> pin >> initialState >> toggleCount)) return 0;

    LED led(pin);

    // Set the initial state based on input
    if (initialState == 1)
        led.on();
    else
        led.off();

    // Perform the toggling logic
    for (int i = 0; i < toggleCount; i++) {
        led.toggle();
    }

    // Output the final state
    cout << led.status() << endl;
    
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5 1 3

Expected Output

0