How to multiply two long values in java - java

I am trying to multiply two largest numbers from an array of numbers. Its working fine for small numbers.
Correct input / output - this is working:
3 10 2 8
80
Correct input / output - this is failing:
2 100000 90000
9000000000
My output is however 10000000000 instead.
Can someone tell me what is wrong in my code?
public static Long sumPairwise(Long[] numbers){
int index=0;
int n = numbers.length;
for(int i=1;i<n;i++){
if(numbers[i]>numbers[index])
index=i;
}
numbers[n-1]= numbers[index];
index=0;
for(int j=1;j<n-1;j++){
if(numbers[j]>numbers[index])
index=j;
}
numbers[n-2]=numbers[index];
Long product = (numbers[n-2])*(numbers[n-1]);
return product ;
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Long numbers[] = new Long[n];
for (int i=0;i<n;i++)
numbers[i]= sc.nextLong();
System.out.println(sumPairwise(numbers));
}

There is a bug in your code: numbers[n-1] may well contain the second highest number. You are overwriting that number with the highest number in your code, before you try and put it at the first to last position.
One way to overcome this is to sort the array using Arrays.sort, this way you are sure that the last two numbers are the highest and second highest number.
public static long multiplyLargestTwoNumbers(long[] numbers) {
long[] sortedNumbers = numbers.clone();
Arrays.sort(sortedNumbers);
int size = numbers.length;
// multiply highest and second highest number
return sortedNumbers[size - 1] * sortedNumbers[size - 2];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long numbers[] = new long[n];
for (int i = 0; i < n; i++) {
numbers[i] = sc.nextLong();
}
System.out.println(multiplyLargestTwoNumbers(numbers));
}
Other changes:
using long instead of Long: try and use primitive types when the objective reference types are not needed (you need Long if you want to use e.g. a List because a List can only hold object references);
spaced out for loops, please use white space;
renamed method, as it does't add anything pairwise;
used curly braces for for loop in main method;
removed spurious parentheses in part that performs multiplication.
You might also introduce an if statement that first checks if the numbers array does indeed contain at least two elements. This is called a guard statement.
Finally remember that byte, short and long all contain signed numbers of a specific bit size. Basically you are performing calculations modulus 2^n where n is the bit size. If the value is too large it may overflow and return an incorrect result. For that you need BigInteger.

You are replacing the original number in that index with another number.
That is causing the issue.
Please just simply find the max 2 numbers from below logic and multiply.
Also, remember to close scanner.
Here the simple solution. This will work only for positive integers.
import java.util.Scanner;
public class Snippet {
public static long multiplyHighestTwoValues(Long[] numbers) {
long maxOne = 0;
long maxTwo = 0;
for (long n : numbers) {
if (maxOne < n) {
maxTwo = maxOne;
maxOne = n;
} else if (maxTwo < n) {
maxTwo = n;
}
}
long product = maxOne * maxTwo;
return product;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Long numbers[] = new Long[n];
for (int i = 0; i < n; i++)
numbers[i] = sc.nextLong();
System.out.println(sumPairwise(numbers));
sc.close();
}
}

Instead of Long try to use BigInteger to multiply larger values that fit into long, otherwise your result may overflow.
Use BigDecimal instead for multiplying floating point numbers.

Related

Not able to proceed with Series sum

I wanted to find the missing number in series so i thought a simple idea why not add all the numbers in array which are in series and hold it in one variable and then calculate the sum of series by formula Sn=n/2(a+l) but while calculating the series sum i am getting some error.
public class Missing {
public static void main(String[] args) {
int ar [] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int sum = 0; int total=0;
for(int num: ar)
{
sum = sum+num;
}
int n = ar.length;
int a = ar[0];
int l =ar[ar.length-1];
total = [n/2*(a+l)];
System.out.print("The missing number is "+(sum-total));
}}
total = [n/2*(a+l)]; ............................(1)
This is where i am getting error.
enter image description here
You can use the below logic which is much simpler to use and understand
for(int i=0;i<ar.length-1;i++)
{
if(ar[i]!=ar[i+1]-1)
{
System.out.print("The missing number is "+(ar[i]+1)+"\n");
break;
}
}
The first thing is in total = [n/2*(a+l)]; [] is not valid syntax in this context. The second thing I noticed, is that your formula to calculate the sum seems odd, maybe you meant Sn = (n * (a + l)) / 2?. After making those two changes the code should look as follows:
public class Missing {
public static void main(String[] args) {
int ar [] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int sum = 0;
for(int num: ar)
{
sum = sum+num;
}
int n = ar.length;
int a = ar[0];
int l =ar[ar.length - 1];
int total = (n * (a + l)) / 2;
System.out.print("The missing number is "+(sum - total));
// outputs 0 which is correct nothing is missing
// Now if you remove say 12 from the array
// by changing the array to int ar [] = {1,2,3,4,5,6,7,8,9,10,11,0,13};
// you should get back -12 which means 12 is missing
}
}
If you don't want to use my above logic. You have to make changes to your code:
Edit this:
int n = ar.length+1;
n has been assigned ar.length + 1 because that +1 is needed to compensate for the missing element in the array list
Also, the formula has not been correctly written into the code:
total = (n* (a + l))/2;
If you first divide n by 2 then, it will truncate the places after decimal point because n is an integer not a floating number. So, your logic would fail when n is not even.
And lastly, the missing number would be (sum-total) not the other way around because 'total' contains the missing number and 'sum' does not.
System.out.print("The missing number is "+(total-sum));

Issue increment sum=0 by long integers pass 9 digits in Java

The program begins by asking the user to give a value assigned to variable n, the user will then be prompted to submit long digits n amounts of times. The digits will then be stored into the initialized array (ar). The function I am trying to create aVeryBigSum is to loop through the numbers in the array, incrementing sum=0 by the numbers in the array to provide the sum (Java).
For some reason however, the program works unless I use two consecutive numbers with greater than 9 digits.
For example:
aVeryBigSum(2,[111111111,111111111]) has n of 2 and two 9 digit numbers in the array
Output:
22222222
aVeryBigSum(2,[1111111111,1111111111]) has n of 2 and two 10 digit numbers in the array
Output:
-2072745074
Any idea what the issue might be? I've provided the program below:
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Main {
static long aVeryBigSum(int n, long[] ar) {
int sum = 0;
for (int i = 0; i < n; i++){
System.out.println(sum);
System.out.println(ar[i]);
sum += ar[i];
System.out.println(" ");
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] ar = new long[n];
for(int ar_i = 0; ar_i < n; ar_i++){
ar[ar_i] = in.nextLong();
}
long result = aVeryBigSum(n, ar);
System.out.println(result);
}
}
Your problem is in the line
int sum = 0;
It should read
long sum = 0;
You are triggering integer overflow when the sum of integers exceeds 32 bits.
Yes, agree with Mad Physicist. You have to use long sum = 0. But to be the completely correct, you have possibility to have sum greater than Long.MAX_VALUE. You could use e.g. BigDecimal:
static BigDecimal aVeryBigSum(int n, long[] ar) {
BigDecimal sum = BigDecimal.ZERO;
for (int i = 0; i < n; i++) {
System.out.println(sum);
System.out.println(ar[i]);
sum = sum.add(new BigDecimal(ar[i]));
System.out.println(" ");
}
return sum;
}

Reduced execution time when using long instead of int

I have been solving competitive coding questions for a brief period of time now, and I have observed something. Sometimes, in any solution involving array manipulations or addition or multiplication, some of the test cases gave me TLE (time limit exceeded).
I used integer variables. When I change such variables to the long type, the execution time for that test case reduces, and sometimes, makes my solution excepted 100% instead of partial acceptance.
My question is, how, and why, is this the case? And in which scenarios would this (long over int) not hold true? The only disadvantage I can think of for not using long over int in such scenarios would be higher memory consumption.
UPDATE - Code Example: Here is a sample code I wrote to rotate a given array of numbers to the right by K places and display the output.
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int numTestCases = sc.nextInt();
for(int i = 0; i< numTestCases;i++)
{
int arraySize = sc.nextInt();
int offset = sc.nextInt();
int [] array = new int[arraySize];
if(offset>=arraySize)
offset = offset%arraySize;
int count = 0;
while(count<arraySize)
{
array[offset] = sc.nextInt();
offset = (offset+1)%arraySize;
count++;
}
for(int j = 0; j< arraySize; j++) {
sb.append(array[j]);
sb.append(" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
sc.close();
}
One of the test cases gave a TLE for this code. However, when changed the array type to long, that test case passed.
The test case contained an array of 89384 integers (all less than 100 - or atleast within int range) and had to be rotated right by 930886 places.

How to use Ctrl+Z to stop asking for input?

I am writing a program that takes integers from the user and stores them in an array, then calculates the arrays average.
The array can hold at maximum 100 integers. If the user wants to do less than 100, they hit CTRL+Z (or Command+D) to stop prompting for numbers.
Here is my main method:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int [] array = new int[100];
System.out.printf("Enter a stream of numbers: ");
readIntoArray(input, array);
for (int i = 0; i<=array.length;i++) {
array[i] = input.nextInt();
}
}
And here is the method that reads into the array.
public static int readIntoArray(Scanner input, int[] nums) {
int count = 0; //number of elements entered into the array
while (count <= nums.length && input.hasNextInt()) {
nums[count]=input.nextInt();
count++;
}
return count;
}
And here is the average method.
public static void printAboveAverage(int[] nums, int size) {
double average;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum =+ nums[i];
}
average = sum/size;
System.out.print(average);
What am I doing wrong?
I keep getting a NoSuchElementException immediately after hitting CTRL+Z.
Here:
while (count <= nums.length && input.hasNextInt()) {
This loop will probably stop when you hit ctrl-z and there is no more int. But next statement is:
array[i] = input.nextInt();
In other words: your read method seems to correctly check if enough numbers are in, or if the scanner stopped having input.
But your main method ignores that, and just asks for another number from the scanner.
So it could be as simple as: just drop that for loop within your main method that wants more numbers.
I suggest you have a try and catch if it gives you an Exception, maybe use it to get unlimited numbers (dont use the max 100 numbers) and when he finds the exception he counts the average?
Not the best solution, excpetion are not meant to always be used, Thanks to domsson for the reminder
something like:
try{
//Get the numbers
}
catch(Exception e){
//Calculate the average
}
Just thinking out loud, it may help you.

Numbers I can get by adding an array in java

I need to get a minimum number that I cant get by adding different numbers of an array. Basically if I have these numbers:1,1,1,5; I can get 1,2,3,5,6... but I cant get 4 so that is the number I am looking for. Now this is my code:
import java.util.Scanner;
public class Broj_6 {
public static void main(String[] args) {
Scanner unos = new Scanner(System.in);
int k;
int n = unos.nextInt();
int niz []= new int [n];
for(int i = 0;i<n;i++){
niz[i]=unos.nextInt();
}
BubbleSort(niz);
for(int i = 0;i<n;i++){
System.out.print(niz[i] + " ");
}
for(int br = 1;br<=10000;br++){
for(k = 1;k<n;k++){
if(niz[k]>br){
break;
}
}
int podniz [] = new int [k];
for(int i=0;i<podniz.length;i++){
niz[i] = podniz[i];
}
//This is where I will need my logic to go
}
}
static void BubbleSort (int [] niz){
int pom;
for(int i = 0;i<niz.length-1;i++){
for(int j = 0;j<niz.length-1-i;j++){
if(niz[j]>niz[j+1]){
pom = niz[j];
niz[j] = niz[j+1];
niz[j+1] = pom;
}
}
}
}
}
So the code goes by testing each number individually from 1 to 100000 and makes a subarray of all numbers given that are less than the number itself. Now here is the problem,I dont know how to mix and match the numbers in the subarray so it can get(or not get) the desired number. When every combination is tested and there is no desired number,I will break; the loop and print i. Just to clarify,I can only use addition,and each number can only go in once
You can achieve this as below:
Use two nested loops, like below to calculate the sum of different numbers:
List<Integer> additionList = new ArrayList<Integer>();
int []inputNumbers = .... // Logic to read inputs
for(int _firstIndex = 0; _firstIndex < totalInputs; _firstIndex++){
for(int _secondIndex = _firstIndex + 1; _secondIndex < totalInputs; _secondIndex++){
additionList.add(inputNumbers[_firstIndex]); // only because you have 1 in the sample output
additionList.add(inputNumbers[_firstIndex] + inputNumbers[_secondIndex ]);
}
}
Then sort additionList and look for any missing entry. The first missing entry will be your answer,
Sorting the whole array and then finding sum of all subarrays does solve the problem, but is costly: O(2n^2) ~ O(n^2).
More efficient way to solve this will be Kadane's Algorithm: http://en.wikipedia.org/wiki/Maximum_subarray_problem
What the algo does:
Start from first element and increase the array size (sub array) till you reach the sum you're desiring.
my_num = 1;
while(true){
if(sum_subarray) > my_num){
current position = new subarray;
}
and this subarray concept is calculated through Kadane's approach:
def sum_subarray(A):
sum_ending_here = sum_so_far = 0
for x in A:
sum_ending_here = max(0, max_ending_here + x)
sum_so_far = max(sum_so_far, sum_ending_here)
return sum_so_far
I couldn't solve the problem completely. 'my_num' mentioned here needs to be incremented from 1, and break when my_num > max_sum. I hope someone can add to it and make it compilable.
Note:
This will also take care if negative elements are present in array.

Categories