Counting Cross Lines In An Array In C++

An array manipulation task is essential to computer science, especially in the area of algorithmic problem solving. Arranged using their indices, arrays are groups of elements kept in consecutive memory locations. Situations where we must manipulate arrays in different ways, for example, by searching, sorting, or counting specific patterns within them, occur frequently. The job is to come up with a method that counts the cross lines in the array as quickly as possible. Left-to-right or right-to-left diagonal lines can be used for these cross lines. In order to determine whether a cross line exists at a given position. we must traverse the array diagonally, which adds complexity to the problem. Considering a unique collection of unsorted items. After sorting the array elements, the task is to count the number of cross lines that are formed in the array elements.

Example

Input:  arr[] = { 3, 2, 1, 4, 5 }
Output: 3
      before sort: 5  4  3   6  7
                             \  |   /   |  | 
                              \ |  /    |  |
                             /  |  \   |  |
      After sort : 3  4  5  6  7 
      line (1st to 1st ) cross line (2nd  to 2nd )
      line (1st  to 1st) cross line (3rd  to 3rd )
      line (2nd  to 2nd) cross line (3rd  to 3rd )

Example 1:

Let us take an example to illustrate the counting cross lines in an array in C++.

Example

#include <iostream>
using namespace std;

// Function to count cross lines in the array
int countCrossLine(int arr[], int n) {
    int countCrossLine = 0;
    int  x, key, y;
    for (x = 1; x < n; x++) {
        key = arr[x];
        y = x - 1;

        // Move elements of arr[0..x-1], that are greater than key, to one position ahead of their current position
        while (y >= 0 && arr[y] > key) {
            arr[y + 1] = arr[y];
            y = y - 1;

            // Increment cross line count by one
            countCrossLine++;
        }
        arr[y + 1] = key;
    }
    return countCrossLine;
}

// Driver program to test the countCrossLine function
int main() {
    int arr[] = { 5, 3, 2, 4, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Number of cross lines: " << countCrossLine(arr, n) << endl;
    return 0;
}

Output:

Output

Number of cross lines: 8

Complexity Analysis:

Time complexity: O(n2)

Auxiliary space: O(1)

Example 2:

Let us take another example to illustrate the counting cross lines in an array in C++.

Example

#include <iostream>
using namespace std;

// Merges two subarrays of arr[]
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r, int* countCrossLine) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    /* create temp arrays */
    int L[n1], R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];

            //Main Section of the Code//
         // Add all lines which are crossed by the current element
            *countCrossLine += (n1 - si);
            j++;
        }
        k++;
    }

    /* Copy the remaining elements of L[], if there are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r, int* countCrossLine) {
    if (l < r) {

        // Same as (l+r)/2, but avoids overflow for large l and h
        int m = l + (r - l) / 2;

        // Sort first and second halves
        mergeSort(arr, l, m, countCrossLine);
        mergeSort(arr, m + 1, r, countCrossLine);

        merge(arr, l, m, r, countCrossLine);
    }
}

// Function to count cross lines in the array
int countCrossLine(int arr[], int n) {
    int countCrossLine = 0;
    mergeSort(arr, 0, n - 1, &countCrossLine);

    return countCrossLine;
}

// Driver program to test the countCrossLine function
int main() {
    int arr[] = { 10, 3, 15, 7, 8, 9 }; // Custom input array
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Number of cross lines: " << countCrossLine(arr, n) << endl;
    return 0;
}

Output:

Output

Number of cross lines: 7

Complexity Analysis:

Time complexity: O(nlogn)

Auxiliary Space: O(n)

Input Required

This code uses input(). Please provide values below: