Skip to main content

LIS - Largest Increasing Subsequence

Problem :- 
  • the largest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
Example :-
Input  : arr[] = {3, 10, 2, 1, 20}
Output : Length of LIS = 3
Algo : -
Input  : arr[] 
Output : Length of LIS
for(i=1 to n)
    LIS[i]=1
for(i=2 to n) 
    for(j=i-1 to 1)
        if(LIS[i]<LIS[j]+1) && (A[j]<A[i])
             LIS[i]=LIS[j]+1
Programme :- 
Download the programme


#include<stdio.h>
main()
{
    int n,i,j;
    printf("ENTER THE SIZE OF ARRAY:");
    scanf("%d",&n);
    int a[n],lis[n];
    printf("ENTER ARRAY ELEMENTS:");
    for(i=0;i<n;i++)
    {
            scanf("%d",&a[i]);
            lis[i]=1;
    }
    int max=0,maxInd;
    for(i=1;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(a[i]>a[j] && lis[i]<lis[j]+1)
            {
                lis[i]=lis[j]+1;
            }

        }
        if(lis[i]>max)
        {
            max=lis[i];
            maxInd=i;
        }

    }

    printf("| LIS | = %d",max);
    /*for(j=0;j<n;j++)
    {
        printf("LIS[%d]: %d\n",j,lis[j]);
    }*/

}



Comments

Popular Posts

Dijkstra's algorithm

Djikstra's algorithm   solves the problem of finding the shortest path from a point in a graph (the  source ) to a destination. It turns out that one can find the shortest paths from a given source to  all  points in a graph in the same time, hence this problem is sometimes called the  single-source shortest paths  problem. This problem is related to the spanning tree one. The graph representing all the paths from one vertex to all the others must be a spanning tree - it must include all vertices. There will also be no cycles as a cycle would define more than one path from the selected vertex to at least one other vertex. For a graph, G = (V,E) where V is a set of vertices and E is a set of edges. Dijkstra's algorithm keeps two sets of vertices: S    the set of vertices whose shortest paths from the source have already been determined  and V-    S :   the remaining v...

Kruskal’s Minimum Spanning Tree Algorithm & Programme

PROBLEM :                  Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. It is a greedy algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost arcs at each step. To understand Kruskal's algorithm let us consider the following example − Step 1 - Remove all loops and Parallel Edges Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one which has the least cost associated and ove all others. Step 2 - Arrange all edges in their increasing order of weight The next step is to create a set of edges and weight, and arrange them in an ascending order of weightage (cost). Step 3 - Add the edge which has the least weightage Now we start adding edges to the graph beginning from the one which has the least weight. Th...

Binary Tree

Given a Binary Tree, convert it into its mirror. What is a Binary Tree? ➤A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root . Every node (excluding a  root ) in a tree is connected by a directed edge from exactly one other node. This node is called a parent.              Input: ➤The task is to complete the method that takes one argument, root of Binary Tree and modifies the tree. Output:   ➤The function should convert the tree to its mirror. Plus:  Check whether the binary tree and mirror tree is same or not. FOLLOW ME ON  :- INSTAGRAM  :-  https://www.instagram.com/akshay_methaniya_2809/ YOUTUBE :-  https://www.youtube.com/channel/UCDbGAU0c_78ue2IXi6ibtYQ FACEBOOK :-  https://www.facebook.com/akshay.methaniya