7843. Bigger than previous

 

An array of integers is given. Print all elements that are greater than their previous element.

 

Input. The first line contains the integer n – the number of elements in the array. The second line contains n integers. Each number does not exceed 100 in absolute value.

 

Output. Print all elements of the array that are strictly greater than the previous element.

 

Sample input

Sample output

7

14 17 16 3 7 19 9

16 7 17 19

 

 

SOLUTION

loops

 

Algorithm analysis

Read the input sequence of numbers and store it in an array. Then, using a loop, print all elements that are greater than their previous element.

 

Algorithm implementation

Declare aarray.

 

int m[101];

 

Read the number of input elements n.

 

scanf("%d",&n);

 

Read the input array.

 

for (i = 0; i < n; i++)

  scanf("%d", &m[i]);

 

Print all elements of the array that are strictly greater than the previous one.

 

for (i = 1; i < n; i++)

  if (m[i] > m[i - 1]) printf("%d ", m[i]);

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);   

    int n = con.nextInt();

    int m[] = new int[n];

    for(int i = 0; i < n; i++)

      m[i] = con.nextInt();

   

    for (int i = 1; i < n; i++)

      if (m[i] > m[i-1]) System.out.print(m[i] + " ");

    System.out.println();

    con.close();

  }

}

 

Python implementation

Read the input data.

 

n = int(input())

lst = list(map(int, input().split()))

 

Print all elements of the array that are strictly greater than the previous one.

 

for i in range(1,n):

  if lst[i] > lst[i-1]: print(lst[i], end = " ")