Knowee
Questions
Features
Study Tools

You are given an array of size N containing unique integers. Find the size of the largest subarray that can be rearranged to form a contiguous sequence.A contiguous sequence means that the difference of adjacent elements should be 1.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the size of the largest subarray that can be rearranged to form a contiguous sequence, on a new line.Constraints30 points1 <= T <= 1004 <= N <= 10070 points1 <= T <= 1004 <= N <= 1000General Constraints0 <= A[i] <= 105ExampleInput251 3 2 6 590 8 6 5 7 10 3 2 1Output34ExplanationTest-Case 1The largest subarray that can be rearranged to form a contiguous sequence is [1, 3, 2] which can be rearranged to form [1, 2, 3].Test-Case 2The largest subarray that can be rearranged to form a contiguous sequence is [8, 6, 5, 7] which can be rearranged to form [5, 6, 7, 8].

Question

You are given an array of size N containing unique integers. Find the size of the largest subarray that can be rearranged to form a contiguous sequence.A contiguous sequence means that the difference of adjacent elements should be 1.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the size of the largest subarray that can be rearranged to form a contiguous sequence, on a new line.Constraints30 points1 <= T <= 1004 <= N <= 10070 points1 <= T <= 1004 <= N <= 1000General Constraints0 <= A[i] <= 105ExampleInput251 3 2 6 590 8 6 5 7 10 3 2 1Output34ExplanationTest-Case 1The largest subarray that can be rearranged to form a contiguous sequence is [1, 3, 2] which can be rearranged to form [1, 2, 3].Test-Case 2The largest subarray that can be rearranged to form a contiguous sequence is [8, 6, 5, 7] which can be rearranged to form [5, 6, 7, 8].

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

Solution

Here is a step-by-step solution to the problem:

  1. Initialize a variable max_len to 0. This variable will keep track of the maximum length of the subarray that can be rearranged to form a contiguous sequence.

  2. For each test case, do the following:

    a. Read the size of the array N and the elements of the array.

    b. Convert the array into a set to remove any duplicate elements and sort the set. This is because a set only contains unique elements and the problem states that the array contains unique integers.

    c

This problem has been solved

Similar Questions

Given an array, find the length of the longest subsequence whose elements can be re-arranged in a strictly increasing contiguous order. The difference between 2 adjacent elements in the subsequence, after re-arrangement, should be exactly 1.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines. The first line of each test case contains N - size of the array. The next line contains N integers - the elements of the array.Output FormatFor each test case, print the length of the longest subsequence, separated by a new line.Constraints1 <= T <= 10001 <= N <= 10000-100000 <= ar[i] <= 100000ExampleInput3821 -22 -22 5 -31 -24 5 -231018 -33 31 33 30 -14 32 30 16 1766 3 8 5 2 5Output342ExplanationTest Case 1Subsequence is: -22, -24, -23.Test Case 2Subsequence is: 31, 33, 30, 32.Test Case 3Subsequence is: 6, 5 or 3, 2.

You are given an array 𝐴A of size 𝑁N.Find the largest integer 𝐾K such that there exists a subsequence 𝑆S of length 𝐾K where 𝐾K is divisible by the number of distinct elements in 𝑆S.Input FormatThe first line contains a single integer 𝑇T, denoting the number of test cases.The first line of each test case contains a positive integer 𝑁N, the length of array 𝐴A.The second line contains 𝑁N space-separated integers, 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ −− denoting the array 𝐴A.Output FormatFor each test case, output the largest valid 𝐾K.Constraints1≤𝑇≤1041≤T≤10 4 1≤𝐴𝑖≤𝑁≤2⋅1051≤A i​ ≤N≤2⋅10 5 The sum of 𝑁N over all test cases won't exceed 2⋅1052⋅10 5 .Sample 1:

longest consecutive elementsYou need to write a function that takes an unsorted array of integers as input and returns the length of the longest consecutive elements sequence in the array. A consecutive elements sequence is a subarray where the elements are adjacent in value, such as [1, 2, 3, 4] or [5, 6, 7, 8]. The order of the elements in the input array does not matter.For example, given the input array [100, 4, 200, 1, 3, 2], the longest consecutive elements sequence is [1, 2, 3, 4], which has a length of 4. Therefore, your function should return 4.In this case, there are no consecutive elements in the array, so the length of the longest consecutive element sequence is 1.Testcase:Input11  // no of elements of array8 10 12 14 1 2 3 4 5 6 7  // array elementsOutput:8

Given an array of integers, find the largest number that can be constructed by concatenating all the elements of the given array.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines. The first line of each test case contains N - the size of the array and the second line contains N integers - the elements of the array.Output FormatFor each test case, print the largest number that can be constructed by concatenating all the elements of the given array, separated by a new line.Constraints1 <= T <= 10001 <= N <= 10000 <= ar[i] <= 1000ExampleInput3849 73 58 30 72 44 78 23469 9 57 60240 4Output78737258494430239696057440

Write a program that takes an array of integers as input and prints all its subarrays, ensuring that they are sorted based on the forward direction and the count of elements.Constraints:The length of the array is at least 1 and at most 100.Each element of the array is an integer between -1000 and 1000.Input:The input consists of two lines. The first line contains an integer, n, which is the length of the array. The second line contains n space-separated integers, representing the elements of the array.Output:The program should print all the subarrays of the given array in increasing order, one subarray per line.

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.