-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.java
More file actions
79 lines (63 loc) · 1.78 KB
/
Copy pathTests.java
File metadata and controls
79 lines (63 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Oct 1 2018
* Just some coding challenges
*/
public class Tests {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
//testing duplicates
System.out.println(duplicates(array));
//testing rotate_Array
int[] eArray = rotateArray(array, 3);
for (int i = 0; i<array.length; i++){
System.out.print(eArray[i] + " ");
}
System.out.println();
//testing sorting algorithms
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;
Sorting ob = new Sorting();
ob.sort(arr, 0, n-1);
System.out.println("sorted array");
Sorting.printArray(arr);
}
/* duplicates (int[] origArray)
* if there are duplicates in an array, then return true
*
* Test case:
* Input: [1,2,3,1]
* Output: true
*/
private static boolean duplicates(int[] origArray){
Arrays.sort(origArray);
boolean fin = false;
for (int i = 0; i< origArray.length - 1; i++){
if (origArray[i] == origArray[i+1]){
fin = true;
}
}
return fin;
}//end of method
/* rotateArray(int[] origArray, int k)
* Given an array, rotate the array to the right by k steps,
* where k is non-negative.
*
* Test case:
* Input: [1,2,3,4,5,6,7] and k = 3
* Output: [5,6,7,1,2,3,4]
*/
private static int[] rotateArray(int[] origArray, int k)
{
int[] editedArray = new int[origArray.length];
int j = 0;
for (int i = k + 1; i< origArray.length; i++){
editedArray[j] = origArray[i];
j++;
}
for (int i = 0; i < k+1; i++){
editedArray[j] = origArray[i];
j++;
}
return editedArray;
}//end of method
}//end of class