Knowee
Questions
Features
Study Tools

You are given an integer array nums, an integer array queries, and an integer x.For each queries[i], you need to find the index of the queries[i]th occurrence of x in the nums array. If there are fewer than queries[i] occurrences of x, the answer should be -1 for that query.Return an integer array answer containing the answers to all queries. Example 1:Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1Output: [0,-1,2,-1]Explanation:For the 1st query, the first occurrence of 1 is at index 0.For the 2nd query, there are only two occurrences of 1 in nums, so the answer is -1.For the 3rd query, the second occurrence of 1 is at index 2.For the 4th query, there are only two occurrences of 1 in nums, so the answer is -1.Example 2:Input: nums = [1,2,3], queries = [10], x = 5Output: [-1]Explanation:For the 1st query, 5 doesn't exist in nums, so the answer is -1. Constraints:1 <= nums.length, queries.length <= 1051 <= queries[i] <= 1051 <= nums[i], x <= 104

Question

You are given an integer array nums, an integer array queries, and an integer x.For each queries[i], you need to find the index of the queries[i]th occurrence of x in the nums array. If there are fewer than queries[i] occurrences of x, the answer should be -1 for that query.Return an integer array answer containing the answers to all queries. Example 1:Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1Output: [0,-1,2,-1]Explanation:For the 1st query, the first occurrence of 1 is at index 0.For the 2nd query, there are only two occurrences of 1 in nums, so the answer is -1.For the 3rd query, the second occurrence of 1 is at index 2.For the 4th query, there are only two occurrences of 1 in nums, so the answer is -1.Example 2:Input: nums = [1,2,3], queries = [10], x = 5Output: [-1]Explanation:For the 1st query, 5 doesn't exist in nums, so the answer is -1. Constraints:1 <= nums.length, queries.length <= 1051 <= queries[i] <= 1051 <= nums[i], x <= 104

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Para resolver este problema, podemos seguir los siguientes pasos:

  1. Crear una lista vacía llamada answer para almacenar las respuestas a las consultas.
  2. Recorrer cada consulta en la lista queries.
  3. Para cada consulta, buscar la queries[i]-ésima ocurrencia de x en la lista nums.
  4. Si se encuentra la ocurrencia, agregar el índice correspondiente a la lista answer. Si no se encuentra, agregar -1 a la lista answer.
  5. Devolver la lista answer al final.

Aquí está el código que implementa estos pasos:

def find_occurrences(nums, queries, x):
    answer = []
    for query in queries:
        count = 0
        found = False
        for i in range(len(nums)):
            if nums[i] == x:
                count += 1
                if count == query:
                    answer.append(i)
                    found = True
                    break
        if not found:
            answer.append(-1)
    return answer

# Ejemplo 1
nums1 = [1, 3, 1, 7]
queries1 = [1, 3, 2, 4]
x1

This problem has been solved

Similar Questions

You are given an array nums consisting of integers. You are also given a 2D array queries, where queries[i] = [posi, xi].For query i, we first set nums[posi] equal to xi, then we calculate the answer to query i which is the maximum sum of a subsequence of nums where no two adjacent elements are selected.Return the sum of the answers to all queries.Since the final answer may be very large, return it modulo 109 + 7.A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1:Input: nums = [3,5,9], queries = [[1,-2],[0,-3]]Output: 21Explanation:After the 1st query, nums = [3,-2,9] and the maximum sum of a subsequence with non-adjacent elements is 3 + 9 = 12.After the 2nd query, nums = [-3,-2,9] and the maximum sum of a subsequence with non-adjacent elements is 9.Example 2:Input: nums = [0,-1], queries = [[0,-5]]Output: 0Explanation:After the 1st query, nums = [-5,-1] and the maximum sum of a subsequence with non-adjacent elements is 0 (choosing an empty subsequence). Constraints:1 <= nums.length <= 5 * 104-105 <= nums[i] <= 1051 <= queries.length <= 5 * 104queries[i] == [posi, xi]0 <= posi <= nums.length - 1-105 <= xi <= 105

Given an array, you have to find the ceil of a number x. The ceil of a number x is nothing but the smallest number in the array greater than or equal to x.Input FormatThe first line of input contains the N-size of the array. The next line contains N integers, the elements of the array. The next line contains Q - number of queries. Each of the next Q lines contains a single integer X, for which you have to find the ceil of X in the given array.Output FormatFor each query, print the ceil of X, separated by a new line. If ceil is not found, print the value of "INT_MAXConstraints30 points1 <= N <=1051 <= Q <=102-108 <= ar[i] <=10870 points1 <= N <=1051 <= Q <=105-108 <= ar[i] <=108

You are given an array of positive integers nums.You need to select a subset of nums which satisfies the following condition:You can place the selected elements in a 0-indexed array such that it follows the pattern: [x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x] (Note that k can be be any non-negative power of 2). For example, [2, 4, 16, 4, 2] and [3, 9, 3] follow the pattern while [2, 4, 8, 4, 2] does not.Return the maximum number of elements in a subset that satisfies these conditions.

Problem Statement You are given a function, int ElementsAndIndices(int arr[], int n); The function takes an integer array 'arr' of size 'n' as its arguments. Implement the function to find and return the number of array elements which are equal to their index value in array i.e. arr[k] = k, 0 <= k < n. Note: Indexing starts from 0. Return -1 if 'arr' is empty or None in case of python Example: Input: 10 1 12 3 5 8 9 7 12 23 Output: 3 Explanation: Index Element 0 10 1 1 2 12 3 3 4 5 5 8 6 9 7 7 8 12 9 23 Elements at index {1, 3, 7} are equal to their index values {1, 3, 7} respectively. Since, these are 3 elements, thus, output is 3. The custom input format for the above case: 10 10 1 12 3 5 8 9 7 12 23 (The first line represents the size of the array, the second line represents the elements of the array) Sample input -3 0 1 3 5 7 Sample Output 1 The custom input format for the above case: 6 -3 0 1 3 5 7 (The first line represents the size of the array, the second line represents the elements of the array) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time.

You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.You are given an integer array nums representing the data status of this set after the error.Find the number that occurs twice and the number that is missing and return them in the form of an array. Example 1:Input: nums = [1,2,2,4]Output: [2,3]

1/3

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.