Sort by

recency

|

37 Discussions

|

  • + 0 comments
    def anotherMinimaxProblem(a):
        if all(x == 0 for x in a): 
            return 0
    
        n = len(a)
        max_bit = max(a).bit_length()
    
        for bit in range(max_bit - 1, -1, -1):
            ones, zeros = [], []
            for num in a:
                if num & (1 << bit):
                    ones.append(num)
                else:
                    zeros.append(num)
            if zeros and ones:
                break
    
        if not zeros or not ones:
            return min(a[i] ^ a[j] for i in range(n) for j in range(i + 1, n))
    
        return min(x ^ y for x in zeros for y in ones)
    
  • + 0 comments

    Hey there again, I ended up taking the piece to chat GBT bot to help me optimize my code. I am not gonna lie it felt good that it failed miserably than myself. Then I used somebody elses code (mpurbey204) and told the bot to implement it to python -- and let me tell you, mpurbey204's code work real nice. Here's the analyze the bot made about the code in steps for newbies like myself:

    The provided code uses bit manipulation and sorting to find the minimum XOR value. Here's a breakdown of the code's logic:

    Sort the input array a in ascending order using std::sort. Create a set s to store unique elements from the sorted array a. If the size of the set s is 1 (indicating all elements in a are the same), return 0 as the minimum XOR value. Create a 2D array b of size n x 32 (where n is the size of a) to store the binary representation of each element in a. Iterate through each element in a and convert it to binary representation using bitwise operations. Store the binary representation in the array b. Iterate through the bits of the binary representation from the most significant bit (MSB) to the least significant bit (LSB). For each bit position, check if there is a difference between consecutive elements in the array b. If a difference is found, set r as the index of the first differing element and c as 1 to indicate a difference was found. Find the minimum XOR value by iterating over the elements before r and after r. Update the ans variable with the minimum XOR value. Return the minimum XOR value stored in ans.

    def anotherMinimaxProblem(a):
        n = len(a)
        a.sort()
        s = set(a)
    
        if len(s) == 1:
            return 0
    
        b = [[0] * 32 for _ in range(n)]
        r, c = 0, 0
    
        for i in range(n):
            for j in range(31, -1, -1):
                x = a[i]
                bit = (x >> j) & 1
                b[i][31 - j] = bit
    
        for i in range(31):
            for j in range(n - 1):
                if b[j][i] != b[j + 1][i]:
                    r = j
                    c = 1
                    break
            if c == 1:
                break
    
        ans = float('inf')
    
        for i in range(r + 1):
            for j in range(r + 1, n):
                ans = min(ans, a[i] ^ a[j])
    
        return ans
    
    
    
  • + 0 comments

    Hey there, I happen to have a very simple solution in python. It works but has a time exceeding problem and needs optimization. Anyone who has an idea to optimize the inverted loops?

    `def anotherMinimaxProblem(a):
    # Write your code here
    
    max_lst = []
    min_lst = []
    temp = []
    n = len(a)
    perm = permutations(a)
    for i in perm:
        for j in range(n-1):
            val = i[j] ^ i[j+1]
            temp.append(val)
        max_lst.append(max(temp))
        temp = []
    return min(max_lst)
            `
    
  • + 0 comments

    Here is Yet Anothe Minimax problem solution in Python JAVA C++ AND C PROGRAMMING - https://programs.programmingoneonone.com/2021/07/hackerrank-yet-another-minimax-problem-solution.html

  • + 0 comments

    help,my c# solution only has 19/23 test failed .Does it has anything to do with using List?

    public static int anotherMinimaxProblem(List a) { int max = int.MaxValue; for (int i = 0; i < a.Count; i++) {

        int maxima = 0;
        for (int j = 0; j < a.Count; j++)
        {
            if (i == j)
            {
                continue;
            }
            int answer = a[i] ^ a[j];
            if (answer > maxima)
            {
                maxima = answer;
            }
        }
        if (maxima < max)
        {
            max = maxima;
        }
    }
    return max;
    

    }