Finding an element in array, whose index number and element value sameLinearithmic lower bound for 1-D “distinct” closest pair of points problemFind The missing number in range [0,n]Divide-and-Conquer ExerciseFinding a segment which has equal number of segments before and after itFind the index of minimum number that is greater than key given of a sorted array, does these two functions return same result?Creating an O(n log n) time and O(n) space algorithm for counting pairs in an arrayFinding a small element in a changing arraySearching a sorted array to find the $k$ closest values to a target value $T$Is there an $O(n^2)$ or $O(n^3)$ time algorithm to check if a number is a sum of $k$ elements of sorted array?Min no.of operations required to convert an array to which it should contain elements of equal frequency

Providence Pentominoes Puzzle By Andrew Bradburn (Jigsaw)

Will this character get back his Infinity Stone?

A ​Note ​on ​N!

How do Bards prepare spells?

Trainer for recumbent bikes

Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?

Will tsunami waves travel forever if there was no land?

Help to reproduce a tcolorbox with a decoration

Sci-fi novel series with instant travel between planets through gates. A river runs through the gates

Why do 401k up to company match, then fill Roth IRA, then finish filling 401k?

Can solid acids and bases have pH values? If not, how are they classified as acids or bases?

Mac Pro install disk keeps ejecting itself

Is DC-to-DC (24 V to 12 V) buck conversion typically more efficient than AC-to-DC (110 V to 12 V) conversion?

Unexpected email from Yorkshire Bank

Alternatives to Overleaf

How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?

Minimum value of 4 digit number divided by sum of its digits

Who is the Umpire in this picture?

Rivers without rain

Counterexample: a pair of linearly ordered sets that are isomorphic to subsets of the other, but not isomorphic between them

Question relating to a number theoretic function

Does this extra sentence in the description of the warlock's Eyes of the Rune Keeper eldritch invocation appear in any official reference?

Was it really necessary for the Lunar module LM to have 2 stages?

With a Canadian student visa, can I spend a night at Vancouver before continuing to Toronto?



Finding an element in array, whose index number and element value same


Linearithmic lower bound for 1-D “distinct” closest pair of points problemFind The missing number in range [0,n]Divide-and-Conquer ExerciseFinding a segment which has equal number of segments before and after itFind the index of minimum number that is greater than key given of a sorted array, does these two functions return same result?Creating an O(n log n) time and O(n) space algorithm for counting pairs in an arrayFinding a small element in a changing arraySearching a sorted array to find the $k$ closest values to a target value $T$Is there an $O(n^2)$ or $O(n^3)$ time algorithm to check if a number is a sum of $k$ elements of sorted array?Min no.of operations required to convert an array to which it should contain elements of equal frequency













1












$begingroup$


Given a sorted array of distinct integer $A[1,2.....n]$ the tightest upper bound to check the existence of any index $I$, for which $A[I]=I$ is equal to _______________ ?




I thought here answer that mean time complexity will be $O(1)$, because directly getting the searching index and then checking if $A[I]=I$, but answer given as $O(log n)$.



Please help me out, which will be correct answer?










share|cite|improve this question











$endgroup$











  • $begingroup$
    You have to find i.
    $endgroup$
    – gnasher729
    15 mins ago















1












$begingroup$


Given a sorted array of distinct integer $A[1,2.....n]$ the tightest upper bound to check the existence of any index $I$, for which $A[I]=I$ is equal to _______________ ?




I thought here answer that mean time complexity will be $O(1)$, because directly getting the searching index and then checking if $A[I]=I$, but answer given as $O(log n)$.



Please help me out, which will be correct answer?










share|cite|improve this question











$endgroup$











  • $begingroup$
    You have to find i.
    $endgroup$
    – gnasher729
    15 mins ago













1












1








1





$begingroup$


Given a sorted array of distinct integer $A[1,2.....n]$ the tightest upper bound to check the existence of any index $I$, for which $A[I]=I$ is equal to _______________ ?




