Open In App

Smallest value of X not present in given Vector by searching X*K repeatedly

Last Updated : 15 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Traverse the array and check if X is present in the array or not.
  2. If found, replace X with X*K, and repeat step 1.
  3. If not found, break the loop.
  4. At the end of the loop, return the final value of X.

Below is the implementation of the above approach: 

C++




// C++ program to implement the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if X*K is present
// in the vector or not repeatedly
int findProductTillExist(vector<int>& 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<int> 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




<script>
       // JavaScript code for the above approach
       function find(vec, x) {
           let index = -1;
           for (let 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
       function findProductTillExist(vec, x, k)
       {
        
           // Calculating the size of the vector vec
           let 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
               let 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
 
       let vec = [1, 2, 6, 10];
       let x = 2, k = 3;
 
       document.write(findProductTillExist(vec, x, k) + '<br>');
 
      // This code is contributed by Potta Lokesh
   </script>


 
 

Output

18

 

Time Complexity: O(N2)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads