Similar Problems

Similar Problems not available

Countries You Can Safely Invest In - Leetcode Solution

Companies:

LeetCode:  Countries You Can Safely Invest In Leetcode Solution

Difficulty: Medium

Topics: database  

Problem:

Given an array of countries and their corresponding political stability indexes, return the countries that have a political stability index of at least 8.

Example:

Input: countries = ["USA", "Germany", "Canada", "France", "Italy", "Spain"], politicalStability = [9, 8, 7, 8, 6, 9]

Output: ["USA", "Germany", "France", "Spain"]

Solution:

To solve this problem, we need to loop through the given arrays and check the political stability index of each country. If it is greater than or equal to 8, we add the name of that country to a result array.

We can implement this solution using a simple for loop and if condition. Here's the detailed solution:

def countries_to_invest(countries, politicalStability):

    # Create an empty array to store the result
    result = []

    # Loop through the countries array
    for i in range(len(countries)):
        # If the political stability index is greater than or equal to 8
        if politicalStability[i] >= 8:
            # Add the name of the country to the result array
            result.append(countries[i])

    # Return the result array
    return result

In this solution, we have created an empty array to store the result. Then, we have looped through the countries array using a for loop and checked the political stability index of each country using an if condition.

If the political stability index is greater than or equal to 8, we have added the name of that country to the result array. Finally, we have returned the result array.

The time complexity of this solution is O(n) where n is the length of the countries array.

Countries You Can Safely Invest In Solution Code

1