Appearance
question:You are given an array of integers, where each integer represents a unique element in the array. You need to determine the length of the longest contiguous subarray which contains all unique elements. A contiguous subarray is a subarray where all the elements are adjacent to each other. A subarray is a sequence derived from the array by deleting some or none of the elements without changing the order of the remaining elements. # Input - The input contains one or more test cases. - The first line contains one integer `q` (`1 ≤ q ≤ 10^3`) — the number of test cases. - Each of the next `q` lines contains an integer array separated by spaces, where the first integer is `n` (`1 ≤ n ≤ 10^5`) — the number of elements in the array, followed by `n` integers representing the elements of the array. # Output - For each test case, print a single integer representing the length of the longest contiguous subarray with all unique elements. # Example Input ``` 3 5 1 2 3 4 5 6 1 2 1 2 3 4 7 1 2 3 1 4 2 5 ``` Output ``` 5 4 5 ``` # Explanation - In the first test case, the entire array is unique, so the length is 5. - In the second test case, the subarray with all unique elements is [1, 2, 3, 4], so the length is 4. - In the third test case, the subarray with all unique elements is [1, 4, 2, 5], so the length is 5.
answer:def longest_unique_subarray_length(arr): Given an array of integers, returns the length of the longest contiguous subarray which contains all unique elements. n = len(arr) seen = {} max_len = 0 start = 0 for end in range(n): if arr[end] in seen and seen[arr[end]] >= start: start = seen[arr[end]] + 1 seen[arr[end]] = end max_len = max(max_len, end - start + 1) return max_len def find_longest_unique_subarray_lengths(test_cases): results = [] for case in test_cases: n, *arr = case results.append(longest_unique_subarray_length(arr)) return results
question:Given a string containing a sequence of words separated by spaces, identify the word with the most vowels. In case of a tie, return the word which appears first. Vowels are defined as 'a', 'e', 'i', 'o', 'u', and their uppercase counterparts. The input will always contain at least one word and no punctuation. Write a function that takes in a single string and returns the word with the most vowels.
answer:def word_with_most_vowels(sentence): Takes a string sentence and returns the word with the most vowels. In case of a tie, returns the first such word. vowels = set('aeiouAEIOU') def count_vowels(word): return sum(1 for char in word if char in vowels) words = sentence.split() max_vowel_word = max(words, key=count_vowels) return max_vowel_word
question:Given a positive integer k, generate the Pythagorean triplet (a, b, c) such that: 1. a, b, and c are positive integers. 2. a^2 + b^2 = c^2 3. a, b, and c are the smallest possible integers that satisfy the above conditions for the given k. The first integer of the triplet (a) should match the given input k. If no such triplet exists, return (-1, -1, -1). Example 1: Input: k = 3 Output: (3, 4, 5) Explanation: The triplet (3, 4, 5) satisfies the conditions: 3^2 + 4^2 = 9 + 16 = 25 = 5^2. Example 2: Input: k = 2 Output: (2, 3, 3) Explanation: The triplet (2, 3, 3) satisfies the conditions: 2^2 + 3^2 = 4 + 9 = 13 ≠ 3^2, but as the prompt says the smallest triplet for rank k. Example 3: Input: k = 5 Output: (5, 12, 13) Explanation: The triplet (5, 12, 13) satisfies the conditions: 5^2 + 12^2 = 25 + 144 = 169 = 13^2. Your Task: You don't need to read input or print anything. Your task is to complete the function findPythagoreanTriplet() which takes an Integer k as input and returns a Pythagorean triplet that includes k in the form of a tuple (a, b, c). Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints: 1 <= k <= 100
answer:def find_pythagorean_triplet(k): Given a positive integer k, generate the smallest Pythagorean triplet (a, b, c) such that a^2 + b^2 = c^2 and a = k. If no such triplet exists, return (-1, -1, -1). if k <= 0: return (-1, -1, -1) # Pythagorean triplet where k is one of the two smaller legs if k == 1: return (-1, -1, -1) elif k == 2: return (-1, -1, -1) elif k % 2 == 0: m = k // 2 n = 1 a = k b = m**2 - n**2 c = m**2 + n**2 else: m = (k + 1) // 2 n = (k - 1) // 2 a = k b = 2 * m * n c = m**2 + n**2 return (a, b, c)
question:A city is represented as a grid of size `N x M` where each cell is either walkable or blocked. The city's planners want to determine if it is possible to walk from the top-left corner of the grid to the bottom-right corner. You can only move up, down, left, or right and cannot walk on blocked cells. Can you help the planners figure out whether a given grid configuration allows for such a walk? ------ Input Format ------ - The first line contains two integers N and M - the dimensions of the grid. - Each of the next N lines contains M characters 'O' or 'X'. 'O' means the cell is walkable and 'X' means the cell is blocked. ------ Output Format ------ Output YES if it is possible to walk from the top-left corner to the bottom-right corner, NO otherwise. You may print each character of YES and NO in uppercase or lowercase (for example, yes, Yes, YES will be considered identical). ------ Constraints ------ 1 ≤ N, M ≤ 100 The grid will only contain the characters 'O' and 'X'. ------ Sample Input 1 ------ 3 3 OXO OOX XOO ------ Sample Output 1 ------ YES ------ Explanation 1 ------ Test case 1: It is possible to walk from the top-left corner (0,0) to the bottom-right corner (2,2) following the path: (0,0) -> (1,0) -> (1,1) -> (2,1) -> (2,2). ------ Sample Input 2 ------ 3 3 OXO XXX XOO ------ Sample Output 2 ------ NO ------ Explanation 2 ------ Test case 2: There is no possible path to walk from the top-left corner (0,0) to the bottom-right corner (2,2) because the middle row is completely blocked.
answer:def is_path_possible(N, M, grid): Determines if there is a path from the top-left to the bottom-right in the given grid. :param N: int, number of rows in the grid :param M: int, number of columns in the grid :param grid: list of strings, representation of the grid :return: str, 'YES' if a path exists, 'NO' otherwise from collections import deque if grid[0][0] == 'X' or grid[N-1][M-1] == 'X': return 'NO' directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] visited = [[False] * M for _ in range(N)] queue = deque([(0, 0)]) visited[0][0] = True while queue: x, y = queue.popleft() if (x, y) == (N-1, M-1): return 'YES' for dx, dy in directions: nx, ny = x + dx, y + dy if 0 <= nx < N and 0 <= ny < M and not visited[nx][ny] and grid[nx][ny] == 'O': visited[nx][ny] = True queue.append((nx, ny)) return 'NO'