Home > Enterprise >  return the smallest element of the given non-empty array
return the smallest element of the given non-empty array

Time:08-12

I need to fix the bug and modify one line of code in the incorrect implementation of a function solution that is supposed to return the smallest element of the given non-empty array A which contains at most 1000 integers within range [-1000..1000]

using System;

class Solution {
    public int solution(int[] A) {
        int ans = 0;
        for (int i = 1; i < A.Length; i  ) {
            if (A[i] < ans) {
                ans = A[i];
            } 
        }
       return ans;
    } 
}

CodePudding user response:

I suggest you to use Linq:

using System.Linq;
[...]
int result = A.Min();

If you have to rewrite your function you must use

    public int solution(int[] A) {
        int ans = A[0];
        for (int i = 1; i < A.Length; i  ) {
            if (A[i] < ans) {
                ans = A[i];
            } 
        }
       return ans;
    } 

CodePudding user response:

You are almost there. There are two problems:

  1. Array indexes range from 0 to Length -1 (you started at 1).
  2. You initialized ans with 0. If all elements of the array are greater than zero, then ans will never be assigned another number.

You can solve both problems at once, by initializing ans with the first element of the array. This is better than initializing it with a magic number (e.g., like 0, or 1000 or Int32.MaxValue etc.) as it does not make an assumption of what the range of the numbers will be.

int ans = A[0];
for (int i = 1; i < A.Length; i  ) {
    if (A[i] < ans) {
        ans = A[i];
    } 
}
return ans;

Note that this works only for non-empty arrays (as you stated). If the array can be empty, i.e., if it has zero elements, then no smallest number exists. You could return null from this function in this case and have the function return a nullable int (int?).

public int? SmallestNumber(int[] A) {
    int? ans = null;
    for (int i = 0; i < A.Length; i  ) { // Start at 0 this time!
        if (ans is null || A[i] < ans) {
            ans = A[i];
        } 
    }
    return ans;
}

CodePudding user response:

I need the problem is your initialize wrong value to ans. You're initializing 0 and the index for loop start 1. You should initialize the first value of value before loop start or start the loop index of 0.

CodePudding user response:

It must be int ans = A[0];, then you check every element if it is less then ans and eventiually ans becomes the smallest.

CodePudding user response:

  1. You start the loop at index 1 but collections are always 0-based
  2. You initialize the min value with 0, but what if the values in the collection are greater than 0?

You could use LINQ:

int minValue = A.Min();

However, since i guess that this is for school, just change your implementartion accordingly. You could initialize the min value with the first value in the list:

if(A == null || A.Length == 0) return int.MinValue; // better return int?, so null
int ans = A[0];
for (int i = 1; i < A.Length; i  ) {
    if (A[i] < ans) {
        ans = A[i];
    } 
}
  •  Tags:  
  • c#
  • Related