JAVA(array index out of bound exception),Nutanix placement question - java

link to the problem for reference: https://www.geeksforgeeks.org/nutanix-interview-question-placement-2019-2020/
Array out-of-bounds exception at line prp[b]=s1.nextInt();
Stuck on a couple of questions with same error.
Appreciate any help.TIA
Code:
package pkg161219;
import java.util.*;
public class xD{
static Scanner s1=new Scanner(System.in);
public static void main(String[] args)
{
int x=s1.nextInt();
for(int i=0;i<x+1;i++)
{
int[] y=new int[3];
for(int a=0;a<3;a++)
{
y[i]=s1.nextInt();
}
int[] prp=new int[y[0]];
int[] prr=new int[y[1]];
int[] prs=new int[y[2]];
for(int b=0;b<y[0];b++)
{
System.out.println("Enter prices of specs of Processor");
prp[b]=s1.nextInt();
}
for(int c=0;c<y[1];c++)
{
System.out.println("Enter prices of specs of RAM");
prr[c]=s1.nextInt();
}
for(int d=0;d<y[2];d++)
{
System.out.println("Enter prices of specs of SSD");
prs[d]=s1.nextInt();
}
for(int q=0;q<y[0];q++)
{
for(int w=0;w<y[1];w++)
{
for(int e=0;e<y[2];e++)
{
if(prr[e]>=prs[w]&&prr[e]>=prp[q])
{
System.out.println(prp[q]*prr[w]+prr[w]*prs[e]);
}
}
}
}
}
}
}

for(int a = 0; a < 3; a++) {
y[i] = s1.nextInt();
}
The above is wrong. It should be y[a]

Related

Why am I getting this error? This is the first question of kickstart 2020 (allocation) in Java

