Find last element of Array by rotating and deleting N-K+1 element
Last Updated :
06 Dec, 2022
Given an array arr[] of N integers, the task is to find the element which is left at last after performing the following operation N – 1 time. For every Kth operation:
- Right-rotate the array clockwise by 1.
- Delete the (n – K + 1)th last element.
Example:
Input: N = 6, arr[] = {1, 2, 3, 4, 5, 6}
Output: 3
Explanation: Rotate the array clockwise i.e. after rotation the array A = {6, 1, 2, 3, 4, 5} and delete the last element that is {5} that will be A = {6, 1, 2, 3, 4}.
Again rotate the array for the second time and deletes the second last element that is {2} that will be A = {4, 6, 1, 3}, doing similar operation when we perform 4th operation, 4th last element does not exist. Then we deletes 1st element ie {1} that will be A = {3, 6}. So, continuing this procedure the last element in A is {3}.
So, the output will be 3.
Input: N = 4, arr = {1, 2, 3, 4}
Output: 3
Approach: To solve the problem follow the steps as mentioned:
Do N – 1 operation and for each operation Right rotate the array clockwise by 1 and Delete the (N – K + 1)th last element then finally return the first element which left in the array.
Follow the steps below to implement the above idea:
- Initialize a variable K = 1, for counting the number of operations done till now
- Do while K is less than the size of the array.
- Do right rotation
- Erase the N – K + 1 element
- Update the current size of the array
- Increment the value of K by 1
- Return the first left element of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int rotateDelete(vector< long long >& v, int n)
{
int k = 1;
while (k < n) {
rotate(v.begin(), v.begin() + v.size() - 1,
v.end());
v.erase(v.begin() + n - k);
n = v.size();
k++;
}
return v[0];
}
int main()
{
vector< long long > arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.size();
cout << rotateDelete(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int rotateDelete(ArrayList<Integer> v,
int n)
{
int k = 1 ;
while (k < n) {
Collections.rotate(v, 1 );
v.remove(v.size() - k);
n = v.size();
k++;
}
return v.get( 0 );
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add( 1 );
arr.add( 2 );
arr.add( 3 );
arr.add( 4 );
arr.add( 5 );
arr.add( 6 );
int N = arr.size();
System.out.print(rotateDelete(arr, N));
}
}
|
Python3
def rotateDelete(v, n):
k = 1
while (k < n):
v = v[ len (v) - 1 : len (v)] + v[ 0 : len (v) - 1 ]
v.pop(n - k)
n = len (v)
k + = 1
return v[ 0 ]
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
print (rotateDelete(arr, N))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG
{
public static void rotate( int [] arr, int d, int n)
{
int [] temp = new int [n];
var k = 0;
for ( int i = d; i < n; i++) {
temp[k] = arr[i];
k++;
}
for ( int i = 0; i < d; i++) {
temp[k] = arr[i];
k++;
}
for ( int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
public static int rotateDelete( int [] v, int n)
{
int k = 1;
while (k < n) {
int l = v.Length;
rotate(v, l - 1, l);
List< int > nums = new List< int >(v);
nums.RemoveAt(n - k);
v = nums.ToArray();
n = v.Length;
k++;
}
return v[0];
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
Console.WriteLine(rotateDelete(arr, N));
}
}
|
Javascript
function Rotate(arr,d,n)
{
var temp= new Array(n);
let k = 0;
for (let i = d; i < n; i++)
temp[k++] = arr[i];
for (let i = 0; i < d; i++)
temp[k++] = arr[i];
for (let i=0;i<arr.length;i++)
arr[i]=temp[i];
}
function rotateDelete( v, n)
{
let k = 1;
while (k < n) {
Rotate(v,v.length-1,v.length);
v.splice(n-k, 1)
n = v.length;
k++;
}
return v[0];
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let N = arr.length;
console.log(rotateDelete(arr, N));
|
PHP
<?php
function rotateDelete( $v , $n )
{
$k = 1;
while ( $k < $n ) {
$lastElement = array_pop ( $v );
array_unshift ( $v , $lastElement );
array_splice ( $v , $n - $k , 1);
$n = count ( $v );
$k ++;
}
return $v [0];
}
$arr = array (1, 2, 3, 4, 5, 6);
$N = count ( $arr );
echo rotateDelete( $arr , $N );
?>
|
Time complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...