I thought here answer that mean time complexity will be $O(1)$, because directly getting the searching index and then checking if $A[I]=I$, but answer given as $O(log n)$.



Please help me out, which will be correct answer?










share|cite|improve this question











$endgroup$




Given a sorted array of distinct integer $A[1,2.....n]$ the tightest upper bound to check the existence of any index $I$, for which $A[I]=I$ is equal to _______________ ?




I thought here answer that mean time complexity will be $O(1)$, because directly getting the searching index and then checking if $A[I]=I$, but answer given as $O(log n)$.



Please help me out, which will be correct answer?







algorithms






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited 18 mins ago









Mr. Sigma.

734626




734626










asked 7 hours ago









SresthaSrestha

286




286











  • $begingroup$
    You have to find i.
    $endgroup$
    – gnasher729
    15 mins ago
















  • $begingroup$
    You have to find i.
    $endgroup$
    – gnasher729
    15 mins ago















$begingroup$
You have to find i.
$endgroup$
– gnasher729
15 mins ago




$begingroup$
You have to find i.
$endgroup$
– gnasher729
15 mins ago










2 Answers
2






active

oldest

votes


















2












$begingroup$

If the array had $A[i]=i, foralli$, then the complexity would have been $theta(1)$ as then all we need to check whether the length of the array is greater than the element or not. If it's greater we have the element, else we haven't.



But the array isn't like that. Array could be like $1,3,4,8,9,12$ as well.



The question is asking to find an element $A[i]$ in the sorted list which is situated at the index $i$.



The algorithm to find such element is mere a slight modification of binary search where you update low & high based on $i<A[i]$ - if it's true update high=mid-1 else if $i>A[i]$ update low=mid+1. The complexity is indeed $theta(log_2n)$






share|cite|improve this answer











$endgroup$












  • $begingroup$
    Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
    $endgroup$
    – Srestha
    5 hours ago










  • $begingroup$
    @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
    $endgroup$
    – Mr. Sigma.
    3 hours ago











  • $begingroup$
    Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
    $endgroup$
    – Srestha
    2 hours ago











  • $begingroup$
    @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
    $endgroup$
    – AcId
    1 hour ago






  • 1




    $begingroup$
    @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
    $endgroup$
    – Mr. Sigma.
    57 mins ago



















0












$begingroup$

Take the hints. The integers can be positive or negative, but they are distinct. The array is sorted, which usually means a1 ≤ a2 ≤ a3 ... But the are distinct, so a1 < a2 < a3 < ... And they are integers, so they are at least 1 apart. So we have a2 ≥ a1 + 1, a3 ≥ a2 + 1, a4 ≥ a3 + 1 and so on. What does that mean for ai - i?



(If you figure that out, the solution is trivial).






share|cite|improve this answer









