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:
- Initialize a variable
luckytotrue. - Iterate through the array from left to right.
- For each element, check if it is equal to the previous element (if it exists).
- If such an equal pair is found, set
luckytofalse. - 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
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;
}