You are given an array (zero-indexed) of N non-negative integers, A 0 , A 1 ,…, A N-1 . Find the minimum sub-array A l , A l+1 ,…, A r so if we sort(in ascending order) that sub-array, then the whole array should get sorted. If A is already sorted, output -1 . Example : Input 1: A = [1, 3, 2, 4, 5] Return: [1, 2] Input 2: A = [1, 2, 3, 4, 5] Return: [-1] In the above example(Input 1), if we sort the subarray A 1 , A 2 , then whole array A should get sorted. ut Solution: class Solution: # @param A : list of integers # @return a list of integers def subUnsort(self, A): s_a=sorted(A) if A==s_a: return [-1] l=len(A) minI=-1 maxI=l ...
Problem link: Codechef Bharath loves to roam the campus of NITW and also he is good at problem-solving. Pratyush decided to give a problem to Bharath. Pratyush gives Bharath a list of places inside NITW (each place is represented by some character from 'a' to 'z'). Starting from the beginning, Bharath has to visit all the places in the same order as given in the list. While roaming Bharath writes the name of a place when he visits it for the first time. At the end of the day, Bharat will tell all the distinct places traveled by him during the entire day in the order he visited them. Input First-line will contain T, the number of test cases. The description of T test cases follows. The first and only line of each test case contains a string S (containing only lowercase alphabets). Output For each test case print the order of visit of Bharath. Constraints · 1 <= T <= 10 · ...
In this post, we’ll solve an interesting problem where we need to find the single element that appears only once in a list of integers, while all other elements appear exactly twice. Problem Explanation Consider a list of integers where every element except one appears twice. We want to efficiently find the element that occurs only once. Here’s an example: n = [23, 2, 2, 24, 7, 23, 5, 24, 5] In this list, every number except 7 appears twice. Our goal is to find that single occurrence using an efficient algorithm. Approach: Using XOR to Find the Single Element We can solve this problem in linear time O ( n ) O(n) O ( n ) and constant space O ( 1 ) O(1) O ( 1 ) using the XOR (bitwise exclusive OR) operation. How XOR Helps XOR Basics: XOR is a bitwise operation that compares the binary representation of two numbers. The key property of XOR is that when you XOR a number with itself, the result is 0 : n ⊕ n = 0 n \oplus n = 0 n ⊕ n = 0 XORing any number with 0 returns the number itself...
Comments
Post a Comment