Computers and Technology

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.

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 17:30, kenyasutton10
What are the most commonly found items in the trash according to the municipal solid waste report?
Answers: 1
image
Computers and Technology, 24.06.2019 02:30, journeyhile5
How to apply the fly in effect to objects on a slide
Answers: 1
image
Computers and Technology, 24.06.2019 10:10, juliana0122
Scanning the road can be thought of as a
Answers: 2
image
Computers and Technology, 24.06.2019 11:40, girdwood6678
100 pts. first person gets brainliest
Answers: 2
Do you know the correct answer?
We'll say that a "reverse" section in an array is a group of contiguous elements such that somewhere...

Questions in other subjects:

Konu
Mathematics, 31.08.2020 01:01