-
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
0f7cc15
commit 91a5ef5
Showing
4 changed files
with
86 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,49 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
// } Driver Code Ends | ||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
// function to convert a given Gray equivalent n to Binary equivalent. | ||
public static int grayToBinary(int n) | ||
{ | ||
// Your code here | ||
int res = n; | ||
|
||
while (n > 0) | ||
{ | ||
n >>= 1; | ||
res ^= n; | ||
} | ||
|
||
return res; | ||
} | ||
} | ||
|
||
|
||
|
||
//{ Driver Code Starts. | ||
|
||
class GFG { | ||
|
||
public static void main (String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt();//testcases | ||
while(t-->0){ | ||
int n = sc.nextInt();//initializing n | ||
|
||
Solution obj = new Solution(); | ||
|
||
//calling grayToBinary() function | ||
System.out.println(obj.grayToBinary(n)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends |
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(logn) | ||
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(n) |
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,33 @@ | ||
class Solution | ||
{ | ||
public String removeKdigits(String num, int k) | ||
{ | ||
if (num.length() == k) | ||
return "0"; | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
LinkedList<Character> stack = new LinkedList<>(); | ||
|
||
for (int i = 0; i < num.length(); ++i) | ||
{ | ||
while (k > 0 && !stack.isEmpty() && stack.getLast() > num.charAt(i)) | ||
{ | ||
stack.pollLast(); | ||
--k; | ||
} | ||
stack.addLast(num.charAt(i)); | ||
} | ||
|
||
while (k-- > 0) | ||
stack.pollLast(); | ||
|
||
for (char c : stack) | ||
{ | ||
if (c == '0' && sb.length() == 0) | ||
continue; | ||
sb.append(c); | ||
} | ||
|
||
return sb.length() == 0 ? "0" : sb.toString(); | ||
} | ||
} |