Skip to main content

My First Week in India: Expectations vs. Reality

My First Week in India: Expectations vs. Reality My First Week in India: Expectations vs. Reality Arriving in a new country is always a mix of excitement and apprehension. As I embarked on my journey to India, I had a set of expectations based on research, stories, and stereotypes. My first week in India has been a whirlwind of experiences that have both aligned with and diverged from my expectations. Here’s a comparison of what I anticipated versus what I actually encountered. Expectations Warm and Humid Weather: I expected the weather to be consistently warm and humid, as is often depicted in travel blogs and movies. Spicy Food: I anticipated a diet heavily spiced with rich and diverse flavors, given India’s renowned culinary reputation. Cultural Festivities: I was excited to witness vibrant cultural festivals and traditional celebrations throughout the year. ...

33. Search in Rotated Sorted Array

 33. Search in Rotated Sorted Array


📄 Problem Statement:

There is an integer array nums sorted in ascending order (with distinct values).

Before being passed to your function, nums may be rotated at an unknown pivot index k (where 1 <= k < nums.length) such that the array becomes [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]].

You are given the array nums after the rotation and an integer target. Your task is to return the index of the target if it exists in nums, or return -1 if it does not exist.

You must solve it in O(log n) time.


📚 Examples:

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

Example 3:

Input: nums = [1], target = 0
Output: -1

📜 Constraints:

  • 1 <= nums.length <= 5000

  • -10⁴ <= nums[i] <= 10⁴

  • All values of nums are unique.

  • nums is a sorted array, possibly rotated.

  • -10⁴ <= target <= 10⁴



class Solution {
    public int search(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            if(nums[i]==target){
                return i;
            }
        }
        return -1;
    }
}

Comments

Popular posts from this blog

How to Improve Your Communication Skills: Essential Tips for Effective Interaction

How to Improve Your Communication Skills: Essential Tips for Effective Interaction How to Improve Your Communication Skills: Essential Tips for Effective Interaction Effective communication is an essential skill that influences every aspect of our lives, from personal relationships to professional success. Improving your communication skills can lead to better interactions, clearer understanding, and more meaningful connections. This comprehensive guide provides detailed strategies and practical tips to help you enhance your communication abilities. 1. Listen Actively Active listening is the foundation of effective communication. It involves fully concentrating, understanding, responding, and then remembering what is being said. Here are some ways to practice active listening: Give Full Attention: Avoid distractions such as checking your phone or thinking about what you'll say next. Focus entirely on the speaker. ...

217. Contains Duplicate

217. Contains Duplicate Difficulty: Easy Problem Statement Given an integer array nums , return true if any value appears at least twice in the array, and return false if every element is distinct . Example 1: Input: nums = [1, 2, 3, 1] Output: true Explanation: The element 1 appears more than once (at indices 0 and 3). Example 2: Input: nums = [1, 2, 3, 4] Output: false Explanation: All elements are unique. Example 3: Input: nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2] Output: true Explanation: Several elements appear multiple times: 1 , 3 , 4 , and 2 . Constraints: 1 <= nums.length <= 10⁵ -10⁹ <= nums[i] <= 10⁹ Solution:   import java.util.Arrays; class Solution {     public boolean containsDuplicate ( int [] nums ) {         Arrays . sort (nums); // Sort the array         for ( int i = 1 ; i < nums . length ; i++) {             if (nums[i]...