Minimize index difference (j – i) such that range [arr[i], arr[j]] contains at least K odd integers
Last Updated :
16 Jun, 2022
Given an array arr[] having N integers in non-decreasing order and an integer K, the task is to find the minimum value of (j – i) for a pair (i, j) such that the range [arr[i], arr[j]] contains at least K odd integers.
Examples:
Input: arr[] = {1, 3, 6, 8, 15, 21}, K = 3
Output: 1
Explanation: For (i, j) = (3, 4), it represents the range [8, 15] having 4 odd integers {9, 11, 13, 15} (i.e, more than K). Hence the value of j – i = 1, which is the minimum possible.
Input: arr[] = {5, 6, 7, 8, 9}, K = 5
Output: -1
Approach: The given problem can be solved using the two-pointers approach and some basic mathematics. The idea is to use a sliding window to check the count of odd numbers in the range and find the size of the smallest window containing at least K odd numbers. It can be done by maintaining two pointers i and j and calculating the count of odd numbers in the range [arr[i], arr[j]]. Maintain the minimum value of (j – i) in a variable for windows with more than K odd integers which is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findEven( int arr[], int N, int K)
{
int L = arr[0];
int R = arr[N - 1];
int Count = (L & 1)
? ceil (( float )(R - L + 1) / 2)
: (R - L + 1) / 2;
if (K > Count) {
return -1;
}
int i = 0, j = 0, ans = INT_MAX;
while (j < N) {
L = arr[i];
R = arr[j];
Count = (L & 1)
? ceil (( float )(R - L + 1) / 2)
: (R - L + 1) / 2;
if (K > Count) {
j++;
}
else {
if (j - i < ans) {
ans = j - i;
}
i++;
}
}
return ans;
}
int main()
{
int arr[] = { 1, 3, 6, 8, 15, 21 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << findEven(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int findEven( int []arr, int N, int K)
{
int L = arr[ 0 ];
int R = arr[N - 1 ];
int Count = 0 ;
if ((L & 1 ) > 0 ) {
Count = ( int )Math.ceil(( float )(R - L + 1 ) / 2.0 );
}
else {
Count = (R - L + 1 ) / 2 ;
}
if (K > Count) {
return - 1 ;
}
int i = 0 , j = 0 , ans = Integer.MAX_VALUE;
while (j < N) {
L = arr[i];
R = arr[j];
if ((L & 1 ) > 0 ) {
Count = ( int )Math.ceil(( float )(R - L + 1 ) / 2 );
}
else {
Count = (R - L + 1 ) / 2 ;
}
if (K > Count) {
j++;
}
else {
if (j - i < ans) {
ans = j - i;
}
i++;
}
}
return ans;
}
public static void main(String args[])
{
int []arr = { 1 , 3 , 6 , 8 , 15 , 21 };
int N = arr.length;
int K = 3 ;
System.out.println(findEven(arr, N, K));
}
}
|
Python3
import math as Math
def findEven(arr, N, K):
L = arr[ 0 ]
R = arr[N - 1 ]
Count = Math.ceil((R - L + 1 ) / 2 ) if (L & 1 ) else (R - L + 1 ) / 2
if (K > Count):
return - 1
i = 0
j = 0
ans = 10 * * 9
while (j < N):
L = arr[i]
R = arr[j]
Count = Math.ceil((R - L + 1 ) / 2 ) if (L & 1 ) else (R - L + 1 ) / 2
if (K > Count):
j + = 1
else :
if (j - i < ans):
ans = j - i
i + = 1
return ans
arr = [ 1 , 3 , 6 , 8 , 15 , 21 ]
N = len (arr)
K = 3
print (findEven(arr, N, K))
|
C#
using System;
public class GFG
{
static int findEven( int []arr, int N, int K)
{
int L = arr[0];
int R = arr[N - 1];
int Count = 0;
if ((L & 1) > 0) {
Count = ( int )Math.Ceiling(( float )(R - L + 1) / 2.0);
}
else {
Count = (R - L + 1) / 2;
}
if (K > Count) {
return -1;
}
int i = 0, j = 0, ans = Int32.MaxValue;
while (j < N) {
L = arr[i];
R = arr[j];
if ((L & 1) > 0) {
Count = ( int )Math.Ceiling(( float )(R - L + 1) / 2);
}
else {
Count = (R - L + 1) / 2;
}
if (K > Count) {
j++;
}
else {
if (j - i < ans) {
ans = j - i;
}
i++;
}
}
return ans;
}
public static void Main()
{
int []arr = { 1, 3, 6, 8, 15, 21 };
int N = arr.Length;
int K = 3;
Console.Write(findEven(arr, N, K));
}
}
|
Javascript
<script>
function findEven(arr, N, K) {
let L = arr[0];
let R = arr[N - 1];
let Count = (L & 1)
? Math.ceil((R - L + 1) / 2)
: (R - L + 1) / 2;
if (K > Count) {
return -1;
}
let i = 0, j = 0, ans = Number.MAX_VALUE;
while (j < N) {
L = arr[i];
R = arr[j];
Count = (L & 1)
? Math.ceil((R - L + 1) / 2)
: (R - L + 1) / 2;
if (K > Count) {
j++;
}
else {
if (j - i < ans) {
ans = j - i;
}
i++;
}
}
return ans;
}
let arr = [1, 3, 6, 8, 15, 21];
let N = arr.length;
let K = 3;
document.write(findEven(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...