HI WELCOME TO SIRIS

Longest consecutive subsequence

Leave a Comment

 The idea is to first sort the array and find the longest subarray with consecutive elements. After sorting the array and removing the multiple occurrences of elements, run a loop and keep a count and max (both initially zero). Run a loop from start to end and if the current element is not equal to the previous (element+1) then set the count to 1 else increase the count. Update max with a maximum of count and max. 

 

 Illustration:

Input: arr[] = {1, 9, 3, 10, 4, 20, 2}

First sort the array to arrange them in consecutive fashion.
arr[] = {1, 2, 3, 4, 9, 10, 20}

Now, store the distinct elements from the sorted array.
dist[] = {1, 2, 3, 4, 9, 10, 20}

Initialise countConsecutive with 0 which will increment when arr[i] == arr[i – 1] + 1 is true otherwise countConsecutive will re-initialise by 1.

Maintain a variable ans to store maximum count of consecutive elements so far.

At i = 0:

  • as i is 0 then re-initialise countConsecutive by 1.
  • ans = max(ans, countConsecutive) = max(0, 1) = 1

At i = 1:

  • check if (dist[1] == dist[0] + 1) = (2 == 1 + 1) = true
  • as the above condition is true, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 1 + 1 = 2
  • ans = max(ans, countConsecutive) = max(1, 2) = 1

At i = 2:

  • check if (dist[2] == dist[1] + 1) = (3 == 2 + 1) = true
  • as the above condition is true, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 2 + 1 = 3
  • ans = max(ans, countConsecutive) = max(2, 3) = 3

At i = 3:

  • check if (dist[3] == dist[2] + 1) = (4 == 3 + 1) = true
  • as the above condition is true, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 3 + 1 = 4
  • ans = max(ans, countConsecutive) = max(3, 4) = 4

At i = 4:

  • check if (dist[4] == dist[3] + 1) = (9 != 4 + 1) = false
  • as the above condition is false, therefore re-initialise countConsecutive by 1
    • countConsecutive = 1
  • ans = max(ans, countConsecutive) = max(4, 1) = 4

At i = 5:

  • check if (dist[5] == dist[4] + 1) = (10 == 9 + 1) = true
  • as the above condition is true, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 1 + 1 = 2
  • ans = max(ans, countConsecutive) = max(4, 2) = 4

At i = 6:

  • check if (dist[6] == dist[5] + 1) = (20 != 10 + 1) = false
  • as the above condition is false, therefore re-initialise countConsecutive by 1
    • countConsecutive = 1
  • ans = max(ans, countConsecutive) = max(4, 1) = 4

Therefore the longest consecutive subsequence is {1, 2, 3, 4}
Hence, ans is 4.

Follow the steps below to solve the problem:

  • Initialise ans and countConsecutive with 0.
  • Sort the arr[].
  • Store the distinct elements in dist[] array by traversing over the arr[].
  • Now, traverse on the dist[] array to find the count of consecutive elements.
  • Simultaneously maintain the answer variable.

Below is the implementation of above approach:

// C# program to find longest
// contiguous subsequence
using System;
using System.Collections.Generic;
 
class GFG {
 
    static int findLongestConseqSubseq(int[] arr, int n)
    {
 
        // Sort the array
        Array.Sort(arr);
 
        int ans = 0, count = 0;
 
        List<int> v = new List<int>();
        v.Add(10);
 
        // Insert repeated elements
        // only once in the vector
        for (int i = 1; i < n; i++) {
            if (arr[i] != arr[i - 1])
                v.Add(arr[i]);
        }
 
        // Find the maximum length
        // by traversing the array
        for (int i = 0; i < v.Count; i++) {
 
            // Check if the current element is
            // equal to previous element +1
            if (i > 0 && v[i] == v[i - 1] + 1)
                count++;
            else
                count = 1;
 
            // Update the maximum
            ans = Math.Max(ans, count);
        }
        return ans;
    }
 
    // Driver code
    static void Main()
    {
        int[] arr = { 1, 9, 3, 10, 4, 20, 2 };
        int n = arr.Length;
 
        Console.WriteLine(
            "Length of the Longest "
            + "contiguous subsequence is "
            + findLongestConseqSubseq(arr, n));
    }
}
 
