Count of ways to convert given Array such that array maximum is not present in the first half
Last Updated :
21 Aug, 2022
Given an array arr[] of even size N. The task is to count the number of ways of converting arr[] such that the first half of the array does not contain the maximum number.
Examples:
Input: arr[] = {2, 2, 5, 2, 2, 2}
Output: 3
Explanation: Following are the ways where the maximum element 5 is not present in the first half of the array.
[2, 2, 2, 5, 2, 2] when x=1 (shifted to the right by 1)
[2, 2, 2, 2, 5, 2] when x=2 (shifted to the right by 2)
[2, 2, 2, 2, 2, 5] when x=3 (shifted to the right by 3)
[5, 2, 2, 2, 2, 2] when x=4 NOT A VALID CASE
Input: arr[] = {3, 3, 6, 3, 3, 6}
Output: 0
Explanation: No matter how many shifts we perform, the maximum number 6 is always present in the first array.
Naive Approach: Do right shifts in arr[] and check for each case according to the given condition. Count all the possible ways and print it.
Time Complexity: O(N * N) //since two nested loops are used the time taken by the algorithm to complete all operation is quadratic.
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Efficient Approach: This problem is implementation based. Follow the steps below to solve the given problem.
- Take two halves of the array arr[].
- Find and save the maximum value in the vector.
- Take a variable to store the maximum value of arr[].
- Since the maximum value can occur more than once in the array, so save the position of the maximum value in front and last.
- If the position of the maximum value is in such a way that it's less than half the size of the array, there won't be any way possible where the front half of the array wouldn't have a value this large.
- And if that is not the case, then the number of ways possible would be N/2 - (last position - first position).
Below is the implementation of the above approach.
C++
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the number of ways to
// achieve the required array
void countWays(vector<int>& arr)
{
int last_pos = -1;
int front_pos = -1;
int N = arr.size();
int maxi = INT_MIN;
for (int i = 0; i < N; i++) {
maxi = max(maxi, arr[i]);
}
for (int i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (N / 2 >= (last_pos - front_pos))
cout << (N / 2 - (last_pos - front_pos));
else
cout << "0";
}
// Driver Code
int main()
{
vector<int> arr = { 2, 2, 5, 2, 2, 2 };
// Function Call
countWays(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the number of ways to
// achieve the required array
static void countWays(int arr[])
{
int last_pos = -1;
int front_pos = -1;
int N = arr.length;
int maxi = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
maxi = Math.max(maxi, arr[i]);
}
for (int i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (N / 2 >= (last_pos - front_pos))
System.out.println(N / 2 - (last_pos - front_pos));
else
System.out.println("0");
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 2, 2, 5, 2, 2, 2 };
// Function Call
countWays(arr);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 program for above approach
INT_MIN = -2147483648
# Function to find the number of ways to
# achieve the required array
def countWays(arr):
last_pos = -1
front_pos = -1
N = len(arr)
maxi = INT_MIN
for i in range(0, N):
maxi = max(maxi, arr[i])
for i in range(0, N):
if (arr[i] == maxi):
front_pos = i
break
for i in range(N - 1, -1, -1):
if (arr[i] == maxi):
last_pos = i
break
if (N // 2 >= (last_pos - front_pos)):
print(N // 2 - (last_pos - front_pos))
else:
print("0")
# Driver Code
if __name__ == "__main__":
arr = [2, 2, 5, 2, 2, 2]
# Function Call
countWays(arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to find the number of ways to
// achieve the required array
static void countWays(int []arr)
{
int last_pos = -1;
int front_pos = -1;
int N = arr.Length;
int maxi = int.MinValue;
for (int i = 0; i < N; i++) {
maxi = Math.Max(maxi, arr[i]);
}
for (int i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (N / 2 >= (last_pos - front_pos))
Console.WriteLine(N / 2 - (last_pos - front_pos));
else
Console.WriteLine("0");
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 2, 5, 2, 2, 2 };
// Function Call
countWays(arr);
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the number of ways to
// achieve the required array
function countWays(arr)
{
let last_pos = -1;
let front_pos = -1;
let N = arr.length;
let maxi = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
maxi = Math.max(maxi, arr[i]);
}
for (let i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (let i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (Math.floor(N / 2) >= (last_pos - front_pos))
document.write(Math.floor(N / 2) - (last_pos - front_pos));
else
document.write("0");
}
// Driver Code
let arr = [2, 2, 5, 2, 2, 2];
// Function Call
countWays(arr);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Similar Reads
Count array elements whose highest power of 2 less than or equal to that number is present in the given array Given an array arr[] consisting of N positive integers, the task is to find the number of array elements whose highest power of 2 less than or equal to that number is present in the array. Examples: Input: arr[] = {3, 4, 6, 9}Output: 2Explanation:There are 2 array elements (4 and 6), whose highest p
6 min read
Count indices where the maximum in the prefix array is less than that in the suffix array Given an array arr[] of size N, the task is to find the number of prefix arrays whose maximum is less than the maximum element in the remaining suffix array. Examples: Input: arr[] = {2, 3, 4, 8, 1, 4}Output: 3Explanation:Prefix array = {2}, {2, 3}, {2, 3, 4}, {2, 3, 4, 8}, {2, 3, 4, 8, 1} Respectiv
8 min read
Count of positions j in Array such that arr[i] is maximum in index range [i, j] with end points as same Given an array arr[] consisting of N positive integers, the task is to find all j such that arr[j] = arr[i] and all the numbers in the range [min(j, i), max(j, i)] is less than or equal to arr[i] where 1 ⤠i ⤠N. Examples: Input: arr[] = {4, 7, 7, 9, 7}, N = 5Output: 1 2 2 1 1Explanation:For i = 1,
8 min read
Count of index range [L, R] in Array such that removing all its instances sorts the Array Given an array arr[] of length N, the task is to find the number of Good Ranges in the array arr[]. A Good Range is defined as the range of left and right indices, i.e, [L. R] in the array arr[] such that by removing all the numbers in the range [L, R] of the array arr[] and the appearances of those
8 min read
Count the maximum number of elements that can be selected from the array Given an array arr[], the task is to count the maximum number of elements that can be selected from the given array following the below selection process: At 1st selection, select an element which is greater than or equal to 1.At 2nd selection, select an element which is greater than or equal to 2.A
5 min read