Question

f a as a sequence of adjacent array elements such that each value in the run array was of size 10 9. We define a run of ele

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi

First of all, the definition of run given here is right but the example does not state the right answer.

as in the given example
input:
7 2 3 9 10 11 12 15 16 9
here the run is from 2 to 16, in increasing order
therefore here run = 8
2<3<9<10<11<12<15<16

import java.util.Arrays;
import java.util.Stack;
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
  
System.out.println("enter number of elements");
//size of elements to enter
int n=s.nextInt();
  
int arr[]=new int[n];
  
System.out.println("enter elements");
//take input array
for(int i=0;i<n;i++){//for reading array
arr[i]=s.nextInt();
  
}//make instance of current class and call reverseList
Main m = new Main();
m.getLongestSeq(arr,n);
}
  
// Prints longest sequence of
// positive integers in an array.
void getLongestSeq(int a[], int size){
int startIndex=0,endIndex=0,count = 1, run = 1;
//iterate over the array
for (int i = 1; i < a.length; i++) {
//check previous element is lesser than current
if (a[i] >= a[i - 1]) {
count++;
} else {
count = 1;
}
//run will be atleast 1 i.e. the element itself, here count acts as temporary variable
if (count > run) {
endIndex = i+1;
run = count;
}
}
startIndex = endIndex - run -1; //it is hard to track startIndex therefore this is best way :)
System.out.println("run length:"+run);
System.out.println("startIndex of run:"+startIndex);
System.out.println("endIndex of run:"+endIndex);
}
  
}

HIT LIKE

Add a comment
Know the answer?
Add Answer to:
F a as a sequence of adjacent array elements such that each value in the run array was of size 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In an array, a "run" occurs when 2 or more consecutive elements match. For example in...

    In an array, a "run" occurs when 2 or more consecutive elements match. For example in the array {2, 3, 3, 3, 3, 5, 5 6} there is a run of threes, and a run of fives. The length of the run of threes is 4 (because there are 4 threes in a row). Write a method that returns the length of the longest run. If no run occurs, return 0. maxRun([2, 3, 3, 3, 3, 5, 5, 6]) rightarrow...

  • Let S be a sequence of n distinct integers stored in an array as array elements...

    Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...

  • (20 points) You are given an array A of distinct integers of size n. The sequence...

    (20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...

  • Write a C++ program to : Create array arr1 of size 10 Assign values to elements...

    Write a C++ program to : Create array arr1 of size 10 Assign values to elements such that - say index = i, arr1[i] = 10 - i. Print arr1 Create arr2 and copy alternate elements from arr1 starting from index 1. Print arr2. Sort arr1 in ascending array and print its elements from indices 3 to 8 Print every output on a different line and separate array elements by spaces.

  • 4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of...

    4 CS136: Computer Science II-Spring 2019 [10 points] Problem #4 A run is a sequence of adjacent repeated values. write a Java class (RunsPrinter. java) with one main method that generates a sequence of 20 random die tosses in an array (and not an ArrayList) and then prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 124 3 (2 2 2 2) 3 6 (5 5) 6 3 1 Examples...

  • 1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from...

    1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from the left of the array, take 2 elements at a time and swap the two elements. Continue in this fashion until you reach the end of the array. Declare an integer array of size 10 and place random numbers in the range [0?9]. Print the original array, pairwise swap the elements of the array and print the final array after swap. Consider the following...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is...

    2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...

  • Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A...

    Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...

  • Write a program that reads a sequence of integers into an array and that computes the...

    Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data: 2 1 4 9 16 9 7 4 9 11 Then it computes 2 - 1 + 4 - 9 + 16 - 9 + 7 - 4 + 9 – 11 = 4 Use a scanner object to gather the inputs from the user....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT