Home > Backend Development > C++ > body text

Find start and end index of elements in unsorted array in C++

WBOY
Release: 2023-08-29 10:17:06
forward
851 people have browsed it

Find start and end index of elements in unsorted array in C++

In this problem, we are given an array aar[] containing n unsorted integer values ​​and an integer val. Our task is to find the starting and ending index of an element in an unsorted array.

For occurrences of an element in the array, we will return,

"start index and end index" (if found twice or more in the array).

"Single index" if found

"Element does not exist" if not found in the array.

Let us take an example to understand the problem,

Example 1

Input : arr[] = {2, 1, 5, 4, 6, 2, 3}, val = 2
Output : starting index = 0, ending index = 5
Copy after login

Explanation

Element 2 appears twice,

appears for the first time at index = 0,

appears for the second time at index = 5

Example 2

Input : arr[] = {2, 1, 5, 4, 6, 2, 3}, val = 5
Output : Present only once at index 2
Copy after login

Explanation

Element 5 appears only once at index = 2,

Example 3

Input : arr[] = {2, 1, 5, 4, 6, 2, 3}, val = 7
Output : Not present in the array!
Copy after login

Solution

Solve this A simple approach to the problem is to iterate over the array.

We will iterate through the array and keep two index values: first and last. The first index will traverse the array from the beginning, and the last index will traverse from the end of the array. The loop ends when the elements at the first and last index have the same value.

Algorithm

  • Step 1 - Loop through the array

    • Step 1.1 - Use the first index to traverse from the beginning and the last index to traverse from the end.

    • Step 1.2 - If the value at any index is equal to val. Do not increase the index value.

    • Step 1.3 - Return if the two indexes have the same value.

    • < /ul>

    Example

    Program that illustrates how our solution works

    #include 
    using namespace std;
    
    void findStartAndEndIndex(int arr[], int n, int val) {
       int start = 0;
       int end = n -1 ;
       while(1){
       if(arr[start] != val)
          start++;
       if(arr[end] != val)
          end--;
       if(arr[start] == arr[end] && arr[start] == val)
          break;
       if(start == end)
          break;
    }
       if (start == end ){
          if(arr[start] == val)
             cout<<"Element is present only once at index : "<
    Copy after login

    Output

    Element present twice at
    Start index: 0
    Last index: 8
    Copy after login

    The above is the detailed content of Find start and end index of elements in unsorted array in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!