Commit 743b077f4a4d7659fbe0ef8e68b00a6bb2bdfae8

Authored by Ronaldo
1 parent 606ab984d8
Exists in main

Update.

Showing 7 changed files with 214 additions and 0 deletions Side-by-side Diff

NumberTheoryandCombinatorics/PrimerNumber/Prime.cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/prime-number2314/1
  5 +
  6 +class Solution{
  7 +public:
  8 + int isPrime(int N){
  9 + if(N <= 1){
  10 + return 0;
  11 + }else{
  12 + for(int i = 2; i <= sqrt(N); i++){
  13 + if(N % i == 0){
  14 + return 0;
  15 + }
  16 + }
  17 + }
  18 + return 1;
  19 + }
  20 +};
NumberTheoryandCombinatorics/SieveofEratosthenes/SieveOfEratosthenes.cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/sieve-of-eratosthenes5242/1
  5 +
  6 +//User function Template for C++
  7 +class Solution
  8 +{
  9 +public:
  10 + vector<int> sieveOfEratosthenes(int N)
  11 + {
  12 + vector<int> primes;
  13 + for(int i = 0; i <= N; i++){
  14 + if(isPrime(i)){
  15 + primes.push_back(i);
  16 + }
  17 + }
  18 + return primes;
  19 + }
  20 +
  21 + bool isPrime(unsigned long long n){
  22 +
  23 + if(n == 2 || n == 3){
  24 + return true;
  25 +
  26 + }else if(n <= 1 || n % 2 == 0 || n % 3 == 0){
  27 + return false;
  28 + }else{
  29 + for(int i = 5; i * i <= n; i += 6){
  30 + if(n % i == 0 || n % (i + 2) == 0){
  31 + return false;
  32 + }
  33 + }
  34 + }
  35 + return true;
  36 + }
  37 +};
SearchingSortingandBasicDataStructures/BinarySearch/Problem1/SearchingAnElementInASortedArray.cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/who-will-win-1587115621/1
  5 +
  6 +class Solution{
  7 +public:
  8 + // Function to find element in sorted array
  9 + // arr: input array
  10 + // N: size of array
  11 + // K: element to be searche
  12 + int searchInSorted(int arr[], int N, int K)
  13 + {
  14 +
  15 + int left = 0;
  16 + int right = N - 1;
  17 + int middle;
  18 +
  19 + while(right >= left){
  20 + middle = (left + right) / 2;
  21 + if(arr[middle] == K){
  22 + return 1;
  23 + }else if(arr[middle] > K){
  24 + right = middle - 1;
  25 + }else {
  26 + left = middle + 1;
  27 + }
  28 + }
  29 + return - 1;
  30 +
  31 +
  32 + }
  33 +};
SearchingSortingandBasicDataStructures/BinarySearch/Problem2/GeekAndHisMarks.cpp .cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/geek-and-his-marks/0
  5 +
  6 +#include <bits/stdc++.h>
  7 +
  8 +using namespace std;
  9 +
  10 +int main() {
  11 + vector<int> v;
  12 + int tests, size, querys, key, value;
  13 +
  14 + cin >> tests;
  15 + for(int i = 0; i < tests; i++){
  16 + cin >> size;
  17 + for(int j = 0; j < size; j++){
  18 + cin >> value;
  19 + v.push_back(value);
  20 + }
  21 +
  22 + cin >> querys;
  23 + for(int k = 0; k < querys; k++){
  24 + cin >> key;
  25 + long long int sum = 0;
  26 + for(int p = 0; p < v.size(); p++){
  27 + if(v[p] > key){
  28 + sum += v[p];
  29 + }
  30 + }
  31 + cout << sum << "\n";
  32 + }
  33 + v.clear();
  34 + }
  35 + return 0;
  36 +}
SearchingSortingandBasicDataStructures/BinarySearch/Problem3/GeekAndHisMarksTwo.cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/geek-and-his-marks-1611824182/0
  5 +
  6 +#include <bits/stdc++.h>
  7 +using namespace std;
  8 +
  9 +int main() {
  10 + vector<int> v;
  11 + int tests, size, value, querys, key, count;
  12 + cin >> tests;
  13 +
  14 + for(int i = 0; i < tests; i++){
  15 + cin >> size;
  16 + for(int j = 0; j < size; j++){
  17 + cin >> value;
  18 + v.push_back(value);
  19 + }
  20 +
  21 + cin >> querys;
  22 + for(int k = 0; k < querys; k++){
  23 + cin >> key;
  24 + count = 0;
  25 + for(int p = 0; p < v.size(); p++){
  26 + if(v[p] > key){
  27 + count++;
  28 + }
  29 + }
  30 + cout << count << "\n";
  31 + }
  32 + v.clear();
  33 +
  34 + }
  35 + return 0;
  36 +}
SearchingSortingandBasicDataStructures/LinearSearch/Problem1/SearchNumber.cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/searching-a-number0324/1
  5 +
  6 +//User function template for C++
  7 +class Solution{
  8 +public:
  9 + int search(int arr[], int n, int k) {
  10 + for(int i = 0; i < n; i++){
  11 + if(arr[i] == k){
  12 + return i + 1;
  13 + }
  14 + }
  15 + return -1;
  16 + }
  17 +};
SearchingSortingandBasicDataStructures/LinearSearch/Problem2/GeekandhisMarks.cpp View file @ 743b077
  1 +//
  2 +// Created by ronal on 2/11/2023.
  3 +// Problem Description Link.
  4 +// https://practice.geeksforgeeks.org/problems/geek-and-his-marks-1611824243/0
  5 +
  6 +#include <iostream>
  7 +#include <vector>
  8 +using namespace std;
  9 +
  10 +int ValuesMorethanInput(vector<int> &v, int x){
  11 + int count = 0;
  12 + for(auto &value : v){
  13 + if(value > x){
  14 + count++;
  15 + }
  16 + }
  17 + return count;
  18 +}
  19 +
  20 +int main() {
  21 + vector<int> v;
  22 + int n, x, tests, value;
  23 + cin >> tests;
  24 +
  25 + for(int i = 0; i < tests; i++){
  26 + cin >> n >> x;
  27 + for(int j = 0; j < n; j++){
  28 + cin >> value;
  29 + v.push_back(value);
  30 + }
  31 + cout << ValuesMorethanInput(v, x) << "\n";
  32 + v.clear();
  33 + }
  34 + return 0;
  35 +}