
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find which episode we have missed to watch
Suppose we have an array A with n elements. There are n episodes of a TV series. They are numbered from 1 to n. We have watched episodes written in array A, but missed one. We have to find which episode we have missed.
Problem Category
Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can. This problem we can solve by some basic logics or brute-force approach. Follow the following contents to understand the approach better.
So, if the input of our problem is like A = [3, 8, 10, 1, 7, 9, 6, 5, 2], then the output will be 4.
Steps
To solve this, we will follow these steps −
sum := 0 n := size of A for initialize i := 0, when i < size of A, update (increase i by 1), do: sum := sum + A[i] return (n * (n + 1) / 2 - sum)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ int sum = 0; int n = A.size() + 1; for (int i = 0; i < A.size(); i++){ sum += A[i]; } return (n * (n + 1) / 2 - sum); } int main(){ vector<int> A = { 3, 8, 10, 1, 7, 9, 6, 5, 2 }; cout << solve(A) << endl; }
Input
{ 3, 8, 10, 1, 7, 9, 6, 5, 2 };
Output
4
- Related Articles
- C++ program to find minimum how much rupees we have to pay to buy exactly n liters of water
- Which clothes we have to wear for summer?
- Program to find minimum number of roads we have to make to reach any city from first one in C++
- Program to find total amount of money we have in bank in Python
- C++ code to find how long TV are on to watch a match
- C++ Program to find out how many movies an attendee can watch entirely at a Film festival
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- C++ Program to find minimum possible ugliness we can achieve of towers
- C++ Program to find tram lines we are in in different trips
- Program to find elements from list which have occurred at least k times in Python
- C program to find in which quadrant the coordinates lie.
- When we subtract a positive integer in which direction we have to move on the number line?
- C++ Program to find number of RBS string we can form bracket sequences
- Program to remove string characters which have occurred before in Python
- Program to find maximum value of k for which we can maintain safe distance in Python
