Skip to main content
edited body
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}

If you want to brute force it:

public static Tuple<int, int> GetPairBrute(List<int> ints, int sum)
{
    for (int i = 0; i < ints.Count - 1; i++)
    {
        for (int j = i =+ 1; j < ints.Count; j++)
        {
            if (ints[i] + ints[j] == sum)
            {
                return new Tuple<int, int>(ints[i], ints[j]);
            }
        }
    }
    return null;
}

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}

If you want to brute force it:

public static Tuple<int, int> GetPairBrute(List<int> ints, int sum)
{
    for (int i = 0; i < ints.Count - 1; i++)
    {
        for (int j = i = 1; j < ints.Count; j++)
        {
            if (ints[i] + ints[j] == sum)
            {
                return new Tuple<int, int>(ints[i], ints[j]);
            }
        }
    }
    return null;
}

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}

If you want to brute force it:

public static Tuple<int, int> GetPairBrute(List<int> ints, int sum)
{
    for (int i = 0; i < ints.Count - 1; i++)
    {
        for (int j = i + 1; j < ints.Count; j++)
        {
            if (ints[i] + ints[j] == sum)
            {
                return new Tuple<int, int>(ints[i], ints[j]);
            }
        }
    }
    return null;
}
added 452 characters in body
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}

If you want to brute force it:

public static Tuple<int, int> GetPairBrute(List<int> ints, int sum)
{
    for (int i = 0; i < ints.Count - 1; i++)
    {
        for (int j = i = 1; j < ints.Count; j++)
        {
            if (ints[i] + ints[j] == sum)
            {
                return new Tuple<int, int>(ints[i], ints[j]);
            }
        }
    }
    return null;
}

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}

If you want to brute force it:

public static Tuple<int, int> GetPairBrute(List<int> ints, int sum)
{
    for (int i = 0; i < ints.Count - 1; i++)
    {
        for (int j = i = 1; j < ints.Count; j++)
        {
            if (ints[i] + ints[j] == sum)
            {
                return new Tuple<int, int>(ints[i], ints[j]);
            }
        }
    }
    return null;
}
Source Link
paparazzo
  • 6k
  • 3
  • 19
  • 41

You sort but don't use the sort. Not getting why you need to reset loopUpperBound = orderedList.Count; Just start with that. Don't use Y N - use a Boolean. You are potentially evaluating each pair twice.

It is pretty simple:

public static Tuple<int, int> GetPair(List<int> ints, int sum)
{
    int upperIndex = ints.Count - 1;
    int lowerIndex = 0;
    ints.Sort();
    while (lowerIndex < upperIndex)
    {
        int curSum = ints[lowerIndex] + ints[upperIndex];
        if (curSum == sum)
        {
            return new Tuple<int, int>(ints[lowerIndex], ints[upperIndex]);
        }
        if (curSum < sum)
        {
            lowerIndex++;
        }
        else
        {
            upperIndex--;
        }
    }
    return null;
}