Display Inversions pairs in array

Basically there are two conditions of inversion pairs in collection of integer array. Such as (i < j) where i and j is index position. And element array[i] is greater than array[j] (array[i] > array[j]).

/*
  Java Program
  Display Inversions pairs in array
*/
public class InversionPairs
{
    // Print the all inversion pairs in given array
    public void printInversionPair(int[] arr, int size)
    {
        if (size <= 1)
        {
            return;
        }
        // Outer loop
        for (int i = 0; i < size; ++i)
        {
            // Inner loop
            for (int j = i + 1; j < size; ++j)
            {
                if (arr[i] > arr[j])
                {   
                    // When element of i is greater than element of j
                    // Print inversion pair
                    System.out.println("(" + arr[i] + "," + arr[j] + ")");
                }
            }
        }
    }
    public static void main(String[] args)
    {
        InversionPairs task = new InversionPairs();
        // Define array elements
        int[] arr = {
            1 , 7 , 6 , 4 , 5 , 9 , 8
        };
        // Count size of array
        int size = arr.length;
        
        // Display result
        task.printInversionPair(arr, size);
    }
}
Inversion pairs example
(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
// C Program 
// Display Inversions pairs in array
#include<stdio.h>

// Print the all inversion pairs in given array
void printInversionPair(int arr[], int size)
{
    if (size <= 1)
    {
        return;
    }
    // Outer loop
    for (int i = 0; i < size; ++i)
    {
        // Inner loop
        for (int j = i + 1; j < size; ++j)
        {
            if (arr[i] > arr[j])
            {
                // When element of i is greater than element of j
                // Print inversion pair
                printf("(%d,%d)\n",arr[i],arr[j]);
            }
        }
    }
}
int main()
{
    //Define array elements
    int arr[] = {
        1 , 7 , 6 , 4 , 5 , 9 , 8
    };
    //Count size of array
    int size = sizeof(arr) / sizeof(arr[0]);
    printInversionPair(arr, size);
    return 0;
}

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Display Inversions pairs in array
*/
class InversionPairs
{
    public:
        // Print the all inversion pairs in given array
        void printInversionPair(int arr[], int size)
        {
            if (size <= 1)
            {
                return;
            }
            // Outer loop
            for (int i = 0; i < size; ++i)
            {
                // Inner loop
                for (int j = i + 1; j < size; ++j)
                {
                    if (arr[i] > arr[j])
                    {
                        // When element of i is greater than element of j
                        // Print inversion pair
                        cout << "(" << arr[i] << "," << arr[j] << ")" << endl;
                    }
                }
            }
        }
};
int main()
{
    InversionPairs *task = new InversionPairs();
    // Define array elements
    int arr[] = {
        1 , 7 , 6 , 4 , 5 , 9 , 8
    };
    // Count size of array
    int size = sizeof(arr) / sizeof(arr[0]);
    // Display result
    task->printInversionPair(arr, size);
    return 0;
}

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
package main
import "fmt"
/*
  Go Program
  Display Inversions pairs in array
*/
type InversionPairs struct {}
func getInversionPairs() * InversionPairs {
    var me *InversionPairs = &InversionPairs {}
    return me
}
// Print the all inversion pairs in given array
func(this InversionPairs) printInversionPair(arr[] int, size int) {
    if size <= 1 {
        return
    }
    // Outer loop
    for i := 0 ; i < size ; i++ {
        // Inner loop
        for j := i + 1 ; j < size ; j++ {
            if arr[i] > arr[j] {
                // When element of i is greater than element of j
                // Print inversion pair
                fmt.Println("(", arr[i], ",", arr[j], ")")
            }
        }
    }
}
func main() {
    var task * InversionPairs = getInversionPairs()
    // Define array elements
    var arr = [] int {1 , 7 , 6 , 4 , 5 , 9 , 8}
    // Count size of array
    var size int = len(arr)
    // Display result
    task.printInversionPair(arr, size)
}

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
// Include namespace system
using System;
/*
  Csharp Program
  Display Inversions pairs in array
*/
public class InversionPairs
{
    // Print the all inversion pairs in given array
    public void printInversionPair(int[] arr, int size)
    {
        if (size <= 1)
        {
            return;
        }
        // Outer loop
        for (int i = 0; i < size; ++i)
        {
            // Inner loop
            for (int j = i + 1; j < size; ++j)
            {
                if (arr[i] > arr[j])
                {
                    // When element of i is greater than element of j
                    // Print inversion pair
                    Console.WriteLine("(" + arr[i] + "," + arr[j] + ")");
                }
            }
        }
    }
    public static void Main(String[] args)
    {
        InversionPairs task = new InversionPairs();
        // Define array elements
        int[] arr = {
            1 , 7 , 6 , 4 , 5 , 9 , 8
        };
        // Count size of array
        int size = arr.Length;
        // Display result
        task.printInversionPair(arr, size);
    }
}

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
<?php
/*
  Php Program
  Display Inversions pairs in array
*/
class InversionPairs
{
    // Print the all inversion pairs in given array
    public  function printInversionPair($arr, $size)
    {
        if ($size <= 1)
        {
            return;
        }
        // Outer loop
        for ($i = 0; $i < $size; ++$i)
        {
            // Inner loop
            for ($j = $i + 1; $j < $size; ++$j)
            {
                if ($arr[$i] > $arr[$j])
                {
                    // When element of i is greater than element of j
                    // Print inversion pair
                    echo("(".$arr[$i].
                        ",".$arr[$j].
                        ")\n");
                }
            }
        }
    }
}

function main()
{
    $task = new InversionPairs();
    // Define array elements
    $arr = array(1, 7, 6, 4, 5, 9, 8);
    // Count size of array
    $size = count($arr);
    // Display result
    $task->printInversionPair($arr, $size);
}
main();

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
/*
  Node JS Program
  Display Inversions pairs in array
*/
class InversionPairs
{
    // Print the all inversion pairs in given array
    printInversionPair(arr, size)
    {
        if (size <= 1)
        {
            return;
        }
        // Outer loop
        for (var i = 0; i < size; ++i)
        {
            // Inner loop
            for (var j = i + 1; j < size; ++j)
            {
                if (arr[i] > arr[j])
                {
                    // When element of i is greater than element of j
                    // Print inversion pair
                    console.log("(" + arr[i] + "," + arr[j] + ")");
                }
            }
        }
    }
}

function main()
{
    var task = new InversionPairs();
    // Define array elements
    var arr = [1, 7, 6, 4, 5, 9, 8];
    // Count size of array
    var size = arr.length;
    // Display result
    task.printInversionPair(arr, size);
}
main();

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
#  Python 3 Program
#  Display Inversions pairs in array
class InversionPairs :
    #  Print the all inversion pairs in given list
    def printInversionPair(self, arr, size) :
        if (size <= 1) :
            return
        
        i = 0
        #  Outer loop
        while (i < size) :
            j = i + 1
            #  Inner loop
            while (j < size) :
                if (arr[i] > arr[j]) :
                    #  When element of i is greater than element of j
                    #  Print inversion pair
                    print("(", arr[i] ,",", arr[j] ,")")
                
                j += 1
            
            i += 1
        
    

def main() :
    task = InversionPairs()
    #  Define list elements
    arr = [1, 7, 6, 4, 5, 9, 8]
    #  Count size of list
    size = len(arr)
    #  Display result
    task.printInversionPair(arr, size)

if __name__ == "__main__": main()

Output

( 7 , 6 )
( 7 , 4 )
( 7 , 5 )
( 6 , 4 )
( 6 , 5 )
( 9 , 8 )
#  Ruby Program
#  Display Inversions pairs in array
class InversionPairs 
    #  Print the all inversion pairs in given array
    def printInversionPair(arr, size) 
        if (size <= 1) 
            return
        end

        i = 0
        #  Outer loop
        while (i < size) 
            j = i + 1
            #  Inner loop
            while (j < size) 
                if (arr[i] > arr[j]) 
                    #  When element of i is greater than element of j
                    #  Print inversion pair
                    print("(", arr[i] ,",", arr[j] ,")", "\n")
                end

                j += 1
            end

            i += 1
        end

    end

end

def main() 
    task = InversionPairs.new()
    #  Define array elements
    arr = [1, 7, 6, 4, 5, 9, 8]
    #  Count size of array
    size = arr.length
    #  Display result
    task.printInversionPair(arr, size)
end

main()

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
/*
  Scala Program
  Display Inversions pairs in array
*/
class InversionPairs()
{
    // Print the all inversion pairs in given array
    def printInversionPair(arr: Array[Int], size: Int): Unit = {
        if (size <= 1)
        {
            return;
        }
        var i: Int = 0;
        // Outer loop
        while (i < size)
        {
            var j: Int = i + 1;
            // Inner loop
            while (j < size)
            {
                if (arr(i) > arr(j))
                {
                    // When element of i is greater than element of j
                    // Print inversion pair
                    println("(" + arr(i) + "," + arr(j) + ")");
                }
                j += 1;
            }
            i += 1;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: InversionPairs = new InversionPairs();
        // Define array elements
        var arr: Array[Int] = Array(1, 7, 6, 4, 5, 9, 8);
        // Count size of array
        var size: Int = arr.length;
        // Display result
        task.printInversionPair(arr, size);
    }
}

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)
import Foundation;
/*
  Swift 4 Program
  Display Inversions pairs in array
*/
class InversionPairs
{
    // Print the all inversion pairs in given array
    func printInversionPair(_ arr: [Int], _ size: Int)
    {
        if (size <= 1)
        {
            return;
        }
        var i: Int = 0;
        // Outer loop
        while (i < size)
        {
            var j: Int = i + 1;
            // Inner loop
            while (j < size)
            {
                if (arr[i] > arr[j])
                {
                    // When element of i is greater than element of j
                    // Print inversion pair
                    print("(", arr[i] ,",", arr[j] ,")");
                }
                j += 1;
            }
            i += 1;
        }
    }
}
func main()
{
    let task: InversionPairs = InversionPairs();
    // Array elements
    let arr: [Int] = [1, 7, 6, 4, 5, 9, 8];
    // Count size of array
    let size: Int = arr.count;
    // Display result
    task.printInversionPair(arr, size);
}
main();

Output

( 7 , 6 )
( 7 , 4 )
( 7 , 5 )
( 6 , 4 )
( 6 , 5 )
( 9 , 8 )
/*
  Kotlin Program
  Display Inversions pairs in array
*/
class InversionPairs
{
    // Print the all inversion pairs in given array
    fun printInversionPair(arr: Array < Int > , size: Int): Unit
    {
        if (size <= 1)
        {
            return;
        }
        var i: Int = 0;
        // Outer loop
        while (i < size)
        {
            var j: Int = i + 1;
            // Inner loop
            while (j < size)
            {
                if (arr[i] > arr[j])
                {
                    // When element of i is greater than element of j
                    // Print inversion pair
                    println("(" + arr[i] + "," + arr[j] + ")");
                }
                j += 1;
            }
            i += 1;
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: InversionPairs = InversionPairs();
    // Define array elements
    val arr: Array < Int > = arrayOf(1, 7, 6, 4, 5, 9, 8);
    // Count size of array
    val size: Int = arr.count();
    // Display result
    task.printInversionPair(arr, size);
}

Output

(7,6)
(7,4)
(7,5)
(6,4)
(6,5)
(9,8)


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved