Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Finding the length of the Arrays

How to find the length of the Array, explain with examples:

A non-empty zero-indexed Array A consisting of N integers is given.
Array A represents a linked list. A list is constructed from this array as follows:

• the first node (the head) is located at index 0;
• the value of a node located at index K is A[K];
• the successor of a node located at index K is located at index A[K];
• if the value of a node is −1 then it is the last node of the list. For example, for array A such that: A[0] = 1
A[1] = 4
A[2] = -1
A[3] = 3
A[4] = 2
the following list is constructed:
• the first node (the head) is located at index 0 and has a value of 1;
• the second node is located at index 1 and has a value of 4;
• the third node is located at index 4 and has a value of 2;
• the fourth node is located at index 2 and has a value of −1.


Write a function:
int FindLength (int[] A);
that, given a non-empty zero-indexed array A consisting of N integers, returns the length of the list constructed from A in the above manner.
For example, given array A such that:
 A[0] = 1
 A[1] = 4
 A[2] = -1
 A[3] = 3
 A[4] = 2
the function should return 4, as explained in the example above.

The below are the assumptions that helps you to solve the problem:
• N is an integer within the range [1..200,000];
• each element of array A is an integer within the range [−1..200,000];
• it will always be possible to construct the list and its length will be finite.

 Answer-1:
#include 

int main ()
{
using namespace std;
int arr[] = { 1, 4, -1, 3, 2 };
auto array_length = end(arr) - begin(arr);
cout }
Answer -2
int arr[] = { 1, 4, -1, 3, 2 };
you should use the array class and the array template. Try:
#include 
array Name_of_Array = {};
so now if you want to find the length of the array all you have to do use the size function in the array class.
Name_of_Array.size();

and that should return the length of elements in the array
Answer-3: If ir is a C-style array, then we can do like the below:
int a[10];
std::cout This doesn't work on pointers, though, i.e. it won't work for either of the following:
int *iptr = new int[10];
std::cout or:
void func(int * iptr)
{ std::cout func(a)


This post first appeared on Tutorials For C,C++ Programming,course Material For beginners, please read the originial post: here

Share the post

Finding the length of the Arrays

×

Subscribe to Tutorials For C,c++ Programming,course Material For beginners

Get updates delivered right to your inbox!

Thank you for your subscription

×