Question

Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a tumble 12, 4 (with height 8) and another tumble 7, 2, 1 (with height 6). Note that we do not say 0, 4, 12 is a tumble because tumbles must be decreasing. Output the maximum height of a tumble. If there is no tumble (i.e. there are never consec- utive steps that decrease in height) then output 0 Input The first line contains a single integer 1 <n < 100, 000 denoting the number of steps. The second line contains n space-separated integers, each between 0 and 109, denoting the heights of the steps Output Output a single line containing an integer indicating the maximum height of a tumble in the input sequence. Sample Input 1 4 1 762 Sample Output 1 Explanation: The tumble 7, 6, 2 has height 5 (= 7-2) Sample Input 2 4 3 66 8 Sample Output 2

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

Hi,

Please find the program and code below.

If your requirement is in another programming language ., this code can be migrate to c and c++ easily.

package staticclasses;

import java.util.Scanner;

public class Tumble {

public static void main(String[] args) {

int size;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of steps");

size = sc.nextInt();

int tumble[]=new int[size];

System.out.println("Enter height of the steps");

for(int i=0;i<size;i++){

tumble[i] = sc.nextInt();

}

sc.close();

int minPoint=0,lastElement=0; //min point shows the tumble value at left side

int maxPoint=-1; //max point shows the tumble value at right side tubme 9 6 1, min =9 max=1

int tumbHeight=0; //it will store tumble height

minPoint = tumble[0]; //initialize min point from 0 position

for(int i=1;i<size;i++){  

lastElement=tumble[i-1]; //keep track of last element to see if new element is in increasing order or decreasing order

if(tumble[i]<lastElement){ //if new element is in increasing order

maxPoint = tumble[i]; //assign that to max point

}else{ //else

if(maxPoint!=-1){ //if maxpoint is not -1.

if(tumbHeight <( minPoint - maxPoint) ){ //calculate new tumble height.

tumbHeight = minPoint - maxPoint; //if new height is greater than old one set it

}

}

minPoint = tumble[i]; //min point is corrent element

maxPoint =-1; //max point is -1

}

}

if(maxPoint!=-1){ //this step is to check height for last element

if(tumbHeight <( minPoint - maxPoint) ){

tumbHeight = minPoint - maxPoint;

}

}

System.out.println("Tumble has height "+tumbHeight);

}

}

Quick Access ||| !哈Java EE Console X | <terminated> Tumble Java Application] CP Enter the size of steps Enter height of the s

Quick Access Java EEJava<C/c+ -O Console X Console terminated Tumble [Java Application] C Program FilesJa Enter the size of s

Add a comment
Know the answer?
Add Answer to:
Given an array of integers representing heights of consecutive steps, we say that a tumble is...
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
  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • Given an array A[] of integers find sum of product of all pairs of array elements...

    Given an array A[] of integers find sum of product of all pairs of array elements Input: First line consists of T test cases. First line of every test case consists of N, denoting number of elements of array. Second line consists of elements of array. Output: Single line output, print the sum of products. Constraints: 1<=T<=100 1<=N<=100 Solve the problem using pointers and (if possible) using operators Example: Input: 2 3 1 3 4 4 2 3 4 5...

  • C++ Given a sequence of integers, check to see if the sequence constitutes a heap Input:...

    C++ Given a sequence of integers, check to see if the sequence constitutes a heap Input: the first line is an integer n(0 < n < 10000), The second line is n integers Output: If the sequence is a very small heap, that is, the top of the heap is the smallest element, then output "min heap". If the sequence is a very large heap, the top of the heap is the largest element The output includes one line with...

  • I want this using while loop This using stringin python Use list or some thing in...

    I want this using while loop This using stringin python Use list or some thing in python Using list in python I want answer as soon as posdible E. Last Number time limit per test: 1 second memory limit per test: 256 megabytes input standard input output standard output You are given a sequence of positive integers aj, , 03, ... Print the last element of the sequence. Input The input consists of multiple lines. The i-th line contains a...

  • C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every...

    C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every time Bibi climbs a set of stairs, she counts the steps starting from 1 to the number of steps in that particular set of stairs while climbing the stairs. For example if she climbs two set of stairs, the first containing 5 steps and the second containing 3 steps, she will say 1, 2, 3, 4, 5, 1, 2, 3 and the total number...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up...

    Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each Input Format For this assignment we use adjacency matrices with positive integer weights. Here a zero entry at row i and column J indicates that no edge i] exists in the graph. The first...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a sequence of n positive integers a1,a2,…,an, where n is even. Swap adjacent elements in the given sequence and print the resulting sequence: a2,a1,a4,a3,a6,a5,… Input The first line contains a positive even integer n (2≤n≤1000) — the length of the sequence. The second line contains n space-separated integers a1,a2,…,an (1≤ai≤1000) — the elements of the sequence. Output Print n...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

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