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; + } +}