Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to find the minimal possible sum of two distinct elements in a list with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

/*
    Goal:
    -----
    Find the minimal value of (a[i] + a[j]) for any two distinct elements in a list.

    Efficient Strategy (O(n)):
    --------------------------
    The smallest possible sum of two distinct elements is obtained by:
        - finding the smallest element
        - finding the second smallest element
    Because any other pair must be >= one of these two.

    We scan the list once, keeping track of:
        - min1  = smallest element seen so far
        - min2  = second smallest element seen so far
*/

class MinimalTwoSumProgram
{
    // A function that computes the minimal sum of two distinct elements.
    public static int MinimalTwoSum(List<int> lst)
    {
        // Handle edge case: need at least two elements
        if (lst.Count < 2)
        {
            throw new ArgumentException("List must contain at least two elements.");
        }

        // Initialize min1 and min2 to very large values
        int min1 = int.MaxValue;
        int min2 = int.MaxValue;

        // Single pass through the list
        foreach (int x in lst)
        {
            if (x < min1)
            {
                // x becomes the new smallest; old min1 becomes min2
                min2 = min1;
                min1 = x;
            }
            else if (x < min2)
            {
                // x is not the smallest, but smaller than the second smallest
                min2 = x;
            }
        }

        // The minimal sum of two distinct elements
        return min1 + min2;
    }

    static void Main()
    {
        List<int> lst = new List<int> { 7, -3, 10, 1, 5, 2, 4 };

        try
        {
            int result = MinimalTwoSum(lst);
            Console.WriteLine("Minimal sum of two elements: " + result);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}


/*
run:

Minimal sum of two elements: -2

*/
using System;
using System.Collections.Generic;

/*
    Goal:
    -----
    Find the minimal value of (a[i] + a[j]) for any two distinct elements in an array.

    Efficient Strategy (O(n)):
    --------------------------
    The smallest possible sum of two distinct elements is obtained by:
        - finding the smallest element
        - finding the second smallest element
    Because any other pair must be >= one of these two.

    We scan the array once, keeping track of:
        - min1  = smallest element seen so far
        - min2  = second smallest element seen so far
*/

class MinimalTwoSumProgram
{
    // A function that computes the minimal sum of two distinct elements.
    public static int MinimalTwoSum(List<int> lst)
    {
        // Handle edge case: need at least two elements
        if (lst.Count < 2)
        {
            throw new ArgumentException("Array must contain at least two elements.");
        }

        // Initialize min1 and min2 to very large values
        int min1 = int.MaxValue;
        int min2 = int.MaxValue;

        // Single pass through the array
        foreach (int x in lst)
        {
            if (x < min1)
            {
                // x becomes the new smallest; old min1 becomes min2
                min2 = min1;
                min1 = x;
            }
            else if (x < min2)
            {
                // x is not the smallest, but smaller than the second smallest
                min2 = x;
            }
        }

        // The minimal sum of two distinct elements
        return min1 + min2;
    }

    static void Main()
    {
        List<int> lst = new List<int> { 7, -3, 10, 1, 5, 2, 4 };

        try
        {
            int result = MinimalTwoSum(lst);
            Console.WriteLine("Minimal sum of two elements: " + result);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}


/*
run:

Minimal sum of two elements: -2

*/

 



answered Jul 21 by avibootz
edited Jul 21 by avibootz
...