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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 02:30, woodpeckerdeejpe8wvh
How to launch an app: steps to be successful? launching an app is a great idea, but it’s not that easy as we supposed to think. the majority of mobile applications don’t generate revenue because companies aren’t ready to be competitive. referring to our experience in successfully building and launching apps we hope to you omit these difficulties. we are going to talk about ideas, marketing, testing your product, its development, distribution and support. you will learn 8 product launch stages to succeed.
Answers: 1
image
Computers and Technology, 23.06.2019 03:00, SKYBLUE1015
What are the different parts of computer
Answers: 2
image
Computers and Technology, 23.06.2019 03:10, nxusasmangaliso8780
Fill in the following program so that it will correctly calculate the price of the orange juice the user is buying based on the buy one get one sale.#include //main functionint main() { int cartons; float price, total; //prompt user for input information printf("what is the cost of one container of oj in dollars? \n"); scanf(" [ select ] ["%d", "%c", "%f", "%lf"] ", & price); printf("how many containers are you buying? \n"); scanf(" [ select ] ["%d", "%c", "%f", "%lf"] ", & cartons); if ( [ select ] ["cartons / 2", "cartons % 1", "cartons % 2", "cartons % price", "cartons / price", "cartons / total"] [ select ] ["=", "==", "! =", "< =", "> =", "< "] 0) total = [ select ] ["price * cartons", "cartons * price / 2 + price", "(cartons / 2) * price", "cartons / (2.0 * price)", "(cartons / 2.0) * price + price", "((cartons / 2) * price) + price"] ; else total = ((cartons / 2) * price) + price; printf("the total cost is $%.2f.\n", total); return 0; }
Answers: 2
image
Computers and Technology, 24.06.2019 02:00, ishmael9332
How are we able to create photographs differently than 100 years ago? explain your answer in relation to your photograph you selected.
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 in other subjects:

Konu
English, 20.03.2021 01:00
Konu
Chemistry, 20.03.2021 01:00