Norway


#include <stdio.h>

#include <limits.h>

 

int (int x, int y) {

    return (x > y) ? x : y ;

}

 

// a having maximum in an

int printTriplet(int A[], int n)

{

    // wrong input

    if (n <= 2) {

        printf(“No triplet exists since the array has less than elements”);

    }

 

    // 1. Find index of the largest, second largest and third largest

    // element in the array

    int max_index1 = 0, max_index2 = 1, max_index3 = 1;

    for (int i = 1; i < n; i++)

    {

        // if current element is less than the largest element found so far

        if (A[i] > A[max_index1]) {

            max_index3 = max_index2;

            max_index2 = max_index1;

            max_index1 = i;

        }

        // if current element is less than the second largest element

        // found so far

        else if (max_index2 == 1 || A[i] > A[max_index2]) {

            max_index3 = max_index2;

            max_index2 = i;

        }

        // if current element is less than the third largest element

        // found so far

        else if (max_index3 == 1 || A[i] > A[max_index3]) {

            max_index3 = i;

        }

    }

 

    // 2. Find index of the smallest and second smallest element in the array

    int min_index1 = 0, min_index2 = 1;

    for (int i = 1; i < n; i++)

    {

        // if current element is more than the smallest element found so far

        if (A[i] < A[min_index1]) {

            min_index2 = min_index1;

            min_index1 = i;

        }

        // if current element is more than the second smallest element

        // found so far

        else if(min_index2 == 1 || A[i] < A[min_index2]) {

            min_index2 = i;

        }

    }

 

    if (A[max_index1] * A[max_index2] * A[max_index3] >

            A[min_index1] * A[min_index2] * A[max_index1]) {

        printf(“Triplet is (%d, %d, %d)”, A[max_index1], A[max_index2],

                                        A[max_index3]);

    }

    else {

        printf(“Triplet is (%d, %d, %d)”, A[min_index1], A[min_index2],

                                        A[max_index1]);

    }

}

 

// main function

int main()

{

    int A[] = { 4, 1, 8, 9, 6 };

    int n = sizeof(A) / sizeof(A[0]);

 

    printTriplet(A, n);

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here