- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
To Continue Learning Please Login
Login with Google