JavaScript Program for Longest Subsequence of a Number having Same Left and Right Rotation


We will implement a code to find the longest subsequence of a given number that has the same left and right rotation in the JavaScript programming language. The left and right rotation of a given number means, for left rotation we have to move the left-most digit of the number to the end position or the rightmost position. Similarly, for the right rotation, we have to move the right-most digit of the number to the first position or the leftmost position. We will see the complete code with the implementation.

Introduction to Problem

In this problem, we are given a number and we have found a subsequence (a subsequence of a number or string that can be found by deleting some characters or the digits from the given number or string) that has the same left and the right rotation and if there are many we have to provide the one with the largest length.

For example −

If the given number is 100310801 then we can have many sequences that have the same right and left rotation −

  • All the sequences with a single size or size one have the same left and right rotation.

  • All the sequences with the size two and the same both digits will also have the same right and left rotation.

  • We can have the sequence with the values: 1010 and 0000 or size four that can have the same left and right rotation.

We have two approaches to solving this problem, Let’s see both of them −

Naive Approach

The direct and simple approach is the brute force method, in which we can finally all the sub sequences of the given number, and then for each subsequence, we can find its left and right rotation and match them. If both are the same then we will check for the length of the subsequence. If the length of the current subsequence is greater than the previously stored then we can update that.

There is an issue with this approach which is, time complexity. The time complexity for finding each subsequence of a given number with N digits is 2 power N, and then for finding the left and right rotation will be extra N which makes the total time complexity O(). For a number higher than 20, this will not be good to compute easily within a good time, making this approach inefficient.

Main Approach

In the main approach, we will go for an optimal approach by simple observation from the example that: if the subsequence is of a single size then there always is a case that the left and the right rotations are equal. Another observation is that the length or the number of digits must be even in the given number and all numbers that are present in the odd position are the same and all the numbers at the even positions must be the same. For example, 898989, 78, 656565, etc, these types of numbers always have the same left and right rotation.

The aim is to find the largest number or largest subsequence, for that we will follow the following steps −

Example

// creating function to just pass the string
// or number to get the result
function findAltSubSeq(str){

   // Length of the given string
   var num = str.length
   
   // answer variable to store the answer
   var ans = -1;
   
   // Iterate for all possible combinations
   // of a two-digit numbers
   
   var digits = 10
   for (var i = 0; i < digits; i++) {
      for (var j = 0; j < digits; j++) {
         var current = 0
         var temp = 0
         
         // Check for alternate occurrence
         // of current combination
         
         for (var k = 0; k < num; k++) {
            if (temp == 0 && str[k] - '0' == i) {
               temp = 1;
               
               // Increment the current value
               current++;
            }
            else if (temp== 1 && str[k] - '0' == j) {
               temp = 0;
               
               // Increment the current value
               current++;
            }
         }
         
         // If alternating sequence is
         // obtained of odd length
         if (i != j && current % 2 == 1)
         // Reduce to even length
         current--;
         
         // Update answer to store
         // the maximum
         
         ans = Math.max(current, ans);
      }
   }
   
   // Return the answer
   return ans;
}

// calling the function
// taking the number in string version
var str = "100210601";
console.log("Subsequcne with the maximum length is of length: " + findAltSubSeq(str));

Time and Space Complexity

The time complexity of the above code is O(N), where N is the length of the given number. The space complexity of the program is O(1) as we are not using any extra space here.

Note: In the above code, we have taken the number as the string instead of the number data type because it's easy to work with strings as compared to the numbers in JavaScript, so if the number is given then it’s better to convert that number into the string.

Conclusion

In this article, we have implement a code to find the longest subsequence of a given number that has the same left and right rotation in the JavaScript programming language. Left rotation is to move the rightmost element or the digit to the leftmost position and right rotation is exact opposite of it. We have discussed two approaches one is the simple brute force method and other is the efficient two pointer approach with time complexity of O(N) and space complexity of O(1).

Updated on: 30-Mar-2023

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements