Insertion Sort pt2 -Hackerrank - java

can someone please check my code as to why the last index is not working as intended? Any advice on how to improve this is much appreciated.
import java.io.*;
import java.util.*;
public class Solution {
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 0 ; i < ar.length-1; i++){
key = ar[i];
seen = i;
while (seen <ar.length-1 && ar[seen+1]<key){
ar[seen]= ar[seen+1];
seen = seen+1;
}
ar[seen]=key;
printArray(ar);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] ar = new int[s];
for(int i=0;i<s;i++){
ar[i]=in.nextInt();
}
insertionSortPart2(ar);
}
private static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}
}
Input (stdin) 6 1 4 3 5 6 2
Your Output (stdout) 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 2 6
Expected Output 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6 Compiler Message Wrong Answer

You are doing it in wrong way. Read the part 1 again.
Assume that first element is sorted. Then try to insert element one by one in sorted list.
Try this:
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 1 ; i < ar.length; i++){
key = ar[i];
seen = i;
while (seen > 0 && ar[seen-1] > key) {
ar[seen] = ar[seen-1];
seen = seen - 1;
}
ar[seen]=key;
printArray(ar);
}
}

Related

Adding large matrices ArrayIndexOutOfBoundsException

I am writing a program which adds two integer matrices together and I am running into an ArayIndexOutOfBounds exception when trying to process larger matrices. Any advice on how to fix this issue would be greatly appreciated.
ArrayOutOfBoundsException on cmd prompt
Here is the class in which the error is occuring:
import java.util.ArrayList;
public class ThreadMaker
{
public static int count=0;
public static void addmatrix(int[][] mat_A,int[][] mat_B,int[][] submatrix1,int[][] submatrix2,int N,int M)throws Exception
{
ThreadOperation m1;
ThreadOperation.initialize(N,M);
ArrayList<Thread> threads = new ArrayList<Thread>();
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for(k=0,i=c; i < c + N/2; i++, k++)
{
for(l=0,j=d; j < d + M/2; j++, l++)
{
submatrix1[k][l]=mat_A[i][j];
submatrix2[k][l]=mat_B[i][j];
}
}
m1=new ThreadOperation(mat_A,mat_B,submatrix1,submatrix2,N,M,c,d);
Thread thread =new Thread(m1);
thread.start();
threads.add(thread);
thread.join();
d=d+M/2;
}
c=c+N/2;
}
}
public static void devidematrix(int[][] mat_A,int[][] submatrix,int N,int M)
{
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for( k=0,i=c;i<c+N/2;i++,k++)
{
for( l=0,j=d;j<d+M/2;j++,l++)
{
submatrix[k][l]=mat_A[i][j];
}
}
d=d+M/2;
}
c=c+N/2;
}
}
public synchronized static void printmatrix(int[][] matrix,int row,int col)
{
System.out.print(" ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print(matrix[i][j]+" ");
}
}
System.out.print(" ");
}
}
Here is the martix from the matrix2.txt file:
4 7
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
If any more information is need please let me know.

Change only one of the many iteration

I have 0-10 for loop I want to change the value to 100 when is only 4 and the rest will not be affected and no break how should I achieve that. And as for the petitioner, do not repel [him].
class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
int n = 0;
for (int i = 0; i < 10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
}
else
{
System.out.println(i);
}
}
}
}
Another Solution!!
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
for (int i = 0; i <=10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
System.out.println(i);
i=4;
continue;
}
else
{
System.out.println(i);
}
}
}
}
Try This one !!
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
for(int i = 0; i < 10; i++){
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if(i == 4){
System.out.println ("100");
}else{
System.out.println(i);
}
}
}

For loop to print a numeric pattern does not print the correct pattern

