Question

There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of...

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

Homework Answers

Answer #1

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);
       }
   }
  

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given the following function: int bar(int n) {     if( n < 0 )         return...
Given the following function: int bar(int n) {     if( n < 0 )         return -2;     else if (n <= 1)         return 2;     else       return (3 + bar(n-1) + bar(n-2)); } What is returned by bar (4)? Show all the steps
In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5,...
In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. Write a function int fib(int n) that returns Fn. For example, if n = 0, then fib() should return 0, PROGRAM: C
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy....
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy. Disk blocks are numbered consecutively from the beginning of the file with the first block numbered as 0. Assume that blocks are 4096 bytes in size. Use the supplied C++ files to implement your LRU Buffer Pool based on the instructions below. • Implement a BufferBlock class using the supplied BufferBlockADT.h o Your Buffer Block must inherit BufferBlockADT or you will not get credit...
Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained...
Q2- Write a function solution that, given an integer N, returns the maximum possible value obtained by inserting one '5' digit inside the decimal representation of integer N. Examples: 1. Given N = 268, the function should return 5268. 2. Given N = 670, the function should return 6750. 3. Given N = 0, the function should return 50. 4. Given N = −999, the function should return −5999. Assume that: N is an integer within the range [−8,000..8,000].
In C++ Please, Write a program that (1) prompts for two integers, (2) prints out their...
In C++ Please, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
6.25.1: Unit testing C++ Add two more statements to main() to test inputs 3 and -1....
6.25.1: Unit testing C++ Add two more statements to main() to test inputs 3 and -1. Use print statements similar to the existing one (don't use assert). #include <iostream> using namespace std; // Function returns origNum cubed int CubeNum(int origNum) { return origNum * origNum * origNum; } int main() { cout << "Testing started" << endl; cout << "2, expecting 8, got: " << CubeNum(2) << endl; /* Your solution goes here */ cout << "Testing completed" << endl;...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
1. Given that 1 /1−x = ∞∑n=0 x^n with convergence in (−1, 1), find the power...
1. Given that 1 /1−x = ∞∑n=0 x^n with convergence in (−1, 1), find the power series for x/1−2x^3 with center 0. ∞∑n=0= Identify its interval of convergence. The series is convergent from x= to x= 2. Use the root test to find the radius of convergence for ∞∑n=1 (n−1/9n+4)^n xn
Problem B.4 The function ?1 = sin(3?) is a solution to ? ′′ + 9? =...
Problem B.4 The function ?1 = sin(3?) is a solution to ? ′′ + 9? = 0. This second-order ODE can be reduced to the first-order ODE ?′ sin(3?) + 6?cos(3?) = 0. Find a second linearly independent solution ?2. Also, obtain the general solution. Do not use the textbook’s formula 5. (Note: If you perform u-substitution to evaluate an integral, notice that the symbol ? normally used in the u-substitution is not the same as the ? in the...