diff --git a/GeeksForGeeks/June/09-6-24/GFG.java b/GeeksForGeeks/June/09-6-24/GFG.java new file mode 100644 index 0000000..4eeb6e3 --- /dev/null +++ b/GeeksForGeeks/June/09-6-24/GFG.java @@ -0,0 +1,97 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + +class IntArray { + public static int[] input(BufferedReader br, int n) throws IOException { + String[] s = br.readLine().trim().split(" "); + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]); + + return a; + } + + public static void print(int[] a) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + + public static void print(ArrayList a) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } +} + +class GFG { + + public static boolean isZigzag(int n, int[] arr) { + int f = 1; + + for (int i = 1; i < n; i++) { + if (f == 1) { + if (arr[i - 1] > arr[i]) return false; + } else { + if (arr[i - 1] < arr[i]) return false; + } + f ^= 1; + } + + return true; + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t; + t = Integer.parseInt(br.readLine()); + while (t-- > 0) { + + int n; + n = Integer.parseInt(br.readLine()); + + int[] arr = IntArray.input(br, n); + + Solution obj = new Solution(); + obj.zigZag(n, arr); + boolean flag = false; + for (int i = 0; i < n; i++) { + if (arr[i] == i % 2) { + flag = false; + } else { + flag = true; + break; + } + } + if (!flag) { + System.out.println("0"); + } else { + + boolean check = isZigzag(n, arr); + if (check) { + System.out.println("1"); + } else { + System.out.println("0"); + } + } + } + } +} + +// } Driver Code Ends + + + +class Solution { + public static void zigZag(int n, int[] arr) { + // code here + for (int i = 1; i < n; i++) + if ((i % 2 == 1 && arr[i - 1] > arr[i]) || (i % 2 == 0 && arr[i - 1] < arr[i])) + swap(arr, i - 1, i); + } + + private static void swap(int[] arr, int i, int j) + { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} diff --git a/GeeksForGeeks/June/09-6-24/README.md b/GeeksForGeeks/June/09-6-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/GeeksForGeeks/June/09-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/GeeksForGeeks/June/10-6-24/GFG.java b/GeeksForGeeks/June/10-6-24/GFG.java new file mode 100644 index 0000000..5a5db93 --- /dev/null +++ b/GeeksForGeeks/June/10-6-24/GFG.java @@ -0,0 +1,50 @@ +//{ Driver Code Starts +// Initial Template for Java + +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int tc = Integer.parseInt(br.readLine().trim()); + while (tc-- > 0) { + String[] inputLine; + int n = Integer.parseInt(br.readLine().trim()); + char[] nuts = new char[n], bolts = new char[n]; + inputLine = br.readLine().trim().split(" "); + for (int i = 0; i < n; i++) { + nuts[i] = (inputLine[i].charAt(0)); + } + inputLine = br.readLine().trim().split(" "); + for (int i = 0; i < n; i++) { + bolts[i] = (inputLine[i].charAt(0)); + } + + new Solution().matchPairs(n, nuts, bolts); + for (int i = 0; i < n; i++) { + System.out.print(nuts[i] + " "); + } + System.out.println(); + for (int i = 0; i < n; i++) { + System.out.print(bolts[i] + " "); + } + System.out.println(); + } + } +} + +// } Driver Code Ends + + +// User function Template for Java + +class Solution { + void matchPairs(int n, char nuts[], char bolts[]) { + // code here + Arrays.sort(nuts); + + Arrays.sort(bolts); + } +} diff --git a/GeeksForGeeks/June/10-6-24/README.md b/GeeksForGeeks/June/10-6-24/README.md new file mode 100644 index 0000000..60370eb --- /dev/null +++ b/GeeksForGeeks/June/10-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(nlogn) +Space complexity - O(1) diff --git a/LeetCode/June/09-6-24/README.md b/LeetCode/June/09-6-24/README.md new file mode 100644 index 0000000..392ea8e --- /dev/null +++ b/LeetCode/June/09-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(k) diff --git a/LeetCode/June/09-6-24/Solution.java b/LeetCode/June/09-6-24/Solution.java new file mode 100644 index 0000000..3098adc --- /dev/null +++ b/LeetCode/June/09-6-24/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int subarraysDivByK(int[] nums, int k) { + int ans = 0; + int prefix = 0; + int[] count = new int[k]; + count[0] = 1; + + for (int num : nums) + { + prefix = (prefix + num % k + k) % k; + ans += count[prefix]; + ++count[prefix]; + } + + return ans; + } +} diff --git a/LeetCode/June/10-6-24/README.md b/LeetCode/June/10-6-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/June/10-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/June/10-6-24/Solution.java b/LeetCode/June/10-6-24/Solution.java new file mode 100644 index 0000000..39b4695 --- /dev/null +++ b/LeetCode/June/10-6-24/Solution.java @@ -0,0 +1,21 @@ +class Solution { + public int heightChecker(int[] heights) { + int ans = 0; + int currentHeight = 1; + int[] count = new int[101]; + + for (int height : heights) + ++count[height]; + + for (int height : heights) + { + while (count[currentHeight] == 0) + ++currentHeight; + if (height != currentHeight) + ++ans; + --count[currentHeight]; + } + + return ans; + } +}