Skip to content

Instantly share code, notes, and snippets.

@mohitkhosla01
Created May 27, 2019 06:41
Show Gist options
  • Save mohitkhosla01/52a9b9b51594c11abb6174d7848f3dc2 to your computer and use it in GitHub Desktop.
Save mohitkhosla01/52a9b9b51594c11abb6174d7848f3dc2 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.lang.*;
import java.io.*;
class AlternatingSubarrayPrefix
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for(int w=0; w<t; ++w) {
int n = sc.nextInt();
sc.nextLine();
int []arr = new int[n];
for(int i=0; i<n; ++i) {
int temp = sc.nextInt();
arr[i] = temp > 0 ? 1 : -1;
}
sc.nextLine();
int []brr = new int[n];
brr[n-1] = 1;
for(int i=n-2; i>=0; --i) {
if(arr[i] != arr[i+1]) {
brr[i] = brr[i+1] + 1;
}
else {
brr[i] = 1;
}
}
for(int i=0; i<brr.length; ++i) {
System.out.print(brr[i] + " ");
}
System.out.println();
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment