lundi 2 octobre 2023

the sum of integers whose index is between n1 and​​​​​​‌​‌​​‌‌​​​‌​‌​​​​​‌​‌​​‌‌ n2

the sum of integers whose index is between n1 and​​​​​​‌​‌​​‌‌​​​‌​‌​​​​​‌​‌​​‌‌ n2


 using System;

class Solution

{

    /// <returns>the sum of integers whose index is between n1 and​​​​​​‌​‌​​‌‌​​​‌​‌​​​​​‌​‌​​‌‌ n2</returns>

public static int Calc(int[] array, int n1, int n2) {

        if(n1 < 0 || n1 > n2 || n2 > array.Length || n1 > array.Length){

            throw new ArgumentException("invalid input");

        }

        int sum = 0;

        for(int i = n1; i <= n2; i++){

            sum = sum + array[i];

        }

        return sum;

    }

}

Aucun commentaire:

Enregistrer un commentaire