How to find Missing Element in Series(1, 3, 5, 9, 11).
Main Program
using System;namespace DotNetByPriyanshu
{
class Program
{
static void Main(string[] args)
{
int[] Series1 = { 1, 3, 5, 9, 11 }; // Output : 7
int[] Series2 = { 2, 4, 6, 10, 12 }; // Output : 8
// creating an object of missingElement Class
missingElement missingElement = new missingElement();
int result1 = missingElement.Missing(Series1.Length, Series1);
Console.WriteLine("Missing Element for Series1= " + result1);
int result2 = missingElement.Missing(Series2.Length, Series2);
Console.WriteLine("Missing Element for Series2= " + result2);
Console.ReadLine();
}
}
}
Method:
namespace DotNetByPriyanshu
{
public class missingElement
{
public int Missing(int size, int[] Array)
{
//Arithmetic Series sum of sequence.
//sn=n/2[2a+(n-1)d]
//sumOfSeries=1+3+5+9+11;
//MissingNumber=sn-sumOfSeries.
int MissingNumber = 0;
int n = Array.Length + 1;
int d = Array[1] - Array[0];
int sn = n / 2 * (2 * Array[0] + (n - 1) * d); // Formula
// Sum of array.
int sumOfArray = 0;
for (int i = 0; i < Array.Length; i++)
{
sumOfArray = sumOfArray + Array[i];
}
// Calculate Missing Element.
MissingNumber = sn - sumOfArray;
return MissingNumber;
}
}
}
Output:
Series1= { 1, 3, 5, 9, 11 }; // Output : 7
Series2= { 2, 4, 6, 10, 12 }; // Output : 8
Series2= { 2, 4, 6, 10, 12 }; // Output : 8
No comments:
Post a Comment