import java.util.Scanner;
public class Main
{
void bubbleSort(int arr[])
{
int temp=0;
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int value(int arr[],int B)
{
int a=0,b=0;
Main ob=new Main();
ob.bubbleSort(arr);
b=b+arr[a];
while(b<=B)
{
a++;
b=b+arr[a];
}
return a;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
int N,B;
int ar[]=new int[T];
Main ob=new Main();
for(int a=0;a<T;a++)
{
N=sc.nextInt();
int arr[]=new int[N];
B=sc.nextInt();
for(int b=0;b<N;b++)
{
arr[b]=sc.nextInt();
}
ar[a]=(ob.value(arr,B));
}
for(int a=0;a<T;a++)
{
System.out.println("Case "+"#"+(a+1)+": "+ar[a]);
}
}
}
Program runs on bluej but not on their site.
ERROR
mesg: ttyname failed: Inappropriate ioctl for device
Solution.java:2: error: class Main is public, should be declared in a file named Main.java
public class Main
^
1 error
The class Main has to be in a file with the name Main.java.
In Java, the filename should always be identical with the name of the public class within.

which insertion sort is better.Either using a Outer for and while or two for loops.both are generating same outputs

Using two for loops:
import java.util.*;
public class InsertionSort{
public static int[] doInsertionSort(int[]a){
int j=a.length;
for(int k=1;k<j;k++){
for(int i=k;i>0;i--){
if(a[i]<a[i-1]){
int temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
//for printing the elements while sorting..
for(int o:a)
System.out.print(o+" ");
System.out.println("");
}
}
return a;
}
public static void main(String[] args) {
int[]arr={99,77,55,33,11,88,66};
int []arra= doInsertionSort(arr);
for(int i:arra)
System.out.print(i+" ");
}
}
Using a for and while loop:
import java.util.*;
public class InsertionSortAgain{
public static void main(String[] args) {
int[]arr={99,77,55,33,11,88,66};
int n=arr.length,j;
for(int i=1;i<n;i++){
j=i-1;
int key=arr[j+1];
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
//for printing elements while sorting.....
for(int k :arr)
System.out.print(k+" ");
System.out.println(" ");
}
arr[j+1]=key;
}
for(int k :arr)
System.out.print(k+" ");
}
}
I have tried both and both do fine, but I couldn't get which is optimized.
And whether insertion sort can be implemented like this or not?

Quick Sort in java in not running if the first element is the largest while considering first element as pivot

I have written a code in java for quicksort while considering the first element as pivot. The code is giving an ArrayOutOfBoundsException if I consider the first element as the largest element of the array. While the logic is running perfectly when implemented the same logic in C++.
import java.util.Scanner;
class QSort
{
private int n;
private int a[] = new int[n];
public QSort(int[] x)
{
n = x.length;
a = x;
}
private int qs(int low,int up)
{
int pivot = a[low];
int p = low+1,q = up;
while(q>=p)
{
while(pivot>=a[p])
p++;
while(pivot<a[q])
q--;
if(q>p)
{
a[p]=a[p]+a[q]-(a[q]=a[p]); //swapping(a[p],a[q])
p++;
q--;
}
}
a[low]=a[low]+a[q]-(a[q]=a[low]); //swapping(a[low],a[q])
return q;
}
public void quicksort(int low,int up)
{
if(low<up)
{
int i = qs(low,up);
quicksort(low,i-1);
quicksort(i+1,up);
}
}
public void print()
{
System.out.println("\nThe sorted array is :");
for(int i=0; i<a.length; i++)
{
System.out.println(a[i]);
}
}
}
class Quick
{
public static void main(String args[])
{
System.out.print("\nEnter the no. of values : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x[] = new int[n];
System.out.println("\nEnter the elements :");
for(int i=0; i<n; i++)
x[i] = sc.nextInt();
QSort s = new QSort(x);
s.quicksort(0,n-1);
s.print();
}
}

unexpected run time error in 2d sort java

import java.util.Scanner;
class Sorting
{
static int m,n;
static int x=0;`enter code here`
static int b[]=new int[m*n];
static int a[][]=new int[m][n];
static void print()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(+a[i][j]);
}
System.out.println();
}
}
static void convertBack()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=b[x];
x++;
}
}
}
static void sort()
{
for(int i=0;i<m*n;i++)
{
for(int j=0;j<m*n;j++)
{
if(b[i]>b[j])
{
int temp=b[i];
b[i]=b[j];
b[j]=temp;
}
else
{
continue;
}
}
}
}
static void convert()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[x]=a[i][j];
x++;
}
}
}
static void enterArray()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=Integer.parseInt(in.nextLine());
}
}
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
m=Integer.parseInt(in.nextLine());
System.out.println("Enter the number of columns");
n=Integer.parseInt(in.nextLine());
enterArray();
convert();
sort();
convertBack();
print();
}
}
The above code compiles fine however on running i am getting an error as follows:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Sorting.enterArray(2dsort.java:76)
at Sorting.main(2dsort.java:87)
please help !
You are initializing your arrays before setting the values of m and n, so you get empty arrays :
static int m,n; // both are 0 by default
static int b[]=new int[m*n]; // equivalent to new int [0];
static int a[][]=new int[m][n]; // equivalent to new int [0][0];
You should create your arrays after initializing m and n :
m=Integer.parseInt(in.nextLine());
System.out.println("Enter the number of columns");
n=Integer.parseInt(in.nextLine());
b=new int[m*n];
a=new int[m][n];
...

Facing issue in getting the largest integer from Array

Receiving 5,9,7 as output.. Where as the expected should be only 9..Below is the code:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
System.out.println(a[i]);
}
}
}
Please help
for expected answer you need to print big (not a[i]) out side of loop
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
}
System.out.println(big);
}
}
code should be like this:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[] = new int[] {1,2,5,9,7};
int big = a[0];
for (int i=1; i<a.length; i++){
if (big < a[i])
big = a[i];
//System.out.println(a[i]);
}
System.out.println("Big: " + big);
}
}
This works!

Categories