Skip to content

Commit

Permalink
Added codes for 31 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 31, 2024
1 parent 137f9fb commit 18f0cfb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions GeeksForGeeks/May/31-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/31-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(1)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/May/31-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
20 changes: 20 additions & 0 deletions LeetCode/May/31-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 18f0cfb

Please sign in to comment.