Count of ways to convert given Array such that array maximum is not present in the first half
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 CASEInput: 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> |
3
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
Please Login to comment...