703030. Rahul lucky-unlucky

Easy

Rahul Lucky-Unlucky

Summary

This problem is about determining whether a given array is considered "lucky" or "unlucky". A lucky array has at least one pair of adjacent elements that are equal, while an unlucky array does not have any such pairs. This problem involves basic array manipulation and pattern recognition.

Detailed Explanation

The problem can be solved by iterating through the array and checking for adjacent elements that are equal. If such a pair is found, the array is considered lucky; otherwise, it's considered unlucky. Here's a step-by-step breakdown of the solution:

  1. Initialize a variable lucky to true.
  2. Iterate through the array from left to right.
  3. For each element, check if it is equal to the previous element (if it exists).
  4. If such an equal pair is found, set lucky to false.
  5. After iterating through the entire array, return the value of lucky.

Time complexity: O(n), where n is the length of the input array. Space complexity: O(1), as only a constant amount of space is used.

Optimized Solutions

Java

View on GeeksforGeeks
public boolean rahulLucky(int[] arr) {
    boolean lucky = true;
    for (int i = 0; i < arr.length - 1; i++) {
        if (arr[i] == arr[i + 1]) {
            lucky = false;
            break;
        }
    }
    return lucky;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.