Categories: CodeChef's Solutions

Flipping Coins Solution with Approach – Codechef

Problem Statement

There are N coins kept on the table, numbered from 0 to N – 1. Initially, each coin is kept tails up. You have to perform two types of operations:

  1. Flip all coins numbered between A and B inclusive. This is represented by the command “0 A B”
  2. Answer how many coins numbered between A and B inclusive are heads up. This is represented by the command “1 A B”.

Input

The first line contains two integers, N and Q. Each of the next Q lines are either of the form “0 A B” or “1 A B” as mentioned above.

Related Post

Output

Output 1 line for each of the queries of the form “1 A B” containing the required answer for the corresponding query.

Sample Input/Output

Sample Input : 
4 7
1 0 3
0 1 2
1 0 1
1 0 0
0 0 3
1 0 3
1 3 3

Sample Output :
0
1
0
2
1

Constraints

1 <= N <= 100000 
1 <= Q <= 100000
0 <= A <= B <= N - 1

Solution

It is a Simple problem related to fast input/ output processing. Key to this problem is using sys.stdin module instead of the default input() function.

Approach

  1. Initialize a Numpy array with N number of zeroes.
  2. For the value of p == 0. use negation to reverse the values in the array.
  3. For p == 1, Calulate the sum and print it.

Code

import numpy as np
import sys

# Using sys.stdin.readline() since it is fast
N,M = map(int,sys.stdin.readline().split())
# Initializing the Numpy Array
L = np.zeros(N, dtype=bool)
for _ in range(M):
    p,x,y = map(int, sys.stdin.readline().split())

    if p == 0:
        L[x:y+1] = ~L[x:y+1]
    else:
        print(L[x:y+1].sum())

This code uses bitwise operators for speedy calculations. Bitwise operators can make you competitive life easy. Take our Quick Guide on bitwise operators to know more.

Aditya Kumar

Share
Tags: CodeChef Python

Recent Posts

  • Programming

Mastering Print Formatting in Python: A Comprehensive Guide

In Python, the print() function is a fundamental tool for displaying output. While printing simple…

8 months ago
  • Programming

Global Variables in Python: Understanding Usage and Best Practices

Python is a versatile programming language known for its simplicity and flexibility. When working on…

8 months ago
  • Programming

Secure Your Documents: Encrypting PDF Files Using Python

PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…

8 months ago
  • Programming

Creating and Modifying PDF Files in Python: A Comprehensive Guide with Code Examples

PDF (Portable Document Format) files are widely used for document exchange due to their consistent…

8 months ago
  • Programming

Boosting Python Performance with Cython: Optimizing Prime Number Detection

Python is a high-level programming language known for its simplicity and ease of use. However,…

8 months ago
  • Programming

Using OOP, Iterator, Generator, and Closure in Python to implement common design patterns

Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…

8 months ago

This website uses cookies.