Skip to main content

Array practice sample question in C++ for Code Clash Vol. 2

Write a C++ program to find the largest element of a given array of integers.

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 5, 3, 4};
    int n = 5, max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) 
            max = arr[i];
    }
    cout << max;
    return 0;
}

Write a C++ program to find the largest three elements in an array.

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 5, 3, 9, 7};
    int n = 5;
    int first = -1, second = -1, third = -1;
    for (int i = 0; i < n; i++) {
        if (arr[i] > first) {
            third = second;
            second = first;
            first = arr[i];
        } else if (arr[i] > second) {
            third = second;
            second = arr[i];
        } else if (arr[i] > third) {
            third = arr[i];
        }
    }
    cout << first << " " << second << " " << third;
    return 0;
}

 

Write a C++ program to find the second largest element in an array of integers.  

#include <iostream>

using namespace std;

int main() {

    int arr[] = {5,3,8,7,6,2,2,2,7,7,7,7,8};

    int n = 5, first = -1, second = -1;

    for (int i = 0; i < n; i++) {

        if (arr[i] > first) {

            second = first;

            first = arr[i];

        } else if (arr[i] > second && arr[i] != first) {

            second = arr[i];

        }

    }

    cout << second;

    return 0;

}

 

Write a C++ program to find the k largest elements in a given array of integers.

#include <iostream>
using namespace std;
int main() {
    int arr[] = {10, 2, 10, 1, 2};
    int n = 5, k = 3;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] < arr[j]) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    for (int i = 0; i < k; i++) cout << arr[i] << " ";
    return 0;
}

 

Write a C++ program to find the second smallest elements in a given array of integers.  

 

#include <iostream>
using namespace std;
int main() {
    int arr[] = {5, 2, 2, 3, 2};
    int n = 5;
    int first = arr[0], second = arr[0];
    for (int i = 0; i < n; i++) {
        if (arr[i] < first) {
            second = first;
            first = arr[i];
        } else if (arr[i] < second && arr[i] != first) {
            second = arr[i];
        }
    }
    if (second == first)
        cout << "No second smallest element exists.";
    else
        cout << second;
    return 0;
}

 

Write a C++ program to find all elements in an array of integers that are greater then or equal  2. 

#include <iostream>
using namespace std;
int main() {
    int arr[] = {3, 1, 4, 1, 0};
    int n = 5, count = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] >= 2) 
            cout << arr[i] << " ";
    }
    return 0;
}

 

Write a C++ program to find the most frequent element in an array of integers.

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 2, 8, 5,5,5,5};
    int n = sizeof(arr)/4; 
      int maxCount = 0, res = -1;
    for (int i = 0; i < n; i++) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j]) count++;
        }
        if (count > maxCount) {
            maxCount = count;
            res = arr[i];
        }
    }
    cout << res;
    return 0;
}

Write a C++ program to find the next more powerful element of every element of a given array of integers. Ignore those elements that have no greater element.

#include <iostream>

using namespace std;

int main() {

    int arr[] = {4,5,2,10,1,0,5,14,7,2};

    int n = sizeof(arr)/4;

    for (int i = 0; i < n; i++) {

        int next = -1;

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

            if (arr[j] > arr[i]) {

                next = arr[j];

                break;

            }

        }

        cout << arr[i] << " -> " << next << endl;

    }

    return 0;

}

 

Write a C++ program to separate even and odd numbers in an array of integers. Put all even numbers first, and then odd numbers.

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = sizeof(arr) / sizeof(arr[0]);
    int even[n], odd[n], e = 0, o = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0) {
            even[e++] = arr[i];
        } else {
            odd[o++] = arr[i];
        }
    }
    for (int i = 0; i < e; i++) {
        cout << even[i] << " ";
    }
    for (int i = 0; i < o; i++) {
        cout << odd[i] << " ";
    }
    return 0;
}
_______________________________________________________

#include <iostream>
using namespace std;
int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = sizeof(arr)/4;
      int even[n], odd[n], e = 0, o = 0;
    for (int i = 0; i < n; i++) 
      {
        if (arr[i] % 2 == 0) even[e++] = arr[i];
        else odd[o++] = arr[i];
    }
    for (int i = 0; i < e; i++) cout << even[i] << " ";
    for (int i = 0; i < o; i++) cout << odd[i] << " ";
    return 0;
}

 

Write a C++ program to separate 0s and 1s from a given array of values 0 and 1.

#include <iostream>

using namespace std;

 

int main() {

    int arr[] = {0, 1, 0, 1, 0, 1,1,1,0,1,0,1,0,1};

    int n =sizeof(arr)/4;

      int count = 0;

    for (int i = 0; i < n; i++) {

        if (arr[i] == 0) count++;

    }

    for (int i = 0; i < count; i++)

      cout << "0 ";

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

      cout << "1 ";

    return 0;

}

Comments

Popular posts from this blog

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...

C++ Lec-1 (Introduction to Programming Languages)

  Introduction to Programming Languages ________________________________________________________________________ P rogramming languages are formal languages comprising a set of instructions that produce various kinds of output. They are used in computer programming to implement algorithms and control the behavior of machines. Language Generation Programming languages have evolved through several generations: First Generation (1GL) : Machine language, the most basic level of programming languages, consisting of binary code. Second Generation (2GL) : Assembly language, a low-level language with a strong correspondence to the machine language instructions. Third Generation (3GL) : High-level languages like C, C++, Java, Python, which are more abstract and easier for humans to understand. Fourth Generation (4GL) : Languages closer to human language, often used for database querying and report generation (e.g., ...

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...