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, 22.06.2019 20:00
What is the worst-case complexity of the maxrepeats function? assume that the longest string in the names array is at most 25 characters wide (i.e., string comparison can be treated as o( class namecounter { private: int* counts; int nc; string* names; int nn; public: namecounter (int ncounts, int nnames); int maxrepeats() const; }; int namecounter: : maxrepeats () { int maxcount = 0; for (int i = 0; i < nc; ++i) { int count = 1; for (int j = i+1; j < nc; ++j) { if (names[i] == names[j]) ++count; } maxcount = max(count, maxcount); } return maxcount; }
Answers: 3
question
Computers and Technology, 22.06.2019 20:00
What statement best describes operating systems? it’s possible for modern computers to function without operating systems. most operating systems are free or very inexpensive. operating systems are managed by the computer’s microprocessor (cpu). operating systems manage the computer’s random access memory (ram).
Answers: 1
question
Computers and Technology, 22.06.2019 23:30
To check spelling errors in a document, the word application uses the to determine appropriate spelling. internet built-in dictionary user-defined words other text in the document
Answers: 1
question
Computers and Technology, 23.06.2019 03:50
Iam a bacterium. i cause stomach cramps and diarrhea. i am caused by eating rotten foodssuch as chicken, fish, or eggs. sometimes turtles carry my bacteria.what am i?
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
question
English, 29.04.2021 02:20
question
Mathematics, 29.04.2021 02:20
question
Mathematics, 29.04.2021 02:20
question
Biology, 29.04.2021 02:20
Questions on the website: 13722361