Ask Question
30 March, 05:18

Write a function that given an integer N (1<=N<=100) returns an array containing N distinct Integers that sum up to 0. The function can return any such array. For example, given N=4, the function could return (1,0,-3, 2], and for N=3 one of the possible answers is [-1,0,1] (but there are many more correct answers)

+4
Answers (1)
  1. 30 March, 07:45
    0
    This is simple. I We begin by creating 2 groups of equal and opposite. The total number of elements in both group is N (if N is even) or N-1 (if N is odd). The total value of both group should equal to 0. Add 0 if N is odd

    We can code this function in python

    def array_maker (N):

    if N % 2 = = 1:

    if N = = 1: # special case

    return [0]

    else:

    left_ray = [i for i in range (-N//2, 1) ]

    right_ray = [i for i in range (N//2) ]

    left_ray. extend (right_ray)

    return left_ray

    else:

    left_ray = [i for i in range (-N/2, 0) ]

    right_ray = [i for i in range (N/2) ]

    left_ray. extend (right_ray)

    return left_ray
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a function that given an integer N (1 ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers