Given an integer array arr[], count the number of distinct special integers. An integer x is called a special integer if x - 1, x, and x + 1 are all present in the array. Return the number of distinct special integers.
Examples:
Input: arr[] = [1, 2, 3, 3, 4]
Output: 2
Explanation: The special integers in this array are 2 and 3.Input: arr[] = [2, 3, 5, 7]
Output: 0
Explanation: There is no special integer in this array.
Table of Content
[Naive Approach] Use Linear Search - O(n^2) Time and O(1) Space
The idea is to check every element x and use linear search to determine whether x - 1 and x + 1 are present in the array. If both are present, x is a special integer.
Working of the Approach:
- Traverse each element x in the array.
- Search for x - 1 in the array.
- Search for x + 1 in the array.
- If both are present, increment the count.
- Return the count.
#include <bits/stdc++.h>
using namespace std;
bool isPresent(vector<int>& arr, int value) {
for (int x : arr) {
if (x == value)
return true;
}
return false;
}
bool seenBefore(vector<int>& arr, int idx) {
for (int i = 0; i < idx; i++) {
if (arr[i] == arr[idx])
return true;
}
return false;
}
int specialIntegers(vector<int>& arr) {
int count = 0;
for (int i = 0; i < arr.size(); i++) {
if (seenBefore(arr, i))
continue;
int x = arr[i];
if (isPresent(arr, x - 1) && isPresent(arr, x + 1))
count++;
}
return count;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
cout << specialIntegers(arr);
return 0;
}
class GFG {
static boolean isPresent(int[] arr, int value) {
for (int x : arr) {
if (x == value)
return true;
}
return false;
}
static boolean seenBefore(int[] arr, int idx) {
for (int i = 0; i < idx; i++) {
if (arr[i] == arr[idx])
return true;
}
return false;
}
static int specialIntegers(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (seenBefore(arr, i))
continue;
int x = arr[i];
if (isPresent(arr, x - 1) && isPresent(arr, x + 1))
count++;
}
return count;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
System.out.println(specialIntegers(arr));
}
}
def isPresent(arr, value):
for x in arr:
if x == value:
return True
return False
def seenBefore(arr, idx):
for i in range(idx):
if arr[i] == arr[idx]:
return True
return False
def specialIntegers(arr):
count = 0
for i in range(len(arr)):
if seenBefore(arr, i):
continue
x = arr[i]
if isPresent(arr, x - 1) and isPresent(arr, x + 1):
count += 1
return count
if __name__ == "__main__":
arr = [1, 2, 3, 4]
print(specialIntegers(arr))
using System;
class GFG
{
static bool isPresent(int[] arr, int value)
{
foreach (int x in arr)
{
if (x == value)
return true;
}
return false;
}
static bool seenBefore(int[] arr, int idx)
{
for (int i = 0; i < idx; i++)
{
if (arr[i] == arr[idx])
return true;
}
return false;
}
static int specialIntegers(int[] arr)
{
int count = 0;
for (int i = 0; i < arr.Length; i++)
{
if (seenBefore(arr, i))
continue;
int x = arr[i];
if (isPresent(arr, x - 1) && isPresent(arr, x + 1))
count++;
}
return count;
}
static void Main()
{
int[] arr = { 1, 2, 3, 4 };
Console.WriteLine(specialIntegers(arr));
}
}
function isPresent(arr, value) {
for (let x of arr) {
if (x === value)
return true;
}
return false;
}
function seenBefore(arr, idx) {
for (let i = 0; i < idx; i++) {
if (arr[i] === arr[idx])
return true;
}
return false;
}
function specialIntegers(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (seenBefore(arr, i))
continue;
let x = arr[i];
if (isPresent(arr, x - 1) && isPresent(arr, x + 1))
count++;
}
return count;
}
// Driver Code
let arr = [1, 2, 3, 4];
console.log(specialIntegers(arr));
Output
2
[Expected Approach] Use a Hash Set - O(n) Time and O(n) Space
The idea is to store all elements of the array in a hash set. This allows us to check whether a number exists in the array in average O(1) time. For each distinct element x, if both x - 1 and x + 1 are present in the set, then x is a special integer.
Working of the Approach:
- Insert all elements of the array into a hash set.
- Traverse the distinct elements of the array.
- For each element x, check whether x - 1 and x + 1 are present in the set.
- If both are present, increment the count.
- Return the count.
#include <bits/stdc++.h>
using namespace std;
int specialIntegers(vector<int>& arr) {
unordered_set<int> st(arr.begin(), arr.end());
int count = 0;
for (int x : st) {
if (st.count(x - 1) && st.count(x + 1))
count++;
}
return count;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
cout << specialIntegers(arr);
return 0;
}
import java.util.HashSet;
class GFG {
static int specialIntegers(int[] arr) {
HashSet<Integer> set = new HashSet<>();
for (int x : arr)
set.add(x);
int count = 0;
for (int x : set) {
if (set.contains(x - 1) && set.contains(x + 1))
count++;
}
return count;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
System.out.println(specialIntegers(arr));
}
}
def specialIntegers(arr):
st = set(arr)
count = 0
for x in st:
if x - 1 in st and x + 1 in st:
count += 1
return count
if __name__ == "__main__":
arr = [1, 2, 3, 4]
print(specialIntegers(arr))
using System;
using System.Collections.Generic;
class GFG
{
static int specialIntegers(int[] arr)
{
HashSet<int> set = new HashSet<int>(arr);
int count = 0;
foreach (int x in set)
{
if (set.Contains(x - 1) && set.Contains(x + 1))
count++;
}
return count;
}
static void Main()
{
int[] arr = { 1, 2, 3, 4 };
Console.WriteLine(specialIntegers(arr));
}
}
function specialIntegers(arr) {
let st = new Set(arr);
let count = 0;
for (let x of st) {
if (st.has(x - 1) && st.has(x + 1))
count++;
}
return count;
}
// Driver Code
let arr = [1, 2, 3, 4];
console.log(specialIntegers(arr));
Output
2