Ask Question

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)

+2
Answers (1)
  1. 4 July, 12:50
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers