Repository navigation
Expand file tree
/
Copy pathProgrammingTest.java
More file actions
96 lines (90 loc) · 2.92 KB
/
Copy pathProgrammingTest.java
File metadata and controls
96 lines (90 loc) · 2.92 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
*
*/
import java.util.*;
import java.util.stream.Collectors;
public class ProgrammingTest {
public int[] RotateArrayByNPositions(int[] arrayToRotate, int positions) {
int arrayToRotateLength = arrayToRotate.length;
int rotatedArray[] = new int[arrayToRotateLength];
if(positions == 0)
return arrayToRotate;
if(positions > arrayToRotateLength){
throw new IllegalArgumentException("The number of positions to rotate the array is invalid");
}
int clonedPositions = positions;
for (int i = 0; i < arrayToRotate.length; i++) {
int value = arrayToRotate.length - positions;
if (positions != 0) {
rotatedArray[i] = arrayToRotate[value];
positions--;
} else {
rotatedArray[i] = arrayToRotate[i - clonedPositions];
}
}
return rotatedArray;
}
// this function is based on the premise that the array has already being sorted
public int[] RemoveDuplicateFromSortedArray(int[] inputArray) {
int inputArrayLength = inputArray.length;
int previoustItem = inputArray[0];
int[] uniqueItems = new int[inputArrayLength];
uniqueItems[0] = previoustItem;
int countOfDistinct = 1;
for (int i = 0; i < inputArrayLength; i++ ) {
if (previoustItem != inputArray[i]) {
uniqueItems[countOfDistinct++] = inputArray[i];
}
previoustItem = inputArray[i];
}
final int[] compactedArray = new int[countOfDistinct];
System.arraycopy(uniqueItems, 0, compactedArray, 0, countOfDistinct);
return compactedArray;
}
public String FindCharsOrderN2N(String string1, String string2) {
String result = "";
for (int i = 0; i < string1.length(); i++) {
char searchString = string1.charAt(i);
for (int j = 0; j < string2.length(); j++) {
if(searchString == string2.charAt(j)){
result = result.concat(String.valueOf(searchString));
break;
}
}
}
return result;
}
public String FindCharsOrderN(String string1, String string2) {
String result = "";
Set<Character> uniqueValues = string2.chars().mapToObj(e->(char)e).collect(Collectors.toSet());
for (int i = 0; i < string1.length(); i++) {
char searchString = string1.charAt(i);
if(uniqueValues.contains(searchString))
result = result.concat(String.valueOf(searchString));
}
return result;
}
public int getGcd(int x, int y){
if (x<y) return getGcd(y,x);
if (x%y==0) return y;
else return getGcd(y, x%y);
}
public int GetLcm(int x, int y){
return ((x*y)/getGcd(x,y));
}
public int GetLcmForArray(int[] inputArray){
int arrayLength = inputArray.length;
if(arrayLength == 1)
return inputArray[0];
int lcm = GetLcm(inputArray[0], inputArray[1]);;
for (int i = 2; i < arrayLength; i++) {
lcm = GetLcm(lcm, inputArray[i]);
}
return lcm;
}
/**
* @param args
*/
public static void main(String[] args) {
}
}