Maximum Suffix length among all distinct elements
Given the array X[], the task is to return the maximum suffix sub-array length of the same elements for each distinct element and the maximum suffix length among all by using the below operation:
- In each operation, you can select a sub-array, remove it, and concatenate the remaining part of X[].
Examples:
Input: N = 11, X[] = {2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2}
Output:
- Element = 2, Maximum suffix length = 3
- Element = 3, Maximum suffix length = 2
- Element = 5, Maximum suffix length = 3
- Maximum length among all = 3
Explanation: There are three distinct elements present in X[], Which are 2, 5 and 3. We will take a look on each of them:
- Element 2 = We can remove the sub-array = {X[ 3 ], X[ 4 ], . . . . ., X[ 10 ]} So the remaining X[] will be = {2, 2, 2}. Which gives the suffix length of element 2 in updated X[] as = 3.
- Element 5 = We can remove the sub-array = {X[ 6 ], X[ 7 ], . . . . ., X[ 11 ]} So the remaining X[] will be = {2, 2, 5, 5, 5}. Which gives the suffix length of element 5 in updated X[] as = 3.
- Element 3 = We can remove the sub-array = { X[ 11 ], . . . X[ 11 ] } So the remaining X[] will be = {2, 2, 5, 5, 5, 3, 5, 5, 3, 3}. Which gives the suffix length of element 3 in updated X[] as = 2.
So, the maximum suffix length for element 2, 5 and 3 using given operation is 3, 3, and 2 respectively. Maximum suffix length among all is 3. It can be verified that all the lengths are optimally longest possible suffix lengths.
Input: N = 5, X[] = { 8, 3, 3, 3, 8 }
Output:
- Element = 8, Maximum suffix length = 2
- Element = 3, Maximum suffix length = 3
- Maximum length among all = 3
Explanation: For element 8, We can remove sub-array {3, 3, 3} so the remaining X[] will be {8, 8} Which has suffix length for element 8 as 2. While in case of 3 we can remove X[5], so the remaining X[] will be = {8, 3, 3, 3} and give suffix length for element 3 as 3. Maximum suffix length among all is 3.
Approach: Implement the idea below to solve the problem
The problem is based on observation and can be solved by using those observation. For understanding the concept see the Concept of approach section below.
Concept of approach:
First reverse the array X[]. So that now the problem has changed, We have to find the maximum length prefix of same elements. This reversal on X[] is done because for solving the problem we have to traverse the array X[] number of times. So it will be convenient to traverse from left to right than right to left.
There can be two cases:
- If an element is present at first index of X[]: In this case the element at first index can be join with other consecutive frequencies for maximizing prefix length( reverse of suffix, Because X[] has reversed ). Let understand it with the example. X[] = {2, 2, 3, 3, 3, 2, 2, 2, 5, 5, 2, 2, 2, 2}. In the X[] there are three consecutive frequencies of element = 2 in X[], Which are 2, 3 and 4 ( mark bold in X[] ). Now we can remove sub-array { X[3] . . . X[5] } or { X[3] . . . . X[10] } for making X[] as: {2, 2, 2, 2, 2, 5, 5, 2, 2, 2, 2} or {2, 2, 2, 2, 2, 2}. Which gives maximum prefix length as 5 or 6. 6 is greater than 5, So 6 is the max possible prefix length for element 2. It should be noted that the element 2 is present at first index of X[] after reversing. Approach for such cases is as:
- Create a vector and initialize it with adjacent frequencies. For X[] = {2, 2, 3, 3, 3, 2, 2, 2, 5, 5, 2, 2, 2, 2}. Vector will contain values as: {2, 3, 4} mark bold in X[].
- Then make the pair of first frequency(2 in list) with rest of the frequency (3 and 4 in list), With which sum will be max. Formally, max = max ( Vector[ 0 ] + Vector[ j ] ) for (2 <= j <= Vector.size()). The pair of first frequency with other frequency will be as follows:
- 2 + 3 = 5
- 2 + 4 = 6
- max(5, 6) = 6. Which is our max possible length for element 2 in X[].
- If an element is not present at first index: In such cases just output the maximum consecutive frequency from Vector. For example X[] = {2, 2, 5, 5, 5, 2, 5, 5, 5, 5, 3, 3, 3}. We can remove sub-array { X[1] . . .X[2] } or {X[1] . . . .X[6]} for making X[] as: {5, 5, 5, 2, 5, 5, 5, 5, 3, 3, 3} or {5, 5, 5, 5, 3, 3, 3} giving maximum prefix length for 5 as: 3 and 4. In which 4 is max possible prefix length for element 5. It should be noted that 5 was not initially present at first index of X[]. For such cases approach is as:
- Just output the maximum frequency for such cases in frequency vector.
- Create frequency vector. For element 5 in X[] = {2, 2, 5, 5, 5, 2, 5, 5, 5, 5, 3, 3, 3}. Frequency vector will be = {3, 4}, In which 4 is maximum.
Steps were taken to solve the problem:
- At first, Reverse the X[].
- Create a Set and initialize it with distinct elements of X[].
- Create a variable let’s say max_all to hold the value of the longest suffix length among all distinct integers.
- Create an ArrayList<Integer> let’s say Adjacent_Frequency to hold the count of occurrence of an element consecutively.
- Run a loop for the traversing set and follow the below-mentioned steps under the scope of the loop:
- Traverse X[] and update consecutive frequencies in Adjacent_Frequency.
- Traverse the Adjacent frequency list and get the max frequency in the variable let’s say max.
- If the first element of X[] is the element for which we are finding the maximum suffix length, Then update max as max( Adjacent_frequency[ 0 ] + Adjacent_frequency[ j ] ), for (2 ≤ j ≤ N), else max = Adjacent_frequency[ 0 ].
- Print the current element of the set and its max suffix length.
- if ( max_all < max ), Then update max_all as max.
- Print the value of max_all.
Below is the Code to implement the approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // function for reversing array vector< int > Reverse_Array(vector< int > X) { int start = 0; int end = X.size() - 1; while (start < end) { int temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] vector< int > Set( int n, vector< int > X) { vector< int > set; for ( int i = 0; i < n; i++) { if (find(set.begin(), set.end(), X[i]) != set.end()) { continue ; } else { set.push_back(X[i]); } } return set; } // function for implementing approach void approach(vector< int > set, int n, vector< int > X) { // Variable initialized to hold // maximum suffix length among // distinct element int max_all = INT_MIN; // Loop for traversing over set // containing distinct elements for ( int i = 0; i < set.size(); i++) { // Variable to hold element // from set int element = set[i]; // Array initialized to // count the frequency // of same adjacent element vector< int > adjacent_frequency; int leftEnd = 0; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] != element) { leftEnd++; } else { int rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] == element) rightEnd = rightEnd + 1; adjacent_frequency.push_back(rightEnd - leftEnd); leftEnd = rightEnd + 1; } } // Variable initialized to hold // maximum suffix length // for distinct element int max = INT_MIN; // Loop for traversing over // frequency Array for ( int k = 0; k < adjacent_frequency.size(); k++) { // Updating max with max // adjacent frequency max = adjacent_frequency[k] > max ? adjacent_frequency[k] : max; } bool flag = false ; // Condition when an element is // not present at first // index of X[] if (X[0] != element) { cout << "Element : " << element << " Max suffix length : " << max << endl; flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag != true ) { for ( int j = 1; j < adjacent_frequency.size(); j++) { max = (adjacent_frequency[0] + adjacent_frequency[j]) > max ? (adjacent_frequency[0] + adjacent_frequency[j]) : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element cout << "element : " << element << " Max suffix length : " << max << endl; } } // Printing maximum length among // all distinct element cout << "Maximum length among all : " << max_all << endl; } // Driver Function int main() { // Input Array vector< int > X = { 2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2 }; // Reversing and updating X[] X = Reverse_Array(X); // Variable to hold length of X[] int N = X.size(); // List for storing distinct // elements present in X[] vector< int > set = ::Set(N, X); // Function call for applying // approach on X[] approach(set, N, X); return 0; } // This Code is Contributed by Prasad Kandekar(prasad264) |
Java
// Java code to implement the approach import java.util.*; public class GFG { // Driver Function public static void main(String[] args) { // Input Array int [] X = { 2 , 2 , 5 , 5 , 5 , 3 , 5 , 5 , 3 , 3 , 2 }; // Reversing and updating X[] X = Reverse_Array(X); // Variable to hold length of X[] int N = X.length; // List for storing distinct // elements present in X[] ArrayList<Integer> set = Set(N, X); // Function call for applying // approach on X[] Approach(set, N, X); } // Method for implementing approach static void Approach(ArrayList<Integer> set, int n, int [] X) { // Variable initialized to hold // maximum suffix length among // distinct element int max_all = Integer.MIN_VALUE; // Loop for traversing over set // containing distinct elements for ( int i = 0 ; i < set.size(); i++) { // Variable to hold element // from set int element = set.get(i); // Arraylist initialized to // count the frequency // of same adjacent element ArrayList<Integer> adjacent_frequency = new ArrayList<>(); int leftEnd = 0 ; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] != element) { leftEnd++; } else { int rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] == element) rightEnd = rightEnd + 1 ; adjacent_frequency.add(rightEnd - leftEnd); leftEnd = rightEnd + 1 ; } } // Variable initialized to hold // maximum suffix length // for distinct element int max = Integer.MIN_VALUE; // Loop for traversing over // frequency ArrayList for ( int k = 0 ; k < adjacent_frequency.size(); k++) { // Updating max with max // adjacent frequency max = adjacent_frequency.get(k) > max ? adjacent_frequency.get(k) : max; } boolean flag = false ; // Condition when an element is // not present at first // index of X[] if (X[ 0 ] != element) { System.out.println( "Element : " + element + " Max suffix length : " + max); flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag != true ) { for ( int j = 1 ; j < adjacent_frequency.size(); j++) { max = (adjacent_frequency.get( 0 ) + adjacent_frequency.get(j)) > max ? (adjacent_frequency.get( 0 ) + adjacent_frequency.get( j)) : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element System.out.println( "element : " + element + " Max suffix length : " + max); } } // Printing maximum length among // all distinct element System.out.println( "Maximum length among all : " + max_all); } // Method for reversing array static int [] Reverse_Array( int [] X) { int start = 0 ; int end = X.length - 1 ; while (start < end) { int temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] static ArrayList<Integer> Set( int n, int [] X) { ArrayList<Integer> set = new ArrayList<>(); for ( int i = 0 ; i < n; i++) { if (set.contains(X[i])) { continue ; } else { set.add(X[i]); } } return (set); } } |
Python3
# Python3 code to implement the approach # function for reversing array def reverse_array(arr): """ Function to reverse an array """ start = 0 end = len (arr) - 1 while start < end: arr[start], arr[end] = arr[end], arr[start] start + = 1 end - = 1 return arr # Method for getting distinct # elements from X[] def get_distinct_elements(arr): """ Method to get distinct elements from an array """ distinct_set = [] for i in range ( len (arr)): if arr[i] not in distinct_set: distinct_set.append(arr[i]) return distinct_set # function for implementing approach def approach(distinct_set, arr): """ Function to implement the approach """ max_all = float ( '-inf' ) # Variable to hold maximum suffix length among distinct elements # Loop for traversing over set # containing distinct elements for element in distinct_set: adjacent_frequency = [] # List initialized to count the frequency of same adjacent element left_end = 0 # Loop for initializing # frequency in # adjacent_frequency list while left_end < len (arr): if arr[left_end] ! = element: left_end + = 1 else : right_end = left_end while right_end < len (arr) and arr[right_end] = = element: right_end + = 1 adjacent_frequency.append(right_end - left_end) left_end = right_end + 1 maxm = float ( '-inf' ) # Variable to hold maximum suffix length for distinct element for k in range ( len (adjacent_frequency)): maxm = max (adjacent_frequency[k], maxm) # Updating max with max adjacent frequency flag = False if arr[ 0 ] ! = element: print (f "Element: {element} Max suffix length: {maxm}" ) flag = True max_all = maxm if maxm > max_all else max_all elif not flag: for j in range ( 1 , len (adjacent_frequency)): maxm = max (adjacent_frequency[ 0 ] + adjacent_frequency[j], maxm) max_all = maxm if maxm > max_all else max_all print (f "Element: {element} Max suffix length: {maxm}" ) print (f "Maximum length among all: {max_all}" ) # Driver code X = [ 2 , 2 , 5 , 5 , 5 , 3 , 5 , 5 , 3 , 3 , 2 ] X = reverse_array(X) distinct_set = get_distinct_elements(X) approach(distinct_set, X) |
C#
// C# code to implement the approach using System; using System.Collections.Generic; using System.Linq; public class GFG { // function for reversing array static List< int > Reverse_Array(List< int > X) { int start = 0; int end = X.Count - 1; while (start < end) { int temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] static List< int > Set( int n, List< int > X) { List< int > set = new List< int >(); for ( int i = 0; i < n; i++) { if ( set .Contains(X[i])) { continue ; } else { set .Add(X[i]); } } return set ; } // function for implementing approach static void approach(List< int > set , int n, List< int > X) { // Variable initialized to hold // maximum suffix length among // distinct element int max_all = int .MinValue; // Loop for traversing over set // containing distinct elements for ( int i = 0; i < set .Count; i++) { // Variable to hold element // from set int element = set [i]; // Array initialized to // count the frequency // of same adjacent element List< int > adjacent_frequency = new List< int >(); int leftEnd = 0; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] != element) { leftEnd++; } else { int rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] == element) { rightEnd = rightEnd + 1; } adjacent_frequency.Add(rightEnd - leftEnd); leftEnd = rightEnd + 1; } } // Variable initialized to hold // maximum suffix length // for distinct element int max = int .MinValue; // Loop for traversing over // frequency Array for ( int k = 0; k < adjacent_frequency.Count; k++) { // Updating max with max // adjacent frequency max = adjacent_frequency[k] > max ? adjacent_frequency[k] : max; } bool flag = false ; // Condition when an element is // not present at first // index of X[] if (X[0] != element) { Console.WriteLine( "Element : " + element + " Max suffix length : " + max); flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag != true ) { for ( int j = 1; j < adjacent_frequency.Count; j++) { max = (adjacent_frequency[0] + adjacent_frequency[j]) > max ? (adjacent_frequency[0] + adjacent_frequency[j]) : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element Console.WriteLine( "element : " + element + " Max suffix length : " + max); } } // Printing maximum length among // all distinct element Console.WriteLine( "Maximum length among all : " + max_all); } // Driver Function static public void Main() { // Input Array List< int > X = new List< int >{ 2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2 }; // Reversing and updating X[] X = Reverse_Array(X); // Variable to hold length of X[] int N = X.Count; // List for storing distinct // elements present in X[] List< int > set = Set(N, X); // Function call for applying // approach on X[] approach( set , N, X); } } // This code is contributed by Prasad Kandekar(prasad264) |
Javascript
// JavaScript code to implement the approach // function for reversing array function reverseArray(X){ let start = 0; let end = X.length - 1; while (start < end) { let temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] function getDistinctElements(X) { return [... new Set(X)]; } // function for implementing approach function approach(set, X){ let n = X.length; // Variable initialized to hold // maximum suffix length among // distinct element let max_all = Number.MIN_SAFE_INTEGER; // Loop for traversing over set // containing distinct elements for (let i = 0; i < set.length; i++) { // Variable to hold element // from set let element = set[i]; // Array initialized to // count the frequency // of same adjacent element let adjacentFrequency = []; let leftEnd = 0; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] !== element) { leftEnd++; } else { let rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] === element) { rightEnd++; } adjacentFrequency.push(rightEnd - leftEnd); leftEnd = rightEnd + 1; } } // Variable initialized to hold // maximum suffix length // for distinct element let max = Number.MIN_SAFE_INTEGER; // Loop for traversing over // frequency Array for (let k = 0; k < adjacentFrequency.length; k++) { // Updating max with max // adjacent frequency max = adjacentFrequency[k] > max ? adjacentFrequency[k] : max; } let flag = false ; // Condition when an element is // not present at first // index of X[] if (X[0] !== element) { console.log( "Element: " + element + " Max suffix length: " + max); flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag !== true ) { for (let j = 1; j < adjacentFrequency.length; j++) { max = adjacentFrequency[0] + adjacentFrequency[j] > max ? adjacentFrequency[0] + adjacentFrequency[j] : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element console.log( "Element: " + element + " Max suffix length: " + max); } } // Printing maximum length among // all distinct element console.log( "Maximum length among all: " + max_all); } // Driver Code // Input Array let X = [ 2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2 ]; // Reversing and updating X[] X = reverseArray(X); // List for storing distinct // elements present in X[] let set = getDistinctElements(X); // Function call for applying // approach on X[] approach(set, X); |
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // function for reversing array vector< int > Reverse_Array(vector< int > X) { int start = 0; int end = X.size() - 1; while (start < end) { int temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] vector< int > Set( int n, vector< int > X) { vector< int > set; for ( int i = 0; i < n; i++) { if (find(set.begin(), set.end(), X[i]) != set.end()) { continue ; } else { set.push_back(X[i]); } } return set; } // function for implementing approach void approach(vector< int > set, int n, vector< int > X) { // Variable initialized to hold // maximum suffix length among // distinct element int max_all = INT_MIN; // Loop for traversing over set // containing distinct elements for ( int i = 0; i < set.size(); i++) { // Variable to hold element // from set int element = set[i]; // Array initialized to // count the frequency // of same adjacent element vector< int > adjacent_frequency; int leftEnd = 0; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] != element) { leftEnd++; } else { int rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] == element) rightEnd = rightEnd + 1; adjacent_frequency.push_back(rightEnd - leftEnd); leftEnd = rightEnd + 1; } } // Variable initialized to hold // maximum suffix length // for distinct element int max = INT_MIN; // Loop for traversing over // frequency Array for ( int k = 0; k < adjacent_frequency.size(); k++) { // Updating max with max // adjacent frequency max = adjacent_frequency[k] > max ? adjacent_frequency[k] : max; } bool flag = false ; // Condition when an element is // not present at first // index of X[] if (X[0] != element) { cout << "Element : " << element << " Max suffix length : " << max << endl; flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag != true ) { for ( int j = 1; j < adjacent_frequency.size(); j++) { max = (adjacent_frequency[0] + adjacent_frequency[j]) > max ? (adjacent_frequency[0] + adjacent_frequency[j]) : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element cout << "element : " << element << " Max suffix length : " << max << endl; } } // Printing maximum length among // all distinct element cout << "Maximum length among all : " << max_all << endl; } // Driver Function int main() { // Input Array vector< int > X = { 2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2 }; // Reversing and updating X[] X = Reverse_Array(X); // Variable to hold length of X[] int N = X.size(); // List for storing distinct // elements present in X[] vector< int > set = ::Set(N, X); // Function call for applying // approach on X[] approach(set, N, X); return 0; } // This Code is Contributed by Prasad Kandekar(prasad264) |
Java
// Java code to implement the approach import java.util.*; public class GFG { // Driver Function public static void main(String[] args) { // Input Array int [] X = { 2 , 2 , 5 , 5 , 5 , 3 , 5 , 5 , 3 , 3 , 2 }; // Reversing and updating X[] X = Reverse_Array(X); // Variable to hold length of X[] int N = X.length; // List for storing distinct // elements present in X[] ArrayList<Integer> set = Set(N, X); // Function call for applying // approach on X[] Approach(set, N, X); } // Method for implementing approach static void Approach(ArrayList<Integer> set, int n, int [] X) { // Variable initialized to hold // maximum suffix length among // distinct element int max_all = Integer.MIN_VALUE; // Loop for traversing over set // containing distinct elements for ( int i = 0 ; i < set.size(); i++) { // Variable to hold element // from set int element = set.get(i); // Arraylist initialized to // count the frequency // of same adjacent element ArrayList<Integer> adjacent_frequency = new ArrayList<>(); int leftEnd = 0 ; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] != element) { leftEnd++; } else { int rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] == element) rightEnd = rightEnd + 1 ; adjacent_frequency.add(rightEnd - leftEnd); leftEnd = rightEnd + 1 ; } } // Variable initialized to hold // maximum suffix length // for distinct element int max = Integer.MIN_VALUE; // Loop for traversing over // frequency ArrayList for ( int k = 0 ; k < adjacent_frequency.size(); k++) { // Updating max with max // adjacent frequency max = adjacent_frequency.get(k) > max ? adjacent_frequency.get(k) : max; } boolean flag = false ; // Condition when an element is // not present at first // index of X[] if (X[ 0 ] != element) { System.out.println( "Element : " + element + " Max suffix length : " + max); flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag != true ) { for ( int j = 1 ; j < adjacent_frequency.size(); j++) { max = (adjacent_frequency.get( 0 ) + adjacent_frequency.get(j)) > max ? (adjacent_frequency.get( 0 ) + adjacent_frequency.get( j)) : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element System.out.println( "element : " + element + " Max suffix length : " + max); } } // Printing maximum length among // all distinct element System.out.println( "Maximum length among all : " + max_all); } // Method for reversing array static int [] Reverse_Array( int [] X) { int start = 0 ; int end = X.length - 1 ; while (start < end) { int temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] static ArrayList<Integer> Set( int n, int [] X) { ArrayList<Integer> set = new ArrayList<>(); for ( int i = 0 ; i < n; i++) { if (set.contains(X[i])) { continue ; } else { set.add(X[i]); } } return (set); } } |
Python3
# Python3 code to implement the approach import sys # function for reversing array def reverse_array(X): start = 0 end = len (X) - 1 while start < end: temp = X[start] X[start] = X[end] X[end] = temp start + = 1 end - = 1 return X # Method for getting distinct # elements from X[] def set_fn(n, X): s = [] for i in range (n): if X[i] in s: continue else : s.append(X[i]) return s # function for implementing approach def approach(s, n, X): # Variable initialized to hold # maximum suffix length among # distinct element max_all = - sys.maxsize - 1 # Loop for traversing over set # containing distinct elements for element in s: # Array initialized to # count the frequency # of same adjacent element adjacent_frequency = [] leftEnd = 0 # Loop for initializing # frequency in # adjacent_frequency list while leftEnd < n: if X[leftEnd] ! = element: leftEnd + = 1 else : rightEnd = leftEnd while rightEnd < n and X[rightEnd] = = element: rightEnd + = 1 adjacent_frequency.append(rightEnd - leftEnd) leftEnd = rightEnd + 1 # maximum suffix length # for distinct element max_val = - sys.maxsize - 1 # Loop for traversing over # frequency Array for k in range ( len (adjacent_frequency)): # Updating max with max # adjacent frequency max_val = max (adjacent_frequency[k], max_val) flag = False # Condition when an element is # not present at first # index of X[] if X[ 0 ] ! = element: print ( "Element :" , element, "Max suffix length :" , max_val) flag = True max_all = max (max_val, max_all) # This will execute when an # element for which we are # finding max suffix length, # is not present at first # index of X[] elif not flag: for j in range ( 1 , len (adjacent_frequency)): max_val = max (adjacent_frequency[ 0 ] + adjacent_frequency[j], max_val) max_all = max (max_val, max_all) # Printing the max length # for an elemen print ( "Element :" , element, "Max suffix length :" , max_val) # Printing maximum length among # all distinct element print ( "Maximum length among all :" , max_all) # Driver code # Input Array X = [ 2 , 2 , 5 , 5 , 5 , 3 , 5 , 5 , 3 , 3 , 2 ] # Reversing and updating X[] X = reverse_array(X) # Variable to hold length of X[] N = len (X) # List for storing distinct # elements present in X[] s = set_fn(N, X) # Function call for applying # approach on X[] approach(s, N, X) # This Code is Contributed by Prajwal Kandekar |
C#
// C# code to implement the approach using System; using System.Collections.Generic; using System.Linq; public class GFG { // function for reversing array static List< int > Reverse_Array(List< int > X) { int start = 0; int end = X.Count - 1; while (start < end) { int temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] static List< int > Set( int n, List< int > X) { List< int > set = new List< int >(); for ( int i = 0; i < n; i++) { if ( set .Contains(X[i])) { continue ; } else { set .Add(X[i]); } } return set ; } // function for implementing approach static void approach(List< int > set , int n, List< int > X) { // Variable initialized to hold // maximum suffix length among // distinct element int max_all = int .MinValue; // Loop for traversing over set // containing distinct elements for ( int i = 0; i < set .Count; i++) { // Variable to hold element // from set int element = set [i]; // Array initialized to // count the frequency // of same adjacent element List< int > adjacent_frequency = new List< int >(); int leftEnd = 0; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < n) { if (X[leftEnd] != element) { leftEnd++; } else { int rightEnd = leftEnd; while (rightEnd < n && X[rightEnd] == element) { rightEnd = rightEnd + 1; } adjacent_frequency.Add(rightEnd - leftEnd); leftEnd = rightEnd + 1; } } // Variable initialized to hold // maximum suffix length // for distinct element int max = int .MinValue; // Loop for traversing over // frequency Array for ( int k = 0; k < adjacent_frequency.Count; k++) { // Updating max with max // adjacent frequency max = adjacent_frequency[k] > max ? adjacent_frequency[k] : max; } bool flag = false ; // Condition when an element is // not present at first // index of X[] if (X[0] != element) { Console.WriteLine( "Element : " + element + " Max suffix length : " + max); flag = true ; max_all = max > max_all ? max : max_all; } // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] else if (flag != true ) { for ( int j = 1; j < adjacent_frequency.Count; j++) { max = (adjacent_frequency[0] + adjacent_frequency[j]) > max ? (adjacent_frequency[0] + adjacent_frequency[j]) : max; max_all = max > max_all ? max : max_all; } // Printing the max length // for an element Console.WriteLine( "element : " + element + " Max suffix length : " + max); } } // Printing maximum length among // all distinct element Console.WriteLine( "Maximum length among all : " + max_all); } // Driver Function static public void Main() { // Input Array List< int > X = new List< int >{ 2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2 }; // Reversing and updating X[] X = Reverse_Array(X); // Variable to hold length of X[] int N = X.Count; // List for storing distinct // elements present in X[] List< int > set = Set(N, X); // Function call for applying // approach on X[] approach( set , N, X); } } // This code is contributed by Prasad Kandekar(prasad264) |
Javascript
// JavaScript code to implement the approach // function for reversing array function reverseArray(X) { let start = 0; let end = X.length - 1; while (start < end) { let temp = X[start]; X[start] = X[end]; X[end] = temp; start++; end--; } return X; } // Method for getting distinct // elements from X[] function getDistinctElements(X) { let s = []; for (let i = 0; i < X.length; i++) { if (s.includes(X[i])) { continue ; } else { s.push(X[i]); } } return s; } // function for implementing approach function approach(s, X) { // Variable initialized to hold // maximum suffix length among // distinct element let max_all = Number.MIN_SAFE_INTEGER; // Loop for traversing over set // containing distinct elements for (let i = 0; i < s.length; i++) { // Array initialized to // count the frequency // of same adjacent element let adjacent_frequency = []; let leftEnd = 0; // Loop for initializing // frequency in // adjacent_frequency list while (leftEnd < X.length) { if (X[leftEnd] != s[i]) { leftEnd++; } else { let rightEnd = leftEnd; while (rightEnd < X.length && X[rightEnd] == s[i]) { rightEnd++; } adjacent_frequency.push(rightEnd - leftEnd); leftEnd = rightEnd + 1; } } // maximum suffix length // for distinct element let max_val = Number.MIN_SAFE_INTEGER; // Loop for traversing over // frequency Array for (let k = 0; k < adjacent_frequency.length; k++) { // Updating max with max // adjacent frequency max_val = Math.max(adjacent_frequency[k], max_val); } let flag = false ; // Condition when an element is // not present at first // index of X[] if (X[0] != s[i]) { console.log( "Element :" , s[i], "Max suffix length :" , max_val); flag = true ; max_all = Math.max(max_val, max_all); // This will execute when an // element for which we are // finding max suffix length, // is not present at first // index of X[] } else if (!flag) { for (let j = 1; j < adjacent_frequency.length; j++) { max_val = Math.max(adjacent_frequency[0] + adjacent_frequency[j], max_val); max_all = Math.max(max_val, max_all); } // Printing the max length // for an element console.log( "Element :" , s[i], "Max suffix length :" , max_val); } } // Printing maximum length among // all distinct element console.log( "Maximum length among all :" , max_all); } // Driver code // Input Array let X = [ 2, 2, 5, 5, 5, 3, 5, 5, 3, 3, 2 ]; // Reversing and updating X[] X = reverseArray(X); // List for storing distinct // elements present in X[] let s = getDistinctElements(X); // Function call for applying // approach on X[] approach(s, X) |
element : 2 Max suffix length : 3 Element : 3 Max suffix length : 2 Element : 5 Max suffix length : 3 Maximum length among all : 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...