Find surpasser count of each element in array
Here given code implementation process.
// C program for
// Find surpasser count of each element in array
#include <stdio.h>
// Display array elements
void printData(int arr[], int n)
{
printf("\n Array : ");
for (int i = 0; i < n; ++i)
{
// print element
printf(" %d", arr[i]);
}
}
void surpasserCount(int arr[], int n)
{
printData(arr, n);
int count = 0;
printf("\n Result :");
// Outer loop (0..n)
for (int i = 0; i < n; ++i)
{
// Inner loop (i+1..n)
for (int j = i + 1; j < n; ++j)
{
if (arr[j] > arr[i])
{
// increase count
count++;
}
}
printf(" %d", count);
// Reset value
count = 0;
}
}
int main(int argc, char const *argv[])
{
int arr1[] = {
4 , 2 , 7 , 3 , 9 , 3 , 2 , 7 , 9
};
int arr2[] = {
7 , 2 , 5 , 5 , 1 , 6
};
// Get the number of element in arr1
int n = sizeof(arr1) / sizeof(arr1[0]);
surpasserCount(arr1, n);
// Get the number of element in arr2
n = sizeof(arr2) / sizeof(arr2[0]);
surpasserCount(arr2, n);
return 0;
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
/*
Java Program
Find surpasser count of each element in array
*/
public class Counting
{
// Display array elements
public void printData(int[] arr, int n)
{
System.out.print("\n Array : ");
for (int i = 0; i < n; ++i)
{
// print element
System.out.print(" " + arr[i]);
}
}
public void surpasserCount(int[] arr, int n)
{
this.printData(arr, n);
int count = 0;
System.out.print("\n Result :");
// Outer loop (0..n)
for (int i = 0; i < n; ++i)
{
// Inner loop (i+1..n)
for (int j = i + 1; j < n; ++j)
{
if (arr[j] > arr[i])
{
// increase count
count++;
}
}
System.out.print(" " + count);
// Reset value
count = 0;
}
}
public static void main(String[] args)
{
Counting task = new Counting();
int[] arr1 = {
4 , 2 , 7 , 3 , 9 , 3 , 2 , 7 , 9
};
int[] arr2 = {
7 , 2 , 5 , 5 , 1 , 6
};
// Get the number of element in arr1
int n = arr1.length;
task.surpasserCount(arr1, n);
// Get the number of element in arr2
n = arr2.length;
task.surpasserCount(arr2, n);
}
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find surpasser count of each element in array
*/
class Counting
{
public:
// Display array elements
void printData(int arr[], int n)
{
cout << "\n Array : ";
for (int i = 0; i < n; ++i)
{
// print element
cout << " " << arr[i];
}
}
void surpasserCount(int arr[], int n)
{
this->printData(arr, n);
int count = 0;
cout << "\n Result :";
// Outer loop (0..n)
for (int i = 0; i < n; ++i)
{
// Inner loop (i+1..n)
for (int j = i + 1; j < n; ++j)
{
if (arr[j] > arr[i])
{
// increase count
count++;
}
}
cout << " " << count;
// Reset value
count = 0;
}
}
};
int main()
{
Counting *task = new Counting();
int arr1[] = {
4 , 2 , 7 , 3 , 9 , 3 , 2 , 7 , 9
};
int arr2[] = {
7 , 2 , 5 , 5 , 1 , 6
};
// Get the number of element in arr1
int n = sizeof(arr1) / sizeof(arr1[0]);
task->surpasserCount(arr1, n);
// Get the number of element in arr2
n = sizeof(arr2) / sizeof(arr2[0]);
task->surpasserCount(arr2, n);
return 0;
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
package main
import "fmt"
/*
Go Program
Find surpasser count of each element in array
*/
// Display array elements
func printData(arr[] int, n int) {
fmt.Print("\n Array : ")
for i := 0 ; i < n ; i++ {
// print element
fmt.Print(" ", arr[i])
}
}
func surpasserCount(arr[] int, n int) {
printData(arr, n)
var count int = 0
fmt.Print("\n Result :")
// Outer loop (0..n)
for i := 0 ; i < n ; i++ {
// Inner loop (i+1..n)
for j := i + 1 ; j < n ; j++ {
if arr[j] > arr[i] {
// increase count
count++
}
}
fmt.Print(" ", count)
// Reset value
count = 0
}
}
func main() {
var arr1 = [] int {
4,
2,
7,
3,
9,
3,
2,
7,
9,
}
var arr2 = [] int {
7,
2,
5,
5,
1,
6,
}
// Get the number of element in arr1
var n int = len(arr1)
surpasserCount(arr1, n)
// Get the number of element in arr2
n = len(arr2)
surpasserCount(arr2, n)
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
// Include namespace system
using System;
/*
Csharp Program
Find surpasser count of each element in array
*/
public class Counting
{
// Display array elements
public void printData(int[] arr, int n)
{
Console.Write("\n Array : ");
for (int i = 0; i < n; ++i)
{
// print element
Console.Write(" " + arr[i]);
}
}
public void surpasserCount(int[] arr, int n)
{
this.printData(arr, n);
int count = 0;
Console.Write("\n Result :");
// Outer loop (0..n)
for (int i = 0; i < n; ++i)
{
// Inner loop (i+1..n)
for (int j = i + 1; j < n; ++j)
{
if (arr[j] > arr[i])
{
// increase count
count++;
}
}
Console.Write(" " + count);
// Reset value
count = 0;
}
}
public static void Main(String[] args)
{
Counting task = new Counting();
int[] arr1 = {
4 , 2 , 7 , 3 , 9 , 3 , 2 , 7 , 9
};
int[] arr2 = {
7 , 2 , 5 , 5 , 1 , 6
};
// Get the number of element in arr1
int n = arr1.Length;
task.surpasserCount(arr1, n);
// Get the number of element in arr2
n = arr2.Length;
task.surpasserCount(arr2, n);
}
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
<?php
/*
Php Program
Find surpasser count of each element in array
*/
class Counting
{
// Display array elements
public function printData($arr, $n)
{
echo("\n Array : ");
for ($i = 0; $i < $n; ++$i)
{
// print element
echo(" ".$arr[$i]);
}
}
public function surpasserCount($arr, $n)
{
$this->printData($arr, $n);
$count = 0;
echo("\n Result :");
// Outer loop (0..n)
for ($i = 0; $i < $n; ++$i)
{
// Inner loop (i+1..n)
for ($j = $i + 1; $j < $n; ++$j)
{
if ($arr[$j] > $arr[$i])
{
// increase count
$count++;
}
}
echo(" ".$count);
// Reset value
$count = 0;
}
}
}
function main()
{
$task = new Counting();
$arr1 = array(4, 2, 7, 3, 9, 3, 2, 7, 9);
$arr2 = array(7, 2, 5, 5, 1, 6);
// Get the number of element in arr1
$n = count($arr1);
$task->surpasserCount($arr1, $n);
// Get the number of element in arr2
$n = count($arr2);
$task->surpasserCount($arr2, $n);
}
main();
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
/*
Node JS Program
Find surpasser count of each element in array
*/
class Counting
{
// Display array elements
printData(arr, n)
{
process.stdout.write("\n Array : ");
for (var i = 0; i < n; ++i)
{
// print element
process.stdout.write(" " + arr[i]);
}
}
surpasserCount(arr, n)
{
this.printData(arr, n);
var count = 0;
process.stdout.write("\n Result :");
// Outer loop (0..n)
for (var i = 0; i < n; ++i)
{
// Inner loop (i+1..n)
for (var j = i + 1; j < n; ++j)
{
if (arr[j] > arr[i])
{
// increase count
count++;
}
}
process.stdout.write(" " + count);
// Reset value
count = 0;
}
}
}
function main()
{
var task = new Counting();
var arr1 = [4, 2, 7, 3, 9, 3, 2, 7, 9];
var arr2 = [7, 2, 5, 5, 1, 6];
// Get the number of element in arr1
var n = arr1.length;
task.surpasserCount(arr1, n);
// Get the number of element in arr2
n = arr2.length;
task.surpasserCount(arr2, n);
}
main();
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
# Python 3 Program
# Find surpasser count of each element in array
class Counting :
# Display list elements
def printData(self, arr, n) :
print("\n Array : ", end = "")
i = 0
while (i < n) :
# print element
print(" ", arr[i], end = "")
i += 1
def surpasserCount(self, arr, n) :
self.printData(arr, n)
count = 0
print("\n Result :", end = "")
i = 0
# Outer loop (0..n)
while (i < n) :
j = i + 1
# Inner loop (i+1..n)
while (j < n) :
if (arr[j] > arr[i]) :
# increase count
count += 1
j += 1
print(" ", count, end = "")
# Reset value
count = 0
i += 1
def main() :
task = Counting()
arr1 = [4, 2, 7, 3, 9, 3, 2, 7, 9]
arr2 = [7, 2, 5, 5, 1, 6]
# Get the number of element in arr1
n = len(arr1)
task.surpasserCount(arr1, n)
# Get the number of element in arr2
n = len(arr2)
task.surpasserCount(arr2, n)
if __name__ == "__main__": main()
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
# Ruby Program
# Find surpasser count of each element in array
class Counting
# Display array elements
def printData(arr, n)
print("\n Array : ")
i = 0
while (i < n)
# print element
print(" ", arr[i])
i += 1
end
end
def surpasserCount(arr, n)
self.printData(arr, n)
count = 0
print("\n Result :")
i = 0
# Outer loop (0..n)
while (i < n)
j = i + 1
# Inner loop (i+1..n)
while (j < n)
if (arr[j] > arr[i])
# increase count
count += 1
end
j += 1
end
print(" ", count)
# Reset value
count = 0
i += 1
end
end
end
def main()
task = Counting.new()
arr1 = [4, 2, 7, 3, 9, 3, 2, 7, 9]
arr2 = [7, 2, 5, 5, 1, 6]
# Get the number of element in arr1
n = arr1.length
task.surpasserCount(arr1, n)
# Get the number of element in arr2
n = arr2.length
task.surpasserCount(arr2, n)
end
main()
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
/*
Scala Program
Find surpasser count of each element in array
*/
class Counting()
{
// Display array elements
def printData(arr: Array[Int], n: Int): Unit = {
print("\n Array : ");
var i: Int = 0;
while (i < n)
{
// print element
print(" " + arr(i));
i += 1;
}
}
def surpasserCount(arr: Array[Int], n: Int): Unit = {
this.printData(arr, n);
var count: Int = 0;
print("\n Result :");
var i: Int = 0;
// Outer loop (0..n)
while (i < n)
{
var j: Int = i + 1;
// Inner loop (i+1..n)
while (j < n)
{
if (arr(j) > arr(i))
{
// increase count
count += 1;
}
j += 1;
}
print(" " + count);
// Reset value
count = 0;
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
var arr1: Array[Int] = Array(4, 2, 7, 3, 9, 3, 2, 7, 9);
var arr2: Array[Int] = Array(7, 2, 5, 5, 1, 6);
// Get the number of element in arr1
var n: Int = arr1.length;
task.surpasserCount(arr1, n);
// Get the number of element in arr2
n = arr2.length;
task.surpasserCount(arr2, n);
}
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
import Foundation;
/*
Swift 4 Program
Find surpasser count of each element in array
*/
class Counting
{
// Display array elements
func printData(_ arr: [Int], _ n: Int)
{
print("\n Array : ", terminator: "");
var i: Int = 0;
while (i < n)
{
// print element
print(" ", arr[i], terminator: "");
i += 1;
}
}
func surpasserCount(_ arr: [Int], _ n: Int)
{
self.printData(arr, n);
var count: Int = 0;
print("\n Result :", terminator: "");
var i: Int = 0;
// Outer loop (0..n)
while (i < n)
{
var j: Int = i + 1;
// Inner loop (i+1..n)
while (j < n)
{
if (arr[j] > arr[i])
{
// increase count
count += 1;
}
j += 1;
}
print(" ", count, terminator: "");
// Reset value
count = 0;
i += 1;
}
}
}
func main()
{
let task: Counting = Counting();
let arr1: [Int] = [4, 2, 7, 3, 9, 3, 2, 7, 9];
let arr2: [Int] = [7, 2, 5, 5, 1, 6];
// Get the number of element in arr1
var n: Int = arr1.count;
task.surpasserCount(arr1, n);
// Get the number of element in arr2
n = arr2.count;
task.surpasserCount(arr2, n);
}
main();
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
/*
Kotlin Program
Find surpasser count of each element in array
*/
class Counting
{
// Display array elements
fun printData(arr: Array < Int > , n: Int): Unit
{
print("\n Array : ");
var i: Int = 0;
while (i < n)
{
// print element
print(" " + arr[i]);
i += 1;
}
}
fun surpasserCount(arr: Array < Int > , n: Int): Unit
{
this.printData(arr, n);
var count: Int = 0;
print("\n Result :");
var i: Int = 0;
// Outer loop (0..n)
while (i < n)
{
var j: Int = i + 1;
// Inner loop (i+1..n)
while (j < n)
{
if (arr[j] > arr[i])
{
// increase count
count += 1;
}
j += 1;
}
print(" " + count);
// Reset value
count = 0;
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Counting = Counting();
val arr1: Array < Int > = arrayOf(4, 2, 7, 3, 9, 3, 2, 7, 9);
val arr2: Array < Int > = arrayOf(7, 2, 5, 5, 1, 6);
// Get the number of element in arr1
var n: Int = arr1.count();
task.surpasserCount(arr1, n);
// Get the number of element in arr2
n = arr2.count();
task.surpasserCount(arr2, n);
}
input
Array : 4 2 7 3 9 3 2 7 9
Result : 4 6 2 3 0 2 2 1 0
Array : 7 2 5 5 1 6
Result : 0 3 1 1 1 0
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