// This code is contributed by divyeshrabadiya07
Output
Length of the Longest contiguous subsequence is 3

Time complexity: O(Nlog(N)), Time to sort the array is O(Nlog(N)).
Auxiliary space: O(N). Extra space is needed for storing distinct elements.

Longest Consecutive Subsequence using Hashing:

The idea is to use Hashing. We first insert all elements in a Set. Then check all the possible starts of consecutive subsequences.

Illustration:

Below image is the dry run for example arr[] = {1, 9, 3, 10, 4, 20, 2}:

Follow the steps below to solve the problem:

  • Create an empty hash.
  • Insert all array elements to hash.
  • Do the following for every element arr[i]
  • Check if this element is the starting point of a subsequence. To check this, simply look for arr[i] – 1 in the hash, if not found, then this is the first element of a subsequence.
  • If this element is the first element, then count the number of elements in the consecutive starting with this element. Iterate from arr[i] + 1 till the last element that can be found.
  • If the count is more than the previous longest subsequence found, then update this.

Below is the implementation of the above approach: 

Output
Length of the Longest contiguous subsequence is 4

Time complexity: O(N), Only one traversal is needed and the time complexity is O(n) under the assumption that hash insert and search takes O(1) time.
Auxiliary space: O(N), To store every element in the hashmap O(n) space is needed

Longest Consecutive Subsequence using Priority Queue:

The Idea is to use Priority Queue. Using priority queue it will sort the elements and eventually it will help to find consecutive elements.

Illustration:

Input: arr[] = {1, 9, 3, 10, 4, 20, 2}

Insert all the elements in the Priority Queue:

123491020

Initialise variable prev with first element of priority queue, prev will contain last element has been picked and it will help to check whether the current element is contributing for consecutive sequence or not.

prev = 1, countConsecutive = 1, ans = 1

Run the algorithm till the priority queue becomes empty.

23491020
  • current element is 2
    • prev + 1 == 2, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 1 + 1 = 2
    • update prev with current element, prev = 2
    • pop the current element
    • ans = max(ans, countConsecutive) = (1, 2) = 2
3491020
  • current element is 3
    • prev + 1 == 3, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 2 + 1 = 3
    • update prev with current element, prev = 3
    • pop the current element
    • ans = max(ans, countConsecutive) = (2, 3) = 3
491020
  • current element is 4
    • prev + 1 == 4, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 3 + 1 = 4
    • update prev with current element, prev = 4
    • pop the current element
    • ans = max(ans, countConsecutive) = (3, 4) = 4
91020
  • current element is 9
    • prev + 1 != 9, therefore re-initialise countConsecutive by 1
    • countConsecutive = 1
    • update prev with current element, prev = 9
    • pop the current element
    • ans = max(ans, countConsecutive) = (4, 1) = 4
1020
  • current element is 10
    • prev + 1 == 10, therefore increment countConsecutive by 1
    • countConsecutive = countConsecutive + 1 = 1 + 1 = 2
    • update prev with current element, prev = 10
    • pop the current element
    • ans = max(ans, countConsecutive) = (4, 2) =4
20
  • current element is 20
    • prev + 1 != 20, therefore re-initialise countConsecutive by 1
    • countConsecutive = 1
    • update prev with current element, prev = 20
    • pop the current element
    • ans = max(ans, countConsecutive) = (4, 1) = 4

Hence, the longest consecutive subsequence is 4.

Follow the steps below to solve the problem:

  • Create a Priority Queue to store the element
  • Store the first element in a variable
  • Remove it from the Priority Queue
  • Check the difference between this removed first element and the new peek element
  • If the difference is equal to 1 increase the count by 1 and repeats step 2 and step 3
  • If the difference is greater than 1 set counter to 1 and repeat step 2 and step 3
  • if the difference is equal to 0 repeat step 2 and 3
  • if counter greater than the previous maximum then store counter to maximum
  • Continue step 4 to 7 until we reach the end of the Priority Queue
  • Return the maximum value