$endgroup$













    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "419"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f108642%2ffinding-an-element-in-array-whose-index-number-and-element-value-same%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2












    $begingroup$

    If the array had $A[i]=i, foralli$, then the complexity would have been $theta(1)$ as then all we need to check whether the length of the array is greater than the element or not. If it's greater we have the element, else we haven't.



    But the array isn't like that. Array could be like $1,3,4,8,9,12$ as well.



    The question is asking to find an element $A[i]$ in the sorted list which is situated at the index $i$.



    The algorithm to find such element is mere a slight modification of binary search where you update low & high based on $i<A[i]$ - if it's true update high=mid-1 else if $i>A[i]$ update low=mid+1. The complexity is indeed $theta(log_2n)$






    share|cite|improve this answer











    $endgroup$












    • $begingroup$
      Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
      $endgroup$
      – Srestha
      5 hours ago










    • $begingroup$
      @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
      $endgroup$
      – Mr. Sigma.
      3 hours ago











    • $begingroup$
      Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
      $endgroup$
      – Srestha
      2 hours ago











    • $begingroup$
      @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
      $endgroup$
      – AcId
      1 hour ago






    • 1




      $begingroup$
      @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
      $endgroup$
      – Mr. Sigma.
      57 mins ago
















    2












    $begingroup$

    If the array had $A[i]=i, foralli$, then the complexity would have been $theta(1)$ as then all we need to check whether the length of the array is greater than the element or not. If it's greater we have the element, else we haven't.



    But the array isn't like that. Array could be like $1,3,4,8,9,12$ as well.



    The question is asking to find an element $A[i]$ in the sorted list which is situated at the index $i$.



    The algorithm to find such element is mere a slight modification of binary search where you update low & high based on $i<A[i]$ - if it's true update high=mid-1 else if $i>A[i]$ update low=mid+1. The complexity is indeed $theta(log_2n)$






    share|cite|improve this answer











    $endgroup$












    • $begingroup$
      Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
      $endgroup$
      – Srestha
      5 hours ago










    • $begingroup$
      @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
      $endgroup$
      – Mr. Sigma.
      3 hours ago











    • $begingroup$
      Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
      $endgroup$
      – Srestha
      2 hours ago











    • $begingroup$
      @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
      $endgroup$
      – AcId
      1 hour ago






    • 1




      $begingroup$
      @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
      $endgroup$
      – Mr. Sigma.
      57 mins ago














    2












    2








    2





    $begingroup$

    If the array had $A[i]=i, foralli$, then the complexity would have been $theta(1)$ as then all we need to check whether the length of the array is greater than the element or not. If it's greater we have the element, else we haven't.



    But the array isn't like that. Array could be like $1,3,4,8,9,12$ as well.



    The question is asking to find an element $A[i]$ in the sorted list which is situated at the index $i$.



    The algorithm to find such element is mere a slight modification of binary search where you update low & high based on $i<A[i]$ - if it's true update high=mid-1 else if $i>A[i]$ update low=mid+1. The complexity is indeed $theta(log_2n)$






    share|cite|improve this answer











    $endgroup$



    If the array had $A[i]=i, foralli$, then the complexity would have been $theta(1)$ as then all we need to check whether the length of the array is greater than the element or not. If it's greater we have the element, else we haven't.



    But the array isn't like that. Array could be like $1,3,4,8,9,12$ as well.



    The question is asking to find an element $A[i]$ in the sorted list which is situated at the index $i$.



    The algorithm to find such element is mere a slight modification of binary search where you update low & high based on $i<A[i]$ - if it's true update high=mid-1 else if $i>A[i]$ update low=mid+1. The complexity is indeed $theta(log_2n)$







    share|cite|improve this answer














    share|cite|improve this answer



    share|cite|improve this answer








    edited 16 mins ago

























    answered 6 hours ago









    Mr. Sigma.Mr. Sigma.

    734626




    734626











    • $begingroup$
      Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
      $endgroup$
      – Srestha
      5 hours ago










    • $begingroup$
      @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
      $endgroup$
      – Mr. Sigma.
      3 hours ago











    • $begingroup$
      Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
      $endgroup$
      – Srestha
      2 hours ago











    • $begingroup$
      @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
      $endgroup$
      – AcId
      1 hour ago






    • 1




      $begingroup$
      @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
      $endgroup$
      – Mr. Sigma.
      57 mins ago

















    • $begingroup$
      Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
      $endgroup$
      – Srestha
      5 hours ago










    • $begingroup$
      @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
      $endgroup$
      – Mr. Sigma.
      3 hours ago











    • $begingroup$
      Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
      $endgroup$
      – Srestha
      2 hours ago











    • $begingroup$
      @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
      $endgroup$
      – AcId
      1 hour ago






    • 1




      $begingroup$
      @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
      $endgroup$
      – Mr. Sigma.
      57 mins ago
















    $begingroup$
    Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
    $endgroup$
    – Srestha
    5 hours ago




    $begingroup$
    Is binary search always take an element a[I]=I?? ok, tell me one thing , if in this algorithm, I want T.C.=O(1), what modification needed for this code?
    $endgroup$
    – Srestha
    5 hours ago












    $begingroup$
    @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
    $endgroup$
    – Mr. Sigma.
    3 hours ago





    $begingroup$
    @Srestha You can't achieve in $O(1)$ time. E.g, 1,4,6,9,12 (index starting with 1)
    $endgroup$
    – Mr. Sigma.
    3 hours ago













    $begingroup$
    Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
    $endgroup$
    – Srestha
    2 hours ago





    $begingroup$
    Mr. Sigma, I have not got the point. Here it is told a[I]=I, that means a[1]=1, a[2]=2,a[3]=3......only this type of sequence possible here. Then how r u telling a[1]=1,a[2]=4, a[3]=6 .... is a possible sequence.
    $endgroup$
    – Srestha
    2 hours ago













    $begingroup$
    @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
    $endgroup$
    – AcId
    1 hour ago




    $begingroup$
    @Srestha Your question specifies that the input is a sorted array of distinct integers - it does not specify that, in the input, $A[i] = i$. If this is the case you should update your question. However, this would make the problem trivial.
    $endgroup$
    – AcId
    1 hour ago




    1




    1




    $begingroup$
    @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
    $endgroup$
    – Mr. Sigma.
    57 mins ago





    $begingroup$
    @Srestha You have understood the question wrongly. Question involving $A[i]=i, foralli$ would be very trivial.
    $endgroup$
    – Mr. Sigma.
    57 mins ago












    0












    $begingroup$

    Take the hints. The integers can be positive or negative, but they are distinct. The array is sorted, which usually means a1 ≤ a2 ≤ a3 ... But the are distinct, so a1 < a2 < a3 < ... And they are integers, so they are at least 1 apart. So we have a2 ≥ a1 + 1, a3 ≥ a2 + 1, a4 ≥ a3 + 1 and so on. What does that mean for ai - i?



    (If you figure that out, the solution is trivial).






    share|cite|improve this answer









    $endgroup$

















      0












      $begingroup$

      Take the hints. The integers can be positive or negative, but they are distinct. The array is sorted, which usually means a1 ≤ a2 ≤ a3 ... But the are distinct, so a1 < a2 < a3 < ... And they are integers, so they are at least 1 apart. So we have a2 ≥ a1 + 1, a3 ≥ a2 + 1, a4 ≥ a3 + 1 and so on. What does that mean for ai - i?



      (If you figure that out, the solution is trivial).






      share|cite|improve this answer









      $endgroup$















        0












        0








        0





        $begingroup$

        Take the hints. The integers can be positive or negative, but they are distinct. The array is sorted, which usually means a1 ≤ a2 ≤ a3 ... But the are distinct, so a1 < a2 < a3 < ... And they are integers, so they are at least 1 apart. So we have a2 ≥ a1 + 1, a3 ≥ a2 + 1, a4 ≥ a3 + 1 and so on. What does that mean for ai - i?



        (If you figure that out, the solution is trivial).






        share|cite|improve this answer









        $endgroup$



        Take the hints. The integers can be positive or negative, but they are distinct. The array is sorted, which usually means a1 ≤ a2 ≤ a3 ... But the are distinct, so a1 < a2 < a3 < ... And they are integers, so they are at least 1 apart. So we have a2 ≥ a1 + 1, a3 ≥ a2 + 1, a4 ≥ a3 + 1 and so on. What does that mean for ai - i?



        (If you figure that out, the solution is trivial).







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered 10 mins ago









        gnasher729gnasher729

        12.3k1318




        12.3k1318



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Computer Science Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f108642%2ffinding-an-element-in-array-whose-index-number-and-element-value-same%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

            How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2

            Nissan Patrol Зміст Перше покоління — 4W60 (1951-1960) | Друге покоління — 60 series (1960-1980) | Третє покоління (1980–2002) | Четверте покоління — Y60 (1987–1998) | П'яте покоління — Y61 (1997–2013) | Шосте покоління — Y62 (2010- ) | Посилання | Зноски | Навігаційне менюОфіційний український сайтТест-драйв Nissan Patrol 2010 7-го поколінняNissan PatrolКак мы тестировали Nissan Patrol 2016рвиправивши або дописавши її