Complete Array: Tricky Question Solution in C#
Complete Array: An array is defined to be complete if the conditions (a), (d) and (e) below hold.
a. The array contains even numbers
b. Let min be the smallest even number in the array.
c. Let max be the largest even number in the array.
d. min does not equal max
e. All numbers between min and max are in the array
For example {-5, 6, 2, 3, 2, 4, 5, 11, 8, 7} is complete because
a. The array contains even numbers
b. 2 is the smallest even number
c. 8 is the largest even number
d. 2 does not equal 8
e. the numbers 3, 4, 5, 6, 7 are in the array.
Examples of arrays that are not complete are:
{5, 7, 9, 13} condition (a) does not hold, there are no even numbers.
{2, 2} condition (d) does not hold
{2, 6, 3, 4} condition (e) does not hold (5 is missing)
Write a function named isComplete that returns 1 if its array argument is a complete array. Otherwise it returns 0.
If you are writing in Java or C#, the function signature is
int isComplete (int[ ] a)
If you are writing in C or C++, the function signature is
int isComplete (int a[ ], int len) where len is the number of elements in the array.
There are three questions on this test. You have two hours to complete it. Please do your own work. You are not allowed to use any methods or functions provided by the system unless explicitly stated in the question. In particular, you are not allowed to convert int to a String or vice-versa
The solution for the problem is provided below in C#:
using System;
namespace CompleteArray
{
class Program
{
// To check if the array is even......
void IsEven(int[] arr,int l)
{
int count = 0;
int e1 = 0;
int e2 = 0;
int temp = 0;
for(int j=0;j<l;j++)
{
if ((arr[j] % 2) == 0)
{
count++;
if (count == 1)
{
e1 = arr[j];
}
if (count == 2)
{
e2 = arr[j];
}
}
}
if ((e1 < e2) && (count>=2))
{
IsCompleteArray(arr, l, e1, e2);
}
else if((e1>e2) && (count>=2))
{
temp=e1;
e1 = e2;
e2 = temp;
//To check if the array is complete array............
IsCompleteArray(arr, l, e1, e2);
}
else
{
Console.WriteLine("The array should contain at least two even numbers:");
}
}
void IsCompleteArray(int[] arr,int l,int e1,int e2)
{
int count = 0;
for(int k=e1;k<e2;k++)
{
if(Array.Exists(arr,x=>x==k))
{
count++;
}
}
if(count==(e2-e1))
{
Console.WriteLine( "The array is CompleteArray" );
}
else
{
Console.WriteLine("The array is incomplete");
}
}
static void Main(string[] args)
{
int[] arr = new int[10];
Console.WriteLine("Enter an array of numbers");
Console.WriteLine("How many numbers do you want to enter:");
int l = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the numbers:");
for(int i=0;i<l;i++)
{
arr[i] =int.Parse(Console.ReadLine());
}
Program p = new Program();
//Invoke the IsEven function...............
p.IsEven(arr,l);
Console.ReadKey();
}
}
}
First the main function gets executed where we take the input from the user to our array from the for loop. Then we call the IsEven() function passing the array and the length of the array as the arguments.In the IsEven() function individual number is checked if the number is even or odd and we also check for the condition.
a. The array contains even numbers
d. min does not equal max
e. the numbers 3, 4, 5, 6, 7 are in the array.
After the condition is checked the IsCompleteArray() is called which checks whether the array is complete or not finally and displays the result.
The Output is given below:
If you are newbie to C# then this will be a challenge and a great tricky solution for you. You can also solve the question in your own way and method. If you have any problem with the solution or have a different kind of solution for the solution please comment down below.
Hope this solution can be helpful for you. This kind of questions are even asked in programming interviews.
Also Read: Best Laptops for Programmers