fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int t = sc.nextInt();
  14. while(t-->0){
  15. int n = sc.nextInt();
  16. int a[]=new int[n];
  17. int res[] = new int[n];
  18. for(int i=0;i<n;i++){
  19. a[i]=sc.nextInt();
  20. }
  21.  
  22. //As last element will always have a alternate length of 1.
  23. res[n-1]=1;
  24. char sign=getSign(a[n-1]);
  25.  
  26. for(int i=n-2;i>=0;i--){
  27. if(sign!=getSign(a[i])){
  28. res[i]=res[i+1] + 1;
  29. sign = getSign(a[i]);
  30. }else{
  31. res[i]=1;
  32. }
  33. }
  34.  
  35. for(int i=0;i<n;i++){
  36. System.out.print(res[i]+" ");
  37. }
  38. System.out.println();
  39. }
  40. }
  41. private static char getSign(int a){
  42. if(a<0)
  43. return '-';
  44. return '+';
  45. }
  46. }
Success #stdin #stdout 0.07s 4386816KB
stdin
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
stdout
1 1 1 1 
4 3 2 1 
1 1 3 2 1 1