Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

// Cpp program to find surpasser count of each // element in array using naive approach…

  1. User

    // Cpp program to find surpasser count of each // element in array using naive approach #include #include using namespace std;

    // Returns surpasser counts of each element // in vector vector findSurpasser(vector& arr) { int n = arr.size();

    // Vector to store surpasser counts
    vector<int> res(n, 0);
    
    for (int i = 0; i < n; i++) {
      
        // Find surpasser for arr[i]
        int count = 0;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] > arr[i])
                count++;
        }      
       
        res[i] = count; 
    }
    
    return res;
    

    }

    int main() { vector arr = {2, 7, 5, 3, 0, 8, 1}; vector res = findSurpasser(arr);
    for (int count : res) { cout << count << " "; } return 0; }

    Give me code in Python, Java, C#, JavaScript

    Remember like Cpp is for C++ and at start replace Cpp with language name

    without changing variable name and also do not change comments

    have one line space after code when comment is written.

    don’t use class if needed then name it GfG

    don’t use public if not needed.

  2. Assistant

    Python:

    # Python program to find surpasser count of each
    # element in array using naive approach
    def findSurpasser(arr):
        # Returns surpasser counts of each element
        # in vector
        n = len(arr)
      
        # Vector to store surpasser counts
        res = [0] * n
        
        for i in range(n):
          
            # Find surpasser for arr[i]
            count = 0
            for j in range(i + 1, n):
                if arr[j] > arr[i]:
                    count += 1
               
            res[i] = count 
        
        return res
    
    def main():
        arr = [2, 7, 5, 3, 0, 8, 1]
        res = findSurpasser(arr)    
        for count in res:
            print(count, end=' ')
    
    if __name__ == "__main__":
        main()
    

    Java:

    // Java program to find surpasser count of each
    // element in array using naive approach
    import java.util.*;
    
    class GfG {
    
        // Returns surpasser counts of each element
        // in vector
        static int[] findSurpasser(int[] arr) {
            // Vector to store surpasser counts
            int n = arr.length;
            int[] res = new int[n];
            
            for (int i = 0; i < n; i++) {
              
                // Find surpasser for arr[i]
                int count = 0;
                for (int j = i + 1; j < n; j++) {
                    if (arr[j] > arr[i])
                        count++;
                }      
               
                res[i] = count; 
            }
            
            return res;
        }
        
        public static void main(String[] args) {
            int[] arr = {2, 7, 5, 3, 0, 8, 1};
            int[] res = findSurpasser(arr);    
            for (int count : res) {
                System.out.print(count + " ");
            }
        }
    }
    

    C#:

    // C# program to find surpasser count of each
    // element in array using naive approach
    using System;
    using System.Collections.Generic;
    
    class GfG
    {
        // Returns surpasser counts of each element
        // in vector
        static int[] findSurpasser(List<int> arr)
        {
            // Vector to store surpasser counts
            int n = arr.Count;
            int[] res = new int[n];
            
            for (int i = 0; i < n; i++)
            {
                // Find surpasser for arr[i]
                int count = 0;
                for (int j = i + 1; j < n; j++)
                {
                    if (arr[j] > arr[i])
                        count++;
                }      
               
                res[i] = count; 
            }
            
            return res;
        }
        
        static void Main()
        {
            List<int> arr = new List<int> {2, 7, 5, 3, 0, 8, 1};
            int[] res = findSurpasser(arr);    
            foreach (int count in res)
            {
                Console.Write(count + " ");
            }
        }
    }
    

    JavaScript:

    // JavaScript program to find surpasser count of each
    // element in array using naive approach
    
    function findSurpasser(arr) {
        // Returns surpasser counts of each element
        // in vector
        let n = arr.length;
      
        // Vector to store surpasser counts
        let res = new Array(n).fill(0);
        
        for (let i = 0; i < n; i++) {
          
            // Find surpasser for arr[i]
            let count = 0;
            for (let j = i + 1; j < n; j++) {
                if (arr[j] > arr[i])
                    count++;
            }      
           
            res[i] = count; 
        }
        
        return res;
    }
    
    function main() {
        let arr = [2, 7, 5, 3, 0, 8, 1];
        let res = findSurpasser(arr);    
        for (let count of res) {
            process.stdout.write(count + " ");
        }
    }
    
    main();