Wednesday, March 28, 2018

Finding the length of the longest increasing subsequence in arr[] of size n in ascending order ( NPTEL Q )

# include <stdio.h>
# include <conio.h>
/* lis() returns the length of the longest increasing
  subsequence in arr[] of size n
array[5] elements are  3,10,2,1,30

the longest order order is    3,10,30
 */
int lis( int arr[], int n )
{
    int *lis, i, j, max = 0;
    lis = (int*) malloc ( sizeof( int ) * n );

    /* Initialize LIS values for all indexes */
    for (i = 0; i < n; i++ )
lis[i] = 1;

    /* Compute optimized LIS values in bottom up manner */
    for (i = 1; i < n; i++ )
for (j = 0; j < i; j++ )
    if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;

    /* Pick maximum of all LIS values */
    for (i = 0; i < n; i++ )
if (max < lis[i])
    max = lis[i];

    /* Free memory to avoid memory leak */
    free(lis);

    return max;
}

main()
{
int A[]={3,10,2,1,30};
clrscr();

printf("\n%d",lis(A,5));

}

Thursday, March 22, 2018

NPTEL(C & DS ) : ROBOT CAN MOVE STEP 1,2 OR 3 AND PRINT YES OR NO , THAT THE ROBOT CAN MOVE THE GIVEN DISTANCE

/* ROBOT CAN MOVE STEP 1,2 OR 3 AND PRINT YES OR NO , THAT THE
ROBOT CAN MOVE THE GIVEN DISTANCE */

# include <stdio.h>
main()
{
  int i=0,j[3]={1,2,3};
  long int n,x=0;
  scanf("%ld",&n);
  while(x<=n)
  {
    x+=j[i];

    if(x==n)
    {
      printf("YES");
      break;
    }
    if(i==2)
      i=0;
      else
      i++;
    }
  if(x!=n)
      printf("NO");

}