Number of valid indices in the permutation of first N natural numbers

Given a permutation P of first N natural numbers. The task is to find the number of i’s such that Pi ≤ Pj for all 1 ≤ j ≤ i in the permutation of first N natural numbers.

Examples:

Input: arr[] = {4, 2, 5, 1, 3}
Output: 3
0, 1 and 3 are such indices.

Input: arr[] = {4, 3, 2, 1}
Output: 4

Approach: For i = 1, …, N, define Mi = min{ Pj, 1 ≤ j ≤ i}. Now, when i(1 ≤ i ≤ N) is fixed, Mi = Pi holds if and only if for all j(1 ≤ j ≤ i), Pi ≤ Pj holds. Therefore, it is enough to calculate all the Mi. These can be calculated in the increasing order of i, so the problem could be solved in a total of O(N) time.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number of i's
// such that Pi <= Pj for all 1 <= j <= i
// in the permutation of first N natural numbers
int min_index(int p[], int n)
{
  
    // To store the count of such indices
    int ans = 0;
  
    // Store the mini value
    int mini = INT_MAX;
  
    // For all the elements
    for (int i = 0; i < n; i++) {
        if (p[i] <= mini)
            mini = p[i];
  
        if (mini == p[i])
            ans++;
    }
  
    // Return the required answer
    return ans;
}
  
// Driver code
int main()
{
    int P[] = { 4, 2, 5, 1, 3 };
    int n = sizeof(P) / sizeof(int);
  
    cout << min_index(P, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
  
    static int INT_MAX = Integer.MAX_VALUE;
      
    // Function to return the number of i's 
    // such that Pi <= Pj for all 1 <= j <= i 
    // in the permutation of first N natural numbers 
    static int min_index(int p[], int n) 
    
      
        // To store the count of such indices 
        int ans = 0
      
        // Store the mini value 
        int mini = INT_MAX; 
      
        // For all the elements 
        for (int i = 0; i < n; i++)
        
            if (p[i] <= mini) 
                mini = p[i]; 
      
            if (mini == p[i]) 
                ans++; 
        
      
        // Return the required answer 
        return ans; 
    
      
    // Driver code 
    public static void main (String[] args) 
    
        int P[] = { 4, 2, 5, 1, 3 }; 
        int n = P.length; 
      
        System.out.println(min_index(P, n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
import sys
  
INT_MAX = sys.maxsize
  
# Function to return the number of i's 
# such that Pi <= Pj for all 1 <= j <= i 
# in the permutation of first N natural numbers 
def min_index(p, n) : 
  
    # To store the count of such indices 
    ans = 0
  
    # Store the mini value 
    mini = INT_MAX; 
  
    # For all the elements 
    for i in range(n) : 
        if (p[i] <= mini) :
            mini = p[i]; 
  
        if (mini == p[i]) :
            ans += 1
  
    # Return the required answer 
    return ans; 
  
# Driver code 
if __name__ == "__main__"
  
    P = [ 4, 2, 5, 1, 3 ]; 
    n = len(P);
    print(min_index(P, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
  
    static int INT_MAX = int.MaxValue; 
      
    // Function to return the number of i's 
    // such that Pi <= Pj for all 1 <= j <= i 
    // in the permutation of first N natural numbers 
    static int min_index(int []p, int n) 
    
      
        // To store the count of such indices 
        int ans = 0; 
      
        // Store the mini value 
        int mini = INT_MAX; 
      
        // For all the elements 
        for (int i = 0; i < n; i++) 
        
            if (p[i] <= mini) 
                mini = p[i]; 
      
            if (mini == p[i]) 
                ans++; 
        
      
        // Return the required answer 
        return ans; 
    
      
    // Driver code 
    public static void Main () 
    
        int []P = { 4, 2, 5, 1, 3 }; 
        int n = P.Length; 
      
        Console.WriteLine(min_index(P, n)); 
    
  
// This code is contributed by AnkitRai01 

chevron_right


Output:

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01