There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of frogs were sitting together on one block when they had a terrible quarrel. Now they want to jump away from one another so that the distance between them will be as large as possible. The distance between blocks numbered J and K, where JK, is computed as K-J+1. The frogs can only jump up, meaning that they can move from one block to another only if the two blocks are adjacent and the second block is of the same or greater height as the first. What is the longest distance that they can possibly create between each other, if they also chose to sit on the optimal starting block initially? Write a function: class Solution { public int solution(int[] blocks); } that, given an array blocks consisting of N integers denoting the heights of the blocks, returns the longest possible distance that two frogs can make between each other starting from one of the blocks. Examples: 1. Given blocks = 12, 6, 8, 5), the function should return 3. If starting from blocks[0), the first frog can stay where it is and the second frog can jump to blocks[2] (but not to blocks[3]). Test Output Fres Examples: tas tes 11. Given blocks = 12,6,8,5), the function should return 3. If starting from blocks[0], the first frog can stay where it is and the second frog can jump to blocks[2] (but not to blocks[3]). 0 1 2 3 Test Output 2. Given blocks = (1,5, 5, 2, 6), the function should return 4. If starting from blocks(3), the first frog can jump to blocks[1], but not blocks[0], and the second frog can jump to blocks[4]. O Java 8 Files IN 2. Given blocks = [1,5, 5, 2, 6), the function should return 4. If starting from blocks(3), the first frog can jump to blocks(1), but not blocks[0), and the second frog can jump to blocks[4]. task2 solutions test-input. 0 2 3. Given blocks = [1,1], the function should return 2. If starting from blocks(11. the first frog can jump to blocks[0] and the second frog can stay where it is. Starting from blocks[0] would result in the same distance. Test Output ? Write an efficient algorithm for the following assumptions: . N is an integer within the range [2..200,000); • each element of array blocks is an Integer within the range [1..1,000,000,000). ☆ 1h 50m Submit Task Files task2 solution.java test-input.txt solution.java 1 VI you can also use imports, for example: 2 // import java.util.*; 3 4 // you can write to stdout for debugging purposes, e.g. 5 // System.out.println("this is a debug message"); 6 9 7 class Solution { 8 public int solution(int[] blocks) { 1/ write your code in Java SE 8 10 } 11 ) I 12 11 11. Al changes saved Run Tests
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static int solution(int[] blocks) {
int longestDistance = 0;
int[] sortedBlocks =
Arrays.copyOf(blocks,blocks.length);
Arrays.sort(sortedBlocks);
/*
* int i;
for(i=0;i<blocks.length;i++) { if(sortedBlocks[0]==blocks[i]) {
break;
* } }
if(i>blocks.length/2) { if(i-1>0) { if(i+1<blocks.length)
{ if(i-2>0 &&
*
blocks[i-2]!=blocks[i-1]) { longestDistance=(i+1)-(i-1)+1; }else
if(i-2>0 &&
*
blocks[i-2]==blocks[i-1]) { longestDistance=(i+1)-(i-2)+1; } } }
}
*/
for(int n
=0;n<blocks.length;n++) {
if(blocks.length == 2) {
longestDistance =
1-0+1;
break;
}
int m;
for( m =0;m<blocks.length;m++) {
if(sortedBlocks[n]==blocks[m]) {
break;
}
}
if(blocks.length == 3) {
if(m > 0 && m <
blocks.length-1 ) {
if(blocks[m-1]>blocks[m] && blocks[m+1]>blocks[m])
{
longestDistance = 2-0+1;
}
else
if(blocks[m-1]>blocks[m] && blocks[m+1]<blocks[m])
{
longestDistance = 1-0+1;
}else {
longestDistance = 1-0+1;
}}else if(m==0) {
if(blocks[m]>blocks[m+1] && blocks[m+1]>blocks[m+2])
{
longestDistance = 2-0+1;
}else
{
longestDistance = 1-0+1;
}
}else {
if(blocks[m-2]>blocks[m-1] && blocks[m-1]>blocks[m])
{
longestDistance = 2-0+1;
}else
{
longestDistance = 1-0+1;
}
}
break;
}
if(m > 0 && m < blocks.length-1 )
{
if(m-1>=0) {
if(m+1<blocks.length) {
if(m-2>0 && blocks[m-2]!=blocks[m-1])
{
longestDistance=(m+1)-(m-1)+1;
}else if(m-2>0 &&
blocks[m-2]>=blocks[m-1]) {
longestDistance=(m+1)-(m-2)+1;
}else if(m-2 < 0) {
if(m+1<=blocks.length-1
&& blocks[m+1]>blocks[m]) {
longestDistance=(m+1)-(m-1)+1;
if(m+2<=blocks.length-1 && blocks[m+2]>blocks[m+1])
{
longestDistance=(m+2)-(m-1)+1;
}
}
}
}
}
break;
}
else {
continue;
}
}
/*
* int j;
for(j=0;j<blocks.length;j++) {
*
if(sortedBlocks[blocks.length/2]==blocks[i]) { break; }
*/
return
longestDistance;
}
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.println("Enter number of
blocks : \n");
int numberOfBlocks =
scanner.nextInt();
if(numberOfBlocks>=2 &&
numberOfBlocks <= 200000) {
int[] blockArray
= new int[numberOfBlocks];
for(int
j=0;j<numberOfBlocks;j++) {
System.out.println("Enter height of "+j+ " block");
blockArray[j] =
scanner.nextInt();
}
int longestDistance =
solution(blockArray);
System.out.println("Longest
distance between the frogs : "+ longestDistance);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.