Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/eac3f60e7ae784f55d0fc6431138839a to your computer and use it in GitHub Desktop.
Save SuryaPratapK/eac3f60e7ae784f55d0fc6431138839a to your computer and use it in GitHub Desktop.
class Solution {
#define ll long long
public:
char kthCharacter(long long k, vector<int>& operations) {
int count_ops=0;
ll val = k;
while(val>1){
int jumps = ceil(log2(val));
val -= pow(2,jumps-1);
count_ops += operations[jumps-1];
}
return char('a'+(count_ops%26));
}
};
/*
//JAVA
import java.util.List;
class Solution {
public char kthCharacter(long k, List<Integer> operations) {
int countOps = 0;
long val = k;
while (val > 1) {
int jumps = (int) Math.ceil(Math.log(val) / Math.log(2));
val -= (long) Math.pow(2, jumps - 1);
countOps += operations.get(jumps - 1);
}
return (char) ('a' + (countOps % 26));
}
}
#Python
import math
class Solution:
def kthCharacter(self, k: int, operations: List[int]) -> str:
count_ops = 0
val = k
while val > 1:
jumps = math.ceil(math.log2(val))
val -= 2 ** (jumps - 1)
count_ops += operations[jumps - 1]
return chr(ord('a') + (count_ops % 26))
*/
@asifshaikh
Copy link

#Java Solution
public char kthCharacter(long k, int[] operations) {
int countOps =0;
long val =k;
while(val>1){
int jumps = (int) Math.ceil(Math.log(val)/Math.log(2));
val-= (long) Math.pow(2,jumps-1);
countOps += operations[jumps-1];
}
return (char)('a'+(countOps%26));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment