Find the two numbers with odd occurrences in an unsorted array
Last Updated :
10 Mar, 2025
Given an unsorted array that contains even number of occurrences for all numbers except two numbers. The task is to find the two numbers which have odd occurrences in O(n) time complexity and O(1) extra space.
Examples:
Input: {12, 23, 34, 12, 12, 23, 12, 45}
Output: 34 45
Input: {4, 4, 100, 5000, 4, 4, 4, 4, 100, 100}
Output: 100 5000
Input: {10, 20}
Output: 10 20
[Naive Approach] - O(n^2) time and O(1) space
The idea is to run two nested loops. The outer loop picks an element and the inner loop counts the number of occurrences of the picked element. If the count of occurrences is odd, then append the element to result, while ensuring that one element is only pushed once into result.
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
vector<int> ans = {-1, -1};
int index = 0;
// Check for each element
for (int i=0; i<n; i++) {
// Get the count of arr[i]
int cnt = 0;
for (int j=0; j<n; j++) {
if (arr[j]==arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt%2==1 && ans[0]!=arr[i] && ans[1]!=arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.*;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
int[] ans = {-1, -1};
int index = 0;
// Check for each element
for (int i = 0; i < n; i++) {
// Get the count of arr[i]
int cnt = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
ans = [-1, -1]
index = 0
# Check for each element
for i in range(n):
# Get the count of arr[i]
cnt = 0
for j in range(n):
if arr[j] == arr[i]:
cnt += 1
# If cnt is odd and arr[i]
# has not been added to result yet.
if cnt % 2 == 1 and ans[0] != arr[i] and ans[1] != arr[i]:
ans[index] = arr[i]
index += 1
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
int[] ans = {-1, -1};
int index = 0;
// Check for each element
for (int i = 0; i < n; i++) {
// Get the count of arr[i]
int cnt = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
let ans = [-1, -1];
let index = 0;
// Check for each element
for (let i = 0; i < n; i++) {
// Get the count of arr[i]
let cnt = 0;
for (let j = 0; j < n; j++) {
if (arr[j] == arr[i]) cnt++;
}
// If cnt is odd and arr[i]
// has not been added to result yet.
if (cnt % 2 == 1 && ans[0] != arr[i] && ans[1] != arr[i]) {
ans[index++] = arr[i];
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
[Better Approach - 1] Using Sorting - O(n logn) time and O(1) space
The idea is to use sorting and then find the frequency of each element in linear time.
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
vector<int> ans = {-1, -1};
int index = 0;
// Sort the array
sort(arr.begin(), arr.end());
// Get count of each element
int i = 0;
while (i<n) {
int val = arr[i];
int cnt = 0;
while (i<n && arr[i]==val) {
cnt++;
i++;
}
// If count is odd
if (cnt%2==1) ans[index++] = val;
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.Arrays;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
int[] ans = {-1, -1};
int index = 0;
// Sort the array
Arrays.sort(arr);
// Get count of each element
int i = 0;
while (i < n) {
int val = arr[i];
int cnt = 0;
while (i < n && arr[i] == val) {
cnt++;
i++;
}
// If count is odd
if (cnt % 2 == 1) ans[index++] = val;
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
ans = [-1, -1]
index = 0
# Sort the array
arr.sort()
# Get count of each element
i = 0
while i < n:
val = arr[i]
cnt = 0
while i < n and arr[i] == val:
cnt += 1
i += 1
# If count is odd
if cnt % 2 == 1:
ans[index] = val
index += 1
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
int[] ans = {-1, -1};
int index = 0;
// Sort the array
Array.Sort(arr);
// Get count of each element
int i = 0;
while (i < n) {
int val = arr[i];
int cnt = 0;
while (i < n && arr[i] == val) {
cnt++;
i++;
}
// If count is odd
if (cnt % 2 == 1) ans[index++] = val;
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
let ans = [-1, -1];
let index = 0;
// Sort the array
arr.sort((a, b) => a - b);
// Get count of each element
let i = 0;
while (i < n) {
let val = arr[i];
let cnt = 0;
while (i < n && arr[i] === val) {
cnt++;
i++;
}
// If count is odd
if (cnt % 2 === 1) {
ans[index++] = val;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
[Better Approach - 2] Using Hash Map - O(n) time and O(n) space
The idea is to use a hash map to store the count of all values. Then iterate through the map and return the values with odd count.
Step by step approach:
- Traverse all elements and insert them in to a hash table. Element is used as key and the frequency is used as the value in the hash table.
- Iterate through the map and append the values with odd count.
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
vector<int> ans = {-1, -1};
int index = 0;
// Map to store count
unordered_map<int,int> cnt;
// Count occurrences in array
for (int i=0; i<n; i++) {
cnt[arr[i]]++;
}
// Append the values with odd
// count to result
for (auto p: cnt) {
if (p.second%2==1) {
ans[index++] = p.first;
}
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.HashMap;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
int[] ans = {-1, -1};
int index = 0;
// Map to store count
HashMap<Integer, Integer> cnt = new HashMap<>();
// Count occurrences in array
for (int i = 0; i < n; i++) {
cnt.put(arr[i], cnt.getOrDefault(arr[i], 0) + 1);
}
// Append the values with odd
// count to result
for (var entry : cnt.entrySet()) {
if (entry.getValue() % 2 == 1) {
ans[index++] = entry.getKey();
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
ans = [-1, -1]
index = 0
# Map to store count
cnt = {}
# Count occurrences in array
for num in arr:
cnt[num] = cnt.get(num, 0) + 1
# Append the values with odd
# count to result
for key, value in cnt.items():
if value % 2 == 1:
ans[index] = key
index += 1
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
using System.Collections.Generic;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
int[] ans = {-1, -1};
int index = 0;
// Map to store count
Dictionary<int, int> cnt = new Dictionary<int, int>();
// Count occurrences in array
for (int i = 0; i < n; i++) {
if (cnt.ContainsKey(arr[i])) {
cnt[arr[i]]++;
} else {
cnt[arr[i]] = 1;
}
}
// Append the values with odd
// count to result
foreach (var entry in cnt) {
if (entry.Value % 2 == 1) {
ans[index++] = entry.Key;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
let ans = [-1, -1];
let index = 0;
// Map to store count
let cnt = new Map();
// Count occurrences in array
for (let i = 0; i < n; i++) {
cnt.set(arr[i], (cnt.get(arr[i]) || 0) + 1);
}
// Append the values with odd
// count to result
for (let [key, value] of cnt) {
if (value % 2 === 1) {
ans[index++] = key;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
[Expected Approach] Using Bit Manipulation - O(n) time and O(1) space
The idea is to first compute the XOR of all array elements, which results in xorVal = x^y (where x and y are our target numbers) since even count elements cancel each other out (as a^a = 0).
Now we have value of x^y, but we need to find the values of individual elements x and y. To find these values, we divide array elements into two sets.
We find the rightmost set bit in xorVal, which indicates a position where x and y differ (one has 1 and other has 0). Using this bit position as a filter, we partition numbers into two sets - one set where this bit is set and another where it's not. This separation ensures that x and y go into different sets.
Finally, we XOR all numbers in each set separately; even count numbers cancel out again, leaving us with one unique number in each set, which we return in decreasing order.
Step by step approach:
- The first step is to do XOR of all elements present in array (lets say xorVal). XOR of all elements gives us XOR of x and y because of the following properties of XOR operation:
- XOR of any number n with itself gives us 0, i.e., n ^ n = 0
- XOR of any number n with 0 gives us n, i.e., n ^ 0 = n
- XOR is cumulative and associative.
- Pick a set bit in xorVal, which indicates a position where x and y differ (one has 1 and other has 0). So we separate x and y into two different groups, along with rest of the numbers of list, based on whether the number has same set-bit or not.
- We will choose right most bit in this case. We can get right most set bit by performing AND operation of xorVal with its negative counterpart (xorVal & -xorVal).
- In third step, we separate x and y into two different groups. We now know that for selected set bit index, x and y have different corresponding bits. If we AND all numbers in list with set bit, some will give 0 and others will give 1. We will put all numbers giving zeroes in one group and ones in another.
- Return the result of the two groups in decreasing order.
Illustration:
Taking example of arr = [4, 2, 4, 10, 2, 3, 3, 12]
- XOR of all values in the array will be equal to
- 4 ^ 2 ^ 4 ^ 10 ^ 2 ^ 3 ^ 3 ^ 12
- (4 ^ 4) ^ (2 ^ 2) ^ 10 ^ (3 ^ 3) ^ 12
- 0 ^ 0 ^ 10 ^ 0 ^ 12
- 10 ^ 12
- 6
- Find right most set bit of xorVal:
- 6 & (-6)
- 0110 & 1010 (Binary representation)
- 0010
- 2
- Separate all values on basis of set bit and find their xor value:
- Values with set bit
- Values with unset bit
- Return the result in decreasing order, i.e., {12, 10}.
Below is the implementation of the above approach:
C++
// C++ program to find the two numbers with
// odd occurrences in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to find the two elements
// with odd occurrences.
vector<int> twoOddNum(vector<int>& arr) {
int n = arr.size();
// Get the XOR of the two numbers we need to find
int xorVal = 0;
for (int i=0; i<n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
vector<int> ans(2, 0);
for (int i=0; i<n; i++) {
int num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) == 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0]<ans[1]) swap(ans[0], ans[1]);
return ans;
}
int main() {
vector<int> arr = {12, 23, 34, 12, 12, 23, 12, 45};
vector<int> ans = twoOddNum(arr);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}
Java
// Java program to find the two numbers with
// odd occurrences in an unsorted array
import java.util.*;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.length;
// Get the XOR of the two numbers we need to find
int xorVal = 0;
for (int i = 0; i < n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
int[] ans = {0, 0};
for (int i = 0; i < n; i++) {
int num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) == 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
public static void main(String[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
System.out.println(ans[0] + " " + ans[1]);
}
}
Python
# Python program to find the two numbers with
# odd occurrences in an unsorted array
# Function to find the two elements
# with odd occurrences.
def twoOddNum(arr):
n = len(arr)
# Get the XOR of the two numbers we need to find
xorVal = 0
for num in arr:
xorVal ^= num
# Get its last set bit
xorVal &= -xorVal
ans = [0, 0]
for num in arr:
# If bit is not set, it
# belongs to first set.
if (num & xorVal) == 0:
ans[0] ^= num
# If bit is set, it
# belongs to 2nd set.
else:
ans[1] ^= num
# Return in decreasing order
if ans[0] < ans[1]:
ans[0], ans[1] = ans[1], ans[0]
return ans
if __name__ == "__main__":
arr = [12, 23, 34, 12, 12, 23, 12, 45]
ans = twoOddNum(arr)
print(ans[0], ans[1])
C#
// C# program to find the two numbers with
// odd occurrences in an unsorted array
using System;
class GfG {
// Function to find the two elements
// with odd occurrences.
static int[] twoOddNum(int[] arr) {
int n = arr.Length;
// Get the XOR of the two numbers we need to find
int xorVal = 0;
for (int i = 0; i < n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
int[] ans = {0, 0};
for (int i = 0; i < n; i++) {
int num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) == 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
int temp = ans[0];
ans[0] = ans[1];
ans[1] = temp;
}
return ans;
}
static void Main(string[] args) {
int[] arr = {12, 23, 34, 12, 12, 23, 12, 45};
int[] ans = twoOddNum(arr);
Console.WriteLine(ans[0] + " " + ans[1]);
}
}
JavaScript
// JavaScript program to find the two numbers with
// odd occurrences in an unsorted array
// Function to find the two elements
// with odd occurrences.
function twoOddNum(arr) {
let n = arr.length;
// Get the XOR of the two numbers we need to find
let xorVal = 0;
for (let i = 0; i < n; i++) {
xorVal = arr[i] ^ xorVal;
}
// Get its last set bit
xorVal &= -xorVal;
let ans = [0, 0];
for (let i = 0; i < n; i++) {
let num = arr[i];
// If bit is not set, it
// belongs to first set.
if ((num & xorVal) === 0) {
ans[0] ^= num;
}
// If bit is set, it
// belongs to 2nd set.
else {
ans[1] ^= num;
}
}
// Return in decreasing order
if (ans[0] < ans[1]) {
[ans[0], ans[1]] = [ans[1], ans[0]];
}
return ans;
}
let arr = [12, 23, 34, 12, 12, 23, 12, 45];
let ans = twoOddNum(arr);
console.log(ans[0], ans[1]);
Similar Reads
Find Unique pair in an array with pairs of numbers
Given an array where every element appears twice except a pair (two elements). Find the elements of this unique pair.Examples: Input : 6, 1, 3, 5, 1, 3, 7, 6 Output : 5 7 All elements appear twice except 5 and 7 Input : 1 3 4 1 Output : 3 4Recommended PracticeFind Unique pair in an array with pairs
8 min read
Find the Kth occurrence of an element in a sorted Array
Given a sorted array arr[] of size N, an integer X, and a positive integer K, the task is to find the index of Kth occurrence of X in the given array. Examples: Input: N = 10, arr[] = [1, 2, 3, 3, 4, 5, 5, 5, 5, 5], X = 5, K = 2Output: Starting index of the array is '0' Second occurrence of 5 is at
15+ min read
Find the only missing number in a sorted array
You are given a sorted array of N integers from 1 to N with one number missing find the missing number Examples: Input :ar[] = {1, 3, 4, 5}Output : 2Input : ar[] = {1, 2, 3, 4, 5, 7, 8}Output : 6A simple solution is to linearly traverse the given array. Find the point where current element is not on
9 min read
Two odd occurring elements in an array where all other occur even times
Given an array where all elements appear even number of times except two, print the two odd occurring elements. It may be assumed that the size of array is at-least two. Examples: Input : arr[] = {2, 3, 8, 4, 4, 3, 7, 8} Output : 2 7 Input : arr[] = {15, 10, 10, 50 7, 5, 5, 50, 50, 50, 50, 50} Outpu
15+ min read
Find Equal (or Middle) Point in a sorted array with duplicates
Given a sorted array of n size, the task is to find whether an element exists in the array from where the number of smaller elements is equal to the number of greater elements.If Equal Point appears multiple times in input array, return the index of its first occurrence. If doesn't exist, return -1.
9 min read
Sort all even numbers in the Array without changing order of odd elements
Given an array arr[] of size N, the task is to sort all the even numbers in the array, without changing the order of odd elementsExamples: Input: arr[] = {4, 7, 2, 11, 15}Output: {2, 7, 4, 11, 15}Explanation: Even numbers are sorted at their corresponding places, without changing the order of odd el
4 min read
Missing in a Sorted Array of Natural Numbers
Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Write an efficient code to find the missing integer. Examples: Input : arr[] = [1, 2, 3, 4, 6, 7, 8]Output : 5Explanation: The mis
12 min read
Array Index with same count of even or odd numbers on both sides
Given an array of N integers. We need to find an index such that Frequency of Even numbers on its left side is equal to the frequency of even numbers on its right sides Or frequency of odd numbers on its left side is equal to the frequency of Odd numbers on its right sides. If No such index exist in
14 min read
Elements that occurred only once in the array
Given an array arr that has numbers appearing twice or once. The task is to identify numbers that occur only once in the array. Note: Duplicates appear side by side every time. There might be a few numbers that can occur at one time and just assume this is a right rotating array (just say an array c
15+ min read
Reduce the array to a single element with the given operation
Given an integer N and an array arr containing integers from 1 to N in a sorted fashion. The task is to reduce the array to a single element by performing the following operation: All the elements in the odd positions will be removed after a single operation. This operation will be performed until o
4 min read