This is what it should look like
9 8 7 6 5 4 3 2 1 0
8 8 7 6 5 4 3 2 1 0
7 7 7 6 5 4 3 2 1 0
6 6 6 6 5 4 3 2 1 0
5 5 5 5 5 4 3 2 1 0
4 4 4 4 4 4 3 2 1 0
3 3 3 3 3 3 3 2 1 0
2 2 2 2 2 2 2 2 1 0
1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
Here's my attempt.
public class Main {
public static void main(String[] args) {
int i = 9;
int count = -1;
while (i >= count) {
int j = i;
while (j > count) {
System.out.print(j + " ");
j--;
}
System.out.println();
count++;
}
}
}
Here's my actual output:
9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2
9 8 7 6 5 4 3
9 8 7 6 5 4
9 8 7 6 5
9 8 7 6
9 8 7
9 8
9
This obviously does not match the expected output. Can someone point out where the mistake is in the code?
This is a Solution that has the right output, but instead of using while-Loops I used for-Loops
public class Main {
public static void main(String[] args) {
int count1 = 9;
for (int i = count1; i >= 0; i--) {
int count2 = i;
if (count1 > count2) {
int tmp = count1 - count2;
for (int j = tmp; j > 0; j--) {
System.out.print(count2 + " ");
}
}
for (int j = count2; j >= 0; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
You can keep two outer variables, counter and multiplier, for the matrix size and repetitions' count respectively:
public class Main {
public static void main(String[] args) {
int counter = 15;
int multiplier = 1;
for (int i = counter; i >= 0; i--) {
for (int j = 0; j<multiplier; j++) {
System.out.printf("%3d", counter); //using %3d for spacing numbers nicely
}
for (int k = counter-1; k >= 0; k--) {
System.out.printf("%3d", k);
}
++multiplier;
--counter;
System.out.println();
}
}
}
For every horizontal line, where counter decreases, and multiplier increases (9 once on 1st line; 8 twice on the second line, etc.):
it will first print the counter, multiplier times;
it will then fill the rest of the line with counter-multiplier number of descending sequence integers, starting from counter-1;
at the end of outer loop's each iteration, a new line is printed.
Output would be:
9 8 7 6 5 4 3 2 1 0
8 8 7 6 5 4 3 2 1 0
7 7 7 6 5 4 3 2 1 0
6 6 6 6 5 4 3 2 1 0
5 5 5 5 5 4 3 2 1 0
4 4 4 4 4 4 3 2 1 0
3 3 3 3 3 3 3 2 1 0
2 2 2 2 2 2 2 2 1 0
1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
Since you already have your answer, here are a couple alternatives.
String str = "9 8 7 6 5 6 3 2 1 0";
System.out.println(str);
for (int i = 9; i > 0; i--) {
str = str.replace(i+"",(i-1)+"");
System.out.println(str);
}
Or use the String.repeatmethod.
for (int i = 9; i >= 0; i--) {
System.out.print((i+" ").repeat(9-i));
for(int k = i; k >= 0; k--) {
System.out.print(k + " ");
}
System.out.println();
}
With the help of Java8 stream you can write the code as below:
public static void main(String[] args) {
IntStream.range(0, 10)
.forEach(i -> {
IntStream.range(0, 10).forEach(j -> {
System.out.print((9- (j < i ? i : j)) + " " );
});
System.out.println("");
});
}

How to store text file into 2d int array

I have two files formatted as follows below where the first int needs to be stored somewhere and the second line and down are going to be stored into a 2d array. The first number, in this case, 4, will indicate the size as int [][] array = new int [2*4][4]; The 4, however, should not be part of the array and should be discarded. The file paths will be provided to the program via the command line. I haven't been able to find any could that could perform this function.
4
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
I have used java 7 API to read file into a list.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class SOTest {
public static void main(String[] args) throws IOException {
int[][] arr = null;
String path = args[0];
List<String> lines = Files.readAllLines(Paths.get(path));
for (int i=0; i<lines.size() ; i++) {
String[] aStr = lines.get(i).split("\\s+");
if(aStr.length==1) {
int size = Integer.valueOf(aStr[0]);
System.out.println("Size of array "+size);
arr = new int[size][size];
populateArr(arr, lines.subList(i+1, i+size+1));
print(arr);
i = i+size;
}
}
}
public static void populateArr(int[][] arr, List<String> list) {
for(int i=0; i<list.size(); i++) {
String[] values = list.get(i).split("\\s+");
for(int j=0; j<values.length; j++) {
arr[i][j] = Integer.parseInt(values[j]);
}
}
}
private static void print(int[][] arr) {
System.out.println("***********PRINT***********");
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Input (File location)
file content is as below
4
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
3
1 2 3
4 5 6
7 8 9
output from java program
Size of array 4
***********PRINT***********
7 5 6 4
5 4 6 7
4 5 6 7
4 5 6 7
Size of array 3
***********PRINT***********
1 2 3
4 5 6
7 8 9

Whats wrong with my QuickSort implementation?

class PartitionIt
{
public static void partitionIt(int[] a, int l, int r, int pivot)
{
int i,j;
i = j = l+1;
while(j<= r)
{
if(a[j] <= a[pivot])
{
swap(a,j,i);
i++;
}
j++;
}
swap(a,pivot,--i);
}
public static void swap(int[] a, int j, int i)
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
public static void displayArray(int[] a)
{
for(int i:a)
System.out.print(i+" ");
System.out.println();
}
public static void QuickSort(int[] a, int l, int r)
{
if(r <= l)
return;
int pivot = getPivot(a,l,r);
partitionIt(a,l,r,pivot);
QuickSort(a,l,pivot);
QuickSort(a,pivot+1,r);
}
public static int getPivot(int[] a,int l,int r)
{
return l;
}
public static void main(String[] args)
{
int[] a = {3,2,8,5,1,4,7,6};
int[] b = {1,2,3,4,5,6,7,8,9,0};
int[] c = {5,4,2,4,7,6,5,3,2,1,10};
displayArray(a);
System.out.println("After Parititon with pivot 3");
QuickSort(a,0,a.length-1);
displayArray(a);
System.out.println();
displayArray(b);
System.out.println("After Parititon with pivot 1");
QuickSort(b,0,b.length-1);
displayArray(b);
System.out.println();
displayArray(c);
System.out.println("After Parititon with pivot 5");
QuickSort(c,0,c.length-1);
displayArray(c);
System.out.println();
}
}
3 2 8 5 1 4 7 6
After Parititon with pivot 3
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 0
After Parititon with pivot 1
0 1 2 3 4 5 6 7 8 9
5 4 2 4 7 6 5 3 2 1 10
After Parititon with pivot 5
1 2 2 4 3 4 5 5 6 7 10
It is not sorting properly in last case.
Can anyone help here. I m stuck from so long.
Thanks in advance!
At This snippet:
if(a[j] <= a[pivot])
{
swap(a,j,i);
i++;
}
The '<=' should be '<'.
while the sequence was sorted to 1 2 2 4 3 4 5 5 7 6 10,the pivot is '4'(the left one),while compare 4 <= 4,i++, this cause the swap(a,pivot,--i) change the location of '4'(the right one ) to '4'(the left one),rather than change the '3' to '4'.

Categories

Resources