Given a decimal number m, convert it into a binary string and apply n iterations. In each iteration, 0 becomes “01” and 1 becomes “10”. Find the (based on indexing) index character in the string after the nth iteration.
Examples:
Input : m = 5, n = 2, i = 3
Output : 1
Input : m = 3, n = 3, i = 6
Output : 1
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
- Change a decimal number into a binary and store it in string s.
- Run loop n times in each iteration. Run another loop of string length s to convert 0 to “01” and 1 to “10” and store in another string s1. After completion of each iteration, assign string s1 to s.
- Finally, return the value of the ith index in string s.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void binary_conversion(string &s, int m) {
while (m) {
int tmp = m % 2;
s += tmp + '0' ;
m = m / 2;
}
reverse(s.begin(), s.end());
}
int find_character( int n, int m, int i) {
string s;
binary_conversion(s, m);
string s1 = "" ;
for ( int x = 0; x < n; x++) {
for ( int y = 0; y < s.length(); y++) {
if (s[y] == '1' )
s1 += "10" ;
else
s1 += "01" ;
}
s = s1;
s1 = "" ;
}
return s[i] - '0' ;
}
int main() {
int m = 5, n = 2, i = 8;
cout << find_character(n, m, i);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static String s = "" ;
static String ReverseString(String s)
{
char [] arr = s.toCharArray();
for ( int i = 0 ;
i < arr.length / 2 ; i++)
{
char temp = arr[i];
arr[i] = arr[arr.length - i - 1 ];
arr[arr.length - i - 1 ] = temp;
}
return new String(arr);
}
static void binary_conversion( int m)
{
while (m != 0 )
{
int tmp = m % 2 ;
s += Integer.toString(tmp);
m = ( int )(m / 2 );
}
s = ReverseString(s);
}
static int find_character( int n,
int m,
int i)
{
binary_conversion(m);
String s1 = "" ;
for ( int x = 0 ; x < n; x++)
{
for ( int y = 0 ;
y < s.length(); y++)
{
if (s.charAt(y) == '1' )
s1 += "10" ;
else
s1 += "01" ;
}
s = s1;
s1 = "" ;
}
return s.charAt(i) - '0' ;
}
public static void main(String args[])
{
int m = 5 , n = 2 , i = 8 ;
System.out.print(
find_character(n, m, i));
}
}
|
Python3
def binary_conversion(s, m):
while (m):
temp = m % 2
s + = str (temp)
m = m / / 2
return s[:: - 1 ]
def find_character(n, m, i):
s = ""
s = binary_conversion(s, m)
s1 = ""
for x in range (n):
for j in range ( len (s)):
if s[j] = = "1" :
s1 + = "10"
else :
s1 + = "01"
s = s1
s1 = ""
e = ord (s[i])
r = ord ( '0' )
return e - r
m, n, i = 5 , 2 , 8
print (find_character(n,m,i))
|
C#
using System;
class GFG
{
static string ReverseString( string s)
{
char [] arr = s.ToCharArray();
Array.Reverse(arr);
return new string (arr);
}
static void binary_conversion( ref string s,
int m)
{
while (m != 0)
{
int tmp = m % 2;
s += tmp.ToString();
m = ( int )(m / 2);
}
s = ReverseString(s);
}
static int find_character( int n,
int m, int i)
{
string s = "" ;
binary_conversion( ref s, m);
string s1 = "" ;
for ( int x = 0; x < n; x++)
{
for ( int y = 0; y < s.Length; y++)
{
if (s[y] == '1' )
s1 += "10" ;
else
s1 += "01" ;
}
s = s1;
s1 = "" ;
}
return s[i] - '0' ;
}
static void Main()
{
int m = 5, n = 2, i = 8;
Console.Write(find_character(n, m, i));
}
}
|
Javascript
<script>
let s = "" ;
function ReverseString(s)
{
let arr = s.split( "" );
for (let i = 0;
i < arr.length / 2; i++)
{
let temp = arr[i];
arr[i] = arr[arr.length - i -1];
arr[arr.length - i - 1] = temp;
}
return arr.join( "" );
}
function binary_conversion(m)
{
while (m != 0)
{
let tmp = m % 2;
s += tmp.toString();
m = Math.floor(m / 2);
}
s = ReverseString(s);
}
function find_character(n,m,i)
{
binary_conversion(m);
let s1 = "" ;
for (let x = 0; x < n; x++)
{
for (let y = 0;
y < s.length; y++)
{
if (s[y] == '1' )
s1 += "10" ;
else
s1 += "01" ;
}
s = s1;
s1 = "" ;
}
return s[i] - '0' ;
}
let m = 5, n = 2, i = 8;
document.write(find_character(n, m, i));
</script>
|
Time complexity : O(n^2)
Auxiliary Space : O(n)
Refer Set-2 for an optimized solution.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!