Counting cross lines in an array
Given an unsorted array of distinct elements. Task is to count number of cross lines formed in an array elements after sorting the array elements.
Note: Draw a line between same array elements before sorting and after sorting the array elements.
Examples :
Input : arr[] = { 3, 2, 1, 4, 5 }
Output : 3
before sort: 3 2 1 4 5
\ | / | |
\|/ | |
/ | \ | |
After sort : 1 2 3 4 5
line (1 to 1) cross line (2 to 2)
line (1 to 1) cross line (3 to 3)
line (2 to 2) cross line (3 to 3)
Note: the line between two 4s and the line
between two 5s don't cross any other lines;
Input : arr[] = { 5, 4, 3, 1 }
Output : 6
Simple solution of this problem is based on the insertion sort. we simply pick each array elements one-by-one and try to find it’s proper position in the sorted array.during finding it’s appropriate position of an element we have to cross all the element_line whose value is greater than current element.
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int countCrossLine( int arr[], int n)
{
int count_crossline = 0;
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
count_crossline++;
}
arr[j + 1] = key;
}
return count_crossline;
}
int main()
{
int arr[] = { 4, 3, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countCrossLine(arr, n) << endl;
return 0;
}
|
Java
class GFG
{
static int countCrossLine( int arr[],
int n)
{
int count_crossline = 0 ;
int i, key, j;
for (i = 1 ; i < n; i++)
{
key = arr[i];
j = i - 1 ;
while (j >= 0 && arr[j] > key)
{
arr[j + 1 ] = arr[j];
j = j - 1 ;
count_crossline++;
}
arr[j + 1 ] = key;
}
return count_crossline;
}
public static void main(String args[])
{
int arr[] = new int []{ 4 , 3 , 1 , 2 };
int n = arr.length;
System.out.print(countCrossLine(arr, n));
}
}
|
Python3
def countCrossLine(arr, n):
count_crossline = 0 ;
i, key, j = 0 , 0 , 0 ;
for i in range ( 1 , n):
key = arr[i];
j = i - 1 ;
while (j > = 0 and arr[j] > key):
arr[j + 1 ] = arr[j];
j = j - 1 ;
count_crossline + = 1 ;
arr[j + 1 ] = key;
return count_crossline;
if __name__ = = '__main__' :
arr = [ 4 , 3 , 1 , 2 ];
n = len (arr);
print (countCrossLine(arr, n));
|
C#
using System;
class GFG {
static int countCrossLine( int []arr, int n)
{
int count_crossline = 0;
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
count_crossline++;
}
arr[j + 1] = key;
}
return count_crossline;
}
public static void Main()
{
int []arr = new int []{ 4, 3, 1, 2 };
int n = arr.Length;
Console.Write(countCrossLine(arr, n));
}
}
|
PHP
<?php
function countCrossLine( $arr , $n )
{
$count_crossline = 0;
$i ; $key ; $j ;
for ( $i = 1; $i < $n ; $i ++)
{
$key = $arr [ $i ];
$j = $i - 1;
while ( $j >= 0 and $arr [ $j ] > $key )
{
$arr [ $j + 1] = $arr [ $j ];
$j = $j - 1;
$count_crossline ++;
}
$arr [ $j + 1] = $key ;
}
return $count_crossline ;
}
$arr = array ( 4, 3, 1, 2 );
$n = count ( $arr );
echo countCrossLine( $arr , $n );
?>
|
Javascript
<script>
function countCrossLine( arr, n)
{
let count_crossline = 0;
let i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
count_crossline++;
}
arr[j + 1] = key;
}
return count_crossline;
}
let arr = [ 4, 3, 1, 2 ];
let n = arr.length;
document.write(countCrossLine(arr, n) + "</br" );
</script>
|
Output:
5
Time complexity: O(n2)
Auxiliary space: O(1)
Efficient solution based on the merge sortand inversions count.
lets we have arr[] { 2, 4, 1, 3 }
\ \ / /
\ / \ /
/ \ / \
After sort arr[] { 1, 2, 3, 4 }
and here all inversion are (2, 1), (4, 1), (4, 3)
that mean line 1 : cross line 4, 2
line 2 : cross line 1
line 4 : cross line 3, 1
line 3 : cross line 3
so total unique cross_line are: 3
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
void merge( int arr[], int l, int m, int r, int * count_crossline)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
*count_crossline += (n1 - i);
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort( int arr[], int l, int r, int * count_crossline)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m, count_crossline);
mergeSort(arr, m + 1, r, count_crossline);
merge(arr, l, m, r, count_crossline);
}
}
int countCrossLine( int arr[], int n)
{
int count_crossline = 0;
mergeSort(arr, 0, n - 1, &count_crossline);
return count_crossline;
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countCrossLine(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int count_crossline;
static void merge( int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1 ;
int n2 = r - m;
int [] L = new int [n1];
int [] R = new int [n2];
for (i = 0 ; i < n1; i++)
{
L[i] = arr[l + i];
}
for (j = 0 ; j < n2; j++)
{
R[j] = arr[m + 1 + j];
}
i = 0 ;
j = 0 ;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
count_crossline += (n1 - i);
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
static void mergeSort( int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2 ;
mergeSort(arr, l, m);
mergeSort(arr, m + 1 , r);
merge(arr, l, m, r);
}
}
static int countCrossLine( int arr[], int n)
{
mergeSort(arr, 0 , n - 1 );
return count_crossline;
}
public static void main(String[] args)
{
int arr[] = { 12 , 11 , 13 , 5 , 6 , 7 };
int n = arr.length;
System.out.println(countCrossLine(arr, n));
}
}
|
Python3
count_crossline = 0
def merge(arr,l,m,r):
global count_crossline
n1 = m - l + 1
n2 = r - m
L = [ 0 ] * n1
R = [ 0 ] * n2
for i in range (n1):
L[i] = arr[l + i]
for j in range (n2):
R[j] = arr[m + 1 + j]
i = 0
j = 0
k = l
while (i<n1 and j<n2):
if (L[i]< = R[j]):
arr[k] = L[i]
i + = 1
else :
arr[k] = R[j]
count_crossline + = (n1 - i)
j + = 1
k + = 1
while (i<n1):
arr[k] = L[i]
i + = 1
k + = 1
while (j<n2):
arr[k] = R[j]
j + = 1
k + = 1
def mergeSort(arr,l,r):
if (l<r):
m = l + (r - l) / / 2
mergeSort(arr,l,m)
mergeSort(arr,m + 1 ,r)
merge(arr,l,m,r)
def countCrossLine(arr,n):
mergeSort(arr, 0 ,n - 1 )
return count_crossline
arr = [ 12 , 11 , 13 , 5 , 6 , 7 ]
n = len (arr)
print (countCrossLine(arr,n))
|
C#
using System;
class GFG
{
static int count_crossline;
static void merge( int []arr, int l,
int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int [] L = new int [n1];
int [] R = new int [n2];
for (i = 0; i < n1; i++)
{
L[i] = arr[l + i];
}
for (j = 0; j < n2; j++)
{
R[j] = arr[m + 1 + j];
}
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
count_crossline += (n1 - i);
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
static void mergeSort( int []arr, int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static int countCrossLine( int []arr, int n)
{
mergeSort(arr, 0, n - 1);
return count_crossline;
}
public static void Main(String[] args)
{
int []arr = {12, 11, 13, 5, 6, 7};
int n = arr.Length;
Console.WriteLine(countCrossLine(arr, n));
}
}
|
Javascript
<script>
let count_crossline = 0;
function merge(arr, l, m, r)
{
let i, j, k;
let n1 = m - l + 1;
let n2 = r - m;
let L = [];
let R = [];
for (i = 0; i < n1; i++)
{
L[i] = arr[l + i];
}
for (j = 0; j < n2; j++)
{
R[j] = arr[m + 1 + j];
}
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
count_crossline += (n1 - i);
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
function mergeSort(arr, l, r)
{
if (l < r)
{
let m = l + Math.floor((r - l) / 2);
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
function countCrossLine(arr, n)
{
mergeSort(arr, 0, n - 1);
document.write(count_crossline);
}
let arr = [12, 11, 13, 5, 6, 7];
let n = arr.length;
countCrossLine(arr, n);
</script>
|
Output:
10
Time complexity: O(nlogn)
Auxiliary Space: O(n)
Reference: https://www.careercup.com/question?id=5669565693427712
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...