# Kth character after replacing each character of String by its frequency exactly X times

Given a string** S **consisting of **N** digits from [1, 9] and a positive integer** K **and **X**. Every day each character of the string is replaced by its frequency and value. The task is to find the **Kth** character of the string after **X** days.

**Examples:**

Input:S = “1214”, K = 10, X = 3Output:4Explanation:

1st day = “12214444”

2nd day = “1222214444444444444444”

3rd day = “122222222444444444444444444444444444444444444444444444444”

So, 10th character after 3rd day is 4.

Input:S =”123″, K = 6, X = 2Output:3

**Naive Approach: **The simplest approach to solve the problem is to create the string by appending the digits in the string digit^{X} times and find the Kth character of the string.

**Time Complexity: **O(∑digit[i]^{X}) for i in range [0, N-1]

**Efficient Approach: **The above approach can be optimized further by instead of creating a string add **digit ^{X}** to the sum and check if

**sum > K**. Follow the steps below to solve the problem:

- Initialize a variable, say
**sum**that stores the sum of the character of the string after**X**days. - Initialize a variable, say
**ans**that stores the**Kth**characters after**X**days. - Iterate in the range
**[0, N-1]**and perform the following steps:- Initialize a variable say
**range**as**(S[i] – ‘0’)**and add it to the variable^{X}**sum**. - If
**sum>=K**, return**S**, modify**ans**as**S[i]**, and terminate the loop.

- Initialize a variable say
- Print the value of
**ans**as the answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the Kth character` `// after X days` `char` `FindKthChar(string str, ` `long` `long` `K, ` `int` `X)` `{` ` ` `// Variable to store the KthChar` ` ` `char` `ans;` ` ` `int` `sum = 0;` ` ` `// Traverse the string` ` ` `for` `(` `int` `i = 0; i < str.length(); i++) {` ` ` `// Convert char into int` ` ` `int` `digit = str[i] - ` `'0'` `;` ` ` `// Calculate characters` ` ` `int` `range = ` `pow` `(digit, X);` ` ` `sum += range;` ` ` `// If K is less than sum` ` ` `// than ans = str[i]` ` ` `if` `(K <= sum) {` ` ` `ans = str[i];` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Return answer` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Input` ` ` `string str = ` `"123"` `;` ` ` `long` `long` `K = 9;` ` ` `int` `X = 3;` ` ` `// Function Call` ` ` `char` `ans = FindKthChar(str, K, X);` ` ` `cout << ans << ` `"\n"` `;` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG{` `// Function to find the Kth character` `// after X days` `static` `char` `FindKthChar(String str, ` `int` `K, ` `int` `X)` `{` ` ` ` ` `// Variable to store the KthChar` ` ` `char` `ans = ` `' '` `;` ` ` `int` `sum = ` `0` `;` ` ` `// Traverse the string` ` ` `for` `(` `int` `i = ` `0` `; i < str.length(); i++)` ` ` `{` ` ` ` ` `// Convert char into int` ` ` `int` `digit = (` `int` `)str.charAt(i) - ` `48` `;` ` ` `// Calculate characters` ` ` `int` `range = (` `int` `)Math.pow(digit, X);` ` ` `sum += range;` ` ` `// If K is less than sum` ` ` `// than ans = str[i]` ` ` `if` `(K <= sum)` ` ` `{` ` ` `ans = str.charAt(i);` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Return answer` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given Input` ` ` `String str = ` `"123"` `;` ` ` `int` `K = ` `9` `;` ` ` `int` `X = ` `3` `;` ` ` `// Function Call` ` ` `char` `ans = FindKthChar(str, K, X);` ` ` `System.out.println(ans);` `}` `}` `// This code is contributed by abhinavjain194` |

## Python3

`# Python3 program for the above approach` `import` `math` `# Function to find the Kth character` `# after X days` `def` `FindKthChar(` `Str` `, K, X):` ` ` ` ` `# Variable to store the KthChar` ` ` `ans ` `=` `' '` ` ` `Sum` `=` `0` ` ` ` ` `# Traverse the string` ` ` `for` `i ` `in` `range` `(` `len` `(` `Str` `)):` ` ` ` ` `# Convert char into int` ` ` `digit ` `=` `ord` `(` `Str` `[i]) ` `-` `48` ` ` ` ` `# Calculate characters` ` ` `Range` `=` `int` `(math.` `pow` `(digit, X))` ` ` `Sum` `+` `=` `Range` ` ` ` ` `# If K is less than sum` ` ` `# than ans = str[i]` ` ` `if` `(K <` `=` `Sum` `):` ` ` `ans ` `=` `Str` `[i]` ` ` `break` ` ` ` ` `# Return answer` ` ` `return` `ans` `# Given Input` `Str` `=` `"123"` `K ` `=` `9` `X ` `=` `3` `# Function Call` `ans ` `=` `FindKthChar(` `Str` `, K, X)` `print` `(ans)` `# This code is contributed by divyeshrabadiya07.` |

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` `// Function to find the Kth character` `// after X days` `static` `char` `FindKthChar(` `string` `str, ` `int` `K, ` `int` `X)` `{` ` ` ` ` `// Variable to store the KthChar` ` ` `char` `ans = ` `' '` `;` ` ` `int` `sum = 0;` ` ` `// Traverse the string` ` ` `for` `(` `int` `i = 0; i < str.Length; i++)` ` ` `{` ` ` ` ` `// Convert char into int` ` ` `int` `digit = (` `int` `)str[i] - 48;` ` ` `// Calculate characters` ` ` `int` `range = (` `int` `)Math.Pow(digit, X);` ` ` `sum += range;` ` ` `// If K is less than sum` ` ` `// than ans = str[i]` ` ` `if` `(K <= sum)` ` ` `{` ` ` `ans = str[i];` ` ` `break` `;` ` ` `}` ` ` `}` ` ` ` ` `// Return answer` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` ` ` `// Given Input` ` ` `string` `str = ` `"123"` `;` ` ` `int` `K = 9;` ` ` `int` `X = 3;` ` ` `// Function Call` ` ` `char` `ans = FindKthChar(str, K, X);` ` ` `Console.Write(ans);` `}` `}` `// This code is contributed by SURENDRA_GANGWAR` |

## Javascript

`<script>` `// javascript program for the above approach ` `// Function to find the Kth character` ` ` `// after X days` ` ` `function` `FindKthChar( str , K , X) {` ` ` `// Variable to store the KthChar` ` ` `var` `ans = ` `""` `;` ` ` `var` `sum = 0;` ` ` `// Traverse the string` ` ` `for` `(i = 0; i < str.length; i++) {` ` ` `// Convert char into int` ` ` `var` `digit = parseInt( str[i]);` ` ` `// Calculate characters` ` ` `var` `range = parseInt( Math.pow(digit, X));` ` ` `sum += range;` ` ` `// If K is less than sum` ` ` `// than ans = str[i]` ` ` `if` `(K <= sum) {` ` ` `ans = str[i];` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Return answer` ` ` `return` `ans;` ` ` `}` ` ` `// Driver code` ` ` ` ` `// Given Input` ` ` `var` `str = ` `"123"` `;` ` ` `var` `K = 9;` ` ` `var` `X = 3;` ` ` `// Function Call` ` ` `var` `ans = FindKthChar(str, K, X);` ` ` `document.write(ans);` `// This code contributed by gauravrajput1` `</script>` |

**Output**

2

**Time Complexity: **O(N), since there is only one loop to carry out the operations the overall complexity turns out to be O(N)**Auxiliary Space: **O(1), since there is no extra array or data structure used, it takes constant space.