Question

Sequence (Longest Progressive Sequence) in C language Given a sequence, the objective is to find the...

Sequence (Longest Progressive Sequence) in C language

Given a sequence, the objective is to find the longest progressive sequence arranged in ascending order. Detailed descriptions are as:

Problem
A sequence is said to be progressive if it doesn’t decrease at any point in time.
For example 1 1 2 2 is a progressive sequence but 1 2 1 is not a progressive sequence. Let S be the sequence and be represented by L spaced integers Ki, now your task is to find out the first longest progressive sequence present in the given sequence (S).

Input Format:

First line will contain T, the length of the sequence and next line will contain T spaced integers Ki (where i = 0,1, …,L).
Line 1 T,where T is the length of the sequence
Line 2 Ki,where Ki is integer in sequence separated by space

Constraints:

1<=T<=10^6(one million)
1<=Ki<=10^9(one billion)

Output Format:

Line 1 longest progressive sequence present in the given sequence

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

Solution:

I have used int to declare T,while long for Array since is of 10^9.

Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
   //Number of elements in sequence
   int T;
   scanf("%d",&T);
   //Array to store t number of values .
   //Long data type is used since Ki is of range 10 power 9
   long seq[T];
   //Values to store longest subsequence length,subsequnce length and start index of increasing subsequence
   int i=0,subseqlen=0,maxsubseqlen=0,start=0;
  
   for(i=0;i<T;i++)
       scanf("%ld",&seq[i]);
  
   for(i = 0; i < T-1; i++)
   {
       //If progressive
       if(seq[i]<=seq[i+1])
       {
           subseqlen++;
           //Calculating maxsubseqlen and starting point
           if(subseqlen>maxsubseqlen)
           {
               maxsubseqlen=subseqlen;
               start=i+1-subseqlen;
           }
       }
       else
       {
           subseqlen=0;
       }
   }
   int subseq[maxsubseqlen+1];
   for(i=0;i<=maxsubseqlen;i++)
   {
       subseq[i]=seq[start+i];
       printf("%d ",subseq[i]);
   }
  
   return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Sequence (Longest Progressive Sequence) in C language Given a sequence, the objective is to find the...
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
  • 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...

  • 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...

  • 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...

  • Write a C program that reads a sequence of numbers and display a message 1. The...

    Write a C program that reads a sequence of numbers and display a message 1. The numbers a re red from the standard input. 2. The first number is the length of the sequence (n) followed by n numbers. 3. If n is 0 or negative, the program displays the message "Error_1" followed 4. If the length is shorter than n, it displays "Error_2" followed by a new line 5. The program inspects the list and display one of the...

  • 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 language Thank you Jojo is going to a restaurant. There are N foods listed in...

    C language Thank you Jojo is going to a restaurant. There are N foods listed in the menu, and the items are sorted increasingly based on its price. Now Jojo is wondering how many foods are there with price P. As the number of food in the menu can be a lot, Jojo will need your help to answer his questions. Jojo knows you can count really fast, so he will give you M questions. Format Input Input begins with...

  • 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)...

  • 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...

  • 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 : i have 15 min to submit the solution please help me   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...

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