Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIFind indices having at least K non-increasing elements before and K non-decreasing...

Find indices having at least K non-increasing elements before and K non-decreasing elements after them

Given an array arr[] of size N and an integer K, the task is to find all the indices in the given array having at least K non-increasing elements before and K non-decreasing elements after them.

Examples:

Input: arr[] = {1, 1, 1, 1, 1}, K = 0
Output: 0 1 2 3 4
Explanation: Since K equals 0, every index satisfies the condition.

Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 2
Output: -1
Explanation: No index has 2 non-increasing before it and 2 non-decreasing elements after it.

 

Approach: The solution can be found by using the concept of prefix and suffix array. Follow the steps mentioned below:

  1. Form the prefix[] array where prefix[i] represents the number of elements before i which obeys non-increasing order.
  2. Form the suffix[] array where suffix[i] represents the number of elements after i which obeys non-decreasing order.
  3. Now only those indexes should be included in the answer for which, both prefix[i] and suffix[i] are greater than or equal to K.

Below is the implementation of the above approach.

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find all the indices
vector<int> findIndices(int arr[], int K, int N)
{
    vector<int> prefix(N), suffix(N);
    vector<int> ans;
 
    prefix[0] = 1;
    for (int i = 1; i < N; i++) {
        if (arr[i] <= arr[i - 1])
            prefix[i] = prefix[i - 1] + 1;
        else
            prefix[i] = 1;
    }
 
    suffix[N - 1] = 1;
    for (int i = N - 2; i >= 0; i--) {
        if (arr[i] <= arr[i + 1])
            suffix[i] = suffix[i + 1] + 1;
        else
            suffix[i] = 1;
    }
 
    for (int i = 0; i < N; i++) {
        if (K == 0
            || i - 1 >= 0 && i + 1 < N && prefix[i - 1] >= K
                   && suffix[i + 1] >= K)
            ans.push_back(i);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 1, 1, 1, 1 };
    int K = 0;
    int N = sizeof(arr) / sizeof(arr[0]);
    vector<int> ans = findIndices(arr, K, N);
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
    if (ans.size() == 0)
        cout << "-1";
    return 0;
}


Java




// Java code for the above approach
import java.util.ArrayList;
class GFG {
 
    // Function to find all the indices
    static ArrayList<Integer> findIndices(int[] arr, int K,
                                          int N)
    {
        int[] prefix = new int[N];
 
        int[] suffix = new int[N];
        ArrayList<Integer> ans = new ArrayList<Integer>();
 
        prefix[0] = 1;
        for (int i = 1; i < N; i++) {
            if (arr[i] <= arr[i - 1])
                prefix[i] = prefix[i - 1] + 1;
            else
                prefix[i] = 1;
        }
 
        suffix[N - 1] = 1;
        for (int i = N - 2; i >= 0; i--) {
            if (arr[i] <= arr[i + 1])
                suffix[i] = suffix[i + 1] + 1;
            else
                suffix[i] = 1;
        }
 
        for (int i = 0; i < N; i++) {
            if (K == 0
                || i - 1 >= 0 && i + 1 < N
                       && prefix[i - 1] >= K
                       && suffix[i + 1] >= K)
                ans.add(i);
        }
 
        return ans;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] arr = { 1, 1, 1, 1, 1 };
        int K = 0;
        int N = arr.length;
        ArrayList<Integer> ans = findIndices(arr, K, N);
        for (int i = 0; i < ans.size(); i++) {
            System.out.print(ans.get(i) + " ");
        }
        if (ans.size() == 0)
            System.out.println("-1");
    }
}
 
// This code is contributed by gfgking


Python3




# Python code for the above approach
 
# Function to find all the indices
 
 
def findIndices(arr, K, N):
    prefix = [0] * N
    suffix = [0] * N
    ans = []
 
    prefix[0] = 1
    for i in range(1, N):
        if (arr[i] <= arr[i - 1]):
            prefix[i] = prefix[i - 1] + 1
        else:
            prefix[i] = 1
 
    suffix[N - 1] = 1
    for i in range(N - 2, 1, -1):
        if (arr[i] <= arr[i + 1]):
            suffix[i] = suffix[i + 1] + 1
        else:
            suffix[i] = 1
 
    for i in range(N):
        if (K == 0 or i - 1 >= 0 and i + 1 < N and prefix[i - 1] >= K
                and suffix[i + 1] >= K):
            ans.append(i)
 
    return ans
 
 
# Driver code
arr = [1, 1, 1, 1, 1]
K = 0
N = len(arr)
ans = findIndices(arr, K, N)
for i in range(len(ans)):
    print(ans[i], end=" ")
if (len(ans) == 0):
    print("-1")
 
# This code is contributed by Saurabh Jaiswal


C#




// C# code for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to find all the indices
    static List<int> findIndices(int[] arr, int K, int N)
    {
        int[] prefix = new int[N];
 
        int[] suffix = new int[N];
        List<int> ans = new List<int>();
 
        prefix[0] = 1;
        for (int i = 1; i < N; i++) {
            if (arr[i] <= arr[i - 1])
                prefix[i] = prefix[i - 1] + 1;
            else
                prefix[i] = 1;
        }
 
        suffix[N - 1] = 1;
        for (int i = N - 2; i >= 0; i--) {
            if (arr[i] <= arr[i + 1])
                suffix[i] = suffix[i + 1] + 1;
            else
                suffix[i] = 1;
        }
 
        for (int i = 0; i < N; i++) {
            if (K == 0
                || i - 1 >= 0 && i + 1 < N
                       && prefix[i - 1] >= K
                       && suffix[i + 1] >= K)
                ans.Add(i);
        }
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 1, 1, 1, 1, 1 };
        int K = 0;
        int N = arr.Length;
        List<int> ans = findIndices(arr, K, N);
        for (int i = 0; i < ans.Count; i++) {
            Console.Write(ans[i] + " ");
        }
        if (ans.Count == 0)
            Console.Write("-1");
    }
}
 
// This code is contributed by ukasp


Javascript




<script>
    // JavaScript code for the above approach
 
    // Function to find all the indices
    const findIndices = (arr, K, N) => {
        let prefix = new Array(N).fill(0);
        let suffix = new Array(N).fill(0);
        let ans = [];
 
        prefix[0] = 1;
        for (let i = 1; i < N; i++) {
            if (arr[i] <= arr[i - 1])
                prefix[i] = prefix[i - 1] + 1;
            else
                prefix[i] = 1;
        }
 
        suffix[N - 1] = 1;
        for (let i = N - 2; i >= 0; i--) {
            if (arr[i] <= arr[i + 1])
                suffix[i] = suffix[i + 1] + 1;
            else
                suffix[i] = 1;
        }
 
        for (let i = 0; i < N; i++) {
            if (K == 0 || i - 1 >= 0 && i + 1 < N && prefix[i - 1] >= K
            && suffix[i + 1] >= K)
                ans.push(i);
        }
 
        return ans;
    }
 
    // Driver code
    let arr = [1, 1, 1, 1, 1];
    let K = 0;
    let N = arr.length;
    let ans = findIndices(arr, K, N);
    for (let i = 0; i < ans.length; i++) {
        document.write(`${ans[i]} `);
    }
    if (ans.length == 0)
        cout << "-1";
 
// This code is contributed by rakeshsahni
 
</script>


Output

0 1 2 3 4 

Time Complexity: O(N)
Auxiliary Space: O(N), since N extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments