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 Crack College Placements: A Complete Guide to Projects, Interviews, and Success

How to Crack College Placements: A Complete Guide to Projects, Interviews, and Success How to Crack College Placements: A Complete Guide to Projects, Interviews, and Success Welcome to the first episode of The Mali Show ! If you're a student preparing for college placements , you're probably feeling both excited and nervous. Landing that dream job or internship is a big deal, but with the right approach, cracking campus placements doesn’t have to be a stressful experience. In this blog post, we’ll break down everything you need to know about how to crack college placements , from selecting the perfect placement projects to preparing for those nerve-wracking interviews . With these tips, you’ll be one step closer to success. 1. How to Crack College Placements: The Basics When it comes to college placements , understanding the entire process is key. Every college has its own str...

Chocolate Distribution Problem

Chocolate Distribution Problem Given an array  arr[]  of positive integers, where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are  m  students, the task is to distribute chocolate packets among  m  students such that -       i. Each student gets  exactly  one packet.      ii. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum and return that minimum possible difference. Examples: Input: arr = [3, 4, 1, 9, 56, 7, 9, 12], m = 5 Output: 6 Explanation: The minimum difference between maximum chocolates and minimum chocolates is 9 - 3 = 6 by choosing following m packets :[3, 4, 9, 7, 9]. Input: arr = [7, 3, 2, 4, 9, 12, 56], m = 3 Output: 2 Explanation: The minimum difference between maximum chocolates and minimum chocolates is 4 - 2 = 2 by choosing following m packe...