[Solved] Favorite Sequence with Java, C++, Python

Favorite Sequence: Lucarnos Film Festival is an annual film festival and is also known for being a prestigious platform for art house films. Lucy, being a movie lover visits the Lucarnos Film Festival. There were many films screened in the show, of which Lucy somehow choose the best movie of her choice and set off to watch it.

The movie which Lucy chose to watch has N sequences. A sequence is defined as a series of scenes in a movie that form a distinct narrative unit. Lucy likes a sequence better if the sequence contains her favorite sequence in the movie as a substring.

Given the sequence and Lucy’c favorite sequence(F) check whether her favorite sequence is contained in the sequence.

Input Format:
The first line of the input contains an integer N, which corresponds to the length of the sequence.
The second line of the input contains N space-separated integers, which corresponds to the sequence.
The third line of the input contains an integer n, which corresponds to the length of favorite sequence F.
The last line of the input contains n space-separated integers, which corresponds to the favorite sequence.

Output Format:
Print “Yes” (Without quotes)if the sequence contains Lucy’sfavourite sequence otherwise print “No” (Without quotes).
Refer sample input and output for formatting specifications

Sample Input 1:
6
1 2 3 4 5 6
3
2 3 4

Sample Output 1:
Yes

Sample Input 2:
6
22 5 6 33 1 4
2
4 15

Sample Output 2:
No

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
public
    static void main(String[] args) throws java.lang.Exception
    {
        // sauravhathi
        Scanner sc = new Scanner(System.in);

        int x,y,i=0,j=0,count = 0;
        x = sc.nextInt();
        int a[] = new int[x];
        for(i=0;i<x;i++)
        {
            a[i] = sc.nextInt();
        }
        y = sc.nextInt();
        int b[] = new int[y];
        for(i=0;i<y;i++)
        {
            b[i] = sc.nextInt();
        }
        i = 0;
        while(j<y)
        {
            while(i<x)
            {
                if(a[i]==b[j])
                {
                    count++;
                    i++;
                    break;
                }
                if(count>0 && a[i]!=b[j])
                {
                    count = 0;
                }
                //sauravhathi
                i++;
            }
            j++;
        }
        if(count==y)
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
    }
}
#include <iostream>
#include <string>
int main()
{
    int x, y, i = 0, j = 0, count = 0;
    scanf("%d", &x);
    int a[x];
    for (i = 0; i < x; i++)
    {
        scanf("%d", &a[i]);
    }
    scanf("%d", &y);
    //sauravhathi
    int b[y];
    for (i = 0; i < y; i++)
    {
        scanf("%d", &b[i]);
    }
    i = 0;
    while (j < y)
    {
        while (i < x)
        {
            if (a[i] == b[j])
            {
                count++;
                i++;
                break;
            }
            if (count > 0 && a[i] != b[j])
            {
                count = 0;
            }
            //sauravhathi
            i++;
        }
        j++;
    }
    if (count == y)
    {
        printf("Yes");
    }
    else
    {
        printf("No");
    }
    return 0;
    //sauravhathi
}
x = int(input())
a = [int(x) for x in input().split()]
y = int(input())
b = [int(x) for x in input().split()]
i = 0
j = 0
count = 0
while (j < y):
    while (i < x):
        if (a[i] == b[j]):
            count += 1
            i += 1
            break
        if (count > 0 and a[i] != b[j]):
            count = 0
#sauravhathi
        i += 1
    j += 1
if (count == y):
    print("Yes")
else:
    print("No")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *