Hướng dẫn sum of two numbers in array python - tổng hai số trong mảng python

Vấn đề chính với các giải pháp kiểm tra tất cả các cặp vợ chồng có thể [với các vòng lặp hoặc itertools.combinations] là O [n^2], vì về cơ bản bạn kiểm tra tất cả các kết hợp có thể của hai yếu tố giữa n [có n*[n-1]/ 2 Kết hợp như vậy] cho đến khi bạn tìm thấy một kết hợp hợp lệ.

Khi N lớn, bạn sẽ cần một thuật toán hiệu quả hơn. Một khả năng là trước tiên sắp xếp danh sách [đây là O [N * log [n]]], tìm giải pháp sau đó có thể được thực hiện trực tiếp trong O [n], cung cấp cho bạn một giải pháp trong O [n * log [n] ].

Chúng tôi sắp xếp danh sách trước, sau đó thêm các giá trị đầu tiên [nhỏ nhất] và cuối cùng [lớn nhất]. Nếu tổng quá lớn, chúng ta có thể loại bỏ giá trị lớn nhất. Nếu nó quá nhỏ, chúng tôi loại bỏ cái nhỏ nhất, cho đến khi chúng tôi đạt được tổng chính xác.

Chúng ta có thể sử dụng một bộ sưu tập.deque để loại bỏ hiệu quả các giá trị ở bất kỳ đầu nào của danh sách.

Để truy xuất các chỉ số, chúng tôi giữ chúng bên cạnh các giá trị trong các bộ dữ liệu.

from collections import deque


def find_target[values, target]:

    dq = deque[sorted[[[val, idx] for idx, val in enumerate[values]]]]

    while True:
        if len[dq] < 2:
            raise ValueError['No match found']

        s =  dq[0][0] + dq[-1][0]

        if s > target:
            dq.pop[]
        elif s < target:
            dq.popleft[]  
        else:
            break
    return dq[0], dq[-1]



values = [23, 5, 55, 11, 2, 12, 26, 16]
target = 27

sol = find_target[values, target]

print[sol]
# [[11, 3], [16, 7]]
# 11 + 16 == 27, indices 3 and 7

print[sol[0][1], sol[1][1]]
# 3 7

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50

    Bàn luận Iterating through the array and adding each element to the sum variable and finally displaying the sum.

    Python3

    def _sum[arr]:

    Đưa ra một loạt các số nguyên, tìm tổng các yếu tố của nó.

    Phương pháp 1: Lặp lại qua mảng và thêm từng phần tử vào biến tổng và cuối cùng hiển thị tổng.

    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    0____11
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    3

    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    0
    Sum of the array is  34
    6
    Sum of the array is  34
    7
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    1
    Sum of the array is  34
    9

    sum[iterable] 
    0____12
    sum[iterable] 
    2

    sum[iterable] 
    0
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    sum[iterable] 
    5
    sum[iterable] 
    6
    sum[iterable] 
    7
    sum[iterable] 
    8
    sum[iterable] 
    7
    Sum of the array is  34
    0
    sum[iterable] 
    7
    Sum of the array is  34
    2
    Sum of the array is  34
    3

    Sum of the array is  34
    4
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    Sum of the array is  34
    6
    Sum of the array is  34
    7

    Sum of the array is  34
    8
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    reduce[ function, Array ];
    0

    Vui lòng tham khảo đầy đủ bài viết về chương trình để tìm tổng các phần tử trong một mảng nhất định để biết thêm chi tiết!

    Output:

    Sum of the array is  34

    Cải thiện bài viết: O[n], Auxiliary Space: O[1]

    Lưu bài viết Using the built-in function sum[]. Python provides an inbuilt function sum[] which sums up the numbers in the list.

    Syntax:  

    sum[iterable] 

    Đọc iterable can be anything list, tuples or dictionaries, but most importantly it should be numbered.

    Python3

    sum[iterable] 
    0____12
    sum[iterable] 
    2

    sum[iterable] 
    0
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    sum[iterable] 
    5
    sum[iterable] 
    6
    sum[iterable] 
    7
    sum[iterable] 
    8
    sum[iterable] 
    7
    Sum of the array is  34
    0
    sum[iterable] 
    7
    Sum of the array is  34
    2
    Sum of the array is  34
    3

    Sum of the array is  34
    4
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    Sum of the array is  34
    6
    Sum of the array is  34
    7

    reduce[ function, Array ];
    1
    Sum of the array is  34
    7
    reduce[ function, Array ];
    3
    reduce[ function, Array ];
    4

    Output:

    Sum of the array is  34

    Cải thiện bài viết: O[n], Auxiliary Space: O[1]

    Lưu bài viết Using the reduce method. Array.reduce[] method is used to iterate over the array and get the summarized result from all elements of array.

    Syntax:

    reduce[ function, Array ];

    Đọc

    Bàn luận

    def _sum[arr]:

    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    0
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2 def0
    Sum of the array is  34
    7def8

    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    0
    Sum of the array is  34
    6
    Sum of the array is  34
    7
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    1
    Sum of the array is  34
    9

    sum[iterable] 
    0____12
    sum[iterable] 
    2

    sum[iterable] 
    0
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    sum[iterable] 
    5
    sum[iterable] 
    6
    sum[iterable] 
    7
    sum[iterable] 
    8
    sum[iterable] 
    7
    Sum of the array is  34
    0
    sum[iterable] 
    7
    Sum of the array is  34
    2
    Sum of the array is  34
    3

    Sum of the array is  34
    4
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    Sum of the array is  34
    6
    Sum of the array is  34
    7

    Sum of the array is  34
    8
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    2
    reduce[ function, Array ];
    0

    reduce[ function, Array ];
    1
    Sum of the array is  34
    7
    reduce[ function, Array ];
    3
    reduce[ function, Array ];
    4

    Output:

    Sum of the array is  34

    Vui lòng tham khảo đầy đủ bài viết về chương trình để tìm tổng các phần tử trong một mảng nhất định để biết thêm chi tiết!


    Bài Viết Liên Quan

    Chủ Đề