📌  相关文章
📜  通过旋转二进制表示来最大化奇数和偶数索引数组元素之间的差异

📅  最后修改于: 2021-09-06 06:13:23             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是通过旋转任意次数的二进制表示,找到位于偶数索引处的数组元素和位于数组奇数索引处的数组元素之和之间的最大绝对差。仅考虑8 位表示。

例子:

方法:通过旋转每个数组元素的二进制表示并找到最大差异,可以通过最小化偶数或奇数索引处的元素并最大化其他索引的元素来解决给定的问题。请按照以下步骤解决问题:

  • 定义一个函数say Rotate(X, f)在旋转任何数字的二进制表示中的位后找到可能的数字的最大值和最小值。
    • 初始化两个变量,比如maxi = Xmini = X来存储可能的数字X的最大值和最小值。
    • 迭代数字X的位,然后通过执行以下操作旋转X的位:
      • 如果X为奇数,则将X的值更新为X >> 1X = X | (1<<7)
      • 否则,将X的值更新为X >> 1
    • maxi的值更新为maxiX的最大值。
    • mini的值更新为miniX的最小值。
    • 如果f 的值为1 ,则返回maxi 。否则,返回mini。
  • 现在,找到通过最大化放置在偶数索引处的元素和最小化放置在奇数索引处的元素获得的差异,并将该差异存储在一个变量中,比如caseOne
  • 现在,找到通过最小化放置在偶数索引处的元素并最大化放置在奇数索引处的元素获得的差异,并将该差异存储在一个变量中,例如caseTwo
  • 完成上述步骤后,打印caseOnecaseTwo的最大值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find maximum and
// minimum value of a number that
// can be obtained by rotating bits
int Rotate(int n, int f)
{
 
    // Stores the value of N
    int temp = n;
 
    // Stores the maximum value
    int maxi = n;
 
    // Stores the minimum value
    int mini = n;
 
    for (int idx = 0; idx < 7; idx++) {
 
        // If temp is odd
        if (temp & 1) {
            temp >>= 1;
            temp += pow(2, 7);
        }
 
        else
            temp >>= 1;
 
        // Update the maximum
        // and the minimum value
        mini = min(mini, temp);
        maxi = max(maxi, temp);
    }
 
    // If flag is 1, then
    // return the maximum value
    if (f)
        return (maxi);
 
    // Otherwise, return
    // the maximum value
    else
        return (mini);
}
 
// Function to find the maximum difference
// between the sum of odd and even-indexed
// array elements possible by rotating bits
int calcMinDiff(int arr[], int n)
{
 
    // Stores the maximum difference
    int caseOne = 0;
 
    // Stores the sum of elements
    // present at odd indices
    int sumOfodd = 0;
 
    // Stores the sum of elements
    // present at even indices
    int sumOfeven = 0;
 
    // Traverse the given array
    for (int i = 0; i < n; i++) {
 
        // If the index is even
        if (i % 2)
            sumOfodd += Rotate(arr[i], 0);
        else
            sumOfeven += Rotate(arr[i], 1);
    }
 
    // Update the caseOne
    caseOne = abs(sumOfodd - sumOfeven);
 
    // Stores the maximum difference
    int caseTwo = 0;
 
    // Stores the sum of elements
    // placed at odd positions
    sumOfodd = 0;
 
    // Stores the sum of elements
    // placed at even positions
    sumOfeven = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // If the index is even
        if (i % 2)
            sumOfodd += Rotate(arr[i], 1);
        else
            sumOfeven += Rotate(arr[i], 0);
    }
   
    // Update the caseTwo
    caseTwo = abs(sumOfodd - sumOfeven);
 
    // Return the maximum of caseOne
    // and caseTwo
    return max(caseOne, caseTwo);
}
 
// Driver Code
int main()
{
    int arr[] = { 123, 86, 234, 189 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (calcMinDiff(arr, n));
}
 
// This code is contributed by ukasp.


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find maximum and
// minimum value of a number that
// can be obtained by rotating bits
static int Rotate(int n, int f)
{
 
    // Stores the value of N
    int temp = n;
 
    // Stores the maximum value
    int maxi = n;
 
    // Stores the minimum value
    int mini = n;
 
    for (int idx = 0; idx < 7; idx++) {
 
        // If temp is odd
        if (temp %2 == 1) {
            temp >>= 1;
            temp += Math.pow(2, 7);
        }
 
        else
            temp >>= 1;
 
        // Update the maximum
        // and the minimum value
        mini = Math.min(mini, temp);
        maxi = Math.max(maxi, temp);
    }
 
    // If flag is 1, then
    // return the maximum value
    if (f==1)
        return (maxi);
 
    // Otherwise, return
    // the maximum value
    else
        return (mini);
}
 
// Function to find the maximum difference
// between the sum of odd and even-indexed
// array elements possible by rotating bits
static int calcMinDiff(int arr[], int n)
{
 
    // Stores the maximum difference
    int caseOne = 0;
 
    // Stores the sum of elements
    // present at odd indices
    int sumOfodd = 0;
 
    // Stores the sum of elements
    // present at even indices
    int sumOfeven = 0;
 
    // Traverse the given array
    for (int i = 0; i < n; i++) {
 
        // If the index is even
        if (i % 2==0)
            sumOfodd += Rotate(arr[i], 0);
        else
            sumOfeven += Rotate(arr[i], 1);
    }
 
    // Update the caseOne
    caseOne = Math.abs(sumOfodd - sumOfeven);
 
    // Stores the maximum difference
    int caseTwo = 0;
 
    // Stores the sum of elements
    // placed at odd positions
    sumOfodd = 0;
 
    // Stores the sum of elements
    // placed at even positions
    sumOfeven = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // If the index is even
        if (i % 2==0)
            sumOfodd += Rotate(arr[i], 1);
        else
            sumOfeven += Rotate(arr[i], 0);
    }
   
    // Update the caseTwo
    caseTwo = Math.abs(sumOfodd - sumOfeven);
 
    // Return the maximum of caseOne
    // and caseTwo
    return Math.max(caseOne, caseTwo);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 123, 86, 234, 189 };
    int n = arr.length;
    System.out.print((calcMinDiff(arr, n)));
}
}
 
