You are given a sequence of n integers S = s1, s2 ... sn and a sequence of m integers Q = q1, q2 ... qm.Please print in ascending order all such i, that si = qi, i<=n, i<=m.Input data specificationIn the first line you are given one integer 2 <= n <= 100, and in the following line n integers:-100 <= si <= 100, si <= si+1.In the third line you are given one integer 2 <= m <= 100, and in the following line m integers:-100 <= qi <= 100, qi <= qi+1.Output data specificationThe sequence of requested indexes separated by spaces.Example 1Input:5-2 -2 -1 1 4 6-3 -2 -1 1 2 3Output:2 3 4Example 2Input:5-2 -1 -3 1 4 5-3 -2 -1 1 2Output:4
Question
You are given a sequence of n integers S = s1, s2 ... sn and a sequence of m integers Q = q1, q2 ... qm.Please print in ascending order all such i, that si = qi, i<=n, i<=m.Input data specificationIn the first line you are given one integer 2 <= n <= 100, and in the following line n integers:-100 <= si <= 100, si <= si+1.In the third line you are given one integer 2 <= m <= 100, and in the following line m integers:-100 <= qi <= 100, qi <= qi+1.Output data specificationThe sequence of requested indexes separated by spaces.Example 1Input:5-2 -2 -1 1 4 6-3 -2 -1 1 2 3Output:2 3 4Example 2Input:5-2 -1 -3 1 4 5-3 -2 -1 1 2Output:4
Solution
This problem is about finding the common elements in two sorted sequences and printing the indices of the common elements from the first sequence. Here is a step-by-step solution:
- Read the number of elements in the first sequence (n) and the sequence itself (S).
- Read the number of elements in the second sequence (m) and the sequence itself (Q).
- Initialize an empty list to store the indices of the common elements.
- Iterate over the elements in the first sequence.
- For each element in the first sequence, check if it is also in the second sequence.
- If the element is in the second sequence, add its index (plus one, because the indices are 1-based) to the list of indices.
- After iterating over all elements in the first sequence, sort the list of indices in ascending order.
- Print the sorted list of indices, separated by spaces.
Here is a Python code snippet that implements this solution:
n = int(input())
S = list(map(int, input().split()))
m = int(input())
Q = list(map(int, input().split()))
indices = []
for i in range(min(n, m)):
if S[i] == Q[i]:
indices.append(i+1)
indices.sort()
print(' '.join(map(str, indices)))
This code reads the input data, finds the indices of the common elements, sorts them, and prints them. The map function is used to convert the input strings to integers and the output integers to strings. The join function is used to concatenate the output strings with spaces in between.
Similar Questions
Provided S - a set of n integers S = s1, s2, ..., sn. Check whether it is possible to divide S into two parts: s1, s2, ..., si and si+1, si+2, ..., sn (1 <= i < n) Remember that the first part is strictly decreasing while the second is strictly increasing one.Input FormatIn the first line you are given an integer 2<=n<=100 and in the following line n integers -100 <= si <= 100.Constraints2<=n<=100 -100 <= si <= 100Output FormatOne word Yes or No.Sample Input 05 -1 2 -1 1 -1Sample Output 0No
The program must accept N integers as the input. The program must sort the N integers using selection sort and print all the iterations of the selection sorting process as the output.Boundary Condition(s):2 <= N <= 1001 <= Each integer value <= 1000Input Format:The first line contains N.The second line contains N integers separated by a space.Output Format:The lines containing all the stages of the selection sort.Example Input/Output 1:Input:512 6 15 9 10Output:6 12 15 9 106 9 15 12 106 9 10 12 156 9 10 12 15
Problem Statement:Write a program to accept 3 numbers as input and sequence them in the descending order. You may assume that the numbers will not be equal.Input Format:Three space Integes - num1, num2 and num3.Output Format:Display the three integers in decreasing order - Integer>Integer>Integer.Constraints1<=num<=2,147,483,647Sample Input 16789 6790 6788Sample Output 16790>6789>6788Sample Input 20 1 2Sample Output 22>1>0
Problem Statement:Write a program to accept 4 numbers as input and sequence them in the descending order. You may assume that the numbers will not be equal.Input Format:Four space separated Integers - num1, num2, num3 and num4.Output Format:Display the four integers in the decreasing order - integer>integer>integer>integerConstraints1<=num<=2,147,483,647Sample Input 16789 6790 6788 6787Sample Output 16790>6789>6788>6787Sample Input 20 1 2 3Sample Output 23>2>1>0
Single File Programming QuestionProblem StatementGiven an integer K and a queue of integers, reverse the order of the first K elements of the queue, leaving the other elements in the same relative order. Note: You can use either an array or a linked list for implementation.Input format :The first line of input consists of an integer N, representing the number of elements in the queue.The second line consists of the value of K.The third line consists of N space-separated queue elements.Output format :The output displays "Reversed queue: " followed by the queue elements after the reversal is done, separated by a space.Refer to the sample output for formatting specifications.Code constraints :N > 0K ≤ NSample test cases :
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.