
- 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 only repetitive element between 1 to n-1 using C++
In this problem, we are given an unordered array arr[] of size N containing values from 1 to N-1 with one value occuring twice in the array. Our task is to find the only repetitive element between 1 to n-1.
Let’s take an example to understand the problem,
Input
arr[] = {3, 5, 4, 1, 2, 1}
Output
1
Solution Approach
A simple solution to the problem is traversing the array and for each value find whether the element exists somewhere else in the array. Return the value with double occurrence.
Example 1
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findRepValArr(int arr[], int n){ for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) if(arr[i] == arr[j]) return arr[i]; } int main(){ int arr[] = { 5, 3, 2, 6, 6, 1, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The repetitive value in the array is "<<findRepValArr(arr, n); return 0; }
Output
The repetitive value in the array is 6
Another approach to solve the problem is by using the fact that the repetitive value in the array is found by subtracting the sum of all integers from 1 to (N-1) from the array sum.
Sum of 1st N-1 natural number (Sn) = n*(n-1)/2
doubleVal = arrSum - (Sn)
Example 2
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findRepValArr(int arr[], int n){ int arrSum = 0; for(int i = 0; i < n; i++) arrSum += arr[i]; int sn = (((n)*(n-1))/2); return arrSum - sn; } int main(){ int arr[] = { 5, 3, 2, 6, 6, 1, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The repetitive value in the array is "<<findRepValArr(arr, n); return 0; }
Output
The repetitive value in the array is 6
- Related Articles
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Find product of prime numbers between 1 to n in C++
- Find the only repeating element in a sorted array of size n using C++
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- Program to find sum of prime numbers between 1 to n in C++
- C program to display all prime numbers between 1 to N using for loop
- Find the value of : $\frac{1}{1+a^{n-m}}+\frac{1}{1+a^{m-n}}$.
- Find the only different element in an array using C++
- Find the only element that appears b times using C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Generate an even squares between 1 to n using for loop in C Programming
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ... (K-1)n in C++
