Skip to content

Commit 6628e86

Browse files
authored
Merge pull request #3321 from adhamahmad/adham-branch
Create: 1968-array-with-elements-not-equal-to-average-of-neighbors.java, 0658-find-k-closest-elements.java
2 parents 1aa28bf + 6166683 commit 6628e86

2 files changed

+31
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public List<Integer> findClosestElements(int[] arr, int k, int x) {
3+
int l =0;
4+
int r = arr.length -k;
5+
List<Integer> result = new ArrayList<>();
6+
while( l < r){
7+
int m = (l+r) /2;
8+
if(x - arr[m] > arr[m+k] -x ){ //right is closer
9+
l = m+1;
10+
}else{ //left is closer
11+
r =m;
12+
}
13+
}
14+
for(int i =l; i < l+k; i++){ //build the solution list
15+
result.add(arr[i]);
16+
}
17+
return result;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int[] rearrangeArray(int[] nums) {
3+
for(int i=1; i <= nums.length-2; i++){
4+
if( ( ( nums[i-1] < nums[i] && nums[i] < nums[i+1] ) || ( nums[i-1] > nums[i] && nums[i] > nums[i+1] ) )){ //swap
5+
int temp = nums[i+1];
6+
nums[i+1] = nums[i];
7+
nums[i] = temp;
8+
}
9+
}
10+
return nums;
11+
}
12+
}

0 commit comments

Comments
 (0)