Created
May 27, 2019 06:41
-
-
Save mohitkhosla01/52a9b9b51594c11abb6174d7848f3dc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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