Smallest value of X not present in given Vector by searching X*K repeatedly
Last Updated :
15 Feb, 2022
Given a vector vec, and integers X and K, the task is to keep replacing X with product of X and K, each time X is found in the vector. Return the final product which is not present in the vector.
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: This problem can be solved by finding X in the vector repeatedly, and replace X with X*K each time it is found.
Follow the steps below to understand how:
- Traverse the array and check if X is present in the array or not.
- If found, replace X with X*K, and repeat step 1.
- If not found, break the loop.
- At the end of the loop, return the final value of X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findProductTillExist(vector< int >& vec, int x, int k)
{
int n = vec.size();
while ( true ) {
auto it = find(vec.begin(), vec.end(), x);
if (it != vec.end()) {
x *= k;
}
else
break ;
}
return x;
}
int main()
{
vector< int > vec = { 1, 2, 6, 10 };
int x = 2, k = 3;
cout << findProductTillExist(vec, x, k) << endl;
}
|
Java
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;
}
static int findProductTillExist( int [] vec, int x, int k) {
int n = vec.length;
while ( true ) {
int it = find(vec, x);
if (it != - 1 ) {
x *= k;
}
else
break ;
}
return x;
}
public static void main(String args[]) {
int [] vec = { 1 , 2 , 6 , 10 };
int x = 2 , k = 3 ;
System.out.println(findProductTillExist(vec, x, k));
}
}
|
Python3
def findProductTillExist(vec, x, k):
n = len (vec)
while ( True ):
it = x in vec
if (it):
x * = k
else :
break
return x
if __name__ = = "__main__" :
vec = [ 1 , 2 , 6 , 10 ]
x, k = 2 , 3
print (findProductTillExist(vec, x, k))
|
C#
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;
}
static int findProductTillExist( int [] vec, int x, int k)
{
int n = vec.Length;
while ( true ) {
int it = find(vec, x);
if (it != -1) {
x *= k;
}
else
break ;
}
return x;
}
public static void Main()
{
int [] vec = { 1, 2, 6, 10 };
int x = 2, k = 3;
Console.WriteLine(findProductTillExist(vec, x, k));
}
}
|
Javascript
<script>
function find(vec, x) {
let index = -1;
for (let i = 0; i < vec.length; i++) {
if (vec[i] == x) {
index = i;
}
}
return index
}
function findProductTillExist(vec, x, k)
{
let n = vec.length;
while ( true ) {
let it = find(vec, x);
if (it != -1) {
x *= k;
}
else
break ;
}
return x;
}
let vec = [1, 2, 6, 10];
let x = 2, k = 3;
document.write(findProductTillExist(vec, x, k) + '<br>' );
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...