给定两个整数N和M ,通过以下步骤生成N 个二进制字符串的序列:
- S 0 = “0”
- S 1 = “1”
- 通过等式S i = reverse(S i – 2 ) + reverse(S i – 1 )生成剩余的字符串
任务是在第N个字符串找到第M个设置位。
例子:
Input: N = 4, M = 3
Output: 0
Explanation:
S0 =”0″
S1 =”1″
S2 =”01″
S3 =”110″
S4 =”10011″
Therefore, the 3rd bit in S4 is ‘0’
Input: N = 5, M = 2
Output: 1
简单方法:最简单的方法是将生成S 2〜S N – 1和遍历字符串S N – 1找到第M位。
时间复杂度: O(N * 2 N)
辅助空间: O(N)
高效方法:按照以下步骤优化上述方法:
- 计算并将前 N 个斐波那契数存储在一个数组中,比如fib[]
- 现在,搜索第N个字符串中的第M位。
- 如果N> 1:考虑S N是反向的字符串S N串接– 2和的字符串S N反向– 1,字符串S N的长度– 2等于FIB [N – 2]和的长度字符串S N – 1等于fib[N – 1] 。
- 如果 M ≤ fib[n-2]:表示M位于S N – 2 中,因此,递归查找字符串S N – 2 的第(fib[N – 2] + 1 – M)位。
- 如果M> FIB [N – 2]:它标志着使得M在于S N – 1,因此,递归搜索(FIB [N – 1] + 1 – (M – FIB [N – 2]))个位的S N – 1 。
- 如果 N ≤ 1:返回N 。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
#define maxN 10
// Function to calculate N
// Fibonacci numbers
void calculateFib(int fib[], int n)
{
fib[0] = fib[1] = 1;
for (int x = 2; x < n; x++) {
fib[x] = fib[x - 1] + fib[x - 2];
}
}
// Function to find the mth bit
// in the string Sn
int find_mth_bit(int n, int m, int fib[])
{
// Base case
if (n <= 1) {
return n;
}
// Length of left half
int len_left = fib[n - 2];
// Length of the right half
int len_right = fib[n - 1];
if (m <= len_left) {
// Recursive check in the left half
return find_mth_bit(n - 2,
len_left + 1 - m, fib);
}
else {
// Recursive check in the right half
return find_mth_bit(
n - 1, len_right + 1
- (m - len_left),
fib);
}
}
void find_mth_bitUtil(int n, int m)
{
int fib[maxN];
calculateFib(fib, maxN);
int ans = find_mth_bit(n, m, fib);
cout << ans << ' ';
}
// Driver Code
int main()
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
static final int maxN = 10;
// Function to calculate N
// Fibonacci numbers
static void calculateFib(int fib[],
int n)
{
fib[0] = fib[1] = 1;
for (int x = 2; x < n; x++)
{
fib[x] = fib[x - 1] +
fib[x - 2];
}
}
// Function to find the mth bit
// in the String Sn
static int find_mth_bit(int n,
int m,
int fib[])
{
// Base case
if (n <= 1)
{
return n;
}
// Length of left half
int len_left = fib[n - 2];
// Length of the right half
int len_right = fib[n - 1];
if (m <= len_left)
{
// Recursive check in
// the left half
return find_mth_bit(n - 2,
len_left +
1 - m, fib);
}
else
{
// Recursive check in
// the right half
return find_mth_bit(n - 1,
len_right +
1 - (m -
len_left), fib);
}
}
static void find_mth_bitUtil(int n, int m)
{
int []fib = new int[maxN];
calculateFib(fib, maxN);
int ans = find_mth_bit(n, m, fib);
System.out.print(ans + " ");
}
// Driver Code
public static void main(String[] args)
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for above approach
maxN = 10
# Function to calculate N
# Fibonacci numbers
def calculateFib(fib, n):
fib[0] = fib[1] = 1
for x in range(2, n):
fib[x] = (fib[x - 1] +
fib[x - 2])
# Function to find the mth bit
# in the string Sn
def find_mth_bit(n, m, fib):
# Base case
if (n <= 1):
return n
# Length of left half
len_left = fib[n - 2]
# Length of the right half
len_right = fib[n - 1]
if (m <= len_left):
# Recursive check in the left half
return find_mth_bit(n - 2,
len_left + 1 - m, fib)
else:
# Recursive check in the right half
return find_mth_bit(n - 1,
len_right + 1 -
(m - len_left), fib)
def find_mth_bitUtil(n, m):
fib = [0 for i in range(maxN)]
calculateFib(fib, maxN)
ans = find_mth_bit(n, m, fib)
print(ans)
# Driver Code
if __name__ == '__main__':
n = 5
m = 3
find_mth_bitUtil(n, m)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
static int maxN = 10;
// Function to calculate N
// Fibonacci numbers
static void calculateFib(int []fib ,
int n)
{
fib[0] = fib[1] = 1;
for (int x = 2; x < n; x++)
{
fib[x] = fib[x - 1] +
fib[x - 2];
}
}
// Function to find the mth bit
// in the String Sn
static int find_mth_bit(int n,
int m,
int []fib)
{
// Base case
if (n <= 1)
{
return n;
}
// Length of left half
int len_left = fib[n - 2];
// Length of the right half
int len_right = fib[n - 1];
if (m <= len_left)
{
// Recursive check in
// the left half
return find_mth_bit(n - 2,
len_left +
1 - m, fib);
}
else
{
// Recursive check in
// the right half
return find_mth_bit(n - 1,
len_right +
1 - (m -
len_left), fib);
}
}
static void find_mth_bitUtil(int n,
int m)
{
int []fib = new int[maxN];
calculateFib(fib, maxN);
int ans = find_mth_bit(n, m, fib);
Console.Write(ans + " ");
}
// Driver Code
public static void Main()
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
}
}
// This code is contributed by Chitranayal
Javascript
输出:
1
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。