ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Complete Array: Tricky Question Solution in C#

.Net C# about 7 years ago || 2/21/2019 || 2.4 K View

  • Hide

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

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Binod  Bhandari

Binod Bhandari

Life is not just about thrieving but all about living.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey