The maximum product of adjacent elements in java - java
i am trying to print The maximum product of adjacent elements in an array but i am getting an error in my program:
if input is [2, 5, 8, 9, 6]
then desired output is : 72
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Product=0;
int len = sc.nextInt();
int[] arr = new int[len];
for(int i=0; i < len; i++){
arr[i] = sc.nextInt();
}
for(int i = 0; i<len-1; i++){
Product = arr[i]*arr[i+1];
int Nproduct = arr[i+1]*arr[i+2];
{
if(Product<Nproduct){
Product = Nproduct;
}
}
}
System.out.println(Product);
}
}
please help me where i am wrong. i am new here so please ignore my mistakes or edit if possible
There is some logical error as well as Run time error in your code.
Take a Look at modified version of your code
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Product=0;
int len = sc.nextInt();
int[] arr = new int[len];
for(int i=0; i < len; i++){
arr[i] = sc.nextInt();
}
for(int i = 0; i<len-1; i++){
if (arr[i]*arr[i+1]>Product){
Product=arr[i]*arr[i+1]; // update Product only if we get greater product
}
}
System.out.println(Product);
}
}
your code needs some changes
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Product=0;
int len = sc.nextInt();
int[] arr = new int[len];
for(int i=0; i < len; i++){
arr[i] = sc.nextInt();
}
for(int i = 0; i<len-1; i++){
if (arr[i]*arr[i+1]>Product){
Product=arr[i]*arr[i+1];
}
}
System.out.println(Product);
}
}
for reference click here : GFG Problem
Change second loop for (int i = 0; i<len-1; i++) to for (int i = 0; i<len-2; i++) to prevent running out of upper array's bound
Related
How to print only distinct integer values once even though they are repeated multiple times
I am trying to print only distinct integer values once even though they are repeated multiple times in an array with the introduced order. Firstly, I need to take array size from user but I cannot determine how to initialize that variable.Can I use n -inclueded in the code- as an array size variable? When I compiled I doesn't print anything. Where are my mistakes, and how to deal with them? Any idea? public static void uniqueNumbers(int arr[], int n) { for (int i=0; i<n; i++) { for (int j =0; j<i;j++) { if (arr [i]==arr[j]) break; if (i==j) System.out.print(arr[i] + " "); } } } public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n =sc.nextInt(); int arr [] = new int [n]; uniqueNumbers(arr,n); } }
You need to fill the array,that is, you need to take input for the array elements. public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n =sc.nextInt(); int arr [] = new int [n]; //Add the following lines. for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } uniqueNumbers(arr,n); } Print the array and try your logic again.
you need to first request for the size of the array as shown below: public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter array size"); int n =scan.nextInt(); // declaring and creating array objects int[] arr = new int[n]; // displaying default values System.out.println("Default values of array:"); for (int i=0; i < arr.length; i++) { System.out.print(arr[i]+"\t"); } // initializing array System.out.println("\n\n***Initializing Array***"); System.out.println("Enter "+ arr.length + " integer values:"); for(int i=0; i < arr.length; i++) { // read input arr[i] = scan.nextInt(); } uniqueNumbers(arr, n); }
public static void uniqueNumbers(int[] arr, int n) { int slow = 1; for (int fast = 1; fast < n; fast++) { if (arr[fast] != arr[slow - 1]) { arr[slow++] = arr[fast]; } } arr = Arrays.copyOf(arr, slow); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }
Storing output of a nested loop as new array - Java
I need to find the largest prime number of a given array when adding two numbers in an array,so I decided to add all possible sums first and displayed it. Now I want to take those output elements to a new array.Please help me solve this problem. import java.util.Scanner; public class main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int noOfElem = scanner.nextInt(); int[] array = new int[noOfElem]; int[][] newArray = new int[5][4]; int i=0; while(scanner.hasNextInt()){ array[i] = scanner.nextInt(); i++; if(i == noOfElem){ break; } } for (int a = 0; a < array.length; a++) { for (int b = a+1; b < array.length; b++) { int m = array[a] + array[b]; newArray[a][b] = } } } }
Not quite sure what the problem is here, just do newArray[a][b] = m; This stores all sums of all 'a's and 'b's such that newArray[a][b] is the sum of array[a] + array[b]
Inputting adjacency matrix from a file
I am attempting to input the following file into my program in the form of an adjacency matrix. 16 -1,1075,716,792,1425,1369,740,802,531,383,811,2211,661,870,999,772 1075,-1,1015,1770,2403,1662,870,1858,941,1426,1437,3026,1486,211,1463,314 716,1015,-1,928,1483,646,390,1085,185,749,530,2034,1377,821,471,772 792,1770,928,-1,633,1089,1111,246,908,409,495,1447,1317,1565,672,1470 1425,2403,1483,633,-1,9999,1630,752,1432,9999,931,814,1938,2198,1016,2103 1369,1662,646,1089,9999,-1,820,1335,832,9999,605,1839,2030,1468,421,1419 740,870,390,1111,1630,820,-1,1224,360,965,690,2197,1480,750,630,705 802,1858,1085,246,752,1335,1224,-1,1021,442,737,1566,1190,1653,918,1558 531,941,185,908,1432,832,360,1021,-1,685,496,2088,1192,736,616,656 383,1426,749,409,9999,9999,965,442,685,-1,738,1858,1938,1221,926,1126 811,1437,530,495,931,605,690,737,496,738,-1,1631,1472,1232,188,1152 2211,3026,2034,1447,814,1839,2197,1566,2088,1858,1631,-1,2752,2824,1563,2744 661,1486,1377,1317,1938,2030,1480,1190,1192,1938,1472,2752,-1,1281,1660,1183 870,211,821,1565,2198,1468,750,1653,736,1221,1232,2824,1281,-1,1269,109 999,1463,471,672,1016,421,630,918,616,926,188,1563,1660,1269,-1,1220 772,314,772,1470,2103,1419,705,1558,656,1126,1152,2744,1183,109,1220,-1 However, I think I have something wrong with my logic or I'm not using the Scanner correctly. This is my code: public class Tour { public static final int N = 16; public static final int INF = Integer.MAX_VALUE; public static void printGrid(int[][] adjMat) { for(int i = 0; i < 16; i++) { for(int j = 0; j < 16; j++) { if(adjMat[i][j] == INF) System.out.printf("%5s", 0); else System.out.printf("%5d", adjMat[i][j]); } System.out.println(); } } public static void main(String[] args) throws IOException { File file = new File("american_tour.dat"); Scanner scanner = new Scanner(file); int[][] adjMat = new int[N][N]; for(int i = 0, n = scanner.nextInt(); i < n; i++) for(int j = 0; j < n; j++) adjMat[i][j] = n; scanner.close(); printGrid(adjMat); } } Could someone show me how to properly input the data from the file into an adjacency matrix?
Improving Mouad's answer, using the scanner's built in support for custom delimiters: Scanner scanner = new Scanner(file); scanner.useDelimiter("[\\s,]+"); int N = scanner.nextInt(); int[][] adjMat = new int[N][N]; for(int i=0; i < N; i++) { for (int j=0; j < N; j++) { adjMat[i][j] = scanner.nextInt(); } } scanner.close();
As your data does not respect a specific pattern for delimiters, try this instead : public static void main(String[] args) throws FileNotFoundException { File file = new File("E:\\american_tour.dat"); Scanner scanner = new Scanner(file); //N in the example equals 16 int N = scanner.nextInt(); //skip the first line scanner.nextLine(); int[][] adjMat = new int[N][N]; for(int i = 0; i < N; i++){ String[] lines = scanner.nextLine().split(","); for (int j=0; j<lines.length; j++) { adjMat[i][j] = Integer.parseInt(lines[j]); } } scanner.close(); }
Although not exceed index, I get error by the index
My question is ( - Write the following method that merges two sorted lists into a new sorted list. Write a test program that prompts the user to enter two sorted lists and displays the merged list. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. - ) When I run this code, Eclipse give error. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at single_dimension_all_arrays.Merge_two_sorted_lists.getNumber(Merge_two_sorted_lists.java:60) at single_dimension_all_arrays.Merge_two_sorted_lists.main(Merge_two_sorted_lists.java:77) I can't understand why give the error. I can't exceed index. private static void sort(int [] list3) { int temp=0; for (int i = 0; i < list3.length; i++) { for (int j = 0; j < list3.length; j++) { if(list3[i]<list3[j]) { temp=list3[i]; list3[i]=list3[j]; list3[j]=temp; } } } for (int i = 0; i < list3.length; i++) { System.out.println(list3[i]); } } private static void getNumber(int [] list1,int [] list2) { Scanner scan = new Scanner(System.in); int [] list3 = new int[list1.length+list2.length]; for (int i = 1; i < list1.length; i++) { System.out.println("Please, enter the number"); list1[i] = scan.nextInt(); } for (int i = 1; i < list2.length; i++) { System.out.println("Please,enter the number"); list2[i]= scan.nextInt(); } for (int i = 0; i <= list3.length; i++) { if(i<list1.length) { list3[i]=list1[i]; } else if(i>list1.length) { list3[i] = list2[i]; } } sort(list3); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please,enter the length of list1"); int l1 = scan.nextInt(); System.out.println("Please, enter the length of list2"); int l2 = scan.nextInt(); int [] list1 = new int[l1]; int [] list2 = new int[l2]; list1[0]=l1; list2[0]=l2; getNumber(list1,list2); } } Thanks..:)
Although not exceed index... Yes you do: for (int i = 0; i <= list3.length; i++) // ----------------^ The valid range of indexes is 0 to length - 1, so that should be < as it is in several other parts of your code. Side note: You're also skipping the first element in arrays in a few places: for (int i = 1; i < list1.length; i++) // ----------^
ways to speed up the Full Counting Sort
I encountered a question on hackerrank. https://www.hackerrank.com/challenges/countingsort4 My first attempt passed all the test cases except the last one, due to timeout. After failed to come up with a more efficient algorithm, I improved the code by using StringBuilder instead of concatenating Strings directly. This brought the running time from more than 5 sec to 3.5 sec. My question is that is there any other way that I can improve the running time? Thanks. The following is my code. public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); scanner.nextLine(); int[] oriNum = new int[N]; String[] oriStr = new String[N]; int[] count = new int[100]; int[] indices = new int[100]; int[] output = new int[N]; // save the originals and the count array for (int i = 0; i < N; i++) { oriNum[i] = scanner.nextInt(); oriStr[i] = scanner.nextLine().trim(); count[oriNum[i]]++; } // accumulate the count array indices[0] = 0; for (int i = 1; i < 100; i++) { indices[i] = indices[i-1] + count[i-1]; } // output order for (int i = 0; i < N; i++) { int num = oriNum[i]; output[indices[num]++] = i; } int bar = N/2; StringBuilder sb = new StringBuilder(); for (int i = 0; i < N; i++) { int index = output[i]; if (index < bar) sb.append("- "); else sb.append(oriStr[index]+ " "); } System.out.println(sb.toString()); } }
You should try a plain buffered reader instead of Scanner. Scanner is surprisingly slow and I have participated in programming competitions where Scanner was the sole reason for "time limit exceeded".
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args)throws Exception { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(in.readLine()); int[] c=new int[100]; String[][] dt=new String[100][10300]; for(int i=0;i<n;i++) { String[] str=in.readLine().split(" "); int val=Integer.parseInt(str[0]); if(i<n/2) dt[val][c[val]]="-"; else dt[val][c[val]]=str[1]; c[val]++; } StringBuilder sb=new StringBuilder(""); for(int i=0;i<100;i++) if(i<n) for(int k=0;k<c[i];k++) if(dt[i][k]!=null) sb.append(dt[i][k]+" "); else break; System.out.println(sb.toString()); } }
This was my approach to problem. (it is in c++). void counting_sort(vector<int> &arr, int size, vector<vector<string> > foo, vector<int> first_half) { int max = *max_element(arr.begin(), arr.end()); int min = *min_element(arr.begin(), arr.end()); int range = max - min + 1; int count[range] = {0}; // counting frequency of numbers in array for (int i = 0; i < size; i++) { count[arr[i] - min]++; } // calculating cumulative sum for (int i = 1; i < range; i++) { count[i] += count[i - 1]; } vector<vector<string> > output(size); // making the new sorted array for (int i = size - 1; i >= 0; i--) // traversing from backward for stability { output[count[arr[i]-min] - 1] = foo[i]; count[arr[i]-min]--; } // copying the sorted array in original array int j=0; for (int i = 0; i < size; i++) { if(stoi(output[i][0]) == first_half[j]) { cout << "- "; j++; } else { cout << output[i][1] << ' '; } } } // Complete the countSort function below. void countSort(vector<vector<string>> arr) { vector<int> num; vector<int> first_half; for(int i=0; (unsigned)i<arr.size(); i++) { num.push_back(stoi(arr[i][0])); if(i < ((unsigned)arr.size()/2)) { first_half.push_back(stoi(arr[i][0])); } } sort(first_half.begin(), first_half.end()); counting_sort(num, num.size(), arr, first_half); }