Below is the implementation of the above approach: 

Output
Length of the Longest consecutive subsequence is 4

Time Complexity: O(N*log(N)), Time required to push and pop N elements is logN for each element.
Auxiliary Space: O(N), Space required by priority queue to store N elements.

create modal popup in blogger and dotnet

Leave a Comment

<div class="modal fade" id="alertModelAds" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true" style="z-index: 1052;">

        <div class="modal-dialog popup">


            <div class="modal-content modal-dialogads" style="background-color:#fff;width:100%">

    <div class="panel panel-blue">

        <a class="close float-end" data-bs-dismiss="modal" aria-label="Close" style="margin: 5px 10px 0; border-radius: 50%;cursor: pointer;position: sticky;z-index: 999;font-weight: bold;font-size: 16px;" onclick="setCookieNotify('cookieAd', 'yes', 1)"><span aria-hidden="true">X</span></a>

    </div>

    <div class="modal-body" style="position:unset;">

        <div>

                <div class="col-lg-12">

<div class="col-xs-12 col-md-12" style="text-align: center;">

                            <img src="/images/youtube-logo.png" />

                        </div>

                    <div class="col-xs-12 col-md-12" style="text-align: center;">

                        <span style="color:#4466c5!important;font-size: 18px;font-weight: 500;padding: 0 0 20px 0;display: inline-block;font-size: 24px;line-height: 30px;">Have you subscribed to DotNetTricks YouTube Channel yet?</span>

                    </div>                        

                    <div class="col-xs-12 col-md-12" style="padding: 2px 0 20px 0;text-align: center;font-size: 16px;">

                       Learn .NET Core, MVC, Azure, Microservices, Docker, Angular, React, DevOps and more.

                    </div>

                    <div style="text-align:center">                       

                            <a href="https://www.youtube.com/dotnettricks?sub_confirmation=1" class="btn btn-primary" title="Subscribe Now" target="_blank" style="background-color:#ff0000;border-radius:4px;">Subscribe Now</a>                       

                    </div>

                </div>            

        </div>

    </div></div>


        </div>

    </div>

<style>

    .popup .modal-content {

        border-radius: 0px !important;

    }

    .panel {

        border: 0px solid transparent;

    }


    .fade-scale {

        transform: scale(0);

        opacity: 0;

        -webkit-transition: all .25s linear;

        -o-transition: all .25s linear;

        transition: all .25s linear;

    }


        .fade-scale.in {

            opacity: 1;

            transform: scale(1);

        }


    .modal-dialog {

        padding-top: 100px;

    }


    .modal-dialogads [data-em=button] {

        position: relative;

        color: #fefefe;

        text-decoration: none;

        font-size: 30px;

        font-weight: bold;

        width: 400px;

        display: block;

        padding: 15px 0;

        -webkit-border-radius: 8px;

        -moz-border-radius: 8px;

        -ms-border-radius: 8px;

        -o-border-radius: 8px;

        border-radius: 8px;

        -webkit-box-shadow: 0px 0px 0px rgba(117,117,117,0.5), 0px -4px 0px rgba(0, 0, 0, 0.2) inset;

        -moz-box-shadow: 0px 0px 0px rgba(117,117,117,0.5), 0px -4px 0px rgba(0, 0, 0, 0.2) inset;

        box-shadow: 0px 0px 0px rgba(117,117,117,0.5), 0px -4px 0px rgba(0, 0, 0, 0.2) inset;

        margin: 10px auto;

        text-align: center;

        cursor: pointer;

        transition: all 100ms;

    }


    .modal {

        position: fixed;

        top: 0;

        right: 0;

        bottom: 0;

        left: 0;

        z-index: 1040;

        display: none;

        overflow: auto;

    }


    .modal-dialogads [data-em=text-line-1] {

        color: #000000;

        font-size: 35px;

        line-height: 65px;

        font-weight: 700;

        margin-top: 5px;

        text-align: center;

        -webkit-font-smoothing: antialiased;

        padding: 0 5%;

    }


    .modal-dialogads input, .modal-dialogads select {

        display: block;

        text-align: center;

        -webkit-border-radius: 8px;

        -moz-border-radius: 8px;

        -ms-border-radius: 8px;

        -o-border-radius: 8px;

        border-radius: 8px;

        font-size: 18px;

        color: #333333;

        font-style: italic;

        font-weight: 100;

        margin: 0 auto;

        border: 1px solid rgba(0,0,0,0.1);

        width: 500px;

        min-height: 48px;

        line-height: 52px;

        margin-bottom: 10px;

    }


    #select-country {

        display: block;

        text-align: center;

        -webkit-border-radius: 6px 0 0 6px;

        -moz-border-radius: 6px 0 0 6px;

        -ms-border-radius: 6px 0 0 6px;

        -o-border-radius: 6px 0 0 6px;

        font-size: 18px;

        color: #212121;

        margin: 0;

        width: 70px;

        height: 55px;

        padding: 0 0 0 7px;

        font-weight: 300;

        position: absolute;

        bottom: 0;

        left: 0;

        border-right: 1px solid #ddd;

        text-align-last: center;

    }


    .template-inputs {

        position: relative;

        width: 500px;

        margin: 0 auto;

    }

