📜  如果我们在数组中每次成功搜索后加倍,则找到最终值

📅  最后修改于: 2021-04-23 05:53:37             🧑  作者: Mango

给定一个数组和一个整数k,遍历该数组,如果数组中的元素为k,则将k的值加倍并继续遍历。最后返回k的值。
例子:

Input : arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2
Output: 16
Explanation:
First k = 2 is found, then we search for 4
which is also found, then we search for 8
which is also found, then we search for 16.
 
Input : arr[] = { 2, 4, 5, 6, 7 }, k = 3
Output: 3

方法– 1 :(蛮力)

1)如果arr [i] == k,则遍历数组的每个元素,然后k = 2 * k。
2)对k的最大值重复相同的过程。
3)最后返回k的值。

方法– 2 :(排序和搜索)

1)对数组进行排序
2)然后,您可以在一个循环中搜索元素,因为我们确定k * 2将在此数组中的k之后。因此,只需在循环中将k的值相乘即可。

C++
// CPP program to find value if we double
// the value after every successful search
#include 
using namespace std;
 
// Function to Find the value of k
int findValue(int a[], int n, int k)
{
 
    // Sort the array
    sort(a, a + n);
 
    // Search for k. After every successful
    // search, double k.
    for (int i = 0; i < n; i++) {
         
        // Check is a[i] is equal to k
        if (a[i] == k)
            k *= 2;
    }
 
    return k;
}
 
// Driver's Code
int main()
{
    int arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findValue(arr, n, k);
    return 0;
}


Java
// Java program to find value
// if we double  the value after
// every successful search
 
class GFG {
    // Function to Find the value of k
    static int findValue(int arr[], int n, int k)
    {
 
        // Search for k. After every successful
        // search, double k.
        for (int i = 0; i < n; i++)
            if (arr[i] == k)
                k *= 2;
 
        return k;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;
        int n = arr.length;
        System.out.print(findValue(arr, n, k));
    }
}
// This code is contriubted by
// Smitha Dinesh Semwal


Python3
# Python program to find
# value if we double
# the value after every
# successful search
 
# Function to Find the value of k
 
 
def findValue(arr, n, k):
 
    # Search for k.
    # After every successful
    # search, double k.
    for i in range(n):
        if (arr[i] == k):
            k = k * 2
 
    return k
 
# Driver's Code
 
 
arr = [2, 3, 4, 10, 8, 1]
k = 2
n = len(arr)
 
print(findValue(arr, n, k))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find value
// if we double the value after
// every successful search
using System;
 
class GFG {
 
    // Function to Find the value of k
    static int findValue(int[] arr, int n, int k)
    {
 
        // Search for k. After every successful
        // search, double k.
        for (int i = 0; i < n; i++)
            if (arr[i] == k)
                k *= 2;
 
        return k;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 3, 4, 10, 8, 1 };
        int k = 2;
        int n = arr.Length;
 
        Console.WriteLine(findValue(arr, n, k));
    }
}
 
// This code is contriubted by vt_m.


PHP


Javascript


C++
// CPP program for the above approach
#include 
using namespace std;
 
// Function to find the value
int findValue(int a[], int n, int k)
{
     
    // Unordered Map
    unordered_set m;
   
    // Iterate from 0 to n - 1
    for (int i = 0; i < n; i++)
        m.insert(a[i]);
 
    while (m.find(k) != m.end())
        k = k * 2;
 
    return k;
}
 
// Driver's Code
int main()
{
    int arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findValue(arr, n, k);
    return 0;
}


输出:

16

时间复杂度:O(nlogn)
方法– 3 :(散列)

1)将所有元素放入哈希图中。

2)搜索k是否在哈希图中,如果是,则将该值乘以k或返回k的值。

C++

// CPP program for the above approach
#include 
using namespace std;
 
// Function to find the value
int findValue(int a[], int n, int k)
{
     
    // Unordered Map
    unordered_set m;
   
    // Iterate from 0 to n - 1
    for (int i = 0; i < n; i++)
        m.insert(a[i]);
 
    while (m.find(k) != m.end())
        k = k * 2;
 
    return k;
}
 
// Driver's Code
int main()
{
    int arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findValue(arr, n, k);
    return 0;
}

时间复杂度: O(n)

空间复杂度: O(n)

?list = PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p
参考: href =“” https://www.geeksforgeeks.org/flipkart-interview-experience-set-35-on-campus-for-sde-1/”