filename
stringlengths
7
140
content
stringlengths
0
76.7M
code/online_challenges/src/codechef/MNMX/MNMX.c
#include <stdio.h> int main() { int t; scanf("%d", & t); while (t--) { long long int n, i; scanf("%lld", & n); long long int a[n]; for (i = 0; i < n; ++i) { scanf("%lld", & a[i]); } long long int min = a[0]; for (i = 0; i < ...
code/online_challenges/src/codechef/MNMX/README.md
# Problem Link: [MNMX](https://www.codechef.com/problems/MNMX/) # Description Chef loves to play with arrays by himself. Today, he has an array A consisting of N distinct integers. He wants to perform the following operation on his array A. Select a pair of adjacent integers and remove the larger one of these two. T...
code/online_challenges/src/codechef/NBONACCI/NBONACCI.cpp
#include <iostream> #include <algorithm> int main () { int n,q; std::cin >> n >> q; int a[n]; for (int i = 0; i < n; ++i) { std::cin>>a[i]; } int x = 0; int xors[n] = {0}; for (int i = 0; i < n; ++i) { x = x ^ a[i]; xors[i] = x; } while (q--) ...
code/online_challenges/src/codechef/NBONACCI/README.md
# Problem link [NBONACCI](https://www.codechef.com/problems/NBONACCI). # Description An N-bonacci sequence is an infinite sequence F1,F2,… such that for each integer i>N, Fi is calculated as f(Fi−1,Fi−2,…,Fi−N), where f is some function. A XOR N-bonacci sequence is an N-bonacci sequence for which f(Fi−1,Fi−2,…,Fi−N)...
code/online_challenges/src/codechef/NEWSCH/NEWSCH.cpp
#include <iostream> #include <cstring> using namespace std; #define MOD 1000000007 uint64_t modmul(const uint64_t x, const uint64_t y) { if (x > (1 << 30) && y > (1 << 30)) return ((x >> 30)*((y << 30) % MOD) + y*(x & ((1 << 30) - 1))) % MOD; uint64_t z = x*y; if (z >= MOD) z %= MOD; return z; } uint64_t m...
code/online_challenges/src/codechef/NEWSCH/README.md
# Problem Link [NEWSCH](https://www.codechef.com/problems/NEWSCH) # Description Scheme? - Too loudly said. Just a new idea. Now Chef is expanding his business. He wants to make some new restaurants in the big city of Lviv. To make his business competitive he should interest customers. Now he knows how. But don't tell...
code/online_challenges/src/codechef/NUKES/NUKES.c
#include <stdio.h> int main(void) { int n, max, k; scanf("%d %d %d", & n, & max, & k); int i = 0; int l = max + 1; while (i < k) { printf("%d ", n % l); n = n / l; ++i; } printf("\n"); return 0; }
code/online_challenges/src/codechef/NUKES/README.md
# Problem Link: [NUKES](https://www.codechef.com/problems/NUKES/) # Description There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there are more than N particles in a chamber, a reaction will caus...
code/online_challenges/src/codechef/NUMGAME2/NUMGAME2.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { long long int n; scanf("%lld", & n); if (n % 4 == 1) printf("ALICE\n"); else printf("BOB\n"); } return 0; }
code/online_challenges/src/codechef/NUMGAME2/README.md
# Problem Link: [NUMGAME2](https://www.codechef.com/problems/NUMGAME2) # Description Alice and Bob play the following game.They choose a number N to play with.The runs are as follows : 1.Bob plays first and the two players alternate. 2.In his/her turn ,a player can subtract from N any prime number(including 1) less ...
code/online_challenges/src/codechef/OJUMPS/OJUMPS.c
#include<stdio.h> int main() { long long int a; scanf("%lld", & a); long long int b = a % 6; if (b == 0) printf("yes\n"); else if (b == 1) printf("yes\n"); else if (b == 3) printf("yes\n"); else printf("no\n"); return 0; }
code/online_challenges/src/codechef/OJUMPS/README.md
# Problem Link: [OJUMPS](https://www.codechef.com/problems/OJUMPS/) # Description This morning Chef wants to jump a little. In a few minutes he will arrive at the point 0. Then he will perform a lot of jumps in such a sequence: 1-jump, 2-jump, 3-jump, 1-jump, 2-jump, 3-jump, 1-jump, and so on. 1-jump means that if Ch...
code/online_challenges/src/codechef/P-BATTLE/PBATTLE.cpp
// Question link : https://www.codechef.com/submit/PBATTLE //Author - Vishwas Kapoor #include<bits/stdc++.h> using namespace std; #define int long long #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); void solve(){ int n; cin>>n; vector<pair<int,int>> pg; // pokemon ground fl(n){ int x; ...
code/online_challenges/src/codechef/PRIME1/PRIME1.c
#include <stdio.h> #include <math.h> int main() { int t; scanf("%d", & t); while (t--) { int m, n; scanf("%d %d", & m, & n); int i; for (i = m; i <= n; ++i) if (isPrime(i)) printf("%d\n", i); printf("\n"); } } int isPrime(int n)...
code/online_challenges/src/codechef/PRIME1/README.md
# Problem Link: [PRIME1](https://www.codechef.com/problems/PRIME1/) # Description Ram wants to generate some prime numbers for his cryptosystem. Help him please! Your task is to generate all prime numbers between two given numbers.
code/online_challenges/src/codechef/README.md
# Codechef From [Codechef](https://www.codechef.com/): CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another...
code/online_challenges/src/codechef/RESQ/README.md
# Problem Link: [RESQ](https://www.codechef.com/problems/RESQ/) # Description Our Chef is catering for a big corporate office party and is busy preparing different mouth watering dishes. The host has insisted that he serves his delicious cupcakes for dessert. On the day of the party, the Chef was over-seeing all the ...
code/online_challenges/src/codechef/RESQ/RESQ.c
#include <stdio.h> #include <math.h> int fun(int area) { int p, j; int flag = area - 1; for (j = 1; j <= (int)(sqrt(area)); ++j) if (area % j == 0) { p = abs(((int) area / j) - j); if (p < flag) flag = p; } return flag; } int main() { ...
code/online_challenges/src/codechef/RIGHTRI/README.md
# Problem Link: [RIGHTRI](https://www.codechef.com/problems/RIGHTRI/) # Description The Chef is given a list of N triangles. Each triangle is identfied by the coordinates of its three corners in the 2-D cartesian plane. His job is to figure out how many of the given triangles are right triangles. A right triangle is a...
code/online_challenges/src/codechef/RIGHTRI/RIGHTRI.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); int i, k = 0; for (i = 0; i < t; i++) { int x1, x2, x3, y1, y2, y3; scanf("%d%d%d%d%d%d", & x1, & y1, & x2, & y2, & x3, & y3); float a = pow(x1 - x2, 2) + pow(y1 - y2, 2); float b = pow(x2 - x3, 2) + pow(...
code/online_challenges/src/codechef/RRCOPY/README.md
# Problem Link: [RRCOPY](https://www.codechef.com/problems/RRCOPY/) # Description You had an array of integer numbers. You also had a beautiful operations called "Copy-Paste" which allowed you to copy any contiguous subsequence of your array and paste it in any position of your array. For example, if you have array [1...
code/online_challenges/src/codechef/RRCOPY/RRCOPY.c
#include <stdio.h> #include <string.h> int main(void) { int t; scanf("%d", & t); while (t--) { int n, h[1000000] = {0}; scanf("%d", & n); int count = 0; while (n--) { int temp; scanf("%d", & temp); if (h[temp] == 0) ...
code/online_challenges/src/codechef/SALARY/README.md
# Problem Link: [SALARY](https://www.codechef.com/problems/SALARY/) # Description Little chief has his own restaurant in the city. There are N workers there. Each worker has his own salary. The salary of the i-th worker equals to Wi (i = 1, 2, ..., N). Once, chief decided to equalize all workers, that is, he wants to ...
code/online_challenges/src/codechef/SALARY/SALARY.c
#include<stdio.h> int main() { int t; scanf("%d", & t); int T[t]; int i, j; for (i = 0; i < t; ++i) { int n; scanf("%d", & n); int A[n]; int min = 32767; for (j = 0; j < n; ++j) { scanf("%d", & A[j]); if (A[j] < min) ...
code/online_challenges/src/codechef/SLAB/README.md
# Problem Link [SLAB](https://www.codechef.com/COOK115B/problems/SLAB) # Description In India, every individual is charged with income tax on the total income each year. This tax is applied to specific ranges of income, which are called income tax slabs. The slabs of income tax keep changing from year to year. Thi...
code/online_challenges/src/codechef/SLAB/SLAB.cpp
// Part of Cosmos by OpenGenus #include <iostream> using namespace std; int main () { int t; cin >> t; while (t --) { long long int total, net, tax; cin >> total; if (total <= 250000) { net = total; } else if ((250000 < total) && (t...
code/online_challenges/src/codechef/SLAB/Slab.java
import java.io.*; import java.util.*; class Slab { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); int a = 12500; int b = 25000; int c = 37500; int d = 50000; int e = 62500; while (t-- > 0) { ...
code/online_challenges/src/codechef/SNUG_FIT/README.md
# Problem Link: [SNUG_FIT](https://www.codechef.com/FEB20B/problems/SNUG_FIT) # Description Geometry expert Nitin is thinking about a problem with parabolas, icosahedrons, crescents and trapezoids, but for now, to encourage beginners, he chooses to work with circles and rectangles. You are given two sequences A1,A2,...
code/online_challenges/src/codechef/SNUG_FIT/snug_fit.cpp
#include <algorithm> #include <iostream> #include <vector> int main() { int t; std::cin >> t; while (t--) { int n, i, sum = 0; std::cin >> n; std::vector<int> a(n), b(n); for (i = 0; i < n; ++i) { std::cin >> a[i]; } for (i = 0; i < n; ++i) { ...
code/online_challenges/src/codechef/SPCANDY/README.md
# Problem Link: [SPCANDY](https://www.codechef.com/problems/SPCANDY/) # Description Cyael is a teacher at a very famous school in Byteland and she is known by her students for being very polite to them and also to encourage them to get good marks on their tests. Then, if they get good marks she will reward them with ...
code/online_challenges/src/codechef/SPCANDY/SPCANDY.c
#include <stdio.h> int main(void) { long long int t; scanf("%lld", & t); while (t--) { long long int n, k; scanf("%lld %lld", & n, & k); if (k != 0) printf("%lld %lld\n", n / k, n % k); else printf("0 %lld\n", n); } return 0; }
code/online_challenges/src/codechef/STFOOD/README.md
# Problem Link: [CASH](https://www.codechef.com/FEB20B/problems/CASH/) # Description in Chefland, there is a very famous street where N types of street food (numbered 1 through N) are offered. For each valid i, there are Si stores that offer food of the i-th type, the price of one piece of food of this type is Vi (t...
code/online_challenges/src/codechef/STFOOD/Stfood.java
import java.io.*; import java.util.*; class Stfood { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int maxprofit = 0; for (int i = 0; i < n; i++) { ...
code/online_challenges/src/codechef/STONES/README.md
# Problem Link: [STONES](https://www.codechef.com/problems/STONES/) # Description Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and whic...
code/online_challenges/src/codechef/STONES/STONES.c
#include <stdio.h> #include <string.h> int main(void) { int t; scanf("%d", & t); while (t--) { char s[100], r[100]; int count = 0; scanf("%s", s); scanf("%s", r); int l = strlen(r); int i; for (i = 0; < l; ++i) { int j; ...
code/online_challenges/src/codechef/STRWN/README.md
# Problem Link [STRWN](https://www.codechef.com/COMR2019/problems/STRWN) # Description This is a fight between weapons. There are 3 type of weapons - Dragonglass, Valyrian Steel and Wild Fire. Now, the result of a fight between any 2 weapons is known to be as below: Dragonglass defeats Valyrian Steel Valyrian Ste...
code/online_challenges/src/codechef/STRWN/STRWN.cpp
#include <iostream> #define ll long long #define ld long double #define pb push_back #define pp pop_back #define mp make_pair #define ff first #define ss second #define maxn 1000000007 #define PI 3.14159265358979323846 using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //...
code/online_challenges/src/codechef/STUPMACH/Stupmach.java
import java.util.Scanner; public class Stupmach { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- >0){ int n = s.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++){ ...
code/online_challenges/src/codechef/TACHSTCK/README.md
# Problem Link: [TACHSTCK](https://www.codechef.com/problems/TACHSTCK/) # Description [Chopsticks (singular: chopstick) are short, frequently tapered sticks used in pairs of equal length, which are used as the traditional eating utensils of China, Japan, Korea and Vietnam. Originated in ancient China, they can also be...
code/online_challenges/src/codechef/TACHSTCK/TACHSTCK.c
#include <stdio.h> #include <stdlib.h> void merge(long long int arr[], long long int l, long long int m, long long int r) { long long int i, j, k; long long int n1 = m - l + 1; long long int n2 = r - m; long long int L[n1], R[n2]; for (i = 0; i < n1; ++i) L[i] = arr[l + i]; for (j = 0;...
code/online_challenges/src/codechef/TOTR/README.md
# Problem Link: [TOTR](https://www.codechef.com/problems/TOTR) # Description A tourist is visiting Byteland. The tourist knows English very well. The language of Byteland is rather different from English. To be exact it differs in following points: Bytelandian alphabet has the same letters as English one, but possibl...
code/online_challenges/src/codechef/TOTR/TOTR.c
#include <stdio.h> #include <string.h> int main(void) { int t; scanf("%d", & t); while (t--) { int n, h[1000000] = {0}; scanf("%d", & n); int count = 0; while (n--) { int temp; scanf("%d", & temp); if (h[temp] == 0) ...
code/online_challenges/src/codechef/VOTERS/README.md
# Problem Link: [VOTERS](https://www.codechef.com/problems/VOTERS/) # Description As you might remember, the collector of Siruseri had ordered a complete revision of the Voters List. He knew that constructing the list of voters is a difficult task, prone to errors. Some voters may have been away on vacation, others ma...
code/online_challenges/src/codechef/VOTERS/VOTERS.c
#include<stdio.h> int main() { int n1, n2, n3; scanf("%ld %ld %ld", & n1, & n2, & n3); int n[1000000] = {0}; int i; for (i = 0; i < n1; ++i) { int x; scanf("%ld", & x); ++n[x]; } for (i = 0; i < n2; ++i) { int x; scanf("%ld", & x); ...
code/online_challenges/src/hackerrank/2d_array_ds/2d_array_ds.java
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the hourglassSum function below. static int hourglassSum(int[][] arr) { int[] sum = new int[16]; Ar...
code/online_challenges/src/hackerrank/3D_aurface_area/3D_surface_area.cpp
#include <iostream> int main() { int h, w; int a[110][110]; std::cin >> h >> w; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) std::cin >> a[i][j]; } int sum = 0; for (int i = 0; i < h; ++i) { if (i == 0) { for (int j = 0; j < w; ++j) { ...
code/online_challenges/src/hackerrank/3D_aurface_area/README.md
### Problem Link [3D_aurface_area](https://www.hackerrank.com/challenges/3d-surface-area/problem?h_r=internal-search) ### Description 2D board A of size H * W with H rows and W columns. The board is divided into cells of size 1 * 1 with each cell indicated by it's coordinate (i,j). The cell (i,j) has an integer A<sub...
code/online_challenges/src/hackerrank/Counting_Valleys/Counting_Valleys.cpp
// Part of Cosmos by OpenGenus #include <bits/stdc++.h> using namespace std; // Complete the countingValleys function below. int countingValleys(int n, string s) { int i,sum=0,count; for(i=0;i<n;i++) { if(s[i]=='U') { sum+=1; if(sum==0) count++; } else { sum-=1; ...
code/online_challenges/src/hackerrank/Counting_Valleys/README.md
<b>Problem Link</b> [Counting Valleys](https://www.hackerrank.com/challenges/counting-valleys/problem) <b>Description</b> Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step he took, he noted if ...
code/online_challenges/src/hackerrank/Electronics_Shop/Electronics_Shop.cpp
// Part of Cosmos by OpenGenus #include <bits/stdc++.h> using namespace std; vector<string> split_string(string); /* * Complete the getMoneySpent function below. */ int getMoneySpent(vector<int> keyboards, vector<int> drives, int b) { int i,sum=0,j,k=0,ans=-1; sort(keyboards.begin(),keyboards.end()); s...
code/online_challenges/src/hackerrank/Electronics_Shop/Electronics_Shop.java
// Part of Cosmos by OpenGenus import java.io.*; import java.util.*; public class Solution { /* * getMoneySpent function to get the total money spent */ static int getMoneySpent(int[] keyboards, int[] drives, int b) { HashMap<Integer, Boolean> ans = new HashMap<>(); for (int i = 0;...
code/online_challenges/src/hackerrank/almost_sorted/README.md
### Problem Link [Almost Sorted ](https://www.hackerrank.com/challenges/almost-sorted/problem?h_r=internal-search) ### Description Determine whether the given array can be sorted by using given operation: - Swap two elements - Reverse one sub-segment
code/online_challenges/src/hackerrank/almost_sorted/almost_sorted.cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> int main() { int n = 0; int p = 0; int s, l; s = l = 0; int t = 0; std::cin >> n; std::vector<int> a(n); std::vector<int> b(n); for (int i; i < n; ++i) { std::cin >> a[i]; b...
code/online_challenges/src/hackerrank/alternating_characters/Alternating.py
count = 0 test_cases = int(input()) def cal(test_cases): arr = input() count = 0 for i in range(len(arr)-1): if arr[i] == arr[i+1]: count += 1 return count for i in range(test_cases): print(cal(test_cases))
code/online_challenges/src/hackerrank/alternating_characters/README.md
Description You are given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string. Your task is to find the minimum number of required deletions.
code/online_challenges/src/hackerrank/array_manipulation/README.md
### Problem Link [Array Manipulation](https://www.hackerrank.com/challenges/crush/problem?h_r=internal-search) ### Description You are given a list of size n, initialized with zeroes. You have to perform m queries on the list and output the maximum of final values of all the n elements in the list. For every query, y...
code/online_challenges/src/hackerrank/array_manipulation/array_manipulation.cpp
#include <iostream> #include <vector> #include <algorithm> int main() { int n; int m; int a; int b; int k; std::cin >> n >> m; std::vector<std::pair<int, int> > v; for (int i = 0; i < m; ++i) { std::cin >> a >> b >> k; v.push_back(std::make_pair(a, k)); v.push_...
code/online_challenges/src/hackerrank/bigger_is_greater/README.md
### Problem Link [Bigger is Greater](https://www.hackerrank.com/challenges/bigger-is-greater/problem?h_r=internal-search) ### Description lexicographically higher string possible, example: given the word ABCD, the next largest word is ABDC.
code/online_challenges/src/hackerrank/bigger_is_greater/bigger_is_greater.cpp
#include <iostream> #include <algorithm> int main() { int t; std::cin >> t; while (t--) { std::string s; std::cin >> s; bool ans = next_permutation(s.begin(), s.end()); // do permutation and store it in string ans if (!ans) // if there is no permutation possi...
code/online_challenges/src/hackerrank/dynamic_array/dynamic_array.java
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; cla...
code/online_challenges/src/hackerrank/encryption/README.md
### Problem Link [Encryption](https://www.hackerrank.com/challenges/encryption/problem?h_r=internal-search) ### Description Number of rows and the number of columns in the rectangle lie between floor(sqrt(len(word))) and ceil(sqrt(len(word))). Choose a set of values for rows and columns, out of the 4 available choices...
code/online_challenges/src/hackerrank/encryption/encryption.cpp
#include <cmath> #include <iostream> int main() { std::string s; std::cin >> s; int r, c; int l = s.size(); r = floor(sqrt(l)); c = ceil(sqrt(l)); for (int i = 0; i < c; ++i) { for (int j = i; j < l; j = j + c) std::cout << s[j]; std::cout << "\n"; } }
code/online_challenges/src/hackerrank/jumping_on_the_clouds/Readme.md
# Problem Link [Jumping on the Clouds](https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem) # Description Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number...
code/online_challenges/src/hackerrank/jumping_on_the_clouds/Solution.java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int c[] = new int[n]; int zero = 0; int jump = 0; for(int c...
code/online_challenges/src/hackerrank/lonely_integer/lonely_integer.cpp
#include <iostream> #include <vector> using namespace std; vector<string> split_string(string); // Complete the lonelyinteger function below. int lonelyinteger(vector<int> a) { int unique = 0; int n = a.size(); for(int i = 0;i < n;++i) { unique ^= a[i]; } return unique; } int main()...
code/online_challenges/src/hackerrank/the_maximum_subarray/README.md
### Problem Link [Maximum Subarray](https://www.hackerrank.com/challenges/maxsubarray/problem) ### Description Given a integer array, find the maximum subarray sum and the maximum subsequence sum. [Subarray](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/)
code/online_challenges/src/hackerrank/the_maximum_subarray/the_maximum_subarray.cpp
#include <iostream> #include <vector> #include <algorithm> int main() { int t; //t is the number of test case std::cin >> t; while (t--) { int n; //n is the size of input array int sum, ans, ans1; sum = ans = ans1 = 0; int flag = 0; std::cin >> n; std...
code/online_challenges/src/leetcode/Arranging_coins/arranging_coins.cpp
#include<bits/stdc++.h> using namespace std; class Solution { public: bool possibol(int n, long long row){ long sum = (row*(row+1))/2; return n>=sum; } int arrangeCoins(int n) { int start=0,end=n,answer=0; while(start<=end){ int mid=start+(end-start)/2; ...
code/online_challenges/src/leetcode/Find_Minimum_in_Rotated_Sorted_Array/FindMinimumInRotatedSortedArray.java
import java.util.*; /* * * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ * * Solution Description: * "Binary Search Approach" * - Since array is sorted we can apply Binary Search but since array is rotated, we cannot apply Binary search directly. * ...
code/online_challenges/src/leetcode/README.md
# LeetCode From [LeetCode](https://leetcode.com/) It's a website where people–mostly software engineers–practice their coding skills. There are 800+ questions (and growing), each with multiple solutions. Questions are ranked by level of difficulty: easy, medium, and hard. The purpose of LeetCode is to provide you han...
code/online_challenges/src/leetcode/Sqrt(x)/Sqrt(x).cpp
#include<bits/stdc++.h> using namespace std; // O(logn) class Solution { public: int mySqrt(int x) { double start=0,end=x; for(int i=0;i<100;++i){ double mid = start + (end - start) / 2; if(mid*mid<=x){ start = mid; }else{ end = mid; ...
code/online_challenges/src/leetcode/decode_string/encryption_string.py
# -*- coding: utf-8 -*- """ Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; No ...
code/online_challenges/src/leetcode/longest_substring_without_repetition/longest_substring_without_repetition.cpp
/* * * Problem: https://leetcode.com/problems/longest-substring-without-repeating-characters/ * * Solution Description: * "Sliding Window Approach" * * 1. We take two pointers i (left pointer) and j (right pointer). * 2. We widen our window on the right side at each step (by incrementing j). * 3. When...
code/online_challenges/src/leetcode/max_distance_to_closest_person/Solution.py
''' Link to the problem: https://leetcode.com/problems/maximize-distance-to-closest-person/ You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed). There is at least one empty seat, and at l...
code/online_challenges/src/leetcode/maximum_subarray/maximumsubarray.cpp
//In this corrected code, we compare max_ending + nums[i] with nums[i] to determine if it's beneficial to extend the current subarray or start a new one. This way, the code correctly calculates the maximum subarray sum. class Solution { public: int maxSubArray(vector<int>& nums) { int len = nums.size(); ...
code/online_challenges/src/leetcode/median_of_two_sorted_arrays/median_of_two_sorted_arrays.cpp
class Solution { public: double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) { int i = 0, j = 0; double m; vector<int> v; while (i < nums1.size() && j < nums2.size()) { if (nums1[i] < nums2[j]) { v.push_back(nums1[...
code/online_challenges/src/leetcode/minimum_number_of_days_to_make_m_bouquets/minimum_number_of_days_to_make_m_bouquets.cpp
#include<bits/stdc++.h> using namespace std; class Solution { public: int minDays(vector<int>& bloomDay, int m, int k) { if(m > (int)bloomDay.size() / k){ return -1; } int start=1,end=*max_element(bloomDay.begin(), bloomDay.end()),res=-1; while(start<=end){ ...
code/online_challenges/src/leetcode/remove_duplicates_from_sorted_list_ii/remove_duplicates_from_sorted_list_ii.cpp
/* link to problem:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ difficulty level: Medium approach: maintained a previous,current and next pointer in the linked list if next pointer and curr pointer is having same value then we increment the next pointer till they are unequal...
code/online_challenges/src/leetcode/symmetric_tree/symmetric_tree.cpp
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isMirror(TreeNode *left, TreeNode *right) { return (!left && left == right) ||...
code/online_challenges/src/leetcode/two_sum/two_sum.cpp
#include<bits/stdc++.h> using namespace std; //O(n^2) class Solution { public: vector<int> twoSum(vector<int>& nums, int target){ vector<int>res; for(int i=0;i<(int)nums.size();++i){ for(int j=i+1;j<(int)nums.size();++j){ if(nums[i]+nums[j]==target){ ...
code/online_challenges/src/project_euler/README.md
# Project Euler From [Project Euler](https://projecteuler.net/): Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and pr...
code/online_challenges/src/project_euler/documentation_guide.md
# Documentation Guide ## README Template When you are adding a new folder for a new Project Euler problem, add a README.md in the folder with the following format: ```Markdown # Project Euler Problem #NUM: Title of Problem ([Problem Link](<- Is alt text. This is the actual Link to the problem)) Problem text copy...
code/online_challenges/src/project_euler/problem_001/README.md
# Project Euler Problem #001: Multiples of 3 and 5 ([Problem Link](https://projecteuler.net/problem=1)) If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. --- <p align="center"> ...
code/online_challenges/src/project_euler/problem_001/problem_001.c
#include <stdio.h> int main(void) { int sum, i; for (i = 0; i < 1000; ++i) sum += (i % 3 == 0) || (i % 5 == 0) ? i : 0; printf("%d\n", sum); }
code/online_challenges/src/project_euler/problem_001/problem_001.cpp
#include <iostream> int main() { int sum; for (int i = 0; i < 1000; ++i) sum += (i % 3 == 0) || (i % 5 == 0) ? i : 0; std::cout << sum << "\n"; }
code/online_challenges/src/project_euler/problem_001/problem_001.java
public class Problem001 { public static void main(String []args) { int sum = 0; for(int i = 1; i < 1000; i++) sum += (i % 3 ==0 || i % 5 == 0) ? i : 0; System.out.print(sum); } }
code/online_challenges/src/project_euler/problem_001/problem_001.js
let sum = 0; for (let i = 0; i < 1000; i += 1) { if (i % 3 === 0 || i % 5 === 0) { sum += i; } } console.log(sum);
code/online_challenges/src/project_euler/problem_001/problem_001.py
def main(): total = 0 for i in range(0, 1000): total += i if (i % 3 == 0) or (i % 5 == 0) else 0 print(total) if __name__ == "__main__": main()
code/online_challenges/src/project_euler/problem_001/problem_001.rs
fn main() { let mut sum = 0; for i in 1..1000 { if i % 3 == 0 || i % 5 == 0 { sum += i } } println!("{}", sum); }
code/online_challenges/src/project_euler/problem_002/README.md
# Project Euler Problem #002: Even Fibonacci numbers ([Problem Link](https://projecteuler.net/problem=2)) Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: <p align="center"> 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... </p> By con...
code/online_challenges/src/project_euler/problem_002/problem_002.c
#include <stdio.h> int main(void) { /* Variables to keep track of Fibonacci numbers */ int p2 = 0; int p1 = 0; int current = 1; int sum = 0; while (current <= 4000000) { /* Add even fibonacci numbers */ sum += (current % 2 == 0) ? current : 0; /* Updat...
code/online_challenges/src/project_euler/problem_002/problem_002.cpp
#include <iostream> int main() { // Variables to keep track of Fibonacci numbers int p2 = 0; int p1 = 0; int current = 1; int sum = 0; while (current <= 4000000) { // Add even fibonacci numbers sum += (current % 2 == 0) ? current : 0; // Update fibonacci numbers ...
code/online_challenges/src/project_euler/problem_002/problem_002.java
public class Problem002 { public static void main(String[] args) { int sum = 0; int first = 0; int second = 1; int third = 0; while(third < 4000000) { third = first + second; sum += (third % 2 == 0) ? third : 0; first = second; second = third; } System.out.println(sum); } }
code/online_challenges/src/project_euler/problem_002/problem_002.js
let sum = 0; let num1 = 0; let num2 = 0; let current = 1; while (current < 4000000) { sum += current % 2 === 0 ? current : 0; num2 = num1; num1 = current; current = num2 + num1; } console.log(sum);
code/online_challenges/src/project_euler/problem_002/problem_002.py
def main(): # Variables to keep track of Fibonacci numbers p2 = 0 p1 = 0 current = 1 total = 0 while current <= 4000000: # Add even fibonacci numbers total += current if current % 2 == 0 else 0 # Update fibonacci numbers p2 = p1 p1 = current curr...
code/online_challenges/src/project_euler/problem_003/README.md
# Project Euler Problem #003: Largest prime factor ([Problem Link](https://projecteuler.net/problem=3)) The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143?. --- <p align="center"> A massive collaborative effort by <a href="https://github.com/OpenGenus/cosm...
code/online_challenges/src/project_euler/problem_003/problem_003.c
#include <stdio.h> int main(void) { long long int n = 600851475143; long long int h = 0; long long int c = 2; while (n != 1) { if ((n % c == 0) && (c > h)) { h = c; n /= c; } ++c; } printf("%lld\n", h); }
code/online_challenges/src/project_euler/problem_003/problem_003.cpp
#include <iostream> #include <vector> #include <cmath> int main() { long long int n = 600851475143; long long int h = 0; long long int c = 2; while (n != 1) { if ((n % c == 0) && (c > h)) { h = c; n /= c; } ++c; } std::cout << h << "\...
code/online_challenges/src/project_euler/problem_003/problem_003.java
public class Problem003 { public static long smallestFactor(long n) { if (n <= 1) throw new IllegalArgumentException(); for (long i = 2 ,end = (long)Math.sqrt(n); i <= end; i++) { if (n % i == 0) return i; } return n; } public static void main(String []args) { long n = 600851475143L; long big ...
code/online_challenges/src/project_euler/problem_003/problem_003.js
let num = 600851475143; let highestPrimeFactor = 0; let currentFactor = 2; while (num > highestPrimeFactor) { if (num % currentFactor === 0) { highestPrimeFactor = currentFactor; num /= currentFactor; } else { ++currentFactor; } } console.log(highestPrimeFactor);