subject

We'll say that a "reverse" section in an array is a group of contiguous elements such that somewhere in the array. For example, the largest reverse section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 position 5. Return the size of the largest reverse section found in the given array. Also, return the last position where the reverse starts or return all if they are all in reverse order. findReverse([1, 2, 3, 8, 9, 3, 2, 1]) β†’ 3 position 5
findReverse([1, 2, 1, 4]) β†’ 2 position 1
findReverse([7, 1, 2, 9, 7, 2, 1]) β†’ 2 position 5

findReverse([7, 1, 2, 9, 7, 2, 10]) β†’ none position none

findReverse([10, 9, 8, 7, 6, 5, 4]) β†’ all
position /*
My code written:

package reversepositions;

import java. util.*;
public class ReversePositions {

public static void main(String[] args) {
// TODO code application logic here
int[] nums = {1,2,3,8,9,3,2,1};
int[] nums2 = {1,2,1,4};
int[] nums3 = {7,1,2,9,7,2,1};
int[] nums4 = {7,1,2,9,7,2,10};
int[] nums5 = {10,9,8,7,6,5,4};

int n = nums. length;
System. out. println(findReverse(nums, n));

n = nums2.length;
System. out. println(findReverse(nums2, n));

n = nums3.length;
System. out. println(findReverse(nums3, n));

n = nums4.length;
System. out. println(findReverse(nums4, n));

n = nums5.length;
System. out. println(findReverse(nums5, n));

}

public static int findReverse(int nums[], int n)
{
HashSet sequence = new HashSet ();
for(int i = 0; i < n; i++)
sequence. add(nums[i]);

int reverse = 0;
for(int i = 0; i < n; i++)
{
if(sequence. contains(nums[i])){
int a = nums[i];

while(sequence. contains(a))
a++;
reverse = Math. max(reverse, a - nums[i]);

}

}

return reverse;

}
}
I have basically solved the problem like a third or half way through. The only problems I have now is that I need a code to find the contiguous of the reverse order, so instead of 1,2,3,8,9,3,2,1, I need it to find the order of 1,2,3,9,8,3,2,1. So I need a way to reverse the order of the array in which it will go through to find the contiguous sequence. Also, for arrays nums4 and nums5 the output I get is wrong. The correct output should be nums4 to be none and for nums5 to be all, but instead I get 7 and 3, so I want to know if there is a way to fix this. Finally, I need a way to find the position in which the contiguous sequence occurs. You can find what positions each array should be above in the example. i have tried many ways to solve this problem but none of them seem to work the way I want them to.

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 06:00
What machine listens for http requests to come in to a website’s domain? a. a router b. a browser c. a server d. a uniform resource locator
Answers: 1
question
Computers and Technology, 23.06.2019 12:30
How is the brightness of oled of the diaplay is controled
Answers: 1
question
Computers and Technology, 24.06.2019 02:30
How to apply the fly in effect to objects on a slide
Answers: 1
question
Computers and Technology, 24.06.2019 06:30
For which utilities, if any, does the landlord pay?
Answers: 2
You know the right answer?
We'll say that a "reverse" section in an array is a group of contiguous elements such that somewhere...
Questions
Questions on the website: 13722361