📌  相关文章
📜  通过根据给定条件向左或向右旋转来减少给定的 [1, N] 数组

📅  最后修改于: 2022-05-13 01:56:09.241000             🧑  作者: Mango

通过根据给定条件向左或向右旋转来减少给定的 [1, N] 数组

给定一个前N个自然数的排序数组arr[]和一个整数X ,任务是在执行以下操作(N – 1)次后打印最后一个剩余元素:

  • 如果X的值为1 ,则将数组右旋1 个单位并删除最后一个元素。
  • 如果X的值为2 ,则将数组左旋1 个单位并删除第一个元素。

例子:

朴素方法:解决给定问题的最简单方法是将范围[1, N]的所有数字压入一个双端队列,并根据给定的X值执行给定的操作(N – 1)次,然后打印最后剩下的元素。

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

有效方法:可以根据以下观察优化给定问题:

  1. 如果X的值为1 ,那么最后一个左元素将是2的最小幂之间的差大于N N。
  2. 如果X的值为2 ,那么最后一个左元素将为2*(N – D) + 1,其中D表示小于或等于N2的最大幂。

请按照以下步骤解决问题:

  • 将大于N的 2 的幂存储在变量中,例如nextPower。
  • 如果X的值为1 ,则打印值(nextPower – N)作为结果。
  • 否则,打印值2*(N – nextPower / 2) + 1作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the last element
// left after performing N - 1 queries
// of type X
int rotate(int arr[], int N, int X)
{
    // Stores the next power of 2
    long long int nextPower = 1;
 
    // Iterate until nextPower is at most N
    while (nextPower <= N)
        nextPower *= 2;
 
    // If X is equal to 1
    if (X == 1)
        return nextPower - N;
 
    // Stores the power of 2 less than or
    // equal to N
    long long int prevPower = nextPower / 2;
 
    // Return the final value
    return 2 * (N - prevPower) + 1;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int X = 1;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << rotate(arr, N, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to find the last element
// left after performing N - 1 queries
// of type X
static int rotate(int arr[], int N, int X)
{
   
    // Stores the next power of 2
    long nextPower = 1;
 
    // Iterate until nextPower is at most N
    while (nextPower <= N)
        nextPower *= 2;
 
    // If X is equal to 1
    if (X == 1)
        return (int) nextPower - N;
 
    // Stores the power of 2 less than or
    // equal to N
    long prevPower = nextPower / 2;
 
    // Return the final value
    return 2 * (N - (int)prevPower) + 1;
}
 
// Driver Code
    public static void main (String[] args) {
        int arr[] = { 1, 2, 3, 4, 5 };
    int X = 1;
    int N =arr.length;
 
   System.out.println(rotate(arr, N, X));
 
    }
}
 
    // This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to find the last element
# left after performing N - 1 queries
# of type X
def rotate(arr, N, X):
    # Stores the next power of 2
    nextPower = 1
 
    # Iterate until nextPower is at most N
    while (nextPower <= N):
        nextPower *= 2
 
    # If X is equal to 1
    if (X == 1):
        ans = nextPower - N
        return ans
 
    # Stores the power of 2 less than or
    # equal to N
    prevPower = nextPower // 2
 
    # Return the final value
    return 2 * (N - prevPower) + 1
 
 
# Driver Code
arr = [1, 2, 3, 4, 5]
X = 1
N = len(arr)
print(rotate(arr, N, X))
 
# This code is contributed by amreshkumar3.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the last element
// left after performing N - 1 queries
// of type X
static int rotate(int []arr, int N, int X)
{
     
    // Stores the next power of 2
    int nextPower = 1;
 
    // Iterate until nextPower is at most N
    while (nextPower <= N)
        nextPower *= 2;
 
    // If X is equal to 1
    if (X == 1)
        return nextPower - N;
 
    // Stores the power of 2 less than or
    // equal to N
    int prevPower = nextPower / 2;
 
    // Return the final value
    return 2 * (N - prevPower) + 1;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 2, 3, 4, 5 };
    int X = 1;
    int N = arr.Length;
 
    Console.Write(rotate(arr, N, X));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出
3

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