Correctness and the Loop Invariant - HackerRank || HackerRank Correctness and the Loop Invariant Solution

Problem Name: Correctness and the Loop Invariant
Judge: HackerRank



HackerRank Correctness and the Loop Invariant Solution in C

#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int array[n];

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &array[i]);
    }

    int temp;

    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (array[i] > array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}


HackerRank Correctness and the Loop Invariant Solution in C++/Cpp

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int array[n];

    for (int i = 0; i < n; i++)
    {
        cin >> array[i];
    }

    int m = sizeof(array) / sizeof (array[0]);
    sort(array, array + m);
    for (int i = 0; i < n; i++)
    {
        cout << array[i] << " ";
    }

    return 0;
}


HackerRank Correctness and the Loop Invariant Solution in Java

import java.util.Arrays;
import java.util.Scanner;

public class Correctness_and_the_Loop_Invariant {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = input.nextInt();
        }
        Arrays.sort(array);
        for (int i = 0; i < n; i++) {
            System.out.print(array[i] + " ");
        }
    }
}





Next Post Previous Post
No Comment
Add Comment
comment url