
- 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
Find the repeating and the missing number using two equations in C++
In this problem, we are given an array arr[] of size N. It consists of integer values ranging from 1 to N. And one element x from the range is missing whereas one element y in the array occurs double. Our task is to find the repeating and the missing number using two equations.
Let’s take an example to understand the problem,
Input
arr[] = {1, 2 , 3, 3}
Output
missing = 4, double = 3
Solution Approach
A method to solve the problem is using two equations for the two values x and y. Then solve the equation to get the value for x and y.
Let’s see the equations and how to create them,
The sum of elements of the array consists of sum of first N natural number with one element extra and one missing.
arrSum = Sum(N) - x + y y - x = arrSum - sum(N)
This is equation 1.
Now, let's take square sum. Similarly,
arrSumsq = sqSum(N) - x2 + y2 (y - x)*(y + x) = arrSumSq - sqSum(N)
Using equation 1,
x + y = (arrSumSq - sqSum(N)) / (arrSum - sum(N))
Add both equations we get
y = (arrSumSq - sqSum(N)) / (arrSum - sum(N)) + (arrSum - sum(N)) / 2
Then using the value of y, we will find x using
x = y - (arrSum - sum(N))
We have formula for
sum(N) = n*(n-1)/2 sqSum(N) = n*(n+1)*(2n + 1)/ 6
arrSum is sum of all elements of array
arrSumSq is the sum of squares of all elements of the array.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; void findExtraAndMissingVal(int arr[], int n){ int sumN = (n * (n + 1)) / 2; int sqSumN = (n * (n + 1) * (2 * n + 1)) / 6; int arrSum = 0, arrSqSum = 0, i; for (i = 0; i < n; i++) { arrSum += arr[i]; arrSqSum += (arr[i]* arr[i]); } int y = (((arrSqSum - sqSumN) / (arrSum - sumN)) + sumN - arrSum) / 2; int x = arrSum - sumN + y; cout<<"The missing value from the array is "<<x; cout<<"\nThe value that occurs twice in the array is "<<y; } int main() { int arr[] = { 1, 2, 2, 3, 4 }; int n = sizeof(arr)/sizeof(arr[0]); findExtraAndMissingVal(arr, n); return 0; }
Output
The missing value from the array is 2 The value that occurs twice in the array is 5
- Related Articles
- Find the one missing number in range using C++
- Find the only missing number in a sorted array using C++
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
- Find the missing number in Arithmetic Progression in C++
- Find the missing number in Geometric Progression in C++
- Find the Number of Solution to Modular Equations using C++
- Write a program to find the first non-repeating number in an integer array using Java?
- Finding the missing number between two arrays of literals in JavaScript
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- Missing Number In Arithmetic Progression using C++
- Find first repeating character using JavaScript
- How to find missing number in A.P.?
- Find the Smallest Positive Number Missing From an Unsorted Array
- Python program to find missing and additional values in two lists?
- Java program to find missing and additional values in two lists
