Knowee
Questions
Features
Study Tools

Make My Array EqualYou are given an array ๐ดA of length ๐‘N.Determine if it is possible to make all the elements of ๐ดA equal by performing the following operation any number of times (possibly, zero):Choose two distinct indices ๐‘–i and ๐‘—j (1โ‰ค๐‘–,๐‘—โ‰ค๐‘1โ‰คi,jโ‰คN, ๐‘–โ‰ ๐‘—i๎€ =j); andUpdate the value at index ๐‘–i by setting ๐ด๐‘–A iโ€‹ to ๐ด๐‘–+๐ด๐‘—A iโ€‹ +A jโ€‹ .For example, if ๐ด=[1,5,3,5,6]A=[1,5,3,5,6],Choosing ๐‘–=5i=5 and ๐‘—=3j=3 would result in the array [1,5,3,5,9][1,5,3,5,9], since we replace ๐ด5A 5โ€‹ with ๐ด5+๐ด3=9A 5โ€‹ +A 3โ€‹ =9.Choosing ๐‘–=2i=2 and ๐‘—=4j=4 would instead result in the array [1,10,3,5,6][1,10,3,5,6].Output "YES" if it is possible to make all elements of the array equal after performing several (possibly, zero) of these operations, otherwise output "NO" (without quotes).Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of two lines of input.The first line of each test case contains a single integer ๐‘N โ€” the length of array.The second line of each test case contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ , denoting the initial array.Output FormatFor each test case, print Yes if it is possible to make all elements of array equal using any number of operations, otherwise print No.You may print each character of the output in either uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).Constraints1โ‰ค๐‘‡โ‰ค1051โ‰คTโ‰ค10 5 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 0โ‰ค๐ด๐‘–โ‰ค1090โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases won't exceed 2โ‹…1052โ‹…10 5 .Sample 1:InputOutput331 1 121 231 0 0YESNOYES

Question

Make My Array EqualYou are given an array ๐ดA of length ๐‘N.Determine if it is possible to make all the elements of ๐ดA equal by performing the following operation any number of times (possibly, zero):Choose two distinct indices ๐‘–i and ๐‘—j (1โ‰ค๐‘–,๐‘—โ‰ค๐‘1โ‰คi,jโ‰คN, ๐‘–โ‰ ๐‘—i๎€ =j); andUpdate the value at index ๐‘–i by setting ๐ด๐‘–A iโ€‹ to ๐ด๐‘–+๐ด๐‘—A iโ€‹ +A jโ€‹ .For example, if ๐ด=[1,5,3,5,6]A=[1,5,3,5,6],Choosing ๐‘–=5i=5 and ๐‘—=3j=3 would result in the array [1,5,3,5,9][1,5,3,5,9], since we replace ๐ด5A 5โ€‹ with ๐ด5+๐ด3=9A 5โ€‹ +A 3โ€‹ =9.Choosing ๐‘–=2i=2 and ๐‘—=4j=4 would instead result in the array [1,10,3,5,6][1,10,3,5,6].Output "YES" if it is possible to make all elements of the array equal after performing several (possibly, zero) of these operations, otherwise output "NO" (without quotes).Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of two lines of input.The first line of each test case contains a single integer ๐‘N โ€” the length of array.The second line of each test case contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ , denoting the initial array.Output FormatFor each test case, print Yes if it is possible to make all elements of array equal using any number of operations, otherwise print No.You may print each character of the output in either uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).Constraints1โ‰ค๐‘‡โ‰ค1051โ‰คTโ‰ค10 5 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 0โ‰ค๐ด๐‘–โ‰ค1090โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases won't exceed 2โ‹…1052โ‹…10 5 .Sample 1:InputOutput331 1 121 231 0 0YESNOYES

...expand
๐Ÿง Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we need to understand that we can make all elements of the array equal if the sum of all elements in the array is divisible by the length of the array (N). This is because we can distribute the sum evenly to all elements.

Here are the steps to solve this problem:

  1. Read the number of test cases (T).
  2. For each test case, do the following: a. Read the length of the array (N). b. Read the elements of the array (A). c. Calculate the sum of all elements in the array. d. If the sum is divisible by N, print "YES". Otherwise, print "NO".

In Python, the code would look like this:

T = int(input())
for _ in range(T):
    N = int(input())
    A = list(map(int, input().split()))
    if sum(A) % N == 0:
        print("YES")
    else:
        print("NO")

