[Solved] Competitive Test with Java, C++, Python

Competitive Test: “Axcent Academy” has arranged for a competitive test for medical students from rural villages. Those successful students of the test will be awarded the scholarship for their NEET preparations at Axcent Academy. Benny, the co-coordinator and founder of the academy has given one problem for the first stage of the test. The problem goes like this:
 
Given an array A1, A2, …, AN, count the number of subarrays of array A which are non-decreasing.
A subarrayA[i, j], where 1 ≤ i ≤ j ≤ N is a sequence of integers Ai, Ai+1, …, Aj.
A subarrayA[i, j] is non-decreasing if Ai ≤ Ai+1 ≤ Ai+2 ≤ … ≤ Aj. Count the total number of such subarrays.
 
Benny himself has not computed the solution of the problem. Write a program to help him find the answer for the same to evaluate the students.

Input Format:
The first line of input contains a single integer N denoting the size of array. Assume that the maximum value for N as 50.
The second line contains N space-separated integers A1A2, …,AN denoting the elements of the array.

Output Format:
Output in a single line, the count of the total number of such subarrays.
Refer sample input and output for formatting specifications.

Sample Input 1:
4
1 4 2 3

Sample Output 1:
6

Sample Input 2:
3
3 1 4

Sample Output 2:
4

Solution

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {//sauravhathi
            arr[i] = sc.nextInt();
        }
        int count = 0;
        for (int i = 0, j; i < n;) {
            for (j = i + 1; j < n; j++) {
                if (arr[j] < arr[j - 1]) {
                    break;
                    //sauravhathi
                }
            }
            count += (j - i) * (j - i + 1) / 2;
            i = j;
        }
        //sauravhathi
        System.out.println(count);
    }
}
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int n;
    long long count;
        cin >> n;
        vector<int> vals(n);
        //sauravhathi
        for (int i = 0; i < n; i++)
        {
            cin >> vals[i];
        }

        count = 0;
        for (int i = 0, j; i < n;)
        {
            //sauravhathi
            for (j = i + 1; j < n; j++)
            {
                //sauravhathi
                if (vals[j] < vals[j - 1])
                {
                    break;
                }
            }
            //sauravhathi

            count += (long long)(j - i) * (j - i + 1) / 2;
            ;
            i = j;
        }
        cout << count << endl;


    return 0;
}
n = int(input())
arr = list(map(int, input().split()))
c = 1
t = 0
#sauravhathi
for i in range(1, n):
    if arr[i] >= arr[i-1]:
        t += 1
    else:
        #sauravhathi
        t = 0
    c += (t+1)
print(c)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *