Skip to content

Commit 8302d6b

Browse files
author
Ram swaroop
committed
hackerrank solutions added
1 parent 23e0bbd commit 8302d6b

21 files changed

+900
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: ramswaroop
8+
* Date: 3/1/15
9+
* Time: 8:58 PM
10+
* To change this template go to Preferences | IDE Settings | File and Code Templates
11+
*/
12+
public class InsertionSort1 {
13+
14+
static void insertIntoSorted(int[] ar) {
15+
int V = ar[ar.length - 1], i = ar.length - 2;
16+
17+
for (; i >= 0; i--) {
18+
if (V < ar[i]) {
19+
ar[i + 1] = ar[i];
20+
} else {
21+
break;
22+
}
23+
printArray(ar);
24+
}
25+
26+
ar[i + 1] = V;
27+
printArray(ar);
28+
}
29+
30+
/* Tail starts here */
31+
public static void main(String[] args) {
32+
Scanner in = new Scanner(System.in);
33+
int s = in.nextInt();
34+
int[] ar = new int[s];
35+
for (int i = 0; i < s; i++) {
36+
ar[i] = in.nextInt();
37+
}
38+
insertIntoSorted(ar);
39+
}
40+
41+
private static void printArray(int[] ar) {
42+
for (int n : ar) {
43+
System.out.print(n + " ");
44+
}
45+
System.out.println("");
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: ramswaroop
8+
* Date: 3/1/15
9+
* Time: 9:42 PM
10+
* To change this template go to Preferences | IDE Settings | File and Code Templates
11+
*/
12+
public class InsertionSort2 {
13+
14+
static void insertionSortPart2(int[] ar) {
15+
for (int i = 1; i < ar.length; i++) {
16+
int V = ar[i], j;
17+
/**
18+
* keep shifting no.s to right until
19+
* right place for insertion(of V) is found
20+
*/
21+
for (j = i - 1; j >= 0 && ar[j] > V; j--) {
22+
ar[j + 1] = ar[j];
23+
}
24+
ar[j + 1] = V;
25+
printArray(ar);
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
Scanner in = new Scanner(System.in);
31+
int s = in.nextInt();
32+
int[] ar = new int[s];
33+
for (int i = 0; i < s; i++) {
34+
ar[i] = in.nextInt();
35+
}
36+
insertionSortPart2(ar);
37+
38+
}
39+
40+
private static void printArray(int[] ar) {
41+
for (int n : ar) {
42+
System.out.print(n + " ");
43+
}
44+
System.out.println("");
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
/**
7+
* Created by IntelliJ IDEA.
8+
* User: ramswaroop
9+
* Date: 3/1/15
10+
* Time: 3:38 PM
11+
* To change this template go to Preferences | IDE Settings | File and Code Templates
12+
*/
13+
public class IntroTutorial {
14+
static int search(int searchVal, int[] arr) {
15+
return Arrays.binarySearch(arr, searchVal);
16+
}
17+
18+
public static void main(String[] a) {
19+
Scanner in = new Scanner(System.in);
20+
21+
int searchVal = in.nextInt();
22+
int arrSize = in.nextInt();
23+
int[] arr = new int[arrSize];
24+
// as nextInt() doesn't read new line character
25+
in.nextLine();
26+
String next = in.nextLine();
27+
String[] next_split = next.split(" ");
28+
29+
for (int _a_i = 0; _a_i < arrSize; _a_i++) {
30+
arr[_a_i] = Integer.parseInt(next_split[_a_i]);
31+
}
32+
33+
System.out.print(search(searchVal, arr));
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: ramswaroop
8+
* Date: 3/2/15
9+
* Time: 3:26 PM
10+
* To change this template go to Preferences | IDE Settings | File and Code Templates
11+
*/
12+
public class LoopInvariant {
13+
14+
public static void insertionSort(int[] A) {
15+
for (int i = 1; i < A.length; i++) {
16+
int value = A[i];
17+
int j = i - 1;
18+
while (j >= 0 && A[j] > value) {
19+
A[j + 1] = A[j];
20+
j = j - 1;
21+
}
22+
A[j + 1] = value;
23+
}
24+
25+
printArray(A);
26+
}
27+
28+
29+
static void printArray(int[] ar) {
30+
for (int n : ar) {
31+
System.out.print(n + " ");
32+
}
33+
}
34+
35+
public static void main(String[] args) {
36+
Scanner in = new Scanner(System.in);
37+
int n = in.nextInt();
38+
int[] ar = new int[n];
39+
for (int i = 0; i < n; i++) {
40+
ar[i] = in.nextInt();
41+
}
42+
insertionSort(ar);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: ramswaroop
8+
* Date: 3/2/15
9+
* Time: 5:13 PM
10+
* To change this template go to Preferences | IDE Settings | File and Code Templates
11+
*/
12+
public class QuickSort1 {
13+
14+
static void partition(int[] ar) {
15+
int pivot = ar[0], j = 0;
16+
int[] arCopy = ar.clone();
17+
18+
for (int i = 0; i < arCopy.length; i++) {
19+
if (arCopy[i] < pivot) ar[j++] = arCopy[i];
20+
}
21+
for (int i = 0; i < arCopy.length; i++) {
22+
if (arCopy[i] >= pivot) ar[j++] = arCopy[i];
23+
}
24+
printArray(ar);
25+
}
26+
27+
static void printArray(int[] ar) {
28+
for (int n : ar) {
29+
System.out.print(n + " ");
30+
}
31+
System.out.println("");
32+
}
33+
34+
public static void main(String[] args) {
35+
Scanner in = new Scanner(System.in);
36+
int n = in.nextInt();
37+
int[] ar = new int[n];
38+
for (int i = 0; i < n; i++) {
39+
ar[i] = in.nextInt();
40+
}
41+
partition(ar);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
/**
8+
* Created by IntelliJ IDEA.
9+
* User: ramswaroop
10+
* Date: 3/3/15
11+
* Time: 1:05 PM
12+
* To change this template go to Preferences | IDE Settings | File and Code Templates
13+
*/
14+
public class QuickSort2 {
15+
16+
static void quickSort(int[] ar, int start, int end) {
17+
int pivot = ar[0];
18+
List<Integer> ar1 = new ArrayList<>();
19+
List<Integer> ar2 = new ArrayList<>();
20+
21+
for (int i = start; i < end; i++) {
22+
if (ar[i] < pivot) {
23+
ar1.add(ar[i]);
24+
} else if (ar[i] > pivot) {
25+
ar2.add(ar[i]);
26+
}
27+
}
28+
29+
//TODO
30+
}
31+
32+
static void printArray(int[] ar) {
33+
for (int n : ar) {
34+
System.out.print(n + " ");
35+
}
36+
System.out.println("");
37+
}
38+
39+
public static void main(String[] args) {
40+
Scanner in = new Scanner(System.in);
41+
int n = in.nextInt();
42+
int[] ar = new int[n];
43+
for (int i = 0; i < n; i++) {
44+
ar[i] = in.nextInt();
45+
}
46+
quickSort(ar, 0, ar.length);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.ramswaroop.algorithms.arraysandsorting;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* User: ramswaroop
8+
* Date: 3/2/15
9+
* Time: 5:02 PM
10+
* To change this template go to Preferences | IDE Settings | File and Code Templates
11+
*/
12+
public class RunningTime {
13+
static void insertionSortPart2(int[] ar) {
14+
int c = 0;
15+
for (int i = 1; i < ar.length; i++) {
16+
int V = ar[i], j;
17+
for (j = i - 1; j >= 0 && ar[j] > V; j--, c++) {
18+
ar[j + 1] = ar[j];
19+
}
20+
ar[j + 1] = V;
21+
//printArray(ar);
22+
}
23+
System.out.print(c);
24+
}
25+
26+
public static void main(String[] args) {
27+
Scanner in = new Scanner(System.in);
28+
int s = in.nextInt();
29+
int[] ar = new int[s];
30+
for (int i = 0; i < s; i++) {
31+
ar[i] = in.nextInt();
32+
}
33+
insertionSortPart2(ar);
34+
35+
}
36+
37+
private static void printArray(int[] ar) {
38+
for (int n : ar) {
39+
System.out.print(n + " ");
40+
}
41+
System.out.println("");
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.ramswaroop.algorithms.strings;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
*
8+
* @author: ramswaroop
9+
* @date: 10/22/15
10+
* @time: 9:24 AM
11+
*/
12+
public class AlternatingCharacters {
13+
14+
public static int countDeletions(String s) {
15+
int count = 0, index = 0;
16+
for (int i = 1; i < s.length(); i++) {
17+
if (s.charAt(i) == s.charAt(index)) {
18+
count++;
19+
} else {
20+
index = i;
21+
}
22+
}
23+
return count;
24+
}
25+
26+
public static void main(String[] args) {
27+
Scanner in = new Scanner(System.in);
28+
int t = Integer.parseInt(in.nextLine());
29+
String[] input = new String[t];
30+
for (int i = 0; i < t; i++) {
31+
input[i] = in.nextLine();
32+
}
33+
for (int i = 0; i < t; i++) {
34+
System.out.println(countDeletions(input[i]));
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)