filename
stringlengths
7
140
content
stringlengths
0
76.7M
code/numerical_analysis/integral/src/integral_rectangle.c
#include <stdio.h> /*Part of Cosmos by OpenGenus Foundation*/ double integral_rectangle(double a, double b, double step, double (*f)(double)) { double fragment = (b - a) / step; double res = step * (f(a) + f(b)) / 6.0; int i; for (i = 1; i <= fragment; ++i) res += 4.0 / 6.0 * step * f(a + step...
code/numerical_analysis/integral/src/integral_rectangle.cpp
#include <iostream> #include <functional> //Part of Cosmos by OpenGenus Foundation double integralRectangle(double a, double b, double step, std::function<double(double)> f) { double fragment, res; fragment = (b - a) / step; res = step * (f(a) + f(b)) / 6.0; for (int i = 1; i <= fragment; ++i) ...
code/numerical_analysis/integral/src/integral_rectangle.java
package cosmos; //Part of Cosmos by OpenGenus Foundation public class IntegralRectangle { public static void main(String[] args) { double a = 0.0; double b = 1.0; int step = 1000; IntegralFunction integralFunction = (x) -> x * x * x; // a∫b x^3 dx double sum = calculateIntegral(a, b, step, integralFunc...
code/numerical_analysis/integral/src/integral_rectangle.py
# Part of Cosmos by OpenGenus Foundation def integral_rectangle(a, b, step, func): fragment = (b - a) / step res = step * (func(a) + func(b)) / 6.0 i = 1 while i <= fragment: res += 4.0 / 6.0 * step * func(a + step * (i - 0.5)) i += 1 i = 1 while i <= fragment - 1: res ...
code/numerical_analysis/integral/src/integral_trapezoid.c
#include <stdio.h> /*Part of Cosmos by OpenGenus Foundation*/ double integral_trapezoid(double a, double b, double step, double (*f)(double)) { double fragment = (b - a) / step; double res = step * (f(a) + f(b)) / 2.0; int i; for (i = 1; i <= fragment - 1; ++i) res = res + step * f(a + step * ...
code/numerical_analysis/integral/src/integral_trapezoid.cpp
#include <iostream> #include <functional> //Part of Cosmos by OpenGenus Foundation double integralTrapezoid(double a, double b, double step, std::function<double(double)> f) { double fragment, res; fragment = (b - a) / step; res = step * (f(a) + f(b)) / 2.0; for (int i = 1; i <= fragment - 1; ++i) ...
code/numerical_analysis/integral/src/integral_trapezoid.java
package cosmos; public class IntegralTrapezoidal { public static void main(String[] args) { double a = 0.0; double b = 1.0; int step = 1000; IntegralFunction integralFunction = (x) -> x * x * x; // a∫b x^3 dx double sum = calculateIntegral(a, b, step, integralFunction); System.out.println("Approximate i...
code/numerical_analysis/integral/src/integral_trapezoid.py
# Part of Cosmos by OpenGenus Foundation def integral_trapezoid(a, b, step, func): fragment = (b - a) / step res = step * (func(a) + func(b)) / 2.0 i = 1 while i <= fragment - 1: res = res + step * func(a + step * i) i += 1 return res print("Enter interval: ") start = float(input...
code/numerical_analysis/iteration/src/Iteration Method.cpp
#include<bits/stdc++.h> using namespace std; /* Program to Find Root of the equation x*x*x+x-5=0 by Iteration Method By simplified this equation we got eq=pow(5-x,(1/3)) then differentiate eq we got (-1/3*pow(5-x,(2/3))) then we checked if we put natural number in x, will it lower than 1 for all natural numbe...
code/numerical_analysis/monte_carlo/src/integral_monte_carlo.cpp
#include <iostream> #include <functional> #include <random> //Part of Cosmos by OpenGenus Foundation double integralMonteCarlo(double a, double b, double pointCount, std::function<double(double)> f) { double res, randX, randY; std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distributio...
code/numerical_analysis/monte_carlo/src/integral_montecarlo.c
#include <stdio.h> #include <stdlib.h> /*Part of Cosmos by OpenGenus Foundation*/ double integral_monte_carlo(double a, double b, double point_count, double (*f)(double)) { double x, y = 0; int i; for (i = 0; i < point_count; ++i){ x = ((b - a) * ((double)random() / RAND_MAX)) + a; y += f(...
code/numerical_analysis/monte_carlo/src/integral_montecarlo.py
import random # Part of Cosmos by OpenGenus Foundation def integral_monte_carlo(a, b, point_count, func): rand_y = 0.0 i = 0 while i < point_count: rand_x = (b - a) * random.random() + a rand_y += func(rand_x) i += 1 res = (b - a) * (rand_y / point_count) return res prin...
code/numerical_analysis/monte_carlo/src/pi_monte_carlo.cpp
#include <iostream> #include <random> //Part of Cosmos by OpenGenus Foundation double piMonteCarlo(double pointCount) { double circlePoints = 0; double pi, x, y; std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dis; for (int i = 0; i < pointCount; ++i) { ...
code/numerical_analysis/monte_carlo/src/pi_montecarlo.c
#include <stdio.h> #include <stdlib.h> /*Part of Cosmos by OpenGenus Foundation*/ double pi_monte_carlo(double point_count) { double x, y, circle_points = 0; int i; for (i = 0; i < point_count; ++i){ x = (double)random() / RAND_MAX; y = (double)random() / RAND_MAX; if (x * x + y * ...
code/numerical_analysis/monte_carlo/src/pi_montecarlo.py
import random # Part of Cosmos by OpenGenus Foundation def pi_monte_carlo(point_count): circle_points = 0.0 i = 0 while i < point_count: x = random.random() y = random.random() if x * x + y * y < 1: circle_points += 1 i += 1 pi = (circle_points / point_coun...
code/numerical_analysis/newton_rapson/src/Newton Rapson Method.cpp
#include<bits/stdc++.h> using namespace std; /* Program to Find Root of the equation x*x*x-18=0 by Newton Rapson Method First we take 2 values from function of the Equation (negative/positive) then place it in x and y then we Differentiate This equation; then apply formula */ double func(double x) { ...
code/numerical_analysis/polynomial_interpolations/src/lagrange_interpolation.py
""" This program provides functionalities to carry out polynomial interpolation using Lagrange's polynomials. Reference: https://en.wikipedia.org/wiki/Lagrange_polynomial. """ class Lagrange: """ This class implements interpolation using Lagrange's polynomial, For a given set of points (xj, yj...
code/numerical_analysis/polynomial_interpolations/src/nevilles_method.py
""" Python program to show how to interpolate and evaluate a polynomial using Neville's method. Neville’s method evaluates a polynomial that passes through a given set of x and y points for a particular x value (x0) using the Newton polynomial form. Reference: https://rpubs.com/aaronsc32...
code/numerical_analysis/runge_kutt/src/rk4.c
#include <stdio.h> #include <stdlib.h> #include <math.h> /*Part of Cosmos by OpenGenus Foundation*/ struct xy{ double x; double y; }; struct xy * runge_kutt(struct xy start_conditions, double start_interval, double finish_interval, double step, int *size_array, ...
code/numerical_analysis/runge_kutt/src/rk4.cpp
#include <iostream> #include <vector> #include <functional> #include <cmath> using xy = std::pair<double, double>; std::vector<xy> rungeKutt(xy startConditions, double startInterval, double finishInterval, double step, ...
code/numerical_analysis/runge_kutt/src/rk4.py
import math def runge_kutt(start_conditions, finish_interval, step, f): res = [] res_x = start_conditions[0] res_y = start_conditions[1] while res_x < finish_interval: res.append((res_x, res_y)) k1 = step * f(res_x, res_y) k2 = step * f(res_x + step / 2.0, res_y + k1 / 2.0) ...
code/online_challenges/src/README.md
# Cosmos Programming Challenge Solutions > Your personal library of every algorithm and data structure code that you will ever encounter This folder contains solutions to various programming problems from a variety of websites, such as [Codechef](https://www.codechef.com/), [Project Euler](https://projecteuler.net/), ...
code/online_challenges/src/codechef/AMSGAME1/AMSGAME1.c
#include <stdio.h> long long int gcd(long long int a, long long int b) { if (b == 0) { return a; } return gcd(b, a % b); } int main(void) { int t; scanf("%d", & t); while (t--) { long long int n; scanf("%lld", & n); long long int a; scanf("%l...
code/online_challenges/src/codechef/AMSGAME1/README.md
# Problem Link: [AMSGAME1](https://www.codechef.com/problems/AMSGAME1) # Description T test cases. The first line of each test case contains a single integer N, the length of the sequence. The second line contains N positive integers, each separated by a single space.
code/online_challenges/src/codechef/BACREP/BACREP.cpp
#include <iostream> using namespace std; #define ll long long #define dd double #define endl "\n" #define pb push_back #define all(v) v.begin(),v.end() #define mp make_pair #define fi first #define se second #define fo(i,n) for(int i=0;i<n;i++) #define fo1(i,n) for(int i=1;i<=n;i++) ll mod=1000000007; const ll N=500...
code/online_challenges/src/codechef/BACREP/README.md
# Problem Link [BACREP](https://www.codechef.com/OCT19A/problems/BACREP) # Description Chef has a rooted tree with N vertices (numbered 1 through N); vertex 1 is the root of the tree. Initially, there are some bacteria in its vertices. Let's denote the number of sons of vertex v by sv; a leaf is a vertex without sons...
code/online_challenges/src/codechef/BRKBKS/Brkbks.java
import java.io.*; import java.util.*; class Brkbks { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int strength = s.nextInt(); int w1 = s.nextInt(); int w2 = s.nextInt(); i...
code/online_challenges/src/codechef/BRKBKS/README.md
# Problem Link: [BRKBKS](https://www.codechef.com/JAN20B/problems/BRKBKS/) # Description Ada's strength is SS. Whenever she hits a stack of bricks, consider the largest k≥0k≥0 such that the sum of widths of the topmost kk bricks does not exceed SS; the topmost kk bricks break and are removed from the stack. Before e...
code/online_challenges/src/codechef/CARVANS/CARVANS.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { int n; scanf("%d", & n); int arr[n], i; for (i = 0; i < n; ++i) { scanf("%d", & arr[i]); } int top, c = 0; top = arr[0]; for (i = 0; i < n; ...
code/online_challenges/src/codechef/CARVANS/README.md
# Problem Link: [CARVANS](https://www.codechef.com/problems/CARVANS) # Description Most problems on CodeChef highlight chef's love for food and cooking but little is known about his love for racing sports. He is an avid Formula 1 fan. He went to watch this year's Indian Grand Prix at New Delhi. He noticed that one seg...
code/online_challenges/src/codechef/CASH/HardCash.java
import java.util.Scanner; class Hardcash { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int k = s.nextInt(); int[] arr = new int[n]; //input ...
code/online_challenges/src/codechef/CASH/README.md
# Problem Link: [CASH](https://www.codechef.com/FEB20B/problems/CASH/) # Description There are N bags with coins in a row (numbered 1 through N); for each valid i, the i - th bag contains A(i) coins. Chef should make the number of coins in each bag divisible by a given integer K in the following way: - choose an in...
code/online_challenges/src/codechef/CASH/hard_cash.cpp
#include <iostream> int main() { int t; std::cin >> t; while (t--) { int n, k, x, sum = 0; std::cin >> n >> k; for (int i = 0; i < n; ++i) { std::cin >> x; sum += x; } sum %= k; std::cout << sum << "\n"; } }
code/online_challenges/src/codechef/CHDIGER/CHDIGER.cpp
#include <iostream> #include <algorithm> #include <list> #include <stack> #include <string> int main () { int t; std::cin >> t; while (t--) { std::string n; int d; std::cin >> n >> d; std::list<char>ans; std::stack<char>st; for (int i = 0; i < n.size();...
code/online_challenges/src/codechef/CHDIGER/README.md
# Problem Link: [CHDIGER](https://www.codechef.com/MARCH19B/problems/CHDIGER) # Description Chef Leonardo has a decimal integer N and a non-zero decimal digit d. N does not contain the digit zero; specifically, N should always be treated as a decimal integer without leading zeroes. Chef likes d and does not like any ...
code/online_challenges/src/codechef/CHEALG/CHEALG.java
// Part of Cosmos by OpenGenus import java.util.*; import java.lang.*; import java.io.*; class chealg { public static void main (String[] args) throws java.lang.Exception {Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T>0) { String s=sc.next(); String str=""; int l=s.length(); int count=1; for(in...
code/online_challenges/src/codechef/CHEALG/README.md
Problem Statement: One day, Saeed was teaching a string compression algorithm. This algorithm finds all maximal substrings which contains only one character repeated one or more times (a substring is maximal if it we cannot add one character to its left or right without breaking this property) and replaces each such s...
code/online_challenges/src/codechef/CHEFING/CHEFING.cpp
#include<bits/stdc++.h> using namespace std; int main() { int t,n,cnt,i,j; string s; cin >> t; while(t--) { cnt = 0; cin >> n; int hash[n][26] = {0}; for(i = 0 ; i < n ; i++) { cin >> s; for(j = 0 ; j < s.length() ; j++) { hash[i][s[...
code/online_challenges/src/codechef/CHEFING/README.md
# Problem Link: [CHEFING](https://www.codechef.com/FEB19B/problems/CHEFING/) # Description Chef recently visited ShareChat Cafe and was highly impressed by the food. Being a food enthusiast, he decided to enquire about the ingredients of each dish. There are N dishes represented by strings S1,S2,…,SN. Each ingredient...
code/online_challenges/src/codechef/CHFING/CHFING.cpp
#include<bits/stdc++.h> using namespace std; #define ll long long int #define FLASH ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define mod 1000000007 int main() { int t; long long k,n,ans,x,y,z,a; cin >> t; while(t--) { ans = x = y = z = a = 0; cin >> n >> k; ...
code/online_challenges/src/codechef/CHFING/README.md
# Problem Link: [CHFING](https://www.codechef.com/JUNE19A/problems/CHFING/) # Description Chef wants to make a feast. In order to do that, he needs a lot of different ingredients. Each ingredient has a certain tastiness; the tastiness of each ingredient may be any positive integer. Initially, for each tastiness betwe...
code/online_challenges/src/codechef/CHNUM/CHNUM.cpp
#include <iostream> #include <algorithm> #include <list> #include <stack> #include <string> int main () { int t; std::cin >> t; while (t--) { int n; std::cin >> n; int a; int cnt_ps = 0, cnt_nt = 0; for (int i = 0; i < n; ++i) { std::cin >> a...
code/online_challenges/src/codechef/CHNUM/README.md
# Problem Link [CHNUM](https://www.codechef.com/MARCH19B/problems/CHNUM) # Description Recently, Chef hosted a strange competition at the Byteland annual fair. There were N participants in the competition (numbered 1 through N); at the end of the competition, their scores were A1,A2,…,AN. Since it was a strange comp...
code/online_challenges/src/codechef/CLEANUP/CLEANUP.c
#include <stdio.h> int main() { int t; scanf("%d", & t); while (t != 0) { int i; int n, m; scanf("%d%d", & n, & m); int a[n]; for (i = 1; i <= n; ++i) a[i] = 1; for (i = 1; i <= m; ++i) { int o; scanf("%d", &...
code/online_challenges/src/codechef/CLEANUP/README.md
# Problem Link: [CLEANUP](https://www.codechef.com/problems/CLEANUP/) # Description After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n. Most of the cooks have alre...
code/online_challenges/src/codechef/CNOTE/CNOTE.c
#include<stdio.h> int main() { int t; scanf("%d", & t); while (t--) { int x, y, k, n; scanf("%d%d%d%d", & x, & y, & k, & n); int p[n], c[n], flag = 0, i; for (i = 0; i < n; ++i) { scanf("%d%d", & p[i], & c[i]); if (p[i] >= x - y && c[i]...
code/online_challenges/src/codechef/CNOTE/README.md
# Problem Link: [CNOTE](https://www.codechef.com/problems/CNOTE/) # Description Chef likes to write poetry. Today, he has decided to write a X pages long poetry, but unfortunately his notebook has only Y pages left in it. Thus he decided to buy a new CHEFMATE notebook and went to the stationary shop. Shopkeeper showed...
code/online_challenges/src/codechef/COINS/COINS.py
a = {} def dollar(n): if n <= 11: a[n] = n return a[n] if n in a: return a[n] a[n] = max(n, dollar(int(n / 2)) + dollar(int(n / 3)) + dollar(int(n / 4))) return a[n] while True: try: n = int(input()) except EOFError: break if n <= 11: print(n...
code/online_challenges/src/codechef/COINS/README.md
# Problem Link: [COINS] (https://www.codechef.com/problems/COINS) # Description In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the bank...
code/online_challenges/src/codechef/COINS/coins.cpp
#include <iostream> #include <unordered_map> #define lli long long int std::unordered_map<lli, lli> mpp; lli check(lli n) { if (n == 0) { mpp[0] = 0; return 0; } if (mpp[n] != 0) return mpp[n]; else { mpp[n] = check(n / 2) + check(n / 3) + check(n / 4); mpp[n] =...
code/online_challenges/src/codechef/CONFLIP/CONFLIP.c
#include<stdio.h> int main() { int t; scanf("%d", & t); while (t--) { int u; scanf("%d", & u); while (u--) { int i, n, q; scanf("%d%d%d", & i, & n, & q); if (i == q) { printf("%d\n", n / 2); ...
code/online_challenges/src/codechef/CONFLIP/README.md
# Problem Link: [CONFLIP](https://www.codechef.com/problems/CONFLIP/) # Description Little Elephant was fond of inventing new games. After a lot of research, Little Elephant came to know that most of the animals in the forest were showing less interest to play the multi-player games.Little Elephant had started to inve...
code/online_challenges/src/codechef/COVID19/COVID19.cpp
#include<bits/stdc++.h> using namespace std; int main() { int T; cin>>T; while(T--) { int n; cin>>n; std::vector<int> x(n); for(int &i:x) cin>>i; int smallest=10,largest=0,infected=1; for(int i=1;i<n;i++) { if(x[i]-x[i-1]<=2) infected++; else { largest=max(largest,infected); ...
code/online_challenges/src/codechef/COVID19/README.md
# Problem Link: [COVID19](https://www.codechef.com/problems/COVID19) # Description There are N people on a street (numbered 1 through N). For simplicity, we'll view them as points on a line. For each valid i, the position of the i-th person is Xi. It turns out that exactly one of these people is infected with the vir...
code/online_challenges/src/codechef/DIVIDING/DIVIDING.c
#include <stdio.h> int main(void) { long int n; scanf("%ld", & n); long int i; long long int a[n]; long long int s = 0; for (i = 0; i < n; ++i) { scanf("%lld", & a[i]); s = s + a[i]; } for (i = 1; i <= n; ++i) s -= i; if (s == 0) printf("YES\n")...
code/online_challenges/src/codechef/DIVIDING/README.md
# Problem Link: [DIVIDING](https://www.codechef.com/problems/DIVIDING/) # Description Are you fond of collecting some kind of stuff? Mike is crazy about collecting stamps. He is an active member of Stamp Collecting Сommunity(SCC). SCC consists of N members which are fond of philately. A few days ago Mike argued with ...
code/online_challenges/src/codechef/EGGFREE/EGGFREE.cpp
#include <iostream> using namespace std; int tt,n,m,old,it,i,j,k,x[222],y[222],u[222]; bool g[222][222],q; char r[222]; int main() { scanf("%d",&tt); while (tt--) { scanf("%d%d",&n,&m); memset(u,0,sizeof(u)); memset(g,0,sizeof(g)); memset(r,0,sizeof(r)); for (i=0; i<m; i++) { s...
code/online_challenges/src/codechef/EGGFREE/README.md
# Problem Link [EGGFREE](https://www.codechef.com/MARCH20A/problems/EGGFREE) # Description Let's call a directed graph egg-free if it is acyclic and for any three pairwise distinct vertices x, y and z, if the graph contains edges x→y and x→z, then it also contains an edge y→z and/or an edge z→y. You are given an und...
code/online_challenges/src/codechef/EID2/README.md
# Problem Link: [EID2](https://www.codechef.com/problems/EID2/) # Description Eidi gifts are a tradition in which children receive money from elder relatives during the Eid celebration. Chef has three children (numbered 1,2,3) and he wants to give them Eidi gifts. The oldest child, Chefu, thinks that a distribution o...
code/online_challenges/src/codechef/EID2/eid2.cpp
#include <algorithm> #include <iostream> #include <vector> bool istrue(std::vector<int> a, std::vector<int> c) { std::pair<int, int> pairt[3]; bool x = true; int i; for (i = 0; i < 3; ++i) { pairt[i].first = a[i]; pairt[i].second = c[i]; } std::sort(pairt, pairt + 3); for (i...
code/online_challenges/src/codechef/ERROR/ERROR.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { char feedback[100000]; scanf("%s", feedback); int i, d = 0; for (i = 2; feedback[i] != '\0'; ++i) { if ((feedback[i] == '0' && feedback[i - 1] == '1' && feedback[i - 2] == '...
code/online_challenges/src/codechef/ERROR/README.md
# Problem Link: [ERROR](https://www.codechef.com/problems/ERROR/) # Description Lots of geeky customers visit our chef's restaurant everyday. So, when asked to fill the feedback form, these customers represent the feedback using a binary string (i.e a string that contains only characters '0' and '1'. Now since chef i...
code/online_challenges/src/codechef/FCTRL/Factorial.c
#include <stdio.h> #include <stdlib.h> int main() { long t; scanf("%ld", & t); while (t--) { long n; scanf("%ld", & n); long s = 1, sum = 0; while (1) { s *= 5; if (s > n) break; else { ...
code/online_challenges/src/codechef/FCTRL/README.md
# Problem Link [FCTRL](https://www.codechef.com/problems/FCTRL/) # Description The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest si...
code/online_challenges/src/codechef/GCD2/GCD2.c
#include <stdio.h> #include <string.h> int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int t; scanf("%d", & t); while (t--) { char arr[250]; int a; scanf("%d", & a); scanf("%s", arr); if (a == 0) printf("%s\n", arr); ...
code/online_challenges/src/codechef/GCD2/README.md
# Problem Link: [GCD2](https://www.codechef.com/problems/GCD2/) # Description Frank explained its friend Felman the algorithm of Euclides to calculate the GCD of two numbers. Then Felman implements it algorithm int gcd(int a, int b) { if (b==0) return a; else return gcd(b,a%b); } and it proposes to Frank that m...
code/online_challenges/src/codechef/GDOG/GDOG.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { int n, k; scanf("%d%d", & n, & k); int i, max = 0, c; for (i = 2; i <= k; ++i) { c = n % i; if (c >= max) { max = c; } ...
code/online_challenges/src/codechef/GDOG/README.md
# Problem Link: [GDOG](https://www.codechef.com/problems/GDOG/) # Description Tuzik is a little dog. But despite the fact he is still a puppy he already knows about the pretty things that coins are. He knows that for every coin he can get very tasty bone from his master. He believes that some day he will find a treasu...
code/online_challenges/src/codechef/GUESSNUM/Guessnum.java
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Guessnum { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { long a = s.nextLong(); long m = s.nextLon...
code/online_challenges/src/codechef/GUESSNUM/README.md
# Problem Link: [GUESSNUM](https://www.codechef.com/problems/GUESSNUM) # Description Chef is playing a game with his brother Chefu. He asked Chefu to choose a positive integer N, multiply it by a given integer A, then choose a divisor of N (possibly N itself) and add it to the product. Let's denote the resulting int...
code/online_challenges/src/codechef/Greedy Puppy/Greedy_Pupy.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { int n, k; scanf("%d%d", & n, & k); int i, max = 0, c; for (i = 2; i <= k; ++i) { c = n % i; if (c >= max) { max = c; } ...
code/online_challenges/src/codechef/Greedy Puppy/README.md
# Problem Link: [GDOG](https://www.codechef.com/problems/GDOG/) # Description Tuzik is a little dog. But despite the fact he is still a puppy he already knows about the pretty things that coins are. He knows that for every coin he can get very tasty bone from his master. He believes that some day he will find a treasu...
code/online_challenges/src/codechef/HILLJUMP/HILLJUMP.cpp
#include<iostream> using namespace std; void jump(int[], int); void inc(int[], int); int main() { std::ios::sync_with_stdio(false); //int j;for(j=0;j<100;j++)printf("1 "); int n, q; cin>>n>>q; int arr[n]; arr[0]=0; int i; for(i=0;i<n;i++) { int temp; cin>>temp; arr[i]+=temp; arr[i+1]=-temp; } while(q--) ...
code/online_challenges/src/codechef/HILLJUMP/README.md
# Problem Link [HILLJUMP](https://www.codechef.com/AUG17/status/HILLJUMP) # Description Chef is going to organize a hill jumping competition and he is going to be one of the judges in it. In this competition there are N hills in a row, and the initial height of i-th hill is Ai. Participants are required to demonstrat...
code/online_challenges/src/codechef/HORSES/HORSES.c
#include <stdio.h> #include<stdlib.h> int main(void) { int t, i, j; scanf("%d", & t); for (i = 1; i <= t; ++i) { int n; scanf("%d", & n); int s[n]; for (j = 0; j < n; ++j) scanf("%d", & s[j]); int min = abs(s[0] - s[1]); for (j = 0; j < n; +...
code/online_challenges/src/codechef/HORSES/README.md
# Problem Link: [HORSES](https://www.codechef.com/problems/HORSES/) # Description Chef is very fond of horses. He enjoys watching them race. As expected, he has a stable full of horses. He, along with his friends, goes to his stable during the weekends to watch a few of these horses race. Chef wants his friends to enj...
code/online_challenges/src/codechef/J7/J7.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { int p, s; scanf("%d%d", & p, & s); double x1 = (p + sqrt(p * p - (24 * s))) / 12; double x2 = (p - sqrt(p * p - (24 * s))) / 12; double y1 = (p - (8 * x1)) / 4; double y2 = (p - ...
code/online_challenges/src/codechef/J7/README.md
# Problem Link: [J7](https://www.codechef.com/problems/J7) # Description Johnny needs to make a rectangular box for his physics class project. He has bought P cm of wire and S cm2 of special paper. He would like to use all the wire (for the 12 edges) and paper (for the 6 sides) to make the box. What is the largest vo...
code/online_challenges/src/codechef/JAIN/JAIN.cpp
#include <iostream> #include <algorithm> #include <list> #include <stack> #include <string> #include <map> #include <vector> #include <set> #include <utility> std::map<char,int>g_mp; void build () { g_mp.insert(std::make_pair('a',16)); g_mp.insert(std::make_pair('e',8)); g_mp.insert(std::make_pair('i',4))...
code/online_challenges/src/codechef/JAIN/README.md
# Problem Link [JAIN](https://www.codechef.com/MARCH19B/problems/JAIN) # Description Chef has N dishes, numbered 1 through N. For each valid i, dish i is described by a string Di containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'. A meal consists of exactly two dishes. Preparing a meal from di...
code/online_challenges/src/codechef/JOHNY/JOHNY.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { int i, j; int n; scanf("%d", & n); long long int a[100]; long long int pj = 0; for (i = 0; i < n; ++i) { scanf("%lld", & a[i]); } int x; ...
code/online_challenges/src/codechef/JOHNY/JOHNY.cpp
#include <iostream> int main() { int t; std::cin >> t; while (t--) { int n; std::cin >> n; int a[100]; for (int i = 0; i < n; ++i) { std::cin >> a[i]; } int k; std::cin >> k; int ans = 0; for (int...
code/online_challenges/src/codechef/JOHNY/JOHNY.py
t = int(input()) for x in range(t): n = int(input()) p = list(map(int, input().split())) k = int(input()) a = p[k - 1] p.sort() print(p.index(a) + 1)
code/online_challenges/src/codechef/JOHNY/README.md
# Problem Link: [JOHNY](https://www.codechef.com/problems/JOHNY/) # Description Vlad enjoys listening to music. He lives in Sam's Town. A few days ago he had a birthday, so his parents gave him a gift: MP3-player! Vlad was the happiest man in the world! Now he can listen his favorite songs whenever he wants! Vlad bui...
code/online_challenges/src/codechef/LAZERTST/LAZERTST.cpp
#include<iostream> using namespace std; typedef long long ll; void solve() { ll n,m,k,q; cin>>n>>m>>k>>q; vector<pair<int,int>> qs(q); vector<ll> ans(q,0); for (int i = 0; i < q; i++) { cin>>qs[i].first>>qs[i].second; } if(k==3) { cout<<2<<' '; for (int i =...
code/online_challenges/src/codechef/LAZERTST/README.md
# Problem Link [EGGFREE](https://www.codechef.com/MARCH20A/problems/LAZERTST) # Description ### This is an interactive problem. When Vivek was creating the problem LAZER, he realised that it can be used to create a different, interactive problem. Can you solve this problem too? Let's frame this problem formally. Yo...
code/online_challenges/src/codechef/LEPERMUT/LEPERMUT.c
#include <stdio.h> int main(void) { int t; scanf("%d", & t); while (t--) { int n; scanf("%d", & n); int a[100]; int i; for (i = 0; i < n; ++i) { scanf("%d", & a[i]); } int c = 0; for (i = 0; i < n; ++i) { ...
code/online_challenges/src/codechef/LEPERMUT/README.md
# Problem Link: [LEPERMUT](https://www.codechef.com/problems/LEPERMUT/) # Description The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ..., N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The...
code/online_challenges/src/codechef/MARBLES/MARBLES.c
#include <stdio.h> int main(void) { int t; scanf("%d", &t); while(t > 0) { t--; int n, k; long long int ans = 1; scanf("%d", &n); scanf("%d", &k); if(n == k) printf("%lld\n", ans); else { n--; k--; ans = 1; if...
code/online_challenges/src/codechef/MARBLES/MARBLES.py
t = int(input()) while t > 0: t -= 1 ans = 1 n, k = map(int, input().split()) if n == k: print(ans) else: n -= 1 k -= 1 if k > n / 2: k = n - k ans = 1 for i in range(k): ans *= n - i ans /= i + 1 print(i...
code/online_challenges/src/codechef/MARBLES/README.md
# Problem Link: [MARBLES](https://www.codechef.com/problems/MARBLES) # Description Rohit dreams he is in a shop with an infinite amount of marbles. He is allowed to select n marbles. There are marbles of k different colors. From each color there are also infinitely many marbles. Rohit wants to have at least one marble...
code/online_challenges/src/codechef/MARCHA1/MARCHA1.c
#include <stdio.h> int main() { int t; scanf("%d", & t); while (t--) { int m, n, i, j; scanf("%d %d", & n, & m); int b[m + 1]; for (i = 1; i <= m; ++i) b[i] = 0; b[0] = 1; for (i = 0; i < n; ++i) { int k; sca...
code/online_challenges/src/codechef/MARCHA1/README.md
# Problem Link: [MARCHA1](https://www.codechef.com/problems/MARCHA1/) # Description In the mysterious country of Byteland, everything is quite different from what you'd normally expect. In most places, if you were approached by two mobsters in a dark alley, they would probably tell you to give them all the money that ...
code/online_challenges/src/codechef/MATCHES/Mathces.cpp
#include <iostream> #include <vector> int main() { std::vector<int> m = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; int t; std::cin >> t; while (t--) { long long int a, b, sum = 0; std::cin >> a >> b; a += b; while (a > 0) { sum += m[a % 10]; a /= 10; ...
code/online_challenges/src/codechef/MATCHES/README.md
# Problem Link: [MATCHES](https://www.codechef.com/problems/MATCHES/) # Description Chef's son Chefu found some matches in the kitchen and he immediately starting playing with them. The first thing Chefu wanted to do was to calculate the result of his homework — the sum of A and B, and write it using matches. Help Ch...
code/online_challenges/src/codechef/MATCHES/matches.c
#include <stdio.h> int main() { int m[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; int t; scanf("%d", &t); while (t--) { long long int a, b, sum = 0; scanf("%lld%lld", &a, &b); a += b; while (a > 0) { sum += m[a % 10]; a /= 10; } printf("%lld\n", s...
code/online_challenges/src/codechef/MAXDIFF/MAXDIFF.c
#include <stdio.h> int main() { int t; scanf("%d", & t); while (t--) { int i, j, n, k; scanf("%d %d", & n, & k); int a[n]; for (i = 0; i < n; ++i) scanf("%d", & a[i]); for (i = 0; i < n - 1; ++i) { for (j = 0; j < n - i - 1; ++j...
code/online_challenges/src/codechef/MAXDIFF/README.md
# Problem Link: [MAXDIFF](https://www.codechef.com/problems/MAXDIFF/) # Description Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants his dad to g...
code/online_challenges/src/codechef/MEETUP/MEETUP.cpp
#include <iostream> #include <fstream> #include <set> #include <map> #include <string> #include <vector> #include <bitset> #include <algorithm> #include <cstring> #include <cstdlib> #include <cmath> #include <cassert> #include <queue> #define mp make_pair #define pb push_back typedef long long ll; typedef long doubl...
code/online_challenges/src/codechef/MEETUP/README.md
# Problem Link [MEETUP](https://www.codechef.com/problems/MEETUP) # Description This is an interactive problem. It means that every time you output something, you must finish with a newline character and flush the output. For example, in C++ use the fflush(stdout) function, in Java — call System.out.flush(), in Pyt...