TECHNOLOGIES:

Interview Challenge Return N Number in an Array

Article:
Viewed:  36
Posted On:  10/30/2017 9:13:49 PM
How you can return N numbers from an Array

Reverse N Number of Elements in an Array

In this article I am going to explain how we can return N number of elements in an array.

Below is the logic:

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApp2
{

class Program
{

static void Main(string[] args)
{

int i, n;
int[] a = new int[5];
Console.WriteLine("Input 5 number of elements in the array :\n");

for (i = 0; i < 5; i++)
{
Console.Write("element - {0} : ", i);
}

Console.WriteLine("\n\n The values store into the array are : \n");

for (i = 0; i < a.Length; i++)
{
Console.Write("{0}  ", a[i]);
}

Console.WriteLine(" \n\n Input the number of elements to reverse in the array :");

int[] b = new int[n];
int[] c = new int[(a.Length) - n];

for (int j = 0; j < n; j++)
{
b[j] = a[j];
}

int pos2 = 0;

for (int j = n; j < a.Length; j++)
{
c[pos2] = a[j];
pos2 = pos2 + 1;
}

Console.WriteLine("\n\n Array To SORT : \n");

for (i = 0; i < b.Length; i++)
{
Console.Write("{0}  ", b[i]);
}

Console.WriteLine("\n\n REST OF The Array  : \n");

for (i = 0; i < c.Length; i++)
{
Console.Write("{0}  ", c[i]);
}

int[] RevArray = new int[a.Length];

int pos3 = 0;

for (int k = b.Length - 1; k >= 0; k--)
{
RevArray[pos3] = b[k];
pos3 = pos3 + 1;
}

int pos4 = b.Length;

for (i = 0; i < c.Length; i++)
{
RevArray[pos4] = c[i];
pos4 = pos4 + 1;
}

Console.WriteLine("\n\n FINAL OUTPUT Array  : \n");

for (i = 0; i < RevArray.Length; i++)
{
Console.Write("{0}  ", RevArray[i]);
}

}

}

}

Output:

Image 1.

Image 2.

 Comment: