Rank of remaining numbers in Array by replacing first and last with max and min alternatively
Last Updated :
27 Jan, 2023
Given an array arr[ ] of size N, the task is to find the rank of the remaining element in an array after performing the given operation:
- In each operation choose elements from both ends and delete them and insert the max of those values at the position of the left element and move one step towards the center from both ends and keep performing this operation.
- In the next cycle keep performing the same operation but instead of max, insert min of the elements this time.
- Perform this operation in alternate cycles until there is only one element left in the array.
- The rank is the position of the remaining element in the original array when it is sorted in increasing order. (The elements with same values are considered only for the sorted order)
Examples:
Input: arr = {4, 5, 3, 56, 3, 24, 5, 6, 22, 4, 55, 50, 89}
Output: 6
Explanation: See the diagram given below
24 is the 6th smallest element. The elements with same values are considered once.
Input: N = {20, 4, 5, 35, 6, 22, 4, 34}
Output: 6
Approach: The solution is based on two pointer approach. Follow the steps mentioned below:
- Take c as 1 which will indicate the number of iterations.
- Take 2 pointers, start as zero and end as N-1.
- Compare element at index s and e.
- If c is odd then take the maximum of the element else take the minimum of element and store in an array.
- Increment s, decrement e and repeat till s != e.
- Increment c by 1
- Repeat step 2 till the length of arr becomes 1.
- Remove duplicate from arr[] and sort it, now find the rank of the remaining element.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int rankOfNum(vector< int >& num)
{
vector< int > S = num;
int c = 1;
while (S.size() != 1) {
int s = 0;
int e = S.size() - 1;
vector< int > l;
while (s <= e) {
if (c % 2 == 1)
l.push_back(max(S[s], S[e]));
else {
l.push_back(min(S[s], S[e]));
}
s += 1;
e -= 1;
}
S = l;
c += 1;
}
set< int > setx;
for ( auto dt : num)
setx.insert(dt);
int p = distance(setx.begin(), setx.find(S[0]));
return p + 1;
}
int main()
{
vector< int > arr
= { 4, 5, 3, 56, 3, 24, 5, 6, 22, 4, 55, 50, 89 };
int s = rankOfNum(arr);
cout << s;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int rankOfNum(Integer[] num)
{
List<Integer> S = Arrays.asList(num);
int c = 1 ;
while (S.size() != 1 ) {
int s = 0 ;
int e = S.size() - 1 ;
ArrayList<Integer> l = new ArrayList<Integer>();
while (s <= e) {
if (c % 2 == 1 )
l.add(Math.max(S.get(s), S.get(e)));
else {
l.add(Math.min(S.get(s), S.get(e)));
}
s += 1 ;
e -= 1 ;
}
S = l;
c += 1 ;
}
HashSet<Integer> setx = new HashSet<Integer>();
for ( int dt : num)
setx.add(dt);
List<Integer> l = new LinkedList<>(setx);
Collections.sort(l);
int p = l.indexOf(S.get( 0 ));
return p + 1 ;
}
public static void main(String[] args)
{
Integer[] arr
= { 4 , 5 , 3 , 56 , 3 , 24 , 5 , 6 , 22 , 4 , 55 , 50 , 89 };
int s = rankOfNum(arr);
System.out.print(s);
}
}
|
Python3
def rankOfNum(num):
S = num[:]
c = 1
while len (S) ! = 1 :
s = 0
e = len (S) - 1
l = []
while s < = e:
if c % 2 = = 1 :
l.append( max (S[s], S[e]))
else :
l.append( min (S[s], S[e]))
s + = 1
e - = 1
S = l
c + = 1
setx = list ( set (num))
setx.sort()
p = setx.index(S[ 0 ])
return p + 1
if __name__ = = "__main__" :
arr = [ 4 , 5 , 3 , 56 , 3 , 24 , 5 , 6 , 22 , 4 , 55 , 50 , 89 ]
s = rankOfNum(arr)
print ( str (s))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static int rankOfNum(List< int > num)
{
List< int > S = num;
int c = 1;
while (S.Count != 1) {
int s = 0;
int e = S.Count - 1;
List< int > l = new List< int >();
while (s <= e) {
if (c % 2 == 1)
l.Add(Math.Max(S[s], S[e]));
else {
l.Add(Math.Min(S[s], S[e]));
}
s += 1;
e -= 1;
}
S = l;
c += 1;
}
HashSet< int > setx = new HashSet< int >();
foreach ( var dt in num) setx.Add(dt);
List< int > setxList = setx.ToList();
setxList.Sort();
int p = setxList.IndexOf(S[0]);
return p + 1;
}
public static void Main()
{
List< int > arr = new List< int >() {
4, 5, 3, 56, 3, 24, 5, 6, 22, 4, 55, 50, 89
};
int s = rankOfNum(arr);
Console.Write(s);
}
}
|
Javascript
<script>
function rankOfNum(num) {
let S = [...num]
c = 1
while (S.length != 1) {
s = 0
e = S.length - 1
l = []
while (s <= e) {
if (c % 2 == 1)
l.push(Math.max(S[s], S[e]))
else {
l.push(Math.min(S[s], S[e]))
}
s += 1
e -= 1
}
S = [...l]
c += 1
}
let setx = new Set([...num])
t = [...setx].sort( function (a, b) { return a - b })
let p = t.indexOf(S[0])
return p + 1
}
arr = [4, 5, 3, 56, 3, 24, 5, 6, 22, 4, 55, 50, 89]
s = rankOfNum(arr)
document.write((s))
</script>
|
Time Complexity: O(N)
Space Complexity: O(N), since n extra space has been taken
Share your thoughts in the comments
Please Login to comment...