Open In App

Minimize index difference (j – i) such that range [arr[i], arr[j]] contains at least K odd integers

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum value of j - i
// such that the count of odd integers in the
// range [arr[i], arr[j]] is more than K
int findEven(int arr[], int N, int K)
{
    // Base Case
    int L = arr[0];
    int R = arr[N - 1];
 
    // Maximum count of odd integers
    int Count = (L & 1)
                    ? ceil((float)(R - L + 1) / 2)
                    : (R - L + 1) / 2;
 
    // If no valid (i, j) exists
    if (K > Count) {
        return -1;
    }
 
    // Initialize the variables
    int i = 0, j = 0, ans = INT_MAX;
 
    // Loop for the two pointer approach
    while (j < N) {
 
        L = arr[i];
        R = arr[j];
 
        // Calculate count of odd numbers in the
        // range [L, R]
        Count = (L & 1)
                    ? ceil((float)(R - L + 1) / 2)
                    : (R - L + 1) / 2;
 
        if (K > Count) {
            j++;
        }
 
        else {
 
            // If the current value of j - i
            // is smaller, update answer
            if (j - i < ans) {
                ans = j - i;
            }
 
            i++;
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
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




// Java program for the above approach
import java.util.*;
 
public class GFG
{
   
// Function to find the minimum value of j - i
// such that the count of odd integers in the
// range [arr[i], arr[j]] is more than K
static int findEven(int []arr, int N, int K)
{
   
    // Base Case
    int L = arr[0];
    int R = arr[N - 1];
 
    int Count = 0;
 
    // Maximum count of odd integers
    if ((L & 1) > 0) {
        Count = (int)Math.ceil((float)(R - L + 1) / 2.0);
    }
    else {
        Count = (R - L + 1) / 2;
    }
    // If no valid (i, j) exists
    if (K > Count) {
        return -1;
    }
 
    // Initialize the variables
    int i = 0, j = 0, ans = Integer.MAX_VALUE;
 
    // Loop for the two pointer approach
    while (j < N) {
 
        L = arr[i];
        R = arr[j];
         
        // Calculate count of odd numbers in the
        // range [L, R]
        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 the current value of j - i
            // is smaller, update answer
            if (j - i < ans) {
                ans = j - i;
            }
 
            i++;
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
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));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3




# Python Program to implement
# the above approach
import math as Math
 
# Function to find the minimum value of j - i
# such that the count of odd integers in the
# range [arr[i], arr[j]] is more than K
def findEven(arr, N, K):
   
    # Base Case
    L = arr[0]
    R = arr[N - 1]
 
    # Maximum count of odd integers
    Count = Math.ceil((R - L + 1) / 2) if (L & 1) else (R - L + 1) / 2
 
    # If no valid (i, j) exists
    if (K > Count):
        return -1
 
    # Initialize the variables
    i = 0
    j = 0
    ans = 10**9
 
    # Loop for the two pointer approach
    while (j < N):
 
        L = arr[i]
        R = arr[j]
 
        # Calculate count of odd numbers in the
        # range [L, R]
        Count = Math.ceil((R - L + 1) / 2) if (L & 1) else (R - L + 1) / 2
 
        if (K > Count):
          j += 1
 
        else:
            # If the current value of j - i
            # is smaller, update answer
            if (j - i < ans):
                ans = j - i
            i += 1
 
    # Return Answer
    return ans
 
# Driver Code
arr = [1, 3, 6, 8, 15, 21]
N = len(arr)
K = 3
 
print(findEven(arr, N, K))
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
 
public class GFG
{
   
// Function to find the minimum value of j - i
// such that the count of odd integers in the
// range [arr[i], arr[j]] is more than K
static int findEven(int []arr, int N, int K)
{
    // Base Case
    int L = arr[0];
    int R = arr[N - 1];
 
    int Count = 0;
 
    // Maximum count of odd integers
    if ((L & 1) > 0) {
        Count = (int)Math.Ceiling((float)(R - L + 1) / 2.0);
    }
    else {
        Count = (R - L + 1) / 2;
    }
    // If no valid (i, j) exists
    if (K > Count) {
        return -1;
    }
 
    // Initialize the variables
    int i = 0, j = 0, ans = Int32.MaxValue;
 
    // Loop for the two pointer approach
    while (j < N) {
 
        L = arr[i];
        R = arr[j];
         
        // Calculate count of odd numbers in the
        // range [L, R]
        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 the current value of j - i
            // is smaller, update answer
            if (j - i < ans) {
                ans = j - i;
            }
 
            i++;
        }
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
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));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
 
        // JavaScript Program to implement
        // the above approach
 
        // Function to find the minimum value of j - i
        // such that the count of odd integers in the
        // range [arr[i], arr[j]] is more than K
        function findEven(arr, N, K) {
            // Base Case
            let L = arr[0];
            let R = arr[N - 1];
 
            // Maximum count of odd integers
            let Count = (L & 1)
                ? Math.ceil((R - L + 1) / 2)
                : (R - L + 1) / 2;
 
            // If no valid (i, j) exists
            if (K > Count) {
                return -1;
            }
 
            // Initialize the variables
            let i = 0, j = 0, ans = Number.MAX_VALUE;
 
            // Loop for the two pointer approach
            while (j < N) {
 
                L = arr[i];
                R = arr[j];
 
                // Calculate count of odd numbers in the
                // range [L, R]
                Count = (L & 1)
                    ? Math.ceil((R - L + 1) / 2)
                    : (R - L + 1) / 2;
 
                if (K > Count) {
                    j++;
                }
 
                else {
 
                    // If the current value of j - i
                    // is smaller, update answer
                    if (j - i < ans) {
                        ans = j - i;
                    }
 
                    i++;
                }
            }
 
            // Return Answer
            return ans;
        }
 
        // Driver Code
        let arr = [1, 3, 6, 8, 15, 21];
        let N = arr.length;
        let K = 3;
 
        document.write(findEven(arr, N, K));
 
    // This code is contributed by Potta Lokesh
    </script>


 
 

Output

1

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads