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 12:10
2. fabulously fit offers memberships for$35 per month plus a $50 enrollmentfee. the fitness studio offersmemberships for $40 per month plus a$35 enrollment fee. in how many monthswill the fitness clubs cost the same? what will the cost be?
Answers: 1
question
Computers and Technology, 24.06.2019 03:00
Click the "draw structure" button to activate the drawing utility. draw two diastereomers of (1z,4r)βˆ’1,4βˆ’dimethylcyclodecene and name them, including (e)/(z) and (r)/(s) notation. part 1 out of 4 draw the diastereomer containing a chiral center with s configuration here. window open
Answers: 1
question
Computers and Technology, 24.06.2019 22:10
Function name: poly parameters: int returns: int description: a polynomial of degree n with coefficients a0,a1,a2,a3, . . ,an is the function p(x) = a0+a1x+a2x2+a3 βˆ— x3+ . . +an βˆ— xn this function can be evaluated at different values of x. for example, if: p(x) = 1+2x+ x2, then p(2) = 1+2 βˆ— 2+22 = 9. if p(x) = 1+x2+x4, then p(2) = 21 and p(3) = 91. write a function poly() that takes as input a list of coefficients a0, a1, a2, a3, . . , an of a polynomial p(x) and a value x. the function will return poly(x), which is the value of the polynomial when evaluated at x.
Answers: 3
question
Computers and Technology, 24.06.2019 22:30
Telling a computer that is already on to turn again is known as what type of boot?
Answers: 1
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
question
History, 29.01.2021 19:20
question
English, 29.01.2021 19:20
question
English, 29.01.2021 19:20
question
Mathematics, 29.01.2021 19:20
question
Mathematics, 29.01.2021 19:20
question
Mathematics, 29.01.2021 19:20