Counting Cross Lines In An Array In C++ - C++ Programming Tutorial
C++ Course / Arrays / Counting Cross Lines In An Array In C++

Counting Cross Lines In An Array In C++

BLUF: Mastering Counting Cross Lines In An Array In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Counting Cross Lines In An Array In C++

C++ is renowned for its efficiency. Learn how Counting Cross Lines In An Array In C++ enables low-level control and high-performance computing in the tutorial below.

An array manipulation task carries significant importance in the field of computer science, particularly within the realm of algorithmic problem-solving. Arrays, structured by their indices, store elements consecutively in memory. Various scenarios demand diverse array operations, such as searching, arranging, or identifying specific patterns within them, which occur frequently. The challenge lies in devising an efficient approach to swiftly count the diagonal lines in the array. These diagonal lines can run from left to right or right to left, forming cross patterns. To ascertain the presence of a cross line at a specific location, diagonal traversal of the array becomes necessary, introducing complexity to the task. Upon handling a distinct assortment of unsorted items, the objective shifts to sorting the array elements and subsequently tallying the count of cross lines emerging from these 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's consider an example to demonstrate how to count intersecting lines in an array using 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's consider another scenario to demonstrate counting intersecting 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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience