Streak Star
Source: codechef
Difficulty: 1129

Streak Star
The
Streak Value
of an array$B$is defined as the maximum length of a non-decreasing subarray, more formally:$\max_{1 \leq i \leq j \leq N} (j - i + 1) \quad \text{where} \quad B_i \leq B_{i+1} \leq B_{i+2} \leq \dots \leq B_j.$Chef has an array$A$of length$N$and a magical number$X$. You are allowed to perform the following operation at most once:
Select an index$i$, and update the element at$A_i$by multiplying it with$X$, i.e., set$A_i:=A_i\cdot X$Your task is to find the maximum possible Streak Value achievable for array$A$.
Input Format
The first line of input will contain a single integer$T$, denoting the number of test cases.
Each test case consists of multiple lines of input.
The first line of each test case contains two space-separated integers$N$and$X$— the length of array and magical number respectively.
The second line of each test case contains$N$space-separated integers$A_1,A_2,A_3...A_N$— the elements of the array.
Output Format
For each test case, output on a new line the maximum possible
Streak Value
of$A$.
Constraints$1 \leq T \leq 10^3$$1 \leq N, X \leq 10^3$$1 \leq A_i \leq 10^5$The sum of$N$over all test cases won't exceed$10^3$.
Sample 1:
Explanation:
Test case$1$:
It is optimal to select index$3$, which changes the array$A$to$[1, 2, 3, 4, 2]$. The Streak Value for the array$[1, 2, 3, 4, 2]$is$4$, as the longest non-decreasing subarray is$[1, 2, 3, 4]$.
Test case$2$:
Its optimal to select index$3$, which changes the array$A$to$[2,5,30]$.
The Streak Value for the array$[2,5,30]$is$3$, as the entire array is already non-decreasing.
Test case$3$:
In this case, it is optimal to either not perform any operation or perform the operation on index$4$, both of which result in a Streak Value of$3$.

Sample Input:
3
5 3
1 2 1 4 2
3 10
2 5 3
4 5
90 2 5 6

Sample Output:
4
3
3

C++ solution:
#include <bits/stdc++.h>
using namespace std;

int maxLength(vector<int>arr,int size,int magicNum){
    int maxLen = 1;
    for(int i=0;i<size;i++){
        int originalArr = arr[i];
        arr[i]=arr[i]*magicNum;
        int count = 1;
        for(int j=1;j<size;j++){
            if(arr[j]>=arr[j-1]){
                count++;
            }else{
                count = 1;
            }
            if(count>maxLen){
                maxLen=count;
            }
        }
        arr[i]=originalArr;
    }
    return maxLen;
}
int main() {
    // your code goes here
     int T;
    cin >> T;
    while (T--) {
        int N, X;
        cin >> N >> X;
        vector<int> arr(N);
        for (int i = 0; i < N; i++) cin >> arr[i];
        cout << maxLength(arr, N, X) << "\n";
    }
    return 0;

}

Go solution:
package main

import (
    "fmt"
)

func Mx1A2B(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func ZxY9WvQ(arr []int, n int) int {
    mxS1T, crS1T := 1, 1
    for i := 1; i < n; i++ {
        if arr[i] >= arr[i-1] {
            crS1T++
        } else {
            crS1T = 1
        }
        mxS1T = Mx1A2B(mxS1T, crS1T)
    }
    return mxS1T
}

func Slv3T() {
    var n, x int
    fmt.Scan(&n, &x)
    A1Z2X := make([]int, n)
    B3Y4W := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&A1Z2X[i])
        B3Y4W[i] = A1Z2X[i]
    }
    
    mxS1T := ZxY9WvQ(A1Z2X, n)
    for i := 0; i < n; i++ {
        B3Y4W[i] = A1Z2X[i] * x
        mxS1T = Mx1A2B(mxS1T, ZxY9WvQ(B3Y4W, n))
        B3Y4W[i] = A1Z2X[i]
    }
    
    fmt.Println(mxS1T)
}

