Ask Question
12 August, 16:48

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. 12 August, 19:40
    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?
Find an answer to your question ✅ “Write a function that given an integer N (1 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers