Question

Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of...

Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of some of the testsets.

Question 2: Understanding Orders

Given an array A of size N, find the number of ordered pairs (i, j) such that i < j and A[i] < A[j].

Input:

• First line contains one integer, N, size of array.

• Second line contains N space separated integers denoting the elements of the array A.

Output:

Print the total number of ordered pairs (i, j) such that i < j and A[i] < A[j].

Constraints:

1. 1 ? N ? 10^6

2. ?105 ? A[i] ? 106

Input

5

4 1 3 2 5

output

6

The 6 cases are: 4 < 5, 1 < 3, 1 < 2, 1 < 5, 3 < 5, 2 < 5.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//TestCode.java

import java.util.Scanner;

public class TestCode {
    public static void main(String args[]){
        int n, count = 0;
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        int arr[] = new int[n];
        for(int i = 0;i<n;i++){
            arr[i] = scanner.nextInt();
        }
        for(int i = 0;i<n;i++){
            for(int j = i+1;j<n;j++){
                if(arr[i] < arr[j]){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

Add a comment
Know the answer?
Add Answer to:
Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given an array A[] of integers find sum of product of all pairs of array elements...

    Given an array A[] of integers find sum of product of all pairs of array elements Input: First line consists of T test cases. First line of every test case consists of N, denoting number of elements of array. Second line consists of elements of array. Output: Single line output, print the sum of products. Constraints: 1<=T<=100 1<=N<=100 Solve the problem using pointers and (if possible) using operators Example: Input: 2 3 1 3 4 4 2 3 4 5...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int n;       ...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • please use JAVA to solve it. Checkers is played on an N XN checker board, with...

    please use JAVA to solve it. Checkers is played on an N XN checker board, with one side playing the black pieces and the ofther side playing the white pieces. Glven a board size N and the positions of the black and white checkers, create a program to print out the resulting checker board. INPUT Row, Column Integer N denoting the size of the board Integer B denoting the number of black pieces to be placed on the board, followed...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. PROBLEM 1 INFORMATION IF YOU NEED IT AS WELL: Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem3. CODE PROVIDED FOR PROBLEM 2: import java.util.Scanner; public class Problem2 { public static void main( String [] args ) { //N...

  • Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The...

    Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The input array is sorted, and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst-case runtime must be O(logn)where n is the number of elements (no credits for slower runtimes) Example: a = [0,0,1,1,1]      return 2 a = [ 0,0,0,1]          return 3 a = [0,0,0]              return -1 int indexFirstOne...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a sequence of n positive integers a1,a2,…,an, where n is even. Swap adjacent elements in the given sequence and print the resulting sequence: a2,a1,a4,a3,a6,a5,… Input The first line contains a positive even integer n (2≤n≤1000) — the length of the sequence. The second line contains n space-separated integers a1,a2,…,an (1≤ai≤1000) — the elements of the sequence. Output Print n...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT