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
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line argument and it is -hw you should simply print hello world and then exit(0). Otherwise, the program should provide a function named mm_alloc. This is the function prototype: void *mm_alloc(int num_bytes_to_allocate) mm_alloc function should obtain space for the user from a block which you obtain via mmap. You should obtain the block on the first invocation and the block size should be some number...
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
For different input n, i.e., n = 1, n = 10, n = 20, write down...
For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation. #include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n;...
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...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
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].
Write a program which inputs an integer value, incr, from the user at the keyboard, and...
Write a program which inputs an integer value, incr, from the user at the keyboard, and then displays a graph of the sine of the degree values from 0 to 180, in increments of incr. You must use the sin function (in a similar manner to ceil and floor) to find the sine. Note that the sin function only operates on radians so the degree value must first be converted to radians. To do so, multiply the degree value by...