Pergunta de entrevista da empresa Meta

Add each number in an array until the sum equals the rest of the array

Respostas da entrevista

Sigiloso

2 de set. de 2017

#!/usr/bin/env python3 # time complexity = O(n) arr = [1, -2, 3, 2, 0, -1, 10, -9, 2] arr_sum = 0 for i in range(0, len(arr)): arr_sum += arr[i] i_sum = 0 r_sum = arr_sum found = None for i in range(0, len(arr)): i_sum += arr[i] r_sum -= arr[i] if i_sum == r_sum: found = i break if found is None: print("no index found that satisfies the given requirement") else: print("array: {}".format(arr)) print("first: {}".format(arr[:found+1])) print("second: {}".format(arr[found+1:]))

2

Sigiloso

6 de jun. de 2018

Out[17]: l = [1, -2, 3, 2, 0, -1, 10, -9, 2] In [16]: for i in xrange(len(l)): l_temp += l[i] if l_temp == sum(l[i+1:]): print "found index = {0}, value={1}".format(i, l[i]) break ....: out: found index = 5, value=-1

1

Sigiloso

11 de nov. de 2018

I dont know what is difficult about this question and why do they ask this from everyone