This code works by first reading the number of test cases. Then, for each test case, it reads the length of the array and the elements of the array. It calculates the sum of the elements and checks if it is divisible by the length of the array. If it is, it prints "YES". Otherwise, it prints "NO".

This problem has been solved

Similar Questions

You are given an array ๐ดA of size ๐‘N.You can rearrange the elements of ๐ดA as you want. After that, construct an array ๐ตB of size ๐‘N as:๐ต1=๐ด1B 1โ€‹ =A 1โ€‹ ;๐ต๐‘–=๐ต๐‘–โˆ’1B iโ€‹ =B iโˆ’1โ€‹ && ๐ด๐‘–A iโ€‹ for all 2โ‰ค๐‘–โ‰ค๐‘2โ‰คiโ‰คN, where && denotes the bitwise AND operator.Find the lexicographically largest possible array ๐ตB.For two arrays ๐‘‹X and ๐‘ŒY, both of size ๐‘N, the array ๐‘‹X is said to be lexicographically larger than array ๐‘ŒY, if, in the first position where ๐‘‹X and ๐‘ŒY differ, ๐‘‹๐‘–>๐‘Œ๐‘–X iโ€‹ >Y iโ€‹ .Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains ๐‘N, the size of the array ๐ดA.The second line of each test case contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ .Output FormatFor each test case, output on a new line, ๐‘N space separated integers denoting the lexicographically largest array ๐ตB that can be formed by any rearrangement of ๐ดA.Constraints1โ‰ค๐‘‡โ‰ค2โ‹…1041โ‰คTโ‰ค2โ‹…10 4 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 1โ‰ค๐ด๐‘–โ‰ค1091โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases does not exceed 2โ‹…1052โ‹…10 5 .

You are given an array ๐ดA of size ๐‘N.You can rearrange the elements of ๐ดA as you want. After that, construct an array ๐ตB of size ๐‘N as:๐ต1=๐ด1B 1โ€‹ =A 1โ€‹ ;๐ต๐‘–=๐ต๐‘–โˆ’1โŠ•๐ด๐‘–B iโ€‹ =B iโˆ’1โ€‹ โŠ•A iโ€‹ for all 2โ‰ค๐‘–โ‰ค๐‘2โ‰คiโ‰คN, where โŠ•โŠ• denotes the bitwise XOR operator.Find a rearrangement of ๐ดA such that ๐ต๐‘–โ‰ 0B iโ€‹ ๎€ =0 for all 1โ‰ค๐‘–โ‰ค๐‘1โ‰คiโ‰คN.Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains ๐‘N, the size of the array ๐ดA.The second line of each test case contains ๐‘N space-separated integers : ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ .Output FormatFor each test case, output on a new line, ๐‘N space-separated integers denoting the rearrangement of ๐ดA satisfying the conditions.In case no such rearrangement exists, print โˆ’1โˆ’1 instead.Constraints1โ‰ค๐‘‡โ‰ค2โ‹…1041โ‰คTโ‰ค2โ‹…10 4 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 1โ‰ค๐ด๐‘–โ‰ค1091โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases does not exceed 2โ‹…1052โ‹…10 5 .Sample 1:InputOutput431 2 351 2 3 4 531 2 211-11 2 4 3 51 2 21Explanation:Test case 11 : It can be shown that it's impossible to rearrange ๐ดA in a valid way.Test case 22 : A possible rearrangement of array ๐ดA is [1,2,4,3,5][1,2,4,3,5] and the corresponding array ๐ตB is [1,3,7,4,1][1,3,7,4,1]. Here ๐ต๐‘–โ‰ 0B iโ€‹ ๎€ =0 for all 1โ‰ค๐‘–โ‰ค๐‘1โ‰คiโ‰คN.

