From 18f0cfb2ef95c91ecfa5088a1670caba1bb1a9f1 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Fri, 31 May 2024 18:07:44 +0530 Subject: [PATCH] Added codes for 31 May --- GeeksForGeeks/May/31-5-24/GFG.java | 26 ++++++++++++++++++++++++++ GeeksForGeeks/May/31-5-24/README.md | 2 ++ LeetCode/May/31-5-24/README.md | 2 ++ LeetCode/May/31-5-24/Solution.java | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 GeeksForGeeks/May/31-5-24/GFG.java create mode 100644 GeeksForGeeks/May/31-5-24/README.md create mode 100644 LeetCode/May/31-5-24/README.md create mode 100644 LeetCode/May/31-5-24/Solution.java diff --git a/GeeksForGeeks/May/31-5-24/GFG.java b/GeeksForGeeks/May/31-5-24/GFG.java new file mode 100644 index 0000000..240f543 --- /dev/null +++ b/GeeksForGeeks/May/31-5-24/GFG.java @@ -0,0 +1,26 @@ +//{ Driver Code Starts +// Initial Template for Java +import java.io.*; +import java.util.*; + +class GFG { + public static void main(String args[]) throws IOException { + BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(read.readLine()); + while (t-- > 0) { + int n = Integer.parseInt(read.readLine()); + Solution ob = new Solution(); + System.out.println(ob.swapNibbles(n)); + } + } +} +// } Driver Code Ends + + +// User function Template for Java +class Solution { + static int swapNibbles(int n) { + // code here + return ((n & 0x0F) << 4 | (n & 0xF0) >> 4); + } +} diff --git a/GeeksForGeeks/May/31-5-24/README.md b/GeeksForGeeks/May/31-5-24/README.md new file mode 100644 index 0000000..b85bed6 --- /dev/null +++ b/GeeksForGeeks/May/31-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(1) +Space complexity - O(1) diff --git a/LeetCode/May/31-5-24/README.md b/LeetCode/May/31-5-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/May/31-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/May/31-5-24/Solution.java b/LeetCode/May/31-5-24/Solution.java new file mode 100644 index 0000000..0996810 --- /dev/null +++ b/LeetCode/May/31-5-24/Solution.java @@ -0,0 +1,20 @@ +class Solution +{ + public int[] singleNumber(int[] nums) + { + int xors = Arrays.stream(nums).reduce((a, b) -> a ^ b).getAsInt(); + int lowbit = xors & -xors; + int[] ans = new int[2]; + + // Seperate `nums` into two groups by `lowbit`. + for (int num : nums) + { + if ((num & lowbit) > 0) + ans[0] ^= num; + else + ans[1] ^= num; + } + + return ans; + } +}