Week 2: Assignment 2 - Question 3
Due on 2024-02-08, 23:59 IST
Count the Number of 0's Between the First and Last 1
You are given a binary sequence.
Write a C program to count the number of 0's between the first and last 1 in the sequence.
Write a C program to count the number of 0's between the first and last 1 in the sequence.
Input
A sequence of bits (0's and 1's) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
The number of 0's Between the First and Last 1 in the sequence.
Note : Make no assumptions about the data in the sequence.
For instance if there is no starting and ending 1 ( say the sequence is all 0), you have to output 0.
Sample Inputs and Outputs
Sample input 1
--------------------
Sample Output 1
----------------------
3
Explanation
----------------
The sequence between first and last 1 is 1 0 0 1 1 0 1 and the sequence has 3 zeroes in between.
Sample input 2
--------------------
Sample Output 2
----------------------
0
Explanation
----------------
The sequence between first and last 1 is 1 1 1 1 1 1 and the sequence has 0 zeroes in between.
Sample input 3
--------------------
Sample Output 1
----------------------
0
Explanation
----------------
The sequence has a single 1, therefore the output (by problem description) is 0.
For instance if there is no starting and ending 1 ( say the sequence is all 0), you have to output 0.
Sample Inputs and Outputs
Sample input 1
--------------------
0 1 0 0 1 1 0 1 0 0 -1
Sample Output 1
----------------------
3
Explanation
----------------
The sequence between first and last 1 is 1 0 0 1 1 0 1 and the sequence has 3 zeroes in between.
Sample input 2
--------------------
0 0 0 1 1 1 1 1 1 0 0 -1
Sample Output 2
----------------------
0
Explanation
----------------
The sequence between first and last 1 is 1 1 1 1 1 1 and the sequence has 0 zeroes in between.
Sample input 3
--------------------
0 0 1 0 0 0 -1
Sample Output 1
----------------------
0
Explanation
----------------
The sequence has a single 1, therefore the output (by problem description) is 0.
Input | Output | |
Test Case 1 | 0 1 0 0 1 1 0 1 0 0 -1 | 3 |
Test Case 2 | 0 0 0 1 1 1 1 1 1 0 0 -1 | 0 |
Test Case 3 | 0 0 0 1 0 1 1 0 1 0 0 -1 | 2 |
Test Case 4 | 0 0 1 0 0 0 -1 | 0 |
NPTEL 2024 Introduction to programming in C Week 2
netaji gandi
Thursday, February 8, 2024