Differences between number of increasing subarrays and decreasing subarrays in k sized windows
Given an array of integers and k, find the difference between the number of the strictly increasing number of subarrays (of size more than one) and the number of the strictly decreasing subarray in the window of size k.
Examples:
Input : nums = {10, 20, 30, 15, 15};
k = 3;
Output : 3, 0, -1
Explanation
For the first window of [10, 20, 30], there are
3 increasing subranges ([10, 20, 30], [10, 20],
and [20, 30]) and 0 decreasing, so the answer
is 3. For the second window of [20, 30, 15],
there is 1 increasing subrange and 1 decreasing,
so the answer is 0. For the third window of
[20, 15, 15], there is 1 decreasing subrange
and 0 increasing, so the answer is -1.
We need to calculate the difference between increasing and decreasing subarrays for each of the window of size K. We iterate through the array and for each of the window of size k, we calculate the number of increasing and decreasing subarrays and insert the difference of them in the output array.
- Create two arrays of size N and initialize them by zero and create the output array.
- Iterate through array
- Calculate the numbers of increasing subarrays
- Calculate the decreasing subarrays
- Insert the difference between increasing and decreasing subarrays in output array.
Time Complexity of the solution: O(n*k)
n = Size of the array
k = Window size
Implementation:
C++
#include <iostream>
#include <vector>
using namespace std;
vector< int > Diffs(vector< int > const & a, int k)
{
vector< int > out;
vector< int > inc, dec;
inc.resize(a.size(), 0);
dec.resize(a.size(), 0);
int inc_sum = 0;
int dec_sum = 0;
for ( int i = 0; i < a.size(); ++i) {
for ( int j = i - 1; j >= 0 && j > i - k &&
a[j + 1] > a[j]; --j) {
++inc[j];
++inc_sum;
}
for ( int j = i - 1; j >= 0 && j > i - k &&
a[j + 1] < a[j]; --j) {
++dec[j];
++dec_sum;
}
if (i >= k - 1) {
if (i >= k) {
inc_sum -= inc[i - k];
dec_sum -= dec[i - k];
}
out.push_back(inc_sum - dec_sum);
}
}
return out;
}
int main()
{
vector< int > out = Diffs({ 10, 20, 30, 15, 15}, 3);
for ( int n : out)
cout << n << ", " ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector<Integer> Diffs( int []a, int k)
{
Vector<Integer> out = new Vector<Integer>();
int [] inc, dec;
inc = new int [a.length];
dec = new int [a.length];
int inc_sum = 0 ;
int dec_sum = 0 ;
for ( int i = 0 ; i < a.length; ++i)
{
for ( int j = i - 1 ; j >= 0 && j > i - k &&
a[j + 1 ] > a[j]; --j)
{
++inc[j];
++inc_sum;
}
for ( int j = i - 1 ; j >= 0 && j > i - k &&
a[j + 1 ] < a[j]; --j)
{
++dec[j];
++dec_sum;
}
if (i >= k - 1 )
{
if (i >= k)
{
inc_sum -= inc[i - k];
dec_sum -= dec[i - k];
}
out.add(inc_sum - dec_sum);
}
}
return out;
}
public static void main(String[] args)
{
int []arr = { 10 , 20 , 30 , 15 , 15 };
Vector<Integer>out = Diffs(arr, 3 );
for ( int n : out)
System.out.print(n + ", " );
}
}
|
Python3
def Diffs(a, k):
out, inc, dec = [], [ 0 ] * len (a), [ 0 ] * len (a)
inc_sum, dec_sum = 0 , 0
for i in range ( 0 , len (a)):
j = i - 1
while (j > = 0 and j > i - k and
a[j + 1 ] > a[j]):
inc[j] + = 1
inc_sum + = 1
j - = 1
j = i - 1
while (j > = 0 and j > i - k and
a[j + 1 ] < a[j]):
dec[j] + = 1
dec_sum + = 1
j - = 1
if i > = k - 1 :
if i > = k:
inc_sum - = inc[i - k]
dec_sum - = dec[i - k]
out.append(inc_sum - dec_sum)
return out
if __name__ = = "__main__" :
out = Diffs([ 10 , 20 , 30 , 15 , 15 ], 3 )
for n in out:
print (n, end = ", " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< int > Diffs( int []a, int k)
{
List< int > Out = new List< int >();
int [] inc, dec;
inc = new int [a.Length];
dec = new int [a.Length];
int inc_sum = 0;
int dec_sum = 0;
for ( int i = 0; i < a.Length; ++i)
{
for ( int j = i - 1; j >= 0 && j > i - k &&
a[j + 1] > a[j]; --j)
{
++inc[j];
++inc_sum;
}
for ( int j = i - 1; j >= 0 && j > i - k &&
a[j + 1] < a[j]; --j)
{
++dec[j];
++dec_sum;
}
if (i >= k - 1)
{
if (i >= k)
{
inc_sum -= inc[i - k];
dec_sum -= dec[i - k];
}
Out.Add(inc_sum - dec_sum);
}
}
return Out;
}
public static void Main(String[] args)
{
int []arr = { 10, 20, 30, 15, 15};
List< int >Out = Diffs(arr, 3);
foreach ( int n in Out)
Console.Write(n + ", " );
}
}
|
Javascript
<script>
function Diffs( a, k)
{
let out = [];
let inc, dec;
inc = [];
dec = [];
for (let i = 0;i<a.length;i++){
inc.push(0);
dec.push(0);
}
let inc_sum = 0;
let dec_sum = 0;
for (let i = 0; i < a.length; ++i) {
for (let j = i - 1; j >= 0 && j > i - k &&
a[j + 1] > a[j]; --j) {
++inc[j];
++inc_sum;
}
for (let j = i - 1; j >= 0 && j > i - k &&
a[j + 1] < a[j]; --j) {
++dec[j];
++dec_sum;
}
if (i >= k - 1) {
if (i >= k) {
inc_sum -= inc[i - k];
dec_sum -= dec[i - k];
}
out.push(inc_sum - dec_sum);
}
}
return out;
}
let out = Diffs([10, 20, 30, 15, 15], 3);
document.write(out)
</script>
|
Output:
3, 0, -1,
Last Updated :
09 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...