Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:33:06+00:00 2026-05-31T21:33:06+00:00

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class InversionCounter { public static void main(String[]

  • 0
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class InversionCounter {
    public static void main(String[] args) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File("src/IntegerArray.txt"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int [] nums = new int [100000];
        int i = 0;
        while(scanner.hasNextInt()){
           nums[i++] = scanner.nextInt();
        }
        System.out.println(countInversions(nums));

    }

public static int countInversions(int[] nums) {
    int count = 0;
    for (int i=0;i<nums.length-1;i++) {
        for (int j=i+1;j<nums.length;j++) {
            if (nums[i]>nums[j]) {
                count++;
            }
            else {continue;}
        }
    }
    return count;
}

}

The code above reads 100,000 integers from a file and counts the inversions for this array of integers. The output is probably a very large number like 1198233847 and should definitely be positive. However, it outputs a negative one like -1887062008. The program logic is likely to be correct as I have tried other algorithms for the same purpose and got the same negative number as output. I suspect that the result is too big a positive number and as a result Java converts it to a negative one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:33:07+00:00Added an answer on May 31, 2026 at 9:33 pm

    The max value of an int is 2,147,483,647 – what you are seeing is overflow. You should make count a long if you expect it to be so big.

    source: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.util.*; import java.io.*; public class LootGenerator{ public static void main (String[] args) throws
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
import java.lang.reflect.Array; public class PrimitiveArrayGeneric { static <T> T[] genericArrayNewInstance(Class<T> componentType) { return (T[])
This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;
package com.csl.bps.util; import java.awt.Event; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.HashMap;
My code at the moment: import java.util.*; import java.io.*; public class Percolation { //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.