// This code contributed by umadevi9616.


Python3
# Python program for the above approach
 
# Function to find maximum and
# minimum value of a number that
# can be obtained by rotating bits
def Rotate(n, f):
 
    # Stores the value of N
    temp = n
     
    # Stores the maximum value
    maxi = n
     
    # Stores the minimum value
    mini = n
 
    for idx in range(7):
       
        # If temp is odd
        if temp & 1:
            temp >>= 1
            temp += 2**7
             
        else:
            temp >>= 1
             
        # Update the maximum
        # and the minimum value
        mini = min(mini, temp)
        maxi = max(maxi, temp)
         
    # If flag is 1, then
    # return the maximum value
    if(f):
        return (maxi)
     
    # Otherwise, return
    # the maximum value
    else:
        return (mini)
 
# Function to find the maximum difference
# between the sum of odd and even-indexed
# array elements possible by rotating bits
def calcMinDiff(arr):
 
    # Stores the maximum difference
    caseOne = 0
     
    # Stores the sum of elements
    # present at odd indices
    sumOfodd = 0
 
    # Stores the sum of elements
    # present at even indices
    sumOfeven = 0
 
    # Traverse the given array
    for i in range(len(arr)):
       
        # If the index is even
        if i % 2:
            sumOfodd += Rotate(arr[i], 0)
        else:
            sumOfeven += Rotate(arr[i], 1)
             
    # Update the caseOne
    caseOne = abs(sumOfodd - sumOfeven)
 
    # Stores the maximum difference
    caseTwo = 0
     
    # Stores the sum of elements
    # placed at odd positions
    sumOfodd = 0
 
    # Stores the sum of elements
    # placed at even positions
    sumOfeven = 0
 
    # Traverse the array
    for i in range(len(arr)):
       
        # If the index is even
        if i % 2:
            sumOfodd += Rotate(arr[i], 1)
        else:
            sumOfeven += Rotate(arr[i], 0)
             
    # Update the caseTwo
    caseTwo = abs(sumOfodd - sumOfeven)
 
    # Return the maximum of caseOne
    # and caseTwo
    return max(caseOne, caseTwo)
 
 
# Driver Code
 
arr = [123, 86, 234, 189]
print(calcMinDiff(arr))


C#
// C# program for the above approach
 
using System;
 
public class GFG {
 
// Function to find maximum and
// minimum value of a number that
// can be obtained by rotating bits
static int Rotate(int n, int f)
{
 
    // Stores the value of N
    int temp = n;
 
    // Stores the maximum value
    int maxi = n;
 
    // Stores the minimum value
    int mini = n;
 
    for (int idx = 0; idx < 7; idx++) {
 
        // If temp is odd
        if (temp %2 == 1) {
            temp >>= 1;
            temp += (int)Math.Pow(2, 7);
        }
 
        else
            temp >>= 1;
 
        // Update the maximum
        // and the minimum value
        mini = Math.Min(mini, temp);
        maxi = Math.Max(maxi, temp);
    }
 
    // If flag is 1, then
    // return the maximum value
    if (f==1)
        return (maxi);
 
    // Otherwise, return
    // the maximum value
    else
        return (mini);
}
 
// Function to find the maximum difference
// between the sum of odd and even-indexed
// array elements possible by rotating bits
static int calcMinDiff(int[] arr, int n)
{
 
    // Stores the maximum difference
    int caseOne = 0;
 
    // Stores the sum of elements
    // present at odd indices
    int sumOfodd = 0;
 
    // Stores the sum of elements
    // present at even indices
    int sumOfeven = 0;
 
    // Traverse the given array
    for (int i = 0; i < n; i++) {
 
        // If the index is even
        if (i % 2==0)
            sumOfodd += Rotate(arr[i], 0);
        else
            sumOfeven += Rotate(arr[i], 1);
    }
 
    // Update the caseOne
    caseOne = Math.Abs(sumOfodd - sumOfeven);
 
    // Stores the maximum difference
    int caseTwo = 0;
 
    // Stores the sum of elements
    // placed at odd positions
    sumOfodd = 0;
 
    // Stores the sum of elements
    // placed at even positions
    sumOfeven = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // If the index is even
        if (i % 2==0)
            sumOfodd += Rotate(arr[i], 1);
        else
            sumOfeven += Rotate(arr[i], 0);
    }
   
    // Update the caseTwo
    caseTwo = Math.Abs(sumOfodd - sumOfeven);
 
    // Return the maximum of caseOne
    // and caseTwo
    return Math.Max(caseOne, caseTwo);
}
 
    // Driver Code
    public static void Main(string[] args)
    {
 
        int[] arr = { 123, 86, 234, 189 };
    int n = arr.Length;
     Console.WriteLine((calcMinDiff(arr, n)));
    }
}
 
// This code is contributed by splevel62.


Javascript


输出:
326

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live