diff --git a/Programmierung2/src/Übungen/NormaleÜbungen.java b/Programmierung2/src/Übungen/NormaleÜbungen.java new file mode 100644 index 0000000..4327c79 --- /dev/null +++ b/Programmierung2/src/Übungen/NormaleÜbungen.java @@ -0,0 +1,93 @@ +package Übungen; + +public class NormaleÜbungen { + + public static void main(String[] main) { + int[] nums = { 1, 2, 3, 4, 2}; + + System.out.println(duplicateElement(nums)); + + } + + static int duplicateElement (int[] arr) { + for (int i = 0; i < arr.length - 1; i++) + for (int j = i + 1; j < arr.length; j++) + if (arr[i] == arr[j]) + return arr[i]; + + return -1; + } + + + static void Sortbinary2(int[] arr) { + int[] sortedArray = new int[arr.length]; + + int anfang = 0; + int ende = sortedArray.length -1; + for (int i = 0; i < arr.length; i++) { + if (arr[i] == 0) + sortedArray[anfang++] = arr[i]; + else + sortedArray[ende--] = arr[i]; + } + + for (int i :sortedArray) + System.out.print(i + " "); + + } + + static void Sortbinary(int[] arr) { + int[] sortedArray = new int[arr.length]; + + int laufindex = 0; + for (int i = 0; i < arr.length; i++) + if (arr[i] == 0) + sortedArray[laufindex++] = arr[i]; + + for (int i = 0; i < arr.length; i++) + if (arr[i] == 1) + sortedArray[laufindex++] = arr[i]; + + for (int i :sortedArray) + System.out.print(i + " "); + + } + + // Function to print all subarrays with a zero-sum + // in a given array + public static void printAllSubarrays(int[] nums) + { + // consider all subarrays starting from `i` + for (int i = 0; i < nums.length; i++) + { + int sum = 0; + + // consider all subarrays ending at `j` + for (int j = i; j < nums.length; j++) + { + // sum of elements so far + sum += nums[j]; + + // if the sum is seen before, we have found a subarray with zero-sum + if (sum == 0) { + System.out.println("Subarray [" + i + "…" + j + "]"); + } + } + } + } + + static int[] findAPair(int[] arr,int target) { + int[] pair = new int[2]; + for (int i = 0; i < arr.length;i++) { + pair[0] = arr[i]; + for (int j = i + 1; j < arr.length; j++) + if ((pair[0] + arr[j]) == target) { + pair[1] = arr[j]; + return pair; + } + } + + return null; + } + +}