通过重复搜索 X*K,在给定向量中不存在 X 的最小值
给定一个向量vec和整数X和K ,任务是每次在向量中找到X时,用X和K的乘积替换X。返回向量中不存在的最终产品。
Examples:
Input: vec = {1, 2, 6, 10}, X = 2, K = 3
Output: 18
Explanation:
Since the original X is 2 which is present in the vector, multiply it by 3 (2*3=6) and store it in X(=6).
Now since 6 also exists in the vector, again multiply it by 3(6*3=18), and store it in X(=18).
Since 18 does not exists in the given vector, final value of X which is not present in the vector = 18.
Input: vec={1, 4, 3, 7, 9, 12, 6, 10}, X = 1, K=3
Output: 27
Explanation:
Since the original X is 1 which is present in the vector, multiply it by 3(1*3=3) and store it in X(=3).
Now since 3 also exists in the vector, again multiply it by 3(3*3=9), and store it in X(=9).
Now since 9 also exists in the vector, again multiply it by 3(9*3=27), and store it in X(=27).
Since 27 does not exists in the given vector, final value of X which is not present in the vector = 27.
Naive Approach:这个问题可以通过在向量中反复找X来解决,每次找到X都用X*K代替。
请按照以下步骤了解如何:
- 遍历数组并检查数组中是否存在 X。
- 如果找到,将 X 替换为 X*K,然后重复步骤 1。
- 如果没有找到,则中断循环。
- 在循环结束时,返回 X 的最终值。
下面是上述方法的实现:
C++
// C++ program to implement the above approach
#include
using namespace std;
// Function to check if X*K is present
// in the vector or not repeatedly
int findProductTillExist(vector& vec, int x, int k)
{
// Calculating the size of the vector vec
int n = vec.size();
// Since we have to keep checking
// and repeating the process we take
// a condition which always holds
while (true) {
// Get an iterator which checks
// if the X exists in the vector
// or not by using the find function
auto it = find(vec.begin(), vec.end(), x);
// If the iterator does not point
// to end it means x is present
// Hence multiply X by K
if (it != vec.end()) {
x *= k;
}
// If x is present in vector
// break the loop
else
break;
}
// Return the final value of x
return x;
}
// Driver code
int main()
{
vector vec = { 1, 2, 6, 10 };
int x = 2, k = 3;
cout << findProductTillExist(vec, x, k) << endl;
}
Java
// Java code for the above approach
class GFG {
static int find(int[] vec, int x) {
int index = -1;
for (int i = 0; i < vec.length; i++) {
if (vec[i] == x) {
index = i;
}
}
return index;
}
// Function to check if X*K is present
// in the vector or not repeatedly
static int findProductTillExist(int[] vec, int x, int k) {
// Calculating the size of the vector vec
int n = vec.length;
// Since we have to keep checking
// and repeating the process we take
// a condition which always holds
while (true) {
// Get an iterator which checks
// if the X exists in the vector
// or not by using the find function
int it = find(vec, x);
// If the iterator does not point
// to end it means x is present
// Hence multiply X by K
if (it != -1) {
x *= k;
}
// If x is present in vector
// break the loop
else
break;
}
// Return the final value of x
return x;
}
// Driver code
public static void main(String args[]) {
int[] vec = { 1, 2, 6, 10 };
int x = 2, k = 3;
System.out.println(findProductTillExist(vec, x, k));
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# python3 program to implement the above approach
# Function to check if X*K is present
# in the vector or not repeatedly
def findProductTillExist(vec, x, k):
# Calculating the size of the vector vec
n = len(vec)
# Since we have to keep checking
# and repeating the process we take
# a condition which always holds
while (True):
# Get an iterator which checks
# if the X exists in the vector
# or not by using the find function
it = x in vec
# If the iterator does not point
# to end it means x is present
# Hence multiply X by K
if (it):
x *= k
# If x is present in vector
# break the loop
else:
break
# Return the final value of x
return x
# Driver code
if __name__ == "__main__":
vec = [1, 2, 6, 10]
x, k = 2, 3
print(findProductTillExist(vec, x, k))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
class GFG {
static int find(int[] vec, int x)
{
int index = -1;
for (int i = 0; i < vec.Length; i++) {
if (vec[i] == x) {
index = i;
}
}
return index;
}
// Function to check if X*K is present
// in the vector or not repeatedly
static int findProductTillExist(int[] vec, int x, int k)
{
// Calculating the size of the vector vec
int n = vec.Length;
// Since we have to keep checking
// and repeating the process we take
// a condition which always holds
while (true) {
// Get an iterator which checks
// if the X exists in the vector
// or not by using the find function
int it = find(vec, x);
// If the iterator does not point
// to end it means x is present
// Hence multiply X by K
if (it != -1) {
x *= k;
}
// If x is present in vector
// break the loop
else
break;
}
// Return the final value of x
return x;
}
// Driver code
public static void Main()
{
int[] vec = { 1, 2, 6, 10 };
int x = 2, k = 3;
Console.WriteLine(findProductTillExist(vec, x, k));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
18
时间复杂度: O(N 2 )
辅助空间: O(1)