func main() {
    var T5Q9X int
    fmt.Scan(&T5Q9X)
    for T5Q9X > 0 {
        Slv3T()
        T5Q9X--
    }
}

Java solution:
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt(),m=0,val=0;
            int ans=0,ans1=0,key=0;
            int k=sc.nextInt();
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=sc.nextInt();
            if(n==1)
                System.out.println("1");
            else
            {
                for(int j=0;j<n;j++)
                {
                    ans=0;
                    key=0;
                    int index=-1;
                    for(int i=j;i<n-1;i++)
                    {
                        if(a[i]<=a[i+1])
                            ans++;
                        else
                        {
                            if(a[i]<=a[i+1]*k && key==0)
                            {
                                    ans++;
                                    val=a[i+1];
                                    index=i+1;
                                    a[i+1]=a[i+1]*k;
                                    m=1;
                                    key=1;
                            }
                            else
                                break;
                        }
                    }
                    if(m==1)
                    {
                        a[index]=val;
                        m=0;
                    }
                    ans1=Math.max(ans,ans1);
                }
                System.out.println(ans1+1);
            }
            
        }
    }
}

JavaScript solution:
process.stdin.resume();
process.stdin.setEncoding("utf8");

let input = "";
process.stdin.on("data", (chunk) => {
    input += chunk;
});

process.stdin.on("end", () => {
    let lines = input.trim().split("\n");
    let T = parseInt(lines[0]);
    let index = 1;

    while (T--) {
        let [N, X] = lines[index].split(" ").map(Number);
        let A = lines[index + 1].split(" ").map(Number);
        index += 2;

        function findMaxStreak(arr) {
            let maxStreak = 1, currentStreak = 1;
            for (let i = 1; i < arr.length; i++) {
                if (arr[i] >= arr[i - 1]) {
                    currentStreak++;
                } else {
                    currentStreak = 1;
                }
                maxStreak = Math.max(maxStreak, currentStreak);
            }
            return maxStreak;
        }
        
        let originalStreak = findMaxStreak(A);
        let maxPossibleStreak = originalStreak;
        
        for (let i = 0; i < N; i++) {
            let modifiedArray = [...A];
            modifiedArray[i] *= X;
            let newStreak = findMaxStreak(modifiedArray);
            maxPossibleStreak = Math.max(maxPossibleStreak, newStreak);
        }

        console.log(maxPossibleStreak);
    }
});

Python solution:
def sol(n,x,a):
    ms=1
    c=1
    for i in range(1,n):
        if a[i]>=a[i-1]:
            c+=1
        else:
            c=1
        ms=max(ms,c)
    for i in range(n):
        o=a[i]
        a[i]=o*x
        l=i
        while l>0 and a[l-1]<=a[l]:
            l-=1
        r=i
        while r+1<n and a[r]<=a[r+1]:
            r+=1
        s=r-l+1
        ms=max(ms,s)
        a[i]=o
    return ms
for _ in range(int(input())):
    n,x=map(int,input().split())
    a=list(map(int,input().split()))
    print(sol(n,x,a))

Ruby solution:
def calculate_max_streak(arr)
  return 0 if arr.empty?
  
  max_len = 1
  current_len = 1
  
  (1...arr.size).each do |i|
    if arr[i] >= arr[i-1]
      current_len += 1
      max_len = [max_len, current_len].max
    else
      current_len = 1
    end
  end
  
  max_len
end

def main
  input = $stdin.read.split("\n")
  idx = 0
  t = input[idx].to_i
  idx += 1
  
  results = []
  
  t.times do
    n, x = input[idx].split.map(&:to_i)
    idx += 1
    a = input[idx].split.map(&:to_i)
    idx += 1
    
    max_streak = calculate_max_streak(a)
    
    # Try multiplying each element by X
    n.times do |i|
      modified = a.dup
      modified[i] *= x
      current_streak = calculate_max_streak(modified)
      max_streak = [max_streak, current_streak].max
    end
    
    results << max_streak
  end
  
  puts results
end

main