 # Need help in Google Kickstart Round E problem B

I have written the code in java but i think logic is clear.
i tried to first add no of buildings A, then added no of buildings seen niether by A or B. then c buildings of height n and then B buildings. I have taken care of overlap of c a and b.
if someone could help with test cases or logic

``````    import java.util.Scanner;
class Solution
{
public static void main(String args[])
{
int t,n,a1,b,c,w,k,i,j,v;
Scanner sc= new Scanner (System.in);
t=sc.nextInt();
w=t;
while(t>0)
{
t--;
n=sc.nextInt();
a1=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
k=n;v=0;
long a[] =new long[n];
System.out.print("Case #"+(w-t)+": ");
if((a1+b+1)!=(n+c))
{System.out.print("IMPOSSIBLE");
System.out.println();}
else
{
i=0;
for(i=0;i<(a1-c);i++)
{
a[i]=2;
}
for(j=0;j<(n+c-(a1+b)) && (a1-c)>0;j++)
{
a[i]=1;
i++;
v++;
}

for(j=1;j<=c;j++)
{
a[i]=n;
i++;
}
for(j=0;j<(n+c-(a1+b)-v) && (b-c)>0;j++)
{
a[i]=1;
i++;
}
for(i=i;i<n;i++)
a[i]=n-2;

for(i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
}
}
``````

please format your code

1 Like

Why this condition? Check the case
1
5 2 2 1
Your code gives the answer IMPOSSIBLE
But there exist answers like 4 5 1 1 4

My approch was to first find the number of buildings seen only by A, only by B, by both and by none. Let’s say these are a1, b1, c, and d = n-(a1+b1+c). If d is negative, the answer is impossible otherwise I assigned (n-1) a1 times, n c times and (n-1) b1 times. Then d 1s can be inserted between any two buildings.

3 Likes

okay now i see. will update the code and try again.

done. sorry for the late reply