You are on page 1of 3

Question 1 :

class Solution{
// blocks : 1 5 5 2 6
// left : 0 0 1 2 0
// right : 2 1 0 1 0

// if both frogs at 0'th block then left[0] + right[0] + 1 = 0 + 2 + 1 = 3


// if both frogs at 1'th block then left[1] + right[1] + 1 = 0 + 1 + 1 = 2
// if both frogs at 2'nd block then left[2] + right[2] + 1 = 1 + 0 + 1 = 2
// if both frogs at 3'rd block then left[3] + right[3] + 1 = 2 + 1 + 1 = 4
// if both frogs at 4'th block then left[4] + right[4] + 1 = 0 + 0 + 1 = 1

// so maximum distance is 4 when both frog are at 3rd block


public static int solution(int[] blocks){
int n = blocks.length;

int[] left = new int[n];


int[] right = new int[n];

// left[i] of left array stores about how


// many elements left to ith block whoose height is greater
// than or equal to block[i]
left[0] = 0;
for(int i = 1; i < n; i++){
if(blocks[i] <= blocks[i - 1])
left[i] = left[i - 1] + 1;
else
left[i] = 0;
}

// right[i] of right array stores about how


// many elements right to ith block whoose height is greater
// than or equal to block[i]
right[n - 1] = 0;
for(int i = n - 2; i >= 0; i--){
if(blocks[i] <= blocks[i + 1])
right[i] = right[i + 1] + 1;
else
right[i] = 0;
}

// placing boths frog at all blocks one by one


// and picking the place which can make both forgs
// at maximum distance apart
int Max = 0;
for(int i = 0; i < n; i++){
int x = left[i] + right[i] + 1;
if(x > Max)
Max = x;
}
// and returning the final result

return Max;
}
}

Question 3 :
class Solution {

public int[] solution(String R, int[]V) {

int a=0,b=0;
int[] sol=new int[2];

int mina=0,minb=0;

for (int i = 0; i < R.length(); i++)


{

if (R.charAt(i)=='A'){

a+=V[i];
b-=V[i];

}else{

a-=V[i];
b+=V[i];

mina=Math.min(mina,a);
minb=Math.min(minb,b);

}
sol[0]=Math.abs(mina);
sol[1]=Math.abs(minb);

return sol;

}
}

Question 2:

You might also like