-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
137f9fb
commit 18f0cfb
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(1) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |