Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

solve as soon as possible

+1 vote
asked Sep 6, 2018 by Abhishek Tiwari (130 points)

You are given a sorted (either in the increasing or in the decreasing order) sequence of numbers, ending with a -1. You can assume that are at least two numbers before the ending -1. 

Let us call the sequence x0 x1 ... xn -1.


You have to output the number of distinct elements in the sorted sequence.

Kindly do not use arrays in the code. 

3 Answers

0 votes
answered Sep 6, 2018 by anonymous

didnt understand what you want the output to be

can you give an example?

commented Sep 7, 2018 by anonymous
Output should be the the no of elements, when we give input as -1.
If i have input as 2,4,5,6 and then i give input as -1. Output should be 4. The input here was in increasing order. But i need it for increasing and decreasing order.
0 votes
answered Sep 7, 2018 by anonymous
explain the problem more clearly
0 votes
answered Sep 7, 2018 by Leroy (390 points)
#include <bits/stdc++.h>

using namespace std;

int lis( int arr[], int n )

{

int result = 0;

int lis[n];

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

lis[i] = 1;

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

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

if ( arr[i] > arr[j] &&

lis[i] < lis[j] + 1)

lis[i] = lis[j] + 1;

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

if (result < lis[i])

result = lis[i];

return result;

}

int minimumNumberOfDeletions(int arr[],

int n)

{

int len = lis(arr, n);

return (n - len);

}

int main()

{

int arr[] = {30, 40, 2, 5, 1,

7, 45, 50, 8};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Minimum number of steps = "

<< minimumNumberOfDeletions(arr, n);

return 0;

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...