You are given an array ๐ดA containing ๐‘N integers.Consider the following process:Let ๐‘†=0S=0 initially.For each ๐‘–i from 11 to ๐‘N in order, update ๐‘†S to either (๐‘†+๐ด๐‘–)(S+A iโ€‹ ) or (๐‘†ร—๐ด๐‘–)(Sร—A iโ€‹ ).That is, either add ๐ด๐‘–A iโ€‹ to ๐‘†S or multiply ๐‘†S by ๐ด๐‘–A iโ€‹ .Before performing the process, you're allowed to freely rearrange the elements of ๐ดA as you like.If you choose the rearrangement of ๐ดA and the sequence of operations optimally, what's the maximum possible value of ๐‘†S that you can obtain?This maximum value can be very large, so print it modulo 109+710 9 +7.Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of two lines of input.The first line of each test case contains a single integer ๐‘N โ€” the number of elements in the array.The second line contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ - the elements of the array.Output FormatFor each test case, output on a new line the maximum possible value of ๐‘†S, modulo 109+710 9 +7.Constraints1โ‰ค๐‘‡โ‰ค1031โ‰คTโ‰ค10 3 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 1โ‰ค๐ด๐‘–โ‰ค1091โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases won't exceed 2โ‹…1052โ‹…10 5 .Sample 1:InputOutput244 2 5 231 2 1804Explanation:Test case 11: Choose the rearrangement ๐ด=[2,2,5,4]A=[2,2,5,4]. Then,Add ๐ด1=2A 1โ€‹ =2 to ๐‘†S. Now, ๐‘†=2S=2.Add ๐ด2=2A 2โ€‹ =2 to ๐‘†S. Now, ๐‘†=4S=4.Multiply ๐‘†S by ๐ด3=5A 3โ€‹ =5. Now, ๐‘†=20S=20.Multiply ๐‘†S by ๐ด4=4A 4โ€‹ =4. Now, ๐‘†=80S=80.This is the maximum value that can be obtained.Test case 22: Choose any rearrangement and sum up all the numbers to get 1+1+2=41+1+2=4.This is the maximum value that can be obtained.

The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.You are given an array a๐‘Ž of length n๐‘›. Start with c=0๐‘=0. Then, for each i๐‘– from 11 to n๐‘› (in increasing order) do exactly one of the following:Option 11: set c๐‘ to c+ai๐‘+๐‘Ž๐‘–.Option 22: set c๐‘ to |c+ai||๐‘+๐‘Ž๐‘–|, where |x||๐‘ฅ| is the absolute value of x๐‘ฅ.Let the maximum final value of c๐‘ after the procedure described above be equal to k๐‘˜. Find k๐‘˜.InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases.The first line of each test case contains a single integer n๐‘› (2โ‰คnโ‰ค2โ‹…1052โ‰ค๐‘›โ‰ค2โ‹…105).The second line of each case contains n๐‘› integers a1๐‘Ž1, a2๐‘Ž2, a3๐‘Ž3, โ€ฆโ€ฆ, an๐‘Ž๐‘› (โˆ’109โ‰คaiโ‰ค109โˆ’109โ‰ค๐‘Ž๐‘–โ‰ค109).The sum of n๐‘› over all test cases does not exceed 3โ‹…1053โ‹…105.OutputFor each test case, output a single integerย โ€” the value of k๐‘˜.ExampleinputCopy5410 -9 -3 481 4 3 4 1 4 3 43-1 -2 -34-1000000000 1000000000 1000000000 100000000041 9 8 4outputCopy6246400000000022NoteIn the first test case, if we set c๐‘ to its absolute value every time we add to it, we end up with 66. It can be shown that this is the maximum result.In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get 2424.In the third test case, it is optimal to wait until the end to set c๐‘ to its absolute value, resulting in an answer of 66.

Redundant ArrayChef has an array ๐ดA containing ๐‘N positive integers.He can perform the following operation on the array as many times as he likes:Choose integers ๐ฟL and ๐‘…R such that 1โ‰ค๐ฟโ‰ค๐‘…โ‰ค๐‘1โ‰คLโ‰คRโ‰คN;Choose a positive integer ๐‘ฅx;For every index ๐‘–i from ๐ฟL to ๐‘…R (inclusive of both), set ๐ด๐‘–=๐‘ฅA iโ€‹ =x.The cost of such an operation is (๐‘…โˆ’๐ฟ+1)โ‹…๐‘ฅ(Rโˆ’L+1)โ‹…x.Find the minimum cost required to make all the elements of ๐ดA equal.Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists two lines of input.The first line of each test case contains a single integer ๐‘N โ€” the size of the array.The second line contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ .Output FormatFor each test case, output on a new line the minimum cost needed to make all the elements of ๐ดA equal.Constraints1โ‰ค๐‘‡โ‰ค2ร—1051โ‰คTโ‰ค2ร—10 5 1โ‰ค๐‘โ‰ค2ร—1051โ‰คNโ‰ค2ร—10 5 1โ‰ค๐ด๐‘–โ‰ค๐‘1โ‰คA iโ€‹ โ‰คNThe sum of ๐‘N over all test cases does not exceed 2ร—1052ร—10 5 Sample 1:InputOutput352 4 3 2 233 3 371 3 7 5 1 4 3405

1/2

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.