Maximum distinct Subarray formed by adding or subtracting k
Last Updated :
24 Jan, 2023
Given an array arr[] of length n and an integer k, the task is to find the length of the maximum distinct subarray formed after adding k or subtracting k or none.
Examples:
Input: arr = [1, 2, 3, 4, 5], k = 1
Output: 5
Explanation: In this test case, the array has 5 distinct elements, and k is 1. The subarray with the maximum number of distinct elements is the entire array, so the function should return 5.
Input: arr = [1, 2, 3, 3, 4, 5], k = 0
Output: 3
Explanation: In this test case, the array has 5 distinct elements, and k is 0. The subarray with the maximum number of distinct elements is [1, 2, 3] and [3, 4, 5], so the function should return 3.
Approach: The above problem can be solved with the below:
The approach used in the algorithm is to iterate through the elements in the array and maintain a set of distinct elements in the current subarray. At each step, the algorithm checks if the current element x or x – k or x + k is in the set.
- If x is not in the set, it is added to the set.
- If x – k is in the set, it is removed and x is added to the set.
- If x + k is in the set, it is removed and x is added to the set.
In this way, the set always contains the distinct elements in the current subarray, and the size of the set is equal to the length of the current distinct subarray. The maximum length of the distinct subarray is updated whenever the size of the set is larger than the current maximum length.
This approach works because the addition or subtraction of k to or from an element does not change the distinctness of the element. If an element is distinct, adding or subtracting k to or from it will still make it distinct. Similarly, if an element is not distinct, adding or subtracting k to or from it will still make it not distinct. Therefore, adding or subtracting k to or from an element does not effect the distinctness of the elements in the current subarray.
Follow the given steps to solve the problem:
- Initialize a variable max_length to 0. This variable will keep track of the maximum length of the distinct subarray found so far.
- Initialize a set of distinct_elements to an empty set. This set will store the distinct elements in the current subarray.
- Iterate through the elements in the array. For each element x, do the following:
- If x – k or x + k is not in the set distinct_elements, add x to the set.
- If x – k or x + k is in the set distinct_elements, remove x – k or x + k from the set and add x to the set.
- Update the variable max_length to the maximum of max_length and the size of the set distinct_elements.
- Return max_length.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_distinct_subarray( int arr[], int n, int k) {
int max_length = 0;
set< int > distinct_elements;
for ( int i = 0; i < n; i++)
{
if (distinct_elements.find(arr[i] - k) == distinct_elements.end() && distinct_elements.find(arr[i] + k) == distinct_elements.end()) {
distinct_elements.insert(arr[i]);
}
else if (distinct_elements.find(arr[i] - k) != distinct_elements.end()) {
distinct_elements.erase(arr[i] - k);
distinct_elements.insert(arr[i]);
}
else if (distinct_elements.find(arr[i] + k) != distinct_elements.end()) {
distinct_elements.erase(arr[i] + k);
distinct_elements.insert(arr[i]);
}
max_length = max(max_length, ( int )distinct_elements.size());
}
return max_length;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int k = 0;
int n = sizeof (arr) / sizeof (arr[0]);
cout << max_distinct_subarray(arr, n, k) << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maxDistinctSubarray( int [] arr, int n, int k)
{
int maxLength = 0 ;
Set<Integer> distinctElements = new HashSet<>();
for ( int i = 0 ; i < n; i++)
{
if (!distinctElements.contains(arr[i] - k)
&& !distinctElements.contains(arr[i] + k)) {
distinctElements.add(arr[i]);
}
else if (distinctElements.contains(arr[i]
- k)) {
distinctElements.remove(arr[i] - k);
distinctElements.add(arr[i]);
}
else if (distinctElements.contains(arr[i]
+ k)) {
distinctElements.remove(arr[i] + k);
distinctElements.add(arr[i]);
}
maxLength = Math.max(maxLength,
distinctElements.size());
}
return maxLength;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int k = 0 ;
int n = arr.length;
System.out.println(maxDistinctSubarray(arr, n, k));
}
}
|
Python3
def max_distinct_subarray(arr, k):
max_length = 0
distinct_elements = set ()
for x in arr:
if x - k not in distinct_elements and x + k not in distinct_elements:
distinct_elements.add(x)
elif x - k in distinct_elements:
distinct_elements.remove(x - k)
distinct_elements.add(x)
elif x + k in distinct_elements:
distinct_elements.remove(x + k)
distinct_elements.add(x)
max_length = max (max_length, len (distinct_elements))
return max_length
arr = [ 1 , 2 , 3 , 4 , 5 ]
k = 0
print (max_distinct_subarray(arr, k))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int maxDistinctSubarray( int [] arr, int n, int k)
{
int maxLength = 0;
HashSet< int > distinctElements = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
if (!distinctElements.Contains(arr[i] - k)
&& !distinctElements.Contains(arr[i] + k)) {
distinctElements.Add(arr[i]);
}
else if (distinctElements.Contains(arr[i]
- k)) {
distinctElements.Remove(arr[i] - k);
distinctElements.Add(arr[i]);
}
else if (distinctElements.Contains(arr[i]
+ k)) {
distinctElements.Remove(arr[i] + k);
distinctElements.Add(arr[i]);
}
maxLength = Math.Max(maxLength,
distinctElements.Count);
}
return maxLength;
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int k = 0;
int n = arr.Length;
Console.WriteLine(maxDistinctSubarray(arr, n, k));
}
}
|
Javascript
function max_distinct_subarray(arr, n, k) {
let max_length = 0;
let distinct_elements= new Set();
for (let i = 0; i < n; i++)
{
if (!distinct_elements.has(arr[i] - k) && !distinct_elements.has(arr[i] + k)) {
distinct_elements.add(arr[i]);
}
else if (distinct_elements.has(arr[i] - k)) {
distinct_elements. delete (arr[i] - k);
distinct_elements.add(arr[i]);
}
else if (distinct_elements.has(arr[i] + k)) {
distinct_elements. delete (arr[i] + k);
distinct_elements.add(arr[i]);
}
max_length = Math.max(max_length, distinct_elements.size);
}
return max_length;
}
let arr = [1, 2, 3, 4, 5];
let k = 0;
let n = arr.length;
console.log(max_distinct_subarray(arr, n, k)) ;
|
Time complexity: O(n)
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...