aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arr_elements_neq_neighbour.c44
-rw-r--r--arr_elements_neq_neighbour.py29
2 files changed, 73 insertions, 0 deletions
diff --git a/arr_elements_neq_neighbour.c b/arr_elements_neq_neighbour.c
new file mode 100644
index 0000000..59a7b8f
--- /dev/null
+++ b/arr_elements_neq_neighbour.c
@@ -0,0 +1,44 @@
+// 1968. Array With Elements Not Equal to Average of Neighbors
+#include "leetcode.h"
+
+/*
+ * you are given a 0-indexed array 'nums' of distinct integers. you want to
+ * rearrange the elements in the array such that every element in the rearranged
+ * array is not equal to the average of its neighbors. more formally the
+ * rearranged array should have the property such that for every 'i' in the
+ * range '1 <= nums.len - 1', '(nums[i - 1] + nums[i + 1]) / 2' is not equal to
+ * 'nums[i]'. return any rearrangement of 'nums' that meets the requirements.
+ */
+
+void swap(int *x, int *y) {
+ if (x == y)
+ return;
+ *x ^= *y;
+ *y ^= *x;
+ *x ^= *y;
+}
+
+int *rearrangeArray(int *nums, int numsSize, int *returnSize) {
+ int *ans = (int *)malloc(numsSize * sizeof(int)), i, j = 0;
+ *returnSize = numsSize;
+ for (int i = 1; i < numsSize - 1; i++)
+ if (nums[i] * 2 == nums[i - 1] + nums[i + 1])
+ swap(&nums[i], &nums[i + 1]);
+ for (int i = numsSize - 2; i; i--)
+ if (nums[i] * 2 == nums[i - 1] + nums[i + 1])
+ swap(&nums[i], &nums[i - 1]);
+ return ans;
+}
+
+int main() {
+ int n1[] = {1, 2, 3, 4, 5}, n2[] = {6, 2, 0, 9, 7}, rs1, rs2;
+ int *ra1 = rearrangeArray(n1, ARRAY_SIZE(n1), &rs1);
+ int *ra2 = rearrangeArray(n2, ARRAY_SIZE(n2), &rs2);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", ra1[i]); // expect: 1,2,4,5,3
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", ra2[i]); // expect: 9,7,6,2,0
+ printf("\n");
+ free(ra1), free(ra2);
+}
diff --git a/arr_elements_neq_neighbour.py b/arr_elements_neq_neighbour.py
new file mode 100644
index 0000000..dc92531
--- /dev/null
+++ b/arr_elements_neq_neighbour.py
@@ -0,0 +1,29 @@
+# 1968. Array With Elements Not Equal to Average of Neighbors
+
+"""
+you are given a 0-indexed array 'nums' of distinct integers. you want to
+rearrange the elements in the array such that every element in the rearranged
+array is not equal to the average of its neighbors. more formally the
+rearranged array should have the property such that for every 'i' in the
+range '1 <= nums.len - 1', '(nums[i - 1] + nums[i + 1]) / 2' is not equal to
+'nums[i]'. return any rearrangement of 'nums' that meets the requirements.
+
+"""
+
+
+class Solution(object):
+ def rearrangeArray(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: List[int]
+ """
+ nums.sort()
+ for i in range(1, len(nums), 2):
+ nums[i], nums[i - 1] = nums[i - 1], nums[i]
+ return nums
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.rearrangeArray(nums=[1, 2, 3, 4, 5]))
+ print(obj.rearrangeArray(nums=[6, 2, 0, 9, 7]))