Appearance
question:You are given an array of integers and an integer k. You need to determine if there are two distinct indices i and j in the array such that the absolute difference between the array values at these indices is at most k and the absolute difference between the indices i and j is exactly d. -----Input:----- - The first line of input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two integers n and d — the number of elements in the array and the required difference between the indices respectively. - The second line of each test case contains n space-separated integers denoting the elements of the array. -----Output:----- For each test case, print "YES" if such indices exist, otherwise print "NO". -----Constraints----- - 1 leq T leq 10^4 - 2 leq n leq 10^5 - 0 leq k leq 10^9 - 0 leq arr[i] leq 10^9 - Sum of n over all test cases does not exceed 10^6 -----Sample Input:----- 3 5 2 1 3 5 2 4 4 1 1 5 9 13 6 3 1 2 3 4 5 6 -----Sample Output:----- YES NO YES Here's a step-by-step break down of the sample cases: 1. For the first test case, the array is [1, 3, 5, 2, 4] and d = 2. We can see that the absolute difference between 1 and 5 is 4 (> k) and the absolute difference between their indices is 2 (== d), so it returns YES. 2. For the second test case, the array is [1, 5, 9, 13] and d = 1. There is no pair where the absolute difference is at most k = 0, so it returns NO. 3. For the third test case, the array is [1, 2, 3, 4, 5, 6] and d = 3. The absolute difference between 1 and 4 is 3 (<= k) and the absolute difference between their indices is 3 (== d), so it returns YES.
answer:def check_indices(arr, k, d): Checks if there exist two distinct indices i and j in the array such that: - The absolute difference between the array values at these indices is at most k. - The absolute difference between the indices i and j is exactly d. n = len(arr) for i in range(n - d): if abs(arr[i] - arr[i + d]) <= k: return "YES" return "NO" def main(): import sys input = sys.stdin.read data = input().split() index = 0 T = int(data[index]); index += 1 results = [] for _ in range(T): n = int(data[index]) d = int(data[index + 1]) index += 2 arr = list(map(int, data[index:index + n])) index += n results.append(check_indices(arr, k, d)) for result in results: print(result) if __name__ == "__main__": main()
question:# Task Alice is organizing a book fair and wants to categorize all the books into different genres. Each book belongs to a specific genre, and she has a list of genres and the respective amount of books for each genre. Her goal is to create as few shelves as possible, where each shelf can only hold up to a certain number of books. Write an algorithm to help Alice determine the minimum number of shelves required to store all the books. # Input - `genres`: an array of positive integers where each integer represents the number of books for a genre. - `shelf_capacity`: an integer representing the maximum number of books one shelf can hold. Constraints: - 1 <= genres.length <= 1000 - 1 <= genres[i] <= 100 - 1 <= shelf_capacity <= 1000 # Output An integer representing the minimum number of shelves required. # Examples For `genres = [100, 200, 300]` and `shelf_capacity = 200`, the output should be `3`. ``` She can use the shelves as follows: - Shelf 1: 100 books (from the first genre). - Shelf 2: 200 books (from the second genre). - Shelf 3: 200 books (remaining 100 from the second genre and 100 from the third genre). Total shelves: 3 ``` For `genres = [50, 50, 50, 50]` and `shelf_capacity = 100`, the output should be `2`. ``` She can use the shelves as follows: - Shelf 1: 50 books (from the first genre) + 50 books (from the second genre). - Shelf 2: 50 books (from the third genre) + 50 books (from the fourth genre). Total shelves: 2 ```
answer:def min_shelves_required(genres, shelf_capacity): Function to calculate the minimum number of shelves required to store all the books. :param genres: List of integers where each integer is the number of books in a genre. :param shelf_capacity: Maximum number of books one shelf can hold. :return: Minimum number of shelves required. total_books = sum(genres) # Calculate the minimum number of shelves required shelves_needed = (total_books + shelf_capacity - 1) // shelf_capacity return shelves_needed
question:# Task A university baseball coach wants to optimize the batting order of his players for a tournament. He has statistics that tell him the batting average of each player. He wants to arrange the players in such a way that the sum of the batting averages in the first half of the order (first half players) is as close as possible to the sum of the batting averages in the second half of the order (second half players). Given an integer array `batting_averages` that represents the batting averages of the players, implement a function that returns a boolean value `True` if it's possible to split the batting_averages into two subarrays with equal sum. If it's not possible, return `False`. # Function Signature ```python def can_split_batting_order(batting_averages: List[int]) -> bool: ``` # Input - `batting_averages` (List[int]): An array of integers where each integer represents the batting average of a player. # Output - A boolean value (`True` or `False`). # Example For `batting_averages = [1, 5, 11, 5]`, the output should be `True`. ``` Explanation: The array can be split into two subarrays with equal sum: [1, 5, 5] and [11]. ``` For `batting_averages = [1, 2, 3, 5]`, the output should be `False`. ``` Explanation: The array cannot be split into two subarrays with equal sum. ``` # Constraints - The length of `batting_averages` will be between 1 and 20 inclusive. - Each integer in `batting_averages` will be between 0 and 100 inclusive.
answer:from typing import List def can_split_batting_order(batting_averages: List[int]) -> bool: total_sum = sum(batting_averages) # If the total sum is odd, it cannot be divided into two equal parts if total_sum % 2 != 0: return False target = total_sum // 2 dp = [False] * (target + 1) dp[0] = True for avg in batting_averages: for i in range(target, avg - 1, -1): dp[i] = dp[i] or dp[i - avg] return dp[target]
question:# Task Given a string consisting of only the characters 'a', 'b', and 'c', you are asked to rearrange the characters in the string so that no two identical characters are adjacent to one another. Return the rearranged string. If it is not possible to rearrange the string in such a way, return an empty string. # Example For `s = "aabbc"`, the output should be `"abcab"`, `"abacb"`, `"acbab"` or any other string where no two identical characters are adjacent. For `s = "aaab"`, the output should be `""` (an empty string) as it is not possible to rearrange the string in such a way. # Input/Output - `[input]` a string `s` A string consisting only of the characters 'a', 'b', and 'c'. Constraints: `1 ≤ s.length ≤ 100` - `[output]` a string Rearranged string where no two identical characters are adjacent, or an empty string if it is not possible.
answer:from collections import Counter import heapq def rearrange_string(s): Rearranges the string so that no two adjacent characters are the same. Returns an empty string if not possible. # Count the frequency of each character freq = Counter(s) max_heap = [] # Create a max heap based on character frequency for char, count in freq.items(): heapq.heappush(max_heap, (-count, char)) prev_char = None prev_count = 0 result = [] while max_heap: count, char = heapq.heappop(max_heap) result.append(char) if prev_char is not None and prev_count < 0: heapq.heappush(max_heap, (prev_count, prev_char)) prev_char = char prev_count = count + 1 if len(result) != len(s): return "" else: return "".join(result)