Skip to content

Commit

Permalink
Added codes for 11 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 11, 2024
1 parent 0f7cc15 commit 91a5ef5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
49 changes: 49 additions & 0 deletions GeeksForGeeks/April/11-4-24/GFG.java
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
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/11-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/April/11-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
33 changes: 33 additions & 0 deletions LeetCode/April/11-4-24/Solution.java
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();
}
}

0 comments on commit 91a5ef5

Please sign in to comment.