Find Subarray ranges having difference between max and min exactly K
Last Updated :
20 Jul, 2022
Given an array arr[] of length N and integer K, the task is to print subarray ranges (starting index, ending index) of the array where difference between max and min elements of the subarray is exactly K.( 1-based index )
Examples:
Input: arr[] = {2, 1, 3, 4, 2, 6}, K = 2
Output: (1, 3), (2, 3), (3, 5), (4, 5)
Explanation: In the above array following sub array ranges have max min difference exactly K
(1, 3) => max = 3 and min = 1. Difference = 3 – 1 = 2
(2, 3) => max = 3 and min = 1. Difference = 3 – 1 = 2
(3, 5) => max = 4 and min = 2. Difference = 4 – 2 = 2
(4, 5) => max = 4 and min = 2. Difference = 4 – 2 = 2
Input: arr[] = {5, 3, 4, 6, 1, 2}, K = 6
Output: -1
Explanation: There is no such sub array ranges.
Approach: The basic idea to solve the problem is to form all the subarrays and find the minimum and maximum and their difference for each subarray. Follow the steps mentioned below to solve the problem.
- Iterate over the array from i = 0 to N-1:
- Iterate from j = i to N-1:
- Insert arr[j] current element in a set storing the current subarray starting from i.
- Find the minimum and maximum of the set.
- Get their difference and check if their difference is equal to K or not.
- If it is, then push this range in the answer.
- Return the ranges.
- If no such range then return -1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void printRanges(vector< int > arr, int n,
int K)
{
int i, j, f = 0;
for (i = 0; i < n; i++) {
set< int > s;
for (j = i; j < n; j++) {
s.insert(arr[j]);
int max = *s.rbegin();
int min = *s.begin();
if (max - min == K) {
cout << i + 1 << " " << j + 1
<< "\n" ;
f = 1;
}
}
}
if (f == 0)
cout << -1 << endl;
}
int main()
{
vector< int > arr = { 2, 1, 3, 4, 2, 6 };
int N = arr.size();
int K = 2;
printRanges(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void printRanges( int arr[], int n,
int K)
{
int i, j, f = 0 ;
for (i = 0 ; i < n; i++) {
Set<Integer> s = new HashSet<>();
for (j = i; j < n; j++) {
s.add(arr[j]);
int max = Collections.max(s);
int min = Collections.min(s);
if (max - min == K) {
System.out.println( (i + 1 ) + " " + (j + 1 ));
f = 1 ;
}
}
}
if (f == 0 )
System.out.println(- 1 );
}
public static void main (String[] args) {
int arr[] = { 2 , 1 , 3 , 4 , 2 , 6 };
int N = arr.length;
int K = 2 ;
printRanges(arr, N, K);
}
}
|
Python3
def printRanges(arr, n, K):
i, j, f = 0 , 0 , 0
for i in range ( 0 , n):
s = set ()
for j in range (i, n):
s.add(arr[j])
ma = max ( list (s))
mi = min ( list (s))
if (ma - mi = = K):
print (f "{i + 1} {j + 1}" )
f = 1
if (f = = 0 ):
print ( - 1 )
if __name__ = = "__main__" :
arr = [ 2 , 1 , 3 , 4 , 2 , 6 ]
N = len (arr)
K = 2
printRanges(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void printRanges( int [] arr, int n, int K)
{
int i, j, f = 0;
for (i = 0; i < n; i++) {
HashSet< int > s = new HashSet< int >();
for (j = i; j < n; j++) {
s.Add(arr[j]);
int max = int .MinValue,min = int .MaxValue;
foreach ( var value in s)
{
max = Math.Max(max,value);
min = Math.Min(min,value);
}
if (max - min == K) {
Console.Write( (i + 1) + " " + (j + 1) + "\n" );
f = 1;
}
}
}
if (f == 0)
Console.Write(-1);
}
public static void Main()
{
int [] arr = { 2, 1, 3, 4, 2, 6 };
int N = arr.Length;
int K = 2;
printRanges(arr, N, K);
}
}
|
Javascript
<script>
function printRanges(arr, n, K)
{
let i = 0, j = 0, f = 0;
for (i = 0; i < n; i++) {
const s = new Set();
for (j = i; j < n; j++) {
s.add(arr[j]);
let max = Math.max(...s);
let min = Math.min(...s);
if (max - min == K) {
document.write((i+1) + " " + (j+1) + "<br>" );
f = 1;
}
}
}
if (f == 0)
document.write(-1);
}
let arr = [ 2, 1, 3, 4, 2, 6 ];
let N = arr.length;
let K = 2;
printRanges(arr, N, K);
</script>
|
Time Complexity: O(N2 * logN)
Auxiliary Space: O(N)
Efficient Approach: Instead of using the HashSet, keep a track of current maximum and minimum element while traversing the array.
C++
#include <bits/stdc++.h>
using namespace std;
void printRanges(vector< int > arr, int n, int K)
{
int i = 0, j = 0, f = 0;
for (i = 0; i < n; i++) {
int mx = INT_MIN, mn = INT_MAX;
for (j = i; j < n; j++) {
mx = max(mx, arr[j]);
mn = min(mn, arr[j]);
if (mx - mn == K) {
cout << (i + 1) << " " << (j + 1) << endl;
f = 1;
}
}
}
if (f == 0)
cout << -1;
}
int main()
{
vector< int > arr = { 2, 1, 3, 4, 2, 6 };
int N = arr.size();
int K = 2;
printRanges(arr, N, K);
}
|
Java
import java.util.*;
public class GFG {
static void printRanges( int [] arr, int n, int K)
{
int i = 0 , j = 0 , f = 0 ;
for (i = 0 ; i < n; i++) {
int mx = Integer.MIN_VALUE, mn
= Integer.MAX_VALUE;
for (j = i; j < n; j++) {
mx = Math.max(mx, arr[j]);
mn = Math.min(mn, arr[j]);
if (mx - mn == K) {
System.out.println((i + 1 ) + " "
+ (j + 1 ));
f = 1 ;
}
}
}
if (f == 0 )
System.out.print(- 1 );
}
public static void main(String args[])
{
int [] arr = { 2 , 1 , 3 , 4 , 2 , 6 };
int N = arr.length;
int K = 2 ;
printRanges(arr, N, K);
}
}
|
Python3
import sys
def printRanges(arr, n, K):
i, j, f = 0 , 0 , 0
for i in range ( 0 , n):
mn = sys.maxsize
mx = - 1 * sys.maxsize
for j in range (i, n):
mx = max (mx, arr[j])
mn = min (mn, arr[j])
if (mx - mn = = K):
print (f "{i + 1} {j + 1}" )
f = 1
if (f = = 0 ):
print ( - 1 )
if __name__ = = "__main__" :
arr = [ 2 , 1 , 3 , 4 , 2 , 6 ]
N = len (arr)
K = 2
printRanges(arr, N, K)
|
C#
using System;
class GFG {
static void printRanges( int [] arr, int n, int K)
{
int i = 0, j = 0, f = 0;
for (i = 0; i < n; i++) {
int mx = Int32.MinValue, mn = Int32.MaxValue;
for (j = i; j < n; j++) {
mx = Math.Max(mx, arr[j]);
mn = Math.Min(mn, arr[j]);
if (mx - mn == K) {
Console.WriteLine((i + 1) + " "
+ (j + 1));
f = 1;
}
}
}
if (f == 0)
Console.Write(-1);
}
public static void Main()
{
int [] arr = { 2, 1, 3, 4, 2, 6 };
int N = arr.Length;
int K = 2;
printRanges(arr, N, K);
}
}
|
Javascript
<script>
function printRanges(arr, n, K)
{
let i = 0, j = 0, f = 0;
for (i = 0; i < n; i++) {
let mx = Number.MIN_SAFE_INTEGER, mn = Number.MAX_SAFE_INTEGER;
for (j = i; j < n; j++) {
mx = Math.max(mx, arr[j]);
mn = Math.min(mn, arr[j]);
if (mx - mn == K) {
document.write((i + 1) + " " + (j + 1));
f = 1;
}
}
}
if (f == 0)
document.write(-1);
}
let arr = [ 2, 1, 3, 4, 2, 6 ];
let N = arr.length;
let K = 2;
printRanges(arr, N, K);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...