</style>

<script>

  function getCookie(c_name) {

            var c_value = document.cookie, c_start = c_value.indexOf(" " + c_name + "=");

            if (c_start == -1) c_start = c_value.indexOf(c_name + "=");

            if (c_start == -1) {

                c_value = null;

            } else {

                c_start = c_value.indexOf("=", c_start) + 1;

                var c_end = c_value.indexOf(";", c_start);

                if (c_end == -1) {

                    c_end = c_value.length;

                }

                c_value = unescape(c_value.substring(c_start, c_end));

            }

            return c_value;

        }


    function setCookieNotify(cname, cvalue, exdays) {

 $('#alertModelAds').modal("hide");

            var d = new Date();

            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));

            var expires = "expires=" + d.toUTCString();

            document.cookie = cname + "=" + cvalue + "; " + expires;

            $("#cookienotify").hide();

    }

    $(document).ready(function () {

        var acookie = getCookie("cookienotify");

        if (!acookie) {

            $("#cookienotify").show();

        }

        else {

            $("#cookienotify").hide();

        }

        

        var value = '0';

        if (value == 0) {

            $('#newslat').hide();

        }

        $("#showhide").click(function () {

            $("#showhidepara").toggle(1000);

            $('html,body').animate({ scrollTop: 9999 }, 'slow');

            $(this).find('i').toggleClass('fa fa-arrow-down fa fa-arrow-up');

        });

    });

     

   //accept cookie popover

        function getCookie(c_name) {

            var c_value = document.cookie,

                c_start = c_value.indexOf(" " + c_name + "=");

            if (c_start == -1) c_start = c_value.indexOf(c_name + "=");

            if (c_start == -1) {

                c_value = null;

            } else {

                c_start = c_value.indexOf("=", c_start) + 1;

                var c_end = c_value.indexOf(";", c_start);

                if (c_end == -1) {

                    c_end = c_value.length;

                }

                c_value = unescape(c_value.substring(c_start, c_end));

            }

            return c_value;

        }

        //ad settings

        $(document).ready(function() {            

            //ad cookie

            var adcookie = getCookie("cookieAdpop");

           // alert(adcookie)

           if (!adcookie) {

                setTimeout(function () {

                     $('#alertModelAds').modal("show");

                }, 10000);

            }

        });

       

    </script>

how to Change your app's target API level in andriod studio

Leave a Comment

 Open build.gradle under android/app/

find the android { } block

Change the following version to the below:

compileSdkVersion 27
buildToolsVersion "27.0.3"
minSdkVersion 16
targetSdkVersion 27

In case your current android/app/build.gradle has lines which look like :

compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

You will have to edit the android/build.gradle to include :

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 21
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }
    ...
}


  • go to build.gradle file in your project
  • look for defaultTargetSdkVersion & defaultCompileSdkVersion
  • then change value to 26 enter image description here
  • Just open your build.gradle(Module: app) and change your compileSdkVersion and targetSdkVersion 26 or more than 26. enter image description here