├── Greedy ├── BeautifulPairs.c ├── ChiefHopper.c ├── ChiefHopper.java ├── CuttingBoard'sRough.c ├── GoodlandElectricity.c ├── GreedyFlorist.c ├── GreedyFlorist.java ├── GridChallenge.c ├── JimAndTheOrders.c ├── LargestPermutation.c ├── LuckBalance.c ├── MarcsCakewalk.c ├── MarcsCakewalk.java ├── MarkAndToys.c ├── MarkAndToys.java ├── MaxMin.c ├── MaximumPerimeterTriangle.c ├── MinimumAbsoluteDifferenceInAnArray.c ├── MinimumAbsoluteDifferenceInAnArray.java ├── PermutingTwoArrays.c ├── PermutingTwoArrays.java ├── PriyankaAndToys.c ├── PriyankaAndToys.java ├── Readme.md └── SherlockAndTheBeast.c ├── Implementation ├── 3DSurfaceArea.c ├── 3DSurfaceArea.java ├── ACM_ICPC_Team.c ├── AlmostSorted.c ├── AngryProfessor.c ├── AppendAndDelete.c ├── AppleAndOrange.c ├── BeautifulDaysAtTheMovies.c ├── BeautifulTriplets.c ├── BetweenTwoSets.c ├── BiggerIsGreater.c ├── BirthdayChocolate.c ├── BonAppétit.c ├── BreakingTheRecords.c ├── CatsAndAMouse.c ├── CavityMap.c ├── ChocolateFeast.c ├── CircularArrayRotation.c ├── ClimbingTheLeaderboard.java ├── ClimbingTheLeaderboard.js ├── CountingValleys.c ├── CutTheSticks.c ├── DayOfTheProgrammer.c ├── Designer_PDF_Viewer.c ├── DivisibleSumPairs.c ├── DrawingBook.c ├── ElectronicsShop.c ├── Ema'sSupercomputer.java ├── Encryption.c ├── EqualizeTheArray.c ├── ExtraLongFactorials.c ├── FairRations.c ├── FindDigits.c ├── FlatlandSpaceStations.c ├── FormingAMagicSquare.c ├── GradingStudents.c ├── HalloweenSale.c ├── JumpingOnTheClouds.c ├── Kangaroo.c ├── LibraryFine.c ├── Lisa'sWorkbook.c ├── ManasaAndStones.c ├── MatrixLayerRotation.java ├── MigratoryBirds.c ├── MinimumDistances.c ├── ModifiedKaprekarNumbers.c ├── OrganizingContainersOfBalls.c ├── PickingNumbers.c ├── Readme.md ├── RepeatedString.c ├── SaveThePrisoner!.c ├── SequenceEquation.c ├── ServiceLane.c ├── SherlockAndSquares.c ├── SockMerchant.c ├── StrangeCounter.c ├── Taum&B'day.c ├── TheGridSearch.java ├── TheHurdleRace.c ├── TheTimeInWords.c ├── UtopianTree.c └── ViralAdvertising.c ├── LICENSE ├── README.md ├── Search ├── ConnectedCellsInAGrid.java ├── HackerlandRadioTransmitters.c ├── Ice Cream Parlor.c ├── Missing Numbers.c ├── Pairs.java ├── README.md └── SherlockAndArray.c ├── Sorting ├── BigSorting.java ├── FindTheMedian.c └── README.md ├── Strings ├── AlternatingCharacters.c ├── Anagram.c ├── BeautifulBinaryString.c ├── CaesarCipher.c ├── CamelCase.c ├── CommonChild.java ├── FunnyString.c ├── GameOfThrones-I.c ├── Gemstones.c ├── HackerRankInAString!.c ├── MakingAnagrams.c ├── MarsExploration.c ├── PalindromeIndex.java ├── Pangrams.c ├── README.md ├── SeparateTheNumbers.c ├── SherlockAndAnagrams.java ├── SherlockAndTheValidString.java ├── StringConstruction.c ├── StrongPassword.c ├── SuperReducedString.c ├── TheLove-LetterMystery.c ├── TwoCharacters.java ├── TwoStrings.c └── WeightedUniformStrings.java ├── Warmup ├── A Very Big Sum │ ├── A Very Big Sum.c │ ├── A Very Big Sum.java │ └── README.md ├── Birthday Cake Candles │ ├── Birthday Cake Candles.c │ ├── BirthdayCakeCandles.java │ └── BirthdayCakeCandles.js ├── Compare the Triplets │ ├── Compare the Triplets.c │ ├── Compare the Triplets.java │ └── README.md ├── Diagonal Difference │ ├── Diagonal Difference.c │ ├── Diagonal Difference.java │ ├── Diagonal Difference.js │ └── README.md ├── Mini-Max Sum │ ├── Mini-MaxSum.c │ ├── Mini-MaxSum.java │ └── Mini-MaxSum.js ├── Plus Minus │ ├── PlusMinus.c │ ├── PlusMinus.java │ └── PlusMinus.js ├── README.md ├── Simple Array Sum │ ├── README.md │ ├── Simple Array Sum.c │ └── SimpleArraySum.java ├── Solve Me First │ ├── README.md │ ├── SolveMeFirst.c │ └── SolveMeFirst.java ├── Staircase │ ├── Staircase.c │ ├── Staircase.java │ └── Staircase.js └── Time Conversion │ ├── TimeConversion.c │ ├── TimeConversion.java │ └── TimeConversion.js ├── _config.yml └── greedy └── _config.yml /Greedy/BeautifulPairs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(int *p,int n) 5 | { 6 | int i,j,key; 7 | for(i=0;i=0&&(*(p+j))>key) 12 | { 13 | *(p+j+1)=*(p+j); 14 | j=j-1; 15 | } 16 | *(p+j+1)=key; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | int n,*a,*b,i,j,count; 23 | scanf("%d",&n); 24 | a=(int *)malloc(n*sizeof(int)); 25 | b=(int *)malloc(n*sizeof(int)); 26 | for(i=0;i*(b+j)) 48 | { 49 | j++; 50 | } 51 | else if(*(a+i)<*(b+j)) 52 | { 53 | i++; 54 | } 55 | } 56 | break; 57 | } 58 | if(count 2 | #include 3 | 4 | int main(){ 5 | int n, *arr, e, i; 6 | scanf("%d", &n); 7 | arr = (int *)malloc(sizeof(int) * n); 8 | for(i = 0; i < n; i++){ 9 | scanf("%d", arr + i); 10 | } 11 | e = 0; 12 | for(i = n - 1; i >= 0; i--){ 13 | e = (e + *(arr + i) + 1) / 2; 14 | } 15 | printf("%d", e); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Greedy/ChiefHopper.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the chiefHopper function below. 12 | static int chiefHopper(int[] arr) { 13 | int e = 0; 14 | for(int i = arr.length - 1; i >= 0; i--){ 15 | e = (e + arr[i] + 1) / 2; 16 | } 17 | return e; 18 | } 19 | 20 | private static final Scanner scanner = new Scanner(System.in); 21 | 22 | public static void main(String[] args) throws IOException { 23 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 24 | 25 | int n = scanner.nextInt(); 26 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 27 | 28 | int[] arr = new int[n]; 29 | 30 | String[] arrItems = scanner.nextLine().split(" "); 31 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 32 | 33 | for (int i = 0; i < n; i++) { 34 | int arrItem = Integer.parseInt(arrItems[i]); 35 | arr[i] = arrItem; 36 | } 37 | 38 | int result = chiefHopper(arr); 39 | 40 | bufferedWriter.write(String.valueOf(result)); 41 | bufferedWriter.newLine(); 42 | 43 | bufferedWriter.close(); 44 | 45 | scanner.close(); 46 | } 47 | } -------------------------------------------------------------------------------- /Greedy/CuttingBoard'sRough.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void merge(long int arr[],long int l,long int m,long int r) 5 | { 6 | long int i, j, k; 7 | long int n1 = m - l + 1; 8 | long int n2 = r - m; 9 | long int L[n1], R[n2]; 10 | for (i = 0; i < n1; i++) 11 | L[i] = arr[l + i]; 12 | for (j = 0; j < n2; j++) 13 | R[j] = arr[m + 1+ j]; 14 | i = 0; 15 | j = 0; 16 | k = l; 17 | while (i < n1 && j < n2) 18 | { 19 | if (L[i] >= R[j]) 20 | { 21 | arr[k] = L[i]; 22 | i++; 23 | } 24 | else 25 | { 26 | arr[k] = R[j]; 27 | j++; 28 | } 29 | k++; 30 | } 31 | while (i < n1) 32 | { 33 | arr[k] = L[i]; 34 | i++; 35 | k++; 36 | } 37 | while (j < n2) 38 | { 39 | arr[k] = R[j]; 40 | j++; 41 | k++; 42 | } 43 | } 44 | void mergeSort(long int arr[],long int l,long int r) 45 | { 46 | if (l < r) 47 | { 48 | long int m = l+(r-l)/2; 49 | mergeSort(arr, l, m); 50 | mergeSort(arr, m+1, r); 51 | merge(arr, l, m, r); 52 | } 53 | } 54 | 55 | int main() 56 | { 57 | long int q,n,m,z,i,j,countx,county,*arr,*brr,cost; 58 | scanf("%ld",&q); 59 | for(z=0;z=*(brr+j)) 82 | { 83 | cost+=(*(arr+i)*county); 84 | countx++; 85 | i++; 86 | } 87 | else 88 | { 89 | cost+=(*(brr+j)*countx); 90 | county++; 91 | j++; 92 | } 93 | } 94 | printf("%ld\n",cost%(1000000007)); 95 | } 96 | return 0; 97 | } -------------------------------------------------------------------------------- /Greedy/GoodlandElectricity.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n, k, *arr, count=0, i, a, x; 7 | scanf("%d %d",&n,&k); 8 | arr=(int *)malloc(n*sizeof(int)); 9 | for(i=0;ix;i--) 18 | { 19 | if(i=n) 34 | { 35 | printf("%d",count); 36 | break; 37 | } 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Greedy/GreedyFlorist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(int *p,int n) 5 | { 6 | int i,j,key; 7 | for(i=0;i=0&&(*(p+j))>key) 12 | { 13 | *(p+j+1)=*(p+j); 14 | j=j-1; 15 | } 16 | *(p+j+1)=key; 17 | } 18 | } 19 | 20 | int GreedyFlorist(int * c,int n,int k) 21 | { 22 | int i, j=0, count=0,x=1; 23 | for(i=n-1;i>=0;i--) 24 | { 25 | j++; 26 | count+=(x*(*(c+i))); 27 | if(j%k==0) 28 | { 29 | x++; 30 | } 31 | } 32 | return count; 33 | } 34 | 35 | int main() 36 | { 37 | int i,n,k,*c,ans; 38 | scanf("%d %d",&n,&k); 39 | c=(int *)malloc(n*sizeof(int)); 40 | for(i=0;i= 0; i--, j++){ 17 | count += x * c[i]; 18 | if(j % k == 0){ 19 | x++; 20 | } 21 | } 22 | return count; 23 | } 24 | 25 | private static final Scanner scanner = new Scanner(System.in); 26 | 27 | public static void main(String[] args) throws IOException { 28 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 29 | 30 | String[] nk = scanner.nextLine().split(" "); 31 | 32 | int n = Integer.parseInt(nk[0]); 33 | 34 | int k = Integer.parseInt(nk[1]); 35 | 36 | int[] c = new int[n]; 37 | 38 | String[] cItems = scanner.nextLine().split(" "); 39 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 40 | 41 | for (int i = 0; i < n; i++) { 42 | int cItem = Integer.parseInt(cItems[i]); 43 | c[i] = cItem; 44 | } 45 | 46 | int minimumCost = getMinimumCost(k, c); 47 | 48 | bufferedWriter.write(String.valueOf(minimumCost)); 49 | bufferedWriter.newLine(); 50 | 51 | bufferedWriter.close(); 52 | 53 | scanner.close(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Greedy/GridChallenge.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSortCharacter(char *p,int n) 5 | { 6 | int i,j; 7 | char key; 8 | for(i=0;i=0&&(*(p+j))>key) 13 | { 14 | *(p+j+1)=*(p+j); 15 | j=j-1; 16 | } 17 | *(p+j+1)=key; 18 | } 19 | } 20 | 21 | int GridChallenge() 22 | { 23 | int i,j,n; 24 | char temp,a[100][100]; 25 | for(i=0;i<100;i++) 26 | { 27 | for(j=0;j<100;j++) 28 | { 29 | a[i][j]=123; 30 | } 31 | } 32 | scanf("%d",&n); 33 | for(i=0;ia[i][j]) 50 | { 51 | return 0; 52 | } 53 | temp=a[i][j]; 54 | } 55 | } 56 | return 1; 57 | } 58 | 59 | int main() 60 | { 61 | int k,t,*ans; 62 | scanf("%d",&t); 63 | ans=(int *)malloc(t*sizeof(int)); 64 | for(k=0;k 2 | #include 3 | 4 | typedef struct time 5 | { 6 | int t,index; 7 | }time; 8 | 9 | void Sort(time *p,int n) 10 | { 11 | int i,j,k,key,key1; 12 | while(--n) 13 | { 14 | for(j=0;jt>(p+j+1)->t) 17 | { 18 | key=(p+j)->t; 19 | key1=(p+j)->index; 20 | (p+j)->t=(p+j+1)->t; 21 | (p+j)->index=(p+j+1)->index; 22 | (p+j+1)->t=key; 23 | (p+j+1)->index=key1; 24 | } 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int i,n,a,b; 32 | time *ti; 33 | scanf("%d",&n); 34 | ti=(time *)malloc(n*sizeof(time)); 35 | for(i=0;it=a+b; 39 | (ti+i)->index=i; 40 | } 41 | Sort(ti,n); 42 | for(i=0;iindex)+1); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Greedy/LargestPermutation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int MaxIndex(int * arr, int n) 5 | { 6 | int i; 7 | for(i=0;i= R[j]) 33 | { 34 | arr[k] = L[i]; 35 | i++; 36 | } 37 | else 38 | { 39 | arr[k] = R[j]; 40 | j++; 41 | } 42 | k++; 43 | } 44 | while (i < n1) 45 | { 46 | arr[k] = L[i]; 47 | i++; 48 | k++; 49 | } 50 | while (j < n2) 51 | { 52 | arr[k] = R[j]; 53 | j++; 54 | k++; 55 | } 56 | } 57 | void mergeSort(int arr[], int l, int r) 58 | { 59 | if (l < r) 60 | { 61 | int m = l+(r-l)/2; 62 | mergeSort(arr, l, m); 63 | mergeSort(arr, m+1, r); 64 | merge(arr, l, m, r); 65 | } 66 | } 67 | 68 | int main() 69 | { 70 | int i,n,m,k,*arr,mi,temp; 71 | scanf("%d %d",&n,&k); 72 | arr=(int *)malloc(n*sizeof(int)); 73 | for(i=0;in) 78 | { 79 | mergeSort(arr,0,n-1); 80 | } 81 | else 82 | { 83 | m=n; 84 | for(i=0;i 2 | #include 3 | 4 | void insertionSort(int *p,int n) 5 | { 6 | int i,j,key; 7 | for(i=0;i=0&&(*(p+j))>key) 12 | { 13 | *(p+j+1)=*(p+j); 14 | j=j-1; 15 | } 16 | *(p+j+1)=key; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | int i, j, n, k, l, *t, temp, count=0, a=0; 23 | scanf("%d %d",&n,&k); 24 | t=(int *)malloc(n*sizeof(int)); 25 | for(i=0;i=a;i--) 48 | { 49 | count-=*(t+i); 50 | } 51 | printf("%d",count); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Greedy/MarcsCakewalk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long int pow(int a,int b) 5 | { 6 | int i; 7 | long int ans=1; 8 | for(i=0;i=0&&(*(p+j))>key) 23 | { 24 | *(p+j+1)=*(p+j); 25 | j=j-1; 26 | } 27 | *(p+j+1)=key; 28 | } 29 | } 30 | 31 | long int Marcs_Cakewalk(int* c , int n ) 32 | { 33 | int i, j; 34 | long int ans=0; 35 | for(i=n-1,j=0;i>=0;j++,i--) 36 | { 37 | ans+=(*(c+i)*pow(2,j)); 38 | } 39 | return ans; 40 | } 41 | 42 | int main() 43 | { 44 | int i, n, *c; 45 | long ans; 46 | scanf("%d",&n); 47 | c=(int *)malloc(n*sizeof(int)); 48 | for(i=0;i= 0; j++,i--){ 16 | ans += calorie[i] * Math.pow(2,j); 17 | } 18 | return ans; 19 | } 20 | 21 | private static final Scanner scanner = new Scanner(System.in); 22 | 23 | public static void main(String[] args) throws IOException { 24 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 25 | 26 | int n = scanner.nextInt(); 27 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 28 | 29 | int[] calorie = new int[n]; 30 | 31 | String[] calorieItems = scanner.nextLine().split(" "); 32 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 33 | 34 | for (int i = 0; i < n; i++) { 35 | int calorieItem = Integer.parseInt(calorieItems[i]); 36 | calorie[i] = calorieItem; 37 | } 38 | 39 | long result = marcsCakewalk(calorie); 40 | 41 | bufferedWriter.write(String.valueOf(result)); 42 | bufferedWriter.newLine(); 43 | 44 | bufferedWriter.close(); 45 | 46 | scanner.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Greedy/MarkAndToys.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(long int *p,int n) 5 | { 6 | int i,j; 7 | long int key; 8 | for(i=0;i=0&&(*(p+j))>key) 13 | { 14 | *(p+j+1)=*(p+j); 15 | j=j-1; 16 | } 17 | *(p+j+1)=key; 18 | } 19 | } 20 | 21 | int MarkAndToys(long int * p,int n,int k) 22 | { 23 | int i, count=-1, add=0; 24 | for(i=0;(i 2 | #include 3 | 4 | void insertionSort(double *p,int n) 5 | { 6 | int i,j; 7 | double key; 8 | for(i=0;i=0&&(*(p+j))>key) 13 | { 14 | *(p+j+1)=*(p+j); 15 | j=j-1; 16 | } 17 | *(p+j+1)=key; 18 | } 19 | } 20 | 21 | double MinValue(double * arr, int n) 22 | { 23 | int i; 24 | double temp=*(arr); 25 | for(i=1;i*(arr+i)) 28 | { 29 | temp=*(arr+i); 30 | } 31 | } 32 | return temp; 33 | } 34 | 35 | int main() 36 | { 37 | int n,k,i; 38 | double *arr,*unfairness,ans; 39 | scanf("%d %d",&n,&k); 40 | arr=(double *)malloc(n*sizeof(double)); 41 | for(i=0;i 2 | #include 3 | 4 | void insertionSort(int *p,int n) 5 | { 6 | int i,j,key; 7 | for(i=0;i=0&&(*(p+j))>key) 12 | { 13 | *(p+j+1)=*(p+j); 14 | j=j-1; 15 | } 16 | *(p+j+1)=key; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | int i, n, *s; 23 | scanf("%d",&n); 24 | s=(int*)malloc(n*sizeof(int)); 25 | for(i=0;i=2;i--) 31 | { 32 | if((*(s+i-2)+*(s+i-1))>*(s+i)) 33 | { 34 | break; 35 | } 36 | } 37 | if(i==1) 38 | { 39 | printf("%d",-1); 40 | } 41 | else 42 | { 43 | printf("%d %d %d",*(s+i-2),*(s+i-1),*(s+i)); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Greedy/MinimumAbsoluteDifferenceInAnArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(long int *p,int n) 5 | { 6 | int i,j; 7 | long int key; 8 | for(i=0;i=0&&(*(p+j))>key) 13 | { 14 | *(p+j+1)=*(p+j); 15 | j=j-1; 16 | } 17 | *(p+j+1)=key; 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | int i,n; 24 | long int *a,d,min=10000000000; 25 | scanf("%d",&n); 26 | a=(long int *)malloc(n*sizeof(long int)); 27 | for(i=0;i 2 | #include 3 | 4 | void insertionSort(long int *p,int n) 5 | { 6 | int i,j; 7 | long int key; 8 | for(i=0;i=0&&(*(p+j))>key) 13 | { 14 | *(p+j+1)=*(p+j); 15 | j=j-1; 16 | } 17 | *(p+j+1)=key; 18 | } 19 | } 20 | 21 | int PermutingTwoArrays(long int * A,long int * B,int n,long int k) 22 | { 23 | int i,j; 24 | for(i=0,j=n-1;i 2 | #include 3 | #include 4 | #include 5 | 6 | void insertionSort(int *p,int n) 7 | { 8 | int i,j,key; 9 | for(i=0;i=0&&(*(p+j))>key) 14 | { 15 | *(p+j+1)=*(p+j); 16 | j=j-1; 17 | } 18 | *(p+j+1)=key; 19 | } 20 | } 21 | 22 | int PriyankaAndToys(int *w,int n) 23 | { 24 | int i,j,count=0,x; 25 | for(i=0;i 2 | #include 3 | 4 | int main() 5 | { 6 | int t, i, j, *n; 7 | scanf("%d",&t); 8 | n=(int *)malloc(t*sizeof(int)); 9 | for(i=0;i=0) 25 | { 26 | for(j=0;j<*(n+i)-10;j++) 27 | { 28 | printf("%d",5); 29 | } 30 | for(j=*(n+i)-10;j<*(n+i);j++) 31 | { 32 | printf("%d",3); 33 | } 34 | } 35 | else 36 | { 37 | printf("%d",-1); 38 | } 39 | } 40 | else if(*(n+i)%3==2) 41 | { 42 | if(*(n+i)-5>=0) 43 | { 44 | for(j=0;j<*(n+i)-5;j++) 45 | { 46 | printf("%d",5); 47 | } 48 | for(j=*(n+i)-5;j<*(n+i);j++) 49 | { 50 | printf("%d",3); 51 | } 52 | } 53 | else 54 | { 55 | printf("%d",-1); 56 | } 57 | } 58 | printf("\n"); 59 | } 60 | } -------------------------------------------------------------------------------- /Implementation/3DSurfaceArea.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int smaller(int i, int j){ 5 | if(i < j) 6 | return i; 7 | return j; 8 | } 9 | 10 | int main(){ 11 | int i, j, n, m, arr[100][100], total=0, price, x, v, h, v1 = 0, v2 = 0; 12 | scanf("%d %d",&n,&m); 13 | for(i = 0; i < n; i++){ 14 | for(j = 0; j < m; j++){ 15 | scanf("%d", &arr[i][j]); 16 | total += arr[i][j]; 17 | } 18 | } 19 | price = total * 6; 20 | for(i = 0; i < n; i++){ 21 | x = arr[i][0]; 22 | for(j = 1; j < m; j++){ 23 | v1 += smaller(x, arr[i][j]) * 2; 24 | x = arr[i][j]; 25 | } 26 | } 27 | for(j = 0; j < m; j++){ 28 | x = arr[0][j]; 29 | for(i = 1; i < n; i++){ 30 | v2 += smaller(x, arr[i][j]) * 2; 31 | x = arr[i][j]; 32 | } 33 | } 34 | v = v1 + v2; 35 | h = (total - m * n) * 2; 36 | price = price - v - h; 37 | printf("%d", price); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Implementation/3DSurfaceArea.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the surfaceArea function below. 12 | static int surfaceArea(int[][] arr) { 13 | int n = arr.length; 14 | int m = arr[0].length; 15 | int v = 0; 16 | int totalCube = 0; 17 | for(int i = 0; i < n; i++){ 18 | for(int j = 0; j < m; j++){ 19 | totalCube += arr[i][j]; 20 | if(j != 0){ 21 | v += Math.min(arr[i][j - 1], arr[i][j]) * 2; 22 | } 23 | if(i != 0){ 24 | v += Math.min(arr[i - 1][j], arr[i][j]) * 2; 25 | } 26 | } 27 | } 28 | int vSA = totalCube * 4 - v; 29 | int hSA = m * n * 2; 30 | return vSA + hSA; 31 | } 32 | 33 | private static final Scanner scanner = new Scanner(System.in); 34 | 35 | public static void main(String[] args) throws IOException { 36 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 37 | 38 | String[] HW = scanner.nextLine().split(" "); 39 | 40 | int H = Integer.parseInt(HW[0]); 41 | 42 | int W = Integer.parseInt(HW[1]); 43 | 44 | int[][] A = new int[H][W]; 45 | 46 | for (int i = 0; i < H; i++) { 47 | String[] ARowItems = scanner.nextLine().split(" "); 48 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 49 | 50 | for (int j = 0; j < W; j++) { 51 | int AItem = Integer.parseInt(ARowItems[j]); 52 | A[i][j] = AItem; 53 | } 54 | } 55 | 56 | int result = surfaceArea(A); 57 | 58 | bufferedWriter.write(String.valueOf(result)); 59 | bufferedWriter.newLine(); 60 | 61 | bufferedWriter.close(); 62 | 63 | scanner.close(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Implementation/ACM_ICPC_Team.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int m, n, i, j, k, max=0, count, temp; 12 | char **arr, *brr; 13 | scanf("%d %d",&n,&m); 14 | arr=(char **)malloc(n*sizeof(char*)); 15 | brr=(char *)malloc(m*sizeof(char)); 16 | for(i=0;imax) 38 | { 39 | count=1; 40 | max=temp; 41 | } 42 | else if(temp==max) 43 | { 44 | count++; 45 | } 46 | } 47 | } 48 | printf("%d\n%d",max,count); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Implementation/AlmostSorted.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | void swap(int *arr,int l,int r) 10 | { 11 | int temp; 12 | temp=arr[l]; 13 | arr[l]=arr[r]; 14 | arr[r]=temp; 15 | } 16 | 17 | void reverse(int * arr,int l,int r) 18 | { 19 | int temp,i,d=(r-l+1)/2; 20 | for(i=1;i<=d;i++) 21 | { 22 | swap(arr,l,r); 23 | l++; 24 | r--; 25 | } 26 | } 27 | void almostSorted(int n, int* arr) 28 | { 29 | int i,j,x=-1,l=-1,r,m,o,temp; 30 | for(i=1;i<=n;i++) 31 | { 32 | if(*(arr+i) 7 | #include 8 | 9 | int main() 10 | { 11 | int t,i,j,n,k,count,temp; 12 | scanf("%i", &t); 13 | for(i=0;i 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | int k,slen,tlen,i,x; 13 | char s[101],t[101]; 14 | scanf(" %s",s); 15 | slen=strlen(s); 16 | scanf(" %s",t); 17 | tlen=strlen(t); 18 | scanf("%d",&k); 19 | for(i=0;ix) 30 | { 31 | k=k-x; 32 | if(k%2==0 || k>(2*i)) 33 | printf("Yes"); 34 | else 35 | printf("No"); 36 | } 37 | else 38 | printf("No"); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Implementation/AppleAndOrange.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int i,s,t,a,b,n,m,*apple,*orange,ac=0,oc=0,x; 12 | scanf("%d %d",&s,&t); 13 | scanf("%d %d",&a,&b); 14 | scanf("%d %d",&n,&m); 15 | apple=(int *)malloc(n*sizeof(int)); 16 | orange=(int *)malloc(m*sizeof(int)); 17 | for(i=0;i=s) && (x<=t) ) 22 | { 23 | ac++; 24 | } 25 | } 26 | for(i=0;i=s) && (x<=t) ) 31 | { 32 | oc++; 33 | } 34 | } 35 | printf("%d\n%d",ac,oc); 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Implementation/BeautifulDaysAtTheMovies.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int AbsoluteDiff(int a, int b) 10 | { 11 | if(a>=b) 12 | { 13 | return a-b; 14 | } 15 | return b-a; 16 | } 17 | 18 | int ReverseNum(int a) 19 | { 20 | int b=0,r; 21 | while(a>0) 22 | { 23 | r=a%10; 24 | b=(b*10)+r; 25 | a/=10; 26 | } 27 | return b; 28 | } 29 | 30 | int main() 31 | { 32 | int i,j,k,a,count=0,ra,ad; 33 | scanf("%d %d %d",&i,&j,&k); 34 | for(a=i;a<=j;a++) 35 | { 36 | ra=ReverseNum(a); 37 | ad=AbsoluteDiff(a,ra); 38 | if(ad%k==0) 39 | { 40 | count++; 41 | } 42 | } 43 | printf("%d",count); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Implementation/BeautifulTriplets.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int n,*arr,i,j,k,x,y,d,count=0; 12 | scanf("%d %d",&n,&d); 13 | arr=(int *)malloc(n*sizeof(int)); 14 | for(i=0;id) 24 | { 25 | break; 26 | } 27 | else if (x==d) 28 | { 29 | for(k=j+1;kd) 33 | { 34 | break; 35 | } 36 | else if (y==d) 37 | { 38 | count++; 39 | break; 40 | } 41 | } 42 | break; 43 | } 44 | } 45 | } 46 | printf("%d",count); 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Implementation/BetweenTwoSets.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int MaxValue(int * arr, int n) 10 | { 11 | int i, temp=*(arr); 12 | for(i=1;i*(arr+i)) 28 | { 29 | temp=*(arr+i); 30 | } 31 | } 32 | return temp; 33 | } 34 | 35 | int getTotalX(int n, int* a, int m, int* b) 36 | { 37 | int A,B,i,j,count=0; 38 | A=MaxValue(a,n); 39 | B=MinValue(b,m); 40 | for(i=A;i<=B;i++) 41 | { 42 | for(j=0;j 7 | #include 8 | 9 | void insertionSortCharacter(char *p,int n) 10 | { 11 | int i,j; 12 | char key; 13 | for(i=0;i=0&&(*(p+j))>key) 18 | { 19 | *(p+j+1)=*(p+j); 20 | j=j-1; 21 | } 22 | *(p+j+1)=key; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int t,len,i,j,temp; 29 | char w[101]; 30 | scanf("%d",&t); 31 | while(t--) 32 | { 33 | scanf(" %s",w); 34 | len=strlen(w); 35 | for(i=len-1;i>0;i--) 36 | { 37 | if(*(w+i)>*(w+i-1)) 38 | { 39 | insertionSortCharacter(w+i,len-i); 40 | for(j=i;j*(w+i-1)) 43 | { 44 | temp=*(w+j); 45 | *(w+j)=*(w+i-1); 46 | *(w+i-1)=temp; 47 | break; 48 | } 49 | } 50 | insertionSortCharacter(w+i,len-i); 51 | printf("%s\n",w); 52 | break; 53 | } 54 | } 55 | if(i==0) 56 | printf("no answer\n"); 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Implementation/BirthdayChocolate.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int solve(int n, int* s, int d, int m) 10 | { 11 | int i,j,sum,count=0; 12 | for(i=0;i 7 | #include 8 | 9 | void bonAppetit(int n, int k, int b,int* arr) 10 | { 11 | int i, sum=0,ans; 12 | for(i=0;i 7 | #include 8 | 9 | 10 | int main() 11 | { 12 | int i,n; 13 | long int *arr,hr=-1,lr=100000001,lc=-1,hc=-1; 14 | scanf("%d",&n); 15 | arr= (long int *)malloc(sizeof(long int) * n); 16 | for(i=0;ihr) 20 | { 21 | hr=*(arr+i); 22 | hc++; 23 | } 24 | if(*(arr+i) 7 | #include 8 | 9 | int AbsoluteDiff(int a, int b) 10 | { 11 | if(a>=b) 12 | { 13 | return a-b; 14 | } 15 | return b-a; 16 | } 17 | 18 | int main() 19 | { 20 | int q,x,y,z,i,*ans,A,B; 21 | scanf("%d",&q); 22 | ans=(int *)malloc(q*sizeof(int)); 23 | for(i=0;iB) 33 | *(ans+i)=2; 34 | } 35 | for(i=0;i 7 | #include 8 | 9 | int main(){ 10 | int n,i,j; 11 | scanf("%d",&n); 12 | char* *grid = malloc(sizeof(char*) * n); 13 | for(i = 0; i < n; i++){ 14 | grid[i] = (char *)malloc(10240 * sizeof(char)); 15 | scanf("%s",grid[i]); 16 | } 17 | for(i=1;i*(grid[i]+j+1)&&*(grid[i]+j)>*(grid[i]+j-1)&&*(grid[i]+j)>*(grid[i-1]+j)&&*(grid[i]+j)>*(grid[i+1]+j)) 22 | { 23 | *(grid[i]+j)='X'; 24 | } 25 | } 26 | } 27 | for(i = 0; i < n; i++) 28 | { 29 | printf("%s",grid[i]); 30 | printf("\n"); 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Implementation/ChocolateFeast.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int t, z, n, c, m, i, x, choco; 12 | scanf("%d",&t); 13 | for(z=0;z=m) 19 | { 20 | x=n/m; 21 | choco+=x; 22 | n+=(x-(m*x)); 23 | } 24 | printf("%d\n",choco); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Implementation/CircularArrayRotation.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | long int *arr, n, k, q, i, j, a; 12 | scanf("%ld %ld %ld",&n,&k,&q); 13 | k=k%n; 14 | arr=(long int *)malloc(n*sizeof(long int)); 15 | for(i=0;i=n) 19 | { 20 | j-=n; 21 | } 22 | scanf("%ld",arr+j); 23 | } 24 | for(i=0;i climbingLeaderboard(ArrayList al1, ArrayList al2) { 13 | int sz1 = al1.size(); 14 | int sz2 = al2.size(); 15 | int i = sz1 - 1, j = 0; 16 | ArrayList al3 = new ArrayList(); 17 | for(; (j < sz2) && (i >= 0); i--){ 18 | if(al1.get(i) > al2.get(j)){ 19 | al3.add(i + 2); 20 | i++; 21 | j++; 22 | } 23 | else if(al1.get(i) == al2.get(j)){ 24 | al3.add(i + 1); 25 | i++; 26 | j++; 27 | } 28 | } 29 | while(j++ < sz2){ 30 | al3.add(1); 31 | } 32 | return al3; 33 | } 34 | private static final Scanner sc = new Scanner(System.in); 35 | 36 | public static void main(String[] args) { 37 | int scoresCount = sc.nextInt(); 38 | TreeSet ts = new TreeSet(); 39 | for (int i = 0; i < scoresCount; i++) { 40 | int scoresItem = sc.nextInt(); 41 | ts.add(scoresItem); 42 | } 43 | ArrayList al1 = new ArrayList(ts); 44 | Collections.reverse(al1); 45 | ArrayList al2 = new ArrayList(); 46 | ArrayList al3 = new ArrayList(); 47 | int aliceCount = sc.nextInt(); 48 | for (int i = 0; i < aliceCount; i++) { 49 | int aliceItem = sc.nextInt(); 50 | al2.add(aliceItem); 51 | } 52 | al3 = climbingLeaderboard(al1,al2); 53 | for (int i = 0; i < aliceCount; i++) { 54 | System.out.println(al3.get(i)); 55 | } 56 | sc.close(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Implementation/ClimbingTheLeaderboard.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | 5 | process.stdin.resume(); 6 | process.stdin.setEncoding('utf-8'); 7 | 8 | let inputString = ''; 9 | let currentLine = 0; 10 | 11 | process.stdin.on('data', inputStdin => { 12 | inputString += inputStdin; 13 | }); 14 | 15 | process.stdin.on('end', _ => { 16 | inputString = inputString.replace(/\s*$/, '') 17 | .split('\n') 18 | .map(str => str.replace(/\s*$/, '')); 19 | 20 | main(); 21 | }); 22 | 23 | function readLine() { 24 | return inputString[currentLine++]; 25 | } 26 | 27 | // Complete the climbingLeaderboard function below. 28 | function climbingLeaderboard(al1, al2) { 29 | al1 = [...new Set(al1)]; 30 | console.log(al1); 31 | let sz1 = al1.length; 32 | let sz2 = al2.length; 33 | let i = sz1 - 1, j = 0; 34 | let al3 = []; 35 | for(; (j < sz2) && (i >= 0); i--){ 36 | if(al1[i] > al2[j]){ 37 | al3.push(i + 2); 38 | i++; 39 | j++; 40 | } 41 | else if(al1[i] === al2[j]){ 42 | al3.push(i + 1); 43 | i++; 44 | j++; 45 | } 46 | } 47 | while(j++ < sz2){ 48 | al3.push(1); 49 | } 50 | return al3; 51 | } 52 | 53 | function main() { 54 | const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 55 | 56 | const scoresCount = parseInt(readLine(), 10); 57 | 58 | const scores = readLine().split(' ').map(scoresTemp => parseInt(scoresTemp, 10)); 59 | 60 | const aliceCount = parseInt(readLine(), 10); 61 | 62 | const alice = readLine().split(' ').map(aliceTemp => parseInt(aliceTemp, 10)); 63 | 64 | let result = climbingLeaderboard(scores, alice); 65 | 66 | ws.write(result.join("\n") + "\n"); 67 | 68 | ws.end(); 69 | } 70 | -------------------------------------------------------------------------------- /Implementation/CountingValleys.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int i,n,level=0,s=1,t=0,count=0; 12 | char *str; 13 | scanf("%d",&n); 14 | str=(char *)malloc(n*sizeof(char)); 15 | fflush(stdin); 16 | for(i=0;i 7 | #include 8 | 9 | void insertionSort(int *p,int n) 10 | { 11 | int i,j,key; 12 | for(i=0;i=0&&(*(p+j))>key) 17 | { 18 | *(p+j+1)=*(p+j); 19 | j=j-1; 20 | } 21 | *(p+j+1)=key; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int count=0,n,x,*arr,i,temp=-1; 28 | scanf("%d",&n); 29 | arr=(int *)malloc(n*sizeof(int)); 30 | for(i=0;i 7 | 8 | int main(){ 9 | int y; 10 | scanf("%d",&y); 11 | if(y==1918){ 12 | printf("26.09.1918"); 13 | } 14 | else if(y<1918){ 15 | if(y%4==0){ 16 | printf("12.09.%d",y); 17 | } 18 | else{ 19 | printf("13.09.%d",y); 20 | } 21 | } 22 | else 23 | { 24 | if(y%400==0){ 25 | printf("12.09.%d",y); 26 | } 27 | else if(y%4==0 && y%100!=0){ 28 | printf("12.09.%d",y); 29 | } 30 | else{ 31 | printf("13.09.%d",y); 32 | } 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Implementation/Designer_PDF_Viewer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int i,h[26],w,c,temp=0; 12 | char str[11]; 13 | for(i=0;i<26;i++) 14 | { 15 | scanf("%d",&h[i]); 16 | } 17 | fflush(stdin); 18 | scanf("%s",str); 19 | w=strlen(str); 20 | for(i=0;i 7 | #include 8 | 9 | int divisibleSumPairs(int n, int k, int* ar) 10 | { 11 | int i,j,x,count=0; 12 | for(i=0;i=k)&&(x%k==0)) 18 | { 19 | count++; 20 | } 21 | } 22 | } 23 | return count; 24 | } 25 | 26 | int main() { 27 | int n; 28 | int k; 29 | scanf("%i %i", &n, &k); 30 | int *ar = malloc(sizeof(int) * n); 31 | for(int ar_i = 0; ar_i < n; ar_i++){ 32 | scanf("%i",&ar[ar_i]); 33 | } 34 | int result = divisibleSumPairs(n, k, ar); 35 | printf("%d\n", result); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Implementation/DrawingBook.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | 8 | int main() 9 | { 10 | int n,p; 11 | scanf("%d %d",&n,&p); 12 | if(p<=(n/2)) 13 | printf("%d",p/2); 14 | else 15 | if(n%2==0) 16 | printf("%d",(n-p+1)/2); 17 | else 18 | printf("%d",(n-p)/2); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Implementation/ElectronicsShop.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int i,j,x,m,n; 12 | long int s,*key,*usb,ans=-1; 13 | scanf("%ld %d %d",&s,&n,&m); 14 | key=(long int *)malloc(n*sizeof(long int)); 15 | usb=(long int *)malloc(m*sizeof(long int)); 16 | for(i=0;i0) 35 | { 36 | if(x>ans) 37 | { 38 | ans=x; 39 | } 40 | } 41 | } 42 | } 43 | printf("%ld",ans); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Implementation/Ema'sSupercomputer.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | static int twoPluses(String[] grid) { 11 | int n = grid.length; 12 | int m = grid[0].length(); 13 | int maxArea1 = 0; 14 | int maxArea = 0; 15 | String[] tempGrid = new String[n]; 16 | int DIS = 0; 17 | for(int i = 0; i < n; i++){ 18 | tempGrid[i] = grid[i]; 19 | } 20 | for(int i = 0; i < n; i++){ 21 | for(int j = 0; j < m; j++){ 22 | int area1 = 0; 23 | int r = i; 24 | int c = j; 25 | int dis = 0; 26 | int maxDis = Math.min(Math.min(r, n - r - 1),Math.min(c, m - c - 1)); 27 | while(dis <= maxDis && grid[r - dis].charAt(c) == 'G' && grid[r].charAt(c - dis) == 'G' && grid[r].charAt(c + dis) == 'G' && grid[r + dis].charAt(c) == 'G'){ 28 | area1 = 1 + (dis * 4); 29 | grid[r - dis] = grid[r - dis].substring(0, c) + "V" + grid[r - dis].substring(c + 1); 30 | grid[r] = grid[r].substring(0, c - dis) + "V" + grid[r].substring(c - dis + 1); 31 | grid[r] = grid[r].substring(0, c + dis) + "V" + grid[r].substring(c + dis + 1); 32 | grid[r + dis] = grid[r + dis].substring(0, c) + "V" + grid[r + dis].substring(c + 1); 33 | dis++; 34 | int maxArea2 = 0; 35 | for(int k = 0; k < n; k++){ 36 | for(int l = 0; l < m; l++){ 37 | int area2 = 0; 38 | int rr = k; 39 | int cc = l; 40 | int dis2 = 0; 41 | int maxDis2 = Math.min(Math.min(rr, n - rr - 1),Math.min(cc, m - cc - 1)); 42 | while(dis2 <= maxDis2 && grid[rr - dis2].charAt(cc) == 'G' && grid[rr].charAt(cc - dis2) == 'G' && grid[rr].charAt(cc + dis2) == 'G' && grid[rr + dis2].charAt(cc) == 'G'){ 43 | area2 = 1 + (dis2 * 4); 44 | dis2++; 45 | } 46 | if(area2 > maxArea2){ 47 | maxArea2 = area2; 48 | } 49 | } 50 | } 51 | if(area1 * maxArea2 > maxArea){ 52 | maxArea = area1 * maxArea2; 53 | } 54 | } 55 | for(int ii = 0; ii < n; ii++){ 56 | grid[ii] = tempGrid[ii]; 57 | } 58 | } 59 | } 60 | return maxArea; 61 | } 62 | 63 | private static final Scanner sc = new Scanner(System.in); 64 | 65 | public static void main(String[] args) { 66 | int n = sc.nextInt(); 67 | int m = sc.nextInt(); 68 | String grid[] = new String[n]; 69 | for(int i = 0; i < n; i++){ 70 | grid[i] = sc.next(); 71 | } 72 | int result = twoPluses(grid); 73 | System.out.println(result); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Implementation/Encryption.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | int r,x,i,j,len; 13 | double a; 14 | char str[100]; 15 | scanf("%s",str); 16 | len=strlen(str); 17 | a=sqrt((double)len); 18 | x=a; 19 | if(x==a) 20 | r=x; 21 | else 22 | r=x+1; 23 | if(x*r 7 | #include 8 | 9 | void insertionSort(int *p,int n) 10 | { 11 | int i,j,key; 12 | for(i=0;i=0&&(*(p+j))>key) 17 | { 18 | *(p+j+1)=*(p+j); 19 | j=j-1; 20 | } 21 | *(p+j+1)=key; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int i,temp,*arr,n,count=0,mcount=0; 28 | scanf("%d",&n); 29 | arr=(int *)malloc(n*sizeof(int)); 30 | for(i=0;imcount) 46 | { 47 | mcount=count; 48 | } 49 | count=1; 50 | } 51 | } 52 | if(count>mcount) 53 | { 54 | mcount=count; 55 | } 56 | printf("%d",n-mcount); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Implementation/ExtraLongFactorials.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | 8 | int main() 9 | { 10 | unsigned long arr[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 11 | int i,j,n,k,y; 12 | scanf("%d",&n); 13 | arr[0]=1; 14 | for(i=1;i<=n;i++) 15 | { 16 | k=y=0; 17 | do 18 | { 19 | arr[k]*=i; 20 | arr[k]+=y; 21 | y=arr[k]/10000000; 22 | arr[k]%=10000000; 23 | k++; 24 | }while(y>0 || arr[k]>0 || arr[k+1]>0 || arr[k+2]>0); 25 | } 26 | printf("%lu",arr[k-1]); 27 | for(j=k-2;j>=0;j--) 28 | { 29 | if(arr[j]==0) 30 | printf("0000000"); 31 | else 32 | { 33 | if(arr[j]<1000000) 34 | printf("0"); 35 | printf("%lu",arr[j]); 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Implementation/FairRations.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int n, *arr, count=0, i; 12 | scanf("%d",&n); 13 | arr=(int *)malloc(n*sizeof(int)); 14 | for(i=0;i 7 | #include 8 | 9 | int main() 10 | { 11 | int t,i,*ans,count; 12 | long int x,y,a; 13 | scanf("%d",&t); 14 | ans=(int *)malloc(t*sizeof(int)); 15 | for(i=0;i0) 21 | { 22 | a=y%10; 23 | if(a==0){} 24 | else if(x%a==0) 25 | { 26 | count++; 27 | } 28 | y/=10; 29 | } 30 | *(ans+i)=count; 31 | } 32 | for(i=0;i 7 | #include 8 | 9 | int main() 10 | { 11 | long int i,j,k,r,temp,tempr,n,m,a,ans=0; 12 | int *arr; 13 | scanf("%ld %ld",&n,&m); 14 | j=0-n; 15 | arr=(int *)malloc(n*sizeof(int)); 16 | for(i=0;i 7 | #include 8 | 9 | int absol(int a, int b) 10 | { 11 | if(a>=b) 12 | return a-b; 13 | else 14 | return b-a; 15 | } 16 | int main() 17 | { 18 | int arr[3][3],i,j,temp1=0,temp2=0,temp3=0,temp4=0,temp5=0,temp6=0,temp7=0,temp8=0; 19 | int brr[3][3]={{2,7,6},{9,5,1},{4,3,8}},crr[3][3]={{4,9,2},{3,5,7},{8,1,6}},drr[3][3]={{8,3,4},{1,5,9},{6,7,2}},err[3][3]={{6,1,8},{7,5,3},{2,9,4}},frr[3][3]={{4,3,8},{9,5,1},{2,7,6}},grr[3][3]={{8,1,6},{3,5,7},{4,9,2}},hrr[3][3]={{6,7,2},{1,5,9},{8,3,4}},irr[3][3]={{2,9,4},{7,5,3},{6,1,8}}; 20 | for(i=0;i<3;i++) 21 | { 22 | scanf("%d %d %d",&arr[i][0],&arr[i][1],&arr[i][2]); 23 | } 24 | for(i=0;i<3;i++) 25 | for(j=0;j<3;j++) 26 | { 27 | temp1+=absol(arr[i][j],brr[i][j]); 28 | temp2+=absol(arr[i][j],crr[i][j]); 29 | temp3+=absol(arr[i][j],drr[i][j]); 30 | temp4+=absol(arr[i][j],err[i][j]); 31 | temp5+=absol(arr[i][j],frr[i][j]); 32 | temp6+=absol(arr[i][j],grr[i][j]); 33 | temp7+=absol(arr[i][j],hrr[i][j]); 34 | temp8+=absol(arr[i][j],irr[i][j]); 35 | } 36 | if(temp1 7 | #include 8 | 9 | int main() { 10 | int i,n,a; 11 | scanf("%d", &n); 12 | int *grade = malloc(sizeof(int) * n); 13 | for(i = 0;i < n;i++) 14 | { 15 | scanf("%d",&grade[i]); 16 | if(grade[i]>37) 17 | { 18 | a=grade[i]/5; 19 | a++; 20 | a*=5; 21 | if((a-grade[i])<3) 22 | { 23 | grade[i]=a; 24 | } 25 | } 26 | } 27 | for(i = 0; i < n;i++) 28 | { 29 | printf("%d\n",grade[i]); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Implementation/HalloweenSale.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | 8 | int main() 9 | { 10 | int p,d,m,s,count=0; 11 | scanf("%d %d %d %d",&p,&d,&m,&s); 12 | while(s>=p) 13 | { 14 | count++; 15 | s-=p; 16 | if(p-d>m) 17 | p-=d; 18 | else 19 | p=m; 20 | } 21 | printf("%d",count); 22 | } 23 | -------------------------------------------------------------------------------- /Implementation/JumpingOnTheClouds.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int pos=0,n,*arr,count=0,i; 12 | scanf("%d",&n); 13 | arr=(int *)malloc((n+1)*sizeof(int)); 14 | for(i=0;i 7 | #include 8 | 9 | void swap(int *a, int *b) 10 | { 11 | int c; 12 | c=*a; 13 | *a=*b; 14 | *b=c; 15 | } 16 | 17 | int main() { 18 | int x1,v1,x2,v2,A,B; 19 | scanf("%i %i %i %i", &x1, &v1, &x2, &v2); 20 | if(x1==x2) 21 | { 22 | printf("YES"); 23 | return 0; 24 | } 25 | if(x1>x2) 26 | { 27 | swap(&x1,&x2); 28 | swap(&v1,&v2); 29 | } 30 | A=x1; 31 | B=x2; 32 | if(v1<=v2) 33 | { 34 | printf("NO"); 35 | return 0; 36 | } 37 | else 38 | { 39 | while(A 7 | 8 | int main() 9 | { 10 | int d1,d2,m1,m2,y1,y2; 11 | scanf("%d %d %d",&d1,&m1,&y1); 12 | scanf("%d %d %d",&d2,&m2,&y2); 13 | if(y1y2) 19 | { 20 | printf("%d",10000); 21 | return 0; 22 | } 23 | else if(y1==y2) 24 | { 25 | if(m1m2) 31 | { 32 | printf("%d",500*(m1-m2)); 33 | return 0; 34 | } 35 | else if(m1==m2) 36 | { 37 | if(d1<=d2) 38 | { 39 | printf("%d",0); 40 | return 0; 41 | } 42 | else if(d1>d2) 43 | { 44 | printf("%d",15*(d1-d2)); 45 | return 0; 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Implementation/Lisa'sWorkbook.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int n,k,*arr,i,j,pg=0,sp=0,prob; 12 | scanf("%d %d",&n,&k); 13 | arr=(int *)malloc(sizeof(int)*n); 14 | for(i=0;i 7 | #include 8 | 9 | void insertionSort(int *p,int n) 10 | { 11 | int i,j,key; 12 | for(i=0;i=0&&(*(p+j))>key) 17 | { 18 | *(p+j+1)=*(p+j); 19 | j=j-1; 20 | } 21 | *(p+j+1)=key; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int t,*n,**ans,i,j,k,*last,a,b,*x; 28 | scanf("%d",&t); 29 | n=(int *)malloc(t*sizeof(int)); 30 | ans=(int * *)malloc(t*sizeof(int *)); 31 | for(i=0;i=0;j++,k--) 44 | { 45 | *(last+j)=(a*k)+(b*j); 46 | } 47 | } 48 | insertionSort(last,*(n+i)); 49 | *(ans+i)=last; 50 | } 51 | for(i=0;i b; j--){ 63 | brr[in] = arr[c][j]; 64 | in++; 65 | } 66 | for(int j = c; j > a; j--){ 67 | brr[in] = arr[j][b]; 68 | in++; 69 | } 70 | brr = matrixRotation(brr, r); 71 | in = 0; 72 | for(int j = b; j < d; j++){ 73 | arr[a][j] = brr[in]; 74 | in++; 75 | } 76 | for(int j = a; j < c; j++){ 77 | arr[j][d] = brr[in]; 78 | in++; 79 | } 80 | for(int j = d; j > b; j--){ 81 | arr[c][j] = brr[in]; 82 | in++; 83 | } 84 | for(int j = c; j > a; j--){ 85 | arr[j][b] = brr[in]; 86 | in++; 87 | } 88 | a++; 89 | b++; 90 | c--; 91 | d--; 92 | } 93 | for(int i = 0; i < m; i++){ 94 | for(int j = 0; j < n; j++){ 95 | System.out.print(arr[i][j] + " "); 96 | } 97 | System.out.println(); 98 | } 99 | 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Implementation/MigratoryBirds.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | int migratoryBirds(int n, int* ar) { 9 | int a[5]={0,0,0,0,0},i,count,ans; 10 | for(i=0;i=0;i--) 36 | { 37 | if(count<=a[i]) 38 | { 39 | count=a[i]; 40 | ans=i+1; 41 | } 42 | } 43 | return ans; 44 | } 45 | 46 | int main() { 47 | int n; 48 | scanf("%i", &n); 49 | int *ar = malloc(sizeof(int) * n); 50 | for(int ar_i = 0; ar_i < n; ar_i++){ 51 | scanf("%i",&ar[ar_i]); 52 | } 53 | int result = migratoryBirds(n, ar); 54 | printf("%d\n", result); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Implementation/MinimumDistances.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | 10 | int MinValue(int * arr, int n) 11 | { 12 | int i, temp=*(arr); 13 | for(i=1;i*(arr+i)) 16 | { 17 | temp=*(arr+i); 18 | } 19 | } 20 | return temp; 21 | } 22 | 23 | int main() 24 | { 25 | int i,j,n,*arr,*dis,ans; 26 | scanf("%d",&n); 27 | arr = (int*)malloc(sizeof(int) * n); 28 | dis = (int*)malloc(sizeof(int) * n); 29 | for(i = 0; i < n; i++) 30 | { 31 | scanf("%d",arr+i); 32 | } 33 | for(i = 0; i < n; i++) 34 | { 35 | for(j=i+1;j 7 | #include 8 | 9 | int main() 10 | { 11 | long int i,m,check,p,q,sqr,sqr2,temp,a,b; 12 | int j,count=0,digit; 13 | scanf("%ld %ld",&p,&q); 14 | for(i=p;i<=q;i++) 15 | { 16 | temp=i; 17 | for(digit=0;temp>0;digit++) 18 | { 19 | temp/=10; 20 | } 21 | sqr=pow(i,2); 22 | sqr2=sqr; 23 | for(j=0,m=1,a=0;j 7 | #include 8 | 9 | void insertionSort(long int *p,long int n) 10 | { 11 | long int i,j,key; 12 | for(i=0;i=0&&(*(p+j))>key) 17 | { 18 | *(p+j+1)=*(p+j); 19 | j=j-1; 20 | } 21 | *(p+j+1)=key; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | long int q,n,*arr,*c,*r,i,j,sum; 28 | scanf("%ld",&q); 29 | while(q--) 30 | { 31 | scanf("%ld",&n); 32 | arr=(long int *)malloc(n*n*sizeof(long int)); 33 | c=(long int *)malloc(n*sizeof(long int)); 34 | r=(long int *)malloc(n*sizeof(long int)); 35 | for(i=0;i 7 | #include 8 | 9 | void insertionSort(int *p,int n) 10 | { 11 | int i,j,key; 12 | for(i=0;i=0&&(*(p+j))>key) 17 | { 18 | *(p+j+1)=*(p+j); 19 | j=j-1; 20 | } 21 | *(p+j+1)=key; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int n,*arr,i,j,temp,count,mcount=0; 28 | scanf("%d",&n); 29 | arr=(int *)malloc(n*sizeof(int)); 30 | for(i=0;imcount) 52 | { 53 | mcount=count; 54 | } 55 | } 56 | printf("%d",mcount); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Implementation/RepeatedString.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main() 12 | { 13 | double n,x,count=0; 14 | int i,l,y; 15 | char *arr; 16 | arr=(char *)malloc(100*sizeof(char)); 17 | scanf("%s",arr); 18 | l=strlen(arr); 19 | scanf("%lf",&n); 20 | x=n/l; 21 | x=trunc(x); 22 | y=n-(l*x); 23 | for(i=0;i 7 | #include 8 | 9 | int main() 10 | { 11 | long int t,n,m,s,*ans,j,a,b; 12 | scanf("%ld",&t); 13 | ans=(long int *)malloc(t*sizeof(long int)); 14 | for(j=0;j 7 | #include 8 | 9 | int Search(int * arr,int n,int a) 10 | { 11 | int i; 12 | for(i=0;i 7 | #include 8 | 9 | int main() 10 | { 11 | int t,*arr,ans; 12 | long int n,i,j,k,l; 13 | scanf("%ld %d",&n,&t); 14 | arr=(int *)malloc(n*sizeof(int)); 15 | for(k=0;k 7 | #include 8 | 9 | int main() 10 | { 11 | long int t,a,b,X,Y,Z; 12 | double x,y; 13 | scanf("%ld",&t); 14 | while(t--) 15 | { 16 | scanf("%ld %ld",&a,&b); 17 | x=sqrt(a); 18 | Z=x; 19 | if(x==Z) 20 | X=Z; 21 | else 22 | X=x+1; 23 | y=sqrt(b); 24 | Y=y; 25 | printf("%ld\n",Y-X+1); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Implementation/SockMerchant.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | void insertionSort(int *p,int n) 10 | { 11 | int i,j,key; 12 | for(i=0;i=0&&(*(p+j))>key) 17 | { 18 | *(p+j+1)=*(p+j); 19 | j=j-1; 20 | } 21 | *(p+j+1)=key; 22 | } 23 | } 24 | 25 | int sockMerchant(int n,int* ar) 26 | { 27 | int i,count=0; 28 | insertionSort(ar,n); 29 | for(i=0;i 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | long int t, i, j, x; 13 | scanf("%ld",&t); 14 | i=1; 15 | for(j=0;;j++) 16 | { 17 | if(t 7 | #include 8 | 9 | int main() 10 | { 11 | long double B,W,X,Y,Z,t,*ans; 12 | int i; 13 | scanf("%Lf",&t); 14 | ans=(long double *)malloc(t*sizeof(long double)); 15 | for(i=0;i 0){ 8 | int R = sc.nextInt(); 9 | int C = sc.nextInt(); 10 | String arr[] = new String[R]; 11 | for(int i = 0; i < R; i++){ 12 | arr[i] = sc.next(); 13 | } 14 | int r = sc.nextInt(); 15 | int c = sc.nextInt(); 16 | String brr[] = new String[r]; 17 | for (int i = 0; i < r; i++) { 18 | brr[i] = sc.next(); 19 | } 20 | int temp = 0, y = 0, i; 21 | for(i = 0; i < R; i++){ 22 | int x = arr[i].indexOf(brr[0],y); 23 | if(x != -1){ 24 | int j, k; 25 | temp = x; 26 | for(j = 1, k = i + 1; j < r && j < R; j++, k++){ 27 | if(!brr[j].equals(arr[k].substring(x,x+c))){ 28 | break; 29 | } 30 | } 31 | if(j == r){ 32 | System.out.println("YES"); 33 | break; 34 | } 35 | else{ 36 | i--; 37 | y = x + 1; 38 | } 39 | } 40 | if(y != 0 && x == -1){ 41 | y = 0; 42 | } 43 | } 44 | if(i == R){ 45 | System.out.println("NO"); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Implementation/TheHurdleRace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int i,n,k,*h,temp,ans; 12 | scanf("%d %d",&n,&k); 13 | h=(int *)malloc(n*sizeof(int)); 14 | temp=k; 15 | for(i=0;itemp) 19 | { 20 | temp=*(h+i); 21 | } 22 | } 23 | ans=temp-k; 24 | printf("%d",ans); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Implementation/TheTimeInWords.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | char* hNumToWord(int num) 10 | { 11 | char *word; 12 | word=(char *)malloc(7*sizeof(char)); 13 | switch(num) 14 | { 15 | case 1: 16 | word="one"; 17 | break; 18 | case 2: 19 | word="two"; 20 | break; 21 | case 3: 22 | word="three"; 23 | break; 24 | case 4: 25 | word="four"; 26 | break; 27 | case 5: 28 | word="five"; 29 | break; 30 | case 6: 31 | word="six"; 32 | break; 33 | case 7: 34 | word="seven"; 35 | break; 36 | case 8: 37 | word="eight"; 38 | break; 39 | case 9: 40 | word="nine"; 41 | break; 42 | case 10: 43 | word="ten"; 44 | break; 45 | case 11: 46 | word="eleven"; 47 | break; 48 | case 12: 49 | word="twelve"; 50 | break; 51 | } 52 | return word; 53 | } 54 | char* mNumToWord(int num) 55 | { 56 | char *word; 57 | word=(char *)malloc(21*sizeof(char)); 58 | switch(num) 59 | { 60 | case 0: 61 | word="zero"; 62 | break; 63 | case 1: 64 | word="one minute"; 65 | break; 66 | case 2: 67 | word="two minutes"; 68 | break; 69 | case 3: 70 | word="three minutes"; 71 | break; 72 | case 4: 73 | word="four minutes"; 74 | break; 75 | case 5: 76 | word="five minutes"; 77 | break; 78 | case 6: 79 | word="six minutes"; 80 | break; 81 | case 7: 82 | word="seven minutes"; 83 | break; 84 | case 8: 85 | word="eight minutes"; 86 | break; 87 | case 9: 88 | word="nine minutes"; 89 | break; 90 | case 10: 91 | word="ten minutes"; 92 | break; 93 | case 11: 94 | word="eleven minutes"; 95 | break; 96 | case 12: 97 | word="twelve minutes"; 98 | break; 99 | case 13: 100 | word="thirteen minutes"; 101 | break; 102 | case 14: 103 | word="fourteen minutes"; 104 | break; 105 | case 15: 106 | word="quarter"; 107 | break; 108 | case 16: 109 | word="sixteen minutes"; 110 | break; 111 | case 17: 112 | word="seventeen minutes"; 113 | break; 114 | case 18: 115 | word="eighteen minutes"; 116 | break; 117 | case 19: 118 | word="nineteen minutes"; 119 | break; 120 | case 20: 121 | word="twenty minutes"; 122 | break; 123 | case 21: 124 | word="twenty one minutes"; 125 | break; 126 | case 22: 127 | word="twenty two minutes"; 128 | break; 129 | case 23: 130 | word="twenty three minutes"; 131 | break; 132 | case 24: 133 | word="twenty four minutes"; 134 | break; 135 | case 25: 136 | word="twenty five minutes"; 137 | break; 138 | case 26: 139 | word="twenty six minutes"; 140 | break; 141 | case 27: 142 | word="twenty seven minutes"; 143 | break; 144 | case 28: 145 | word="twenty eight minutes"; 146 | break; 147 | case 29: 148 | word="twenty nine minutes"; 149 | break; 150 | case 30: 151 | word="half"; 152 | break; 153 | } 154 | return word; 155 | } 156 | 157 | int main() 158 | { 159 | int i,h,m; 160 | char *hour,*min; 161 | hour=(char *)malloc(7*sizeof(char)); 162 | min=(char *)malloc(21*sizeof(char)); 163 | scanf("%d %d",&h,&m); 164 | if(m<=30) 165 | { 166 | hour=hNumToWord(h); 167 | min=mNumToWord(m); 168 | } 169 | else 170 | { 171 | min=mNumToWord(60-m); 172 | if(h==12) 173 | { 174 | hour="one"; 175 | } 176 | else 177 | { 178 | hour=hNumToWord(h+1); 179 | } 180 | } 181 | if(m==0) 182 | { 183 | printf("%s o' clock",hour); 184 | } 185 | else if(m>0 && m<=30) 186 | { 187 | printf("%s past %s",min,hour); 188 | } 189 | else if(m>30 && m<60) 190 | { 191 | printf("%s to %s",min,hour); 192 | } 193 | return 0; 194 | } 195 | -------------------------------------------------------------------------------- /Implementation/UtopianTree.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int t,n,ans,i,j; 12 | scanf("%d",&t); 13 | for(i=0;i 7 | 8 | int main() 9 | { 10 | int i,n,count,a; 11 | scanf("%d",&n); 12 | a=2; 13 | count=2; 14 | for(i=2;i<=n;i++) 15 | { 16 | a*=3; 17 | a/=2; 18 | count+=a; 19 | } 20 | printf("%d",count); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Anmol53 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Search/ConnectedCellsInAGrid.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | import java.util.*; 7 | 8 | public class Solution { 9 | static void regionX(int[][] matrix, int n, int m, int i, int j){ 10 | if(matrix[i][j] == 1){ 11 | matrix[i][j] = 2; 12 | if(i < 0 || j < 0){} 13 | else if( i == 0 && j == 0){ 14 | regionX(matrix, n, m, i + 1, j); 15 | regionX(matrix, n, m, i + 1, j + 1); 16 | regionX(matrix, n, m, i, j + 1); 17 | } 18 | else if( i == 0 && j == m - 1){ 19 | regionX(matrix, n, m, i, j - 1); 20 | regionX(matrix, n, m, i + 1, j - 1); 21 | regionX(matrix, n, m, i + 1, j); 22 | } 23 | else if( i == n - 1 && j == 0){ 24 | regionX(matrix, n, m, i - 1, j); 25 | regionX(matrix, n, m, i - 1, j + 1); 26 | regionX(matrix, n, m, i, j + 1); 27 | } 28 | else if( i == n - 1 && j == m - 1){ 29 | regionX(matrix, n, m, i - 1, j - 1); 30 | regionX(matrix, n, m, i - 1, j); 31 | regionX(matrix, n, m, i, j - 1); 32 | } 33 | else if( i == (n - 1) ){ 34 | regionX(matrix, n, m, i, j - 1); 35 | regionX(matrix, n, m, i, j + 1); 36 | regionX(matrix, n, m, i - 1, j + 1); 37 | regionX(matrix, n, m, i - 1, j - 1); 38 | regionX(matrix, n, m, i - 1, j); 39 | } 40 | else if(j == (m - 1)){ 41 | regionX(matrix, n, m, i - 1, j - 1); 42 | regionX(matrix, n, m, i, j - 1); 43 | regionX(matrix, n, m, i + 1, j - 1); 44 | regionX(matrix, n, m, i - 1, j); 45 | regionX(matrix, n, m, i + 1, j); 46 | } 47 | else if(i == 0){ 48 | regionX(matrix, n, m, i, j - 1); 49 | regionX(matrix, n, m, i, j + 1); 50 | regionX(matrix, n, m, i + 1, j - 1); 51 | regionX(matrix, n, m, i + 1, j); 52 | regionX(matrix, n, m, i + 1, j + 1); 53 | } 54 | else if(j == 0){ 55 | regionX(matrix, n, m, i - 1, j); 56 | regionX(matrix, n, m, i + 1, j); 57 | regionX(matrix, n, m, i - 1, j + 1); 58 | regionX(matrix, n, m, i, j + 1); 59 | regionX(matrix, n, m, i + 1, j + 1); 60 | } 61 | else{ 62 | regionX(matrix, n, m, i - 1, j - 1); 63 | regionX(matrix, n, m, i - 1, j); 64 | regionX(matrix, n, m, i - 1, j + 1); 65 | regionX(matrix, n, m, i, j - 1); 66 | regionX(matrix, n, m, i, j + 1); 67 | regionX(matrix, n, m, i + 1, j - 1); 68 | regionX(matrix, n, m, i + 1, j); 69 | regionX(matrix, n, m, i + 1, j + 1); 70 | } 71 | } 72 | } 73 | static int Scan(int[][] matrix, int n, int m){ 74 | int i, j, k, l, count = 0, count1 = 0; 75 | for(i = 0; i < n-1; i++){ 76 | for(j = 0; j < m-1; j++){ 77 | if(matrix[i][j] == 1) 78 | { 79 | regionX(matrix,n,m,i,j); 80 | count = 0; 81 | for(k = 0; k < n; k++){ 82 | for(l = 0; l < m; l++){ 83 | if(matrix[k][l] == 2){ 84 | count++; 85 | matrix[k][l] = 3; 86 | } 87 | } 88 | } 89 | if(count > count1){ 90 | count1 = count; 91 | } 92 | } 93 | } 94 | } 95 | return count1; 96 | } 97 | public static void main(String[] args) { 98 | Scanner sc = new Scanner(System.in); 99 | int n = sc.nextInt(); 100 | sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 101 | int m = sc.nextInt(); 102 | sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 103 | int[][] matrix = new int[n][m]; 104 | for (int i = 0; i < n; i++) { 105 | String[] matrixRowItems = sc.nextLine().split(" "); 106 | sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 107 | for (int j = 0; j < m; j++) { 108 | int matrixItem = Integer.parseInt(matrixRowItems[j]); 109 | matrix[i][j] = matrixItem; 110 | } 111 | } 112 | System.out.println(Scan(matrix,n,m)); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /Search/HackerlandRadioTransmitters.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | int Loc(int * arr,int n,int a) 9 | { 10 | int i; 11 | for(i=0;ia) 14 | { 15 | return *(arr+i-1); 16 | } 17 | } 18 | return *(arr+n-1); 19 | } 20 | int LocX(int * arr,int n,int a) 21 | { 22 | int i; 23 | for(i=0;ia) 26 | { 27 | return i; 28 | } 29 | } 30 | return n+1; 31 | } 32 | void insertionSort(int *p,int n) 33 | { 34 | int i,j,key; 35 | for(i=0;i=0&&(*(p+j))>key) 40 | { 41 | *(p+j+1)=*(p+j); 42 | j=j-1; 43 | } 44 | *(p+j+1)=key; 45 | } 46 | } 47 | 48 | int main() 49 | { 50 | int n, k, *arr, count=0, i, a, b, x; 51 | scanf("%d %d",&n,&k); 52 | arr=(int *)malloc(n*sizeof(int)); 53 | for(i=0;i 7 | #include 8 | 9 | int main(){ 10 | int t,tc,i,j,pair,m,n,*arr,*ans1,*ans2; 11 | scanf("%d",&t); 12 | ans1=(int *)malloc(t*sizeof(int)); 13 | ans2=(int *)malloc(t*sizeof(int)); 14 | for(tc=0;tc 7 | #include 8 | 9 | void merge(int arr[], int l, int m, int r){ 10 | int i, j, k; 11 | int n1 = m - l + 1; 12 | int n2 = r - m; 13 | int L[n1], R[n2]; 14 | for (i = 0; i < n1; i++){ 15 | L[i] = arr[l + i]; 16 | } 17 | for (j = 0; j < n2; j++){ 18 | R[j] = arr[m + 1+ j]; 19 | } 20 | i = 0; 21 | j = 0; 22 | k = l; 23 | while (i < n1 && j < n2){ 24 | if (L[i] <= R[j]){ 25 | arr[k] = L[i]; 26 | i++; 27 | } 28 | else{ 29 | arr[k] = R[j]; 30 | j++; 31 | } 32 | k++; 33 | } 34 | while (i < n1){ 35 | arr[k] = L[i]; 36 | i++; 37 | k++; 38 | } 39 | while (j < n2){ 40 | arr[k] = R[j]; 41 | j++; 42 | k++; 43 | } 44 | } 45 | void mergeSort(int arr[], int l, int r){ 46 | if (l < r){ 47 | int m = l+(r-l)/2; 48 | mergeSort(arr, l, m); 49 | mergeSort(arr, m+1, r); 50 | merge(arr, l, m, r); 51 | } 52 | } 53 | 54 | int main(){ 55 | int i,j,k,l=0,n,m,temp,*A,*B,*C; 56 | scanf("%d",&n); 57 | A=(int *)malloc(n*sizeof(int)); 58 | for(i=0;i k){ 27 | break; 28 | } 29 | } 30 | } 31 | return count; 32 | } 33 | 34 | private static final Scanner scanner = new Scanner(System.in); 35 | 36 | public static void main(String[] args) throws IOException { 37 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 38 | 39 | String[] nk = scanner.nextLine().split(" "); 40 | 41 | int n = Integer.parseInt(nk[0]); 42 | 43 | int k = Integer.parseInt(nk[1]); 44 | 45 | int[] arr = new int[n]; 46 | 47 | String[] arrItems = scanner.nextLine().split(" "); 48 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 49 | 50 | for (int i = 0; i < n; i++) { 51 | int arrItem = Integer.parseInt(arrItems[i]); 52 | arr[i] = arrItem; 53 | } 54 | 55 | int result = pairs(k, arr); 56 | 57 | bufferedWriter.write(String.valueOf(result)); 58 | bufferedWriter.newLine(); 59 | 60 | bufferedWriter.close(); 61 | 62 | scanner.close(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Search/README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | # Hackerrank Practice Questions (Problem-Solving / Search) 4 | 5 | ![GitHub last commit](https://img.shields.io/github/last-commit/Anmol53/Hackerrank-Problem-Solving) 6 | ![GitHub commit activity](https://img.shields.io/github/commit-activity/y/Anmol53/Hackerrank-Problem-Solving?color=%23ff9900) 7 | ![License Badge](https://img.shields.io/github/license/Anmol53/Hackerrank-Problem-Solving) 8 | ![Forks Badge](https://img.shields.io/github/forks/Anmol53/Hackerrank-Problem-Solving) 9 | ![Stars Badge](https://img.shields.io/github/stars/Anmol53/Hackerrank-Problem-Solving) 10 | ![Languages](https://img.shields.io/badge/languages-C%2C%20Java%2C%20Javascript-yellow.svg) 11 | ![GitHub repo size in bytes](https://img.shields.io/github/repo-size/Anmol53/Hackerrank-Problem-Solving) 12 | [![GitHub followers](https://img.shields.io/github/followers/Anmol53.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/Anmol53?tab=followers) 13 | 14 | ## Why this repository? 15 | 16 | This is mostly for my personal learning and future references, and if anyone needs help with a certain problem. 17 | 18 | ***"Try the problem yourself first, then only proceed to the solution. BE FAITHFUL TO YOUR WORK."*** 19 | 20 | If found helpful please press a ⭐. 21 | 22 | ## INDEX 23 | #### Algorithms 24 | - [Warmup](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Warmup) 25 | - [Implementation](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Implementation) 26 | - [Strings](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Strings) 27 | - [Sorting](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Sorting) 28 | - [Search](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Search) 👈 ***You are here.*** 29 | - [Greedy](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Greedy) 30 | 31 | ## Content 32 | 33 | | Subdomain | Problem Statement | Solution c | Solution java | Solution js | Difficulty | Score | 34 | | :-------: | :---------------: | :--------: | :-----------: | :---------: | :--------: | :---: | 35 | |Search|[Connected Cells In A Grid](https://www.hackerrank.com/challenges/connected-cell-in-a-grid)|[]()|[Solution.java](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Search/ConnectedCellsInAGrid.java)|[]()|Medium|50| 36 | |Search|[Hackerland Radio Transmitters](https://www.hackerrank.com/challenges/hackerland-radio-transmitters)|[Solution.c](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Search/HackerlandRadioTransmitters.c)|[]()|[]()|Medium|25| 37 | |Search|[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor)|[Solution.c](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Search/Ice%20Cream%20Parlor.c)|[]()|[]()|Easy|30| 38 | |Search|[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers)|[Solution.c](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Search/Missing%20Numbers.c)|[]()|[]()|Easy|45| 39 | |Search|[Pairs](https://www.hackerrank.com/challenges/pairs)|[]()|[Solution.java](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Search/Pairs.java)|[]()|Medium|50| 40 | |Search|[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array/problem)|[Solution.c](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Search/SherlockAndArray.c)|[]()|[]()|Easy|40| 41 | 42 | ## Author 43 | ***Anmol Agrawal*** 44 | 45 | [Mail ID](mailto:anmol.ag53@gmail.com?subject=[GitHub]) | [LinkedIn](https://www.linkedin.com/in/anmol-53/) | [GitHub](https://github.com/Anmol53/) | [LeetCode](https://leetcode.com/anmol_53/) | [CodeChef](https://www.codechef.com/users/uniquecoder_) | [HackerRank](https://www.hackerrank.com/anmol_53) 46 | 47 | **"Any suggestions would be appreciated"** 48 | -------------------------------------------------------------------------------- /Search/SherlockAndArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int ArraySum(int *arr,int n) 5 | { 6 | int i,sum=0; 7 | for(i=0;i { 4 | Integer length; 5 | String str; 6 | 7 | public Integer getLen() { 8 | return length; 9 | } 10 | public String getStr() { 11 | return str; 12 | } 13 | 14 | @Override 15 | public int compareTo(Solution o) { 16 | int a = this.getLen().compareTo(o.getLen()); 17 | if(a == 0){ 18 | return this.getStr().compareTo(o.getStr()); 19 | } 20 | else{ 21 | return a; 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | int n = sc.nextInt(); 28 | ArrayList solList = new ArrayList(); 29 | for(int i = 0; i < n; i++){ 30 | String a = sc.next(); 31 | Integer aLen = a.length(); 32 | Solution sl = new Solution(); 33 | sl.length = aLen; 34 | sl.str = a; 35 | solList.add(sl); 36 | } 37 | Collections.sort(solList); 38 | for(int i = 0; i < n; i++){ 39 | System.out.println(solList.get(i).str); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Sorting/FindTheMedian.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(int *p,int n) 5 | { 6 | int i,j,key; 7 | for(i=0;i=0&&(*(p+j))>key) 12 | { 13 | *(p+j+1)=*(p+j); 14 | j=j-1; 15 | } 16 | *(p+j+1)=key; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | int n,*p,i,a; 23 | scanf("%d",&n); 24 | p=(int *)malloc(n*sizeof(int)); 25 | for(i=0;i

2 | 3 | # Hackerrank Practice Questions (Problem-Solving / Sorting) 4 | 5 | ![GitHub last commit](https://img.shields.io/github/last-commit/Anmol53/Hackerrank-Problem-Solving) 6 | ![GitHub commit activity](https://img.shields.io/github/commit-activity/y/Anmol53/Hackerrank-Problem-Solving?color=%23ff9900) 7 | ![License Badge](https://img.shields.io/github/license/Anmol53/Hackerrank-Problem-Solving) 8 | ![Forks Badge](https://img.shields.io/github/forks/Anmol53/Hackerrank-Problem-Solving) 9 | ![Stars Badge](https://img.shields.io/github/stars/Anmol53/Hackerrank-Problem-Solving) 10 | ![Languages](https://img.shields.io/badge/languages-C%2C%20Java%2C%20Javascript-yellow.svg) 11 | ![GitHub repo size in bytes](https://img.shields.io/github/repo-size/Anmol53/Hackerrank-Problem-Solving) 12 | [![GitHub followers](https://img.shields.io/github/followers/Anmol53.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/Anmol53?tab=followers) 13 | 14 | ## Why this repository? 15 | 16 | This is mostly for my personal learning and future references, and if anyone needs help with a certain problem. 17 | 18 | ***"Try the problem yourself first, then only proceed to the solution. BE FAITHFUL TO YOUR WORK."*** 19 | 20 | If found helpful please press a ⭐. 21 | 22 | ## INDEX 23 | #### Algorithms 24 | - [Warmup](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Warmup) 25 | - [Implementation](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Implementation) 26 | - [Strings](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Strings) 27 | - [Sorting](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Sorting) 👈 ***You are here.*** 28 | - [Search](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Search) 29 | - [Greedy](https://github.com/Anmol53/Hackerrank-Problem-Solving/tree/master/Greedy) 30 | 31 | ## Content 32 | 33 | | Subdomain | Problem Statement | Solution c | Solution java | Solution js | Difficulty | Score | 34 | | :-------: | :---------------: | :--------: | :-----------: | :---------: | :--------: | :---: | 35 | |Sorting|[Big Sorting](https://www.hackerrank.com/challenges/big-sorting)|[]()|[Solution.java](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Sorting/BigSorting.java)|[]()|Easy|20| 36 | |Sorting|[Find the Median](https://www.hackerrank.com/challenges/find-the-median)|[Solution.c](https://github.com/Anmol53/Hackerrank-Problem-Solving/blob/master/Sorting/FindTheMedian.c)|[]()|[]()|Easy|35| 37 | 38 | 39 | ## Author 40 | ***Anmol Agrawal*** 41 | 42 | [Mail ID](mailto:anmol.ag53@gmail.com?subject=[GitHub]) | [LinkedIn](https://www.linkedin.com/in/anmol-53/) | [GitHub](https://github.com/Anmol53/) | [LeetCode](https://leetcode.com/anmol_53/) | [CodeChef](https://www.codechef.com/users/uniquecoder_) | [HackerRank](https://www.hackerrank.com/anmol_53) 43 | 44 | **"Any suggestions would be appreciated"** 45 | -------------------------------------------------------------------------------- /Strings/AlternatingCharacters.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int t,i,len,count; 12 | char str[100001]; 13 | scanf("%d",&t); 14 | while(t--) 15 | { 16 | scanf("%s",str); 17 | len=strlen(str); 18 | count=0; 19 | for(i=1;i 7 | #include 8 | #include 9 | 10 | void merge(char arr[], int l, int m, int r) 11 | { 12 | int i, j, k; 13 | int n1 = m - l + 1; 14 | int n2 = r - m; 15 | char L[n1], R[n2]; 16 | for (i = 0; i < n1; i++) 17 | L[i] = arr[l + i]; 18 | for (j = 0; j < n2; j++) 19 | R[j] = arr[m + 1+ j]; 20 | i = 0; 21 | j = 0; 22 | k = l; 23 | while (i < n1 && j < n2) 24 | { 25 | if (L[i] <= R[j]) 26 | { 27 | arr[k] = L[i]; 28 | i++; 29 | } 30 | else 31 | { 32 | arr[k] = R[j]; 33 | j++; 34 | } 35 | k++; 36 | } 37 | while (i < n1) 38 | { 39 | arr[k] = L[i]; 40 | i++; 41 | k++; 42 | } 43 | while (j < n2) 44 | { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | void mergeSort(char arr[], int l, int r) 51 | { 52 | if (l < r) 53 | { 54 | int m = l+(r-l)/2; 55 | mergeSort(arr, l, m); 56 | mergeSort(arr, m+1, r); 57 | merge(arr, l, m, r); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int q,i,j,k,slen,tlen,count; 64 | char s[10001]; 65 | scanf("%d",&q); 66 | while(q--) 67 | { 68 | count=0; 69 | scanf("%s",s); 70 | slen=strlen(s); 71 | k=tlen=slen/2; 72 | if(slen%2!=0) 73 | printf("-1\n"); 74 | else 75 | { 76 | mergeSort(s,0,tlen-1); 77 | mergeSort(s,tlen,slen-1); 78 | for(i=0;i 7 | #include 8 | 9 | int main() 10 | { 11 | int n,i,count=0; 12 | char *str; 13 | scanf("%d",&n); 14 | str=(char *)malloc((n+1)*sizeof(char)); 15 | scanf("%s",str); 16 | for(i=2;i 7 | #include 8 | 9 | int main() 10 | { 11 | int n,k,i,j; 12 | char *str; 13 | scanf("%d",&n); 14 | str=(char *)malloc(n*sizeof(char)); 15 | scanf("%s",str); 16 | scanf("%d",&k); 17 | for(i=0;i=97 && *(str+i)<=122) 20 | { 21 | j=*(str+i)+k; 22 | for(;j>122;) 23 | j=j-26; 24 | printf("%c",j); 25 | } 26 | else if(*(str+i)>=65 && *(str+i)<=90) 27 | { 28 | j=*(str+i)+k; 29 | for(;j>90;) 30 | j=j-26; 31 | printf("%c",j); 32 | } 33 | else 34 | { 35 | printf("%c",*(str+i)); 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Strings/CamelCase.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | char str[100001]; 12 | int i,count=0,len; 13 | scanf("%s",str); 14 | len=strlen(str); 15 | if(len) 16 | { 17 | count++; 18 | for(i=0;i=65 && str[i]<=90) 20 | count++; 21 | } 22 | printf("%d",count); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Strings/CommonChild.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | import java.io.*; 7 | import java.math.*; 8 | import java.security.*; 9 | import java.text.*; 10 | import java.util.*; 11 | import java.util.concurrent.*; 12 | import java.util.regex.*; 13 | 14 | public class Solution { 15 | 16 | public static void main(String[] args){ 17 | Scanner sc = new Scanner(System.in); 18 | String s1 = sc.next(); 19 | String s2 = sc.next(); 20 | int n = s1.length(); 21 | n++; 22 | int arr[][] = new int[n][n]; 23 | Arrays.fill(arr[0], 0); 24 | for(int i = 0; i < n; i++){ 25 | arr[i][0] = 0; 26 | } 27 | for(int i = 1; i < n; i++){ 28 | for(int j = 1; j < n; j++){ 29 | if(s1.charAt(i-1) != s2.charAt(j-1)){ 30 | arr[i][j] = Math.max(arr[i][j-1], arr[i-1][j]); 31 | } 32 | else{ 33 | arr[i][j] = arr[i-1][j-1] + 1; 34 | } 35 | } 36 | } 37 | System.out.println(arr[n - 1][n - 1]); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Strings/FunnyString.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int Absolute(int a) 15 | { 16 | if(a<0) 17 | { 18 | return a*(-1); 19 | } 20 | else 21 | return a; 22 | } 23 | 24 | int funnyString(char* s){ 25 | int l,i,j,*a; 26 | l=strlen(s); 27 | a=(int *)malloc(l*sizeof(int)); 28 | for(i=1;i 7 | #include 8 | #include 9 | 10 | void merge(char arr[], int l, int m, int r) 11 | { 12 | int i, j, k; 13 | int n1 = m - l + 1; 14 | int n2 = r - m; 15 | char L[n1], R[n2]; 16 | for (i = 0; i < n1; i++) 17 | L[i] = arr[l + i]; 18 | for (j = 0; j < n2; j++) 19 | R[j] = arr[m + 1+ j]; 20 | i = 0; 21 | j = 0; 22 | k = l; 23 | while (i < n1 && j < n2) 24 | { 25 | if (L[i] <= R[j]) 26 | { 27 | arr[k] = L[i]; 28 | i++; 29 | } 30 | else 31 | { 32 | arr[k] = R[j]; 33 | j++; 34 | } 35 | k++; 36 | } 37 | while (i < n1) 38 | { 39 | arr[k] = L[i]; 40 | i++; 41 | k++; 42 | } 43 | while (j < n2) 44 | { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | void mergeSort(char arr[], int l, int r) 51 | { 52 | if (l < r) 53 | { 54 | int m = l+(r-l)/2; 55 | mergeSort(arr, l, m); 56 | mergeSort(arr, m+1, r); 57 | merge(arr, l, m, r); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int len,count,i,freq,temp; 64 | char str[100001]; 65 | scanf("%s",str); 66 | len=strlen(str); 67 | mergeSort(str,0,len-1); 68 | temp=*str; 69 | freq=1; 70 | count=0; 71 | for(i=1;i 7 | #include 8 | 9 | int main() 10 | { 11 | int n,t,len,alpha[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},a=0,i,count=0; 12 | char str[101]; 13 | scanf("%d",&n); 14 | t=n; 15 | while(t--) 16 | { 17 | scanf("%s",str); 18 | len=strlen(str); 19 | for(i=0;i 7 | #include 8 | 9 | int main() 10 | { 11 | char str[10001],hr[11]; 12 | int q,len,i,j; 13 | strcpy(hr,"hackerrank"); 14 | scanf("%d",&q); 15 | while(q--) 16 | { 17 | scanf("%s",str); 18 | len=strlen(str); 19 | for(i=0,j=0;i 7 | #include 8 | #include 9 | 10 | void merge(char arr[], int l, int m, int r) 11 | { 12 | int i, j, k; 13 | int n1 = m - l + 1; 14 | int n2 = r - m; 15 | char L[n1], R[n2]; 16 | for (i = 0; i < n1; i++) 17 | L[i] = arr[l + i]; 18 | for (j = 0; j < n2; j++) 19 | R[j] = arr[m + 1+ j]; 20 | i = 0; 21 | j = 0; 22 | k = l; 23 | while (i < n1 && j < n2) 24 | { 25 | if (L[i] <= R[j]) 26 | { 27 | arr[k] = L[i]; 28 | i++; 29 | } 30 | else 31 | { 32 | arr[k] = R[j]; 33 | j++; 34 | } 35 | k++; 36 | } 37 | while (i < n1) 38 | { 39 | arr[k] = L[i]; 40 | i++; 41 | k++; 42 | } 43 | while (j < n2) 44 | { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | void mergeSort(char arr[], int l, int r) 51 | { 52 | if (l < r) 53 | { 54 | int m = l+(r-l)/2; 55 | mergeSort(arr, l, m); 56 | mergeSort(arr, m+1, r); 57 | merge(arr, l, m, r); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int slen,tlen,i,j,del=0; 64 | char s[10001],t[10001]; 65 | scanf("%s",s); 66 | scanf("%s",t); 67 | slen=strlen(s); 68 | tlen=strlen(t); 69 | mergeSort(s,0,slen-1); 70 | mergeSort(t,0,tlen-1); 71 | i=j=0; 72 | while(s[i]!='\0' || t[j]!='\0') 73 | { 74 | if(s[i]=='\0') 75 | { 76 | j++; 77 | del++; 78 | } 79 | else if(t[j]=='\0') 80 | { 81 | i++; 82 | del++; 83 | } 84 | else if(s[i]>t[j]) 85 | { 86 | j++; 87 | del++; 88 | } 89 | else if(s[i] 7 | #include 8 | 9 | int main() 10 | { 11 | char str[100]; 12 | int len,count=0; 13 | scanf("%s",str); 14 | len=strlen(str); 15 | while(len--) 16 | { 17 | if(len==0 || len==2) 18 | { 19 | if(str[len]!='S') 20 | count++; 21 | } 22 | else if(len==1) 23 | { 24 | if(str[len]!='O') 25 | count++; 26 | } 27 | else if(len%3==0 || len%3==2) 28 | { 29 | if(str[len]!='S') 30 | count++; 31 | } 32 | else 33 | { 34 | if(str[len]!='O') 35 | count++; 36 | } 37 | } 38 | printf("%d",count); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Strings/PalindromeIndex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | import java.util.Scanner; 7 | 8 | class PalindromeIndex { 9 | static int pC(String str) { 10 | int len = str.length(); 11 | int mid = len/2; 12 | for(int i = 0, j = len - 1; i < mid; i++, j--) 13 | if(str.charAt(i) != str.charAt(j)) 14 | return i; 15 | return -1; 16 | } 17 | 18 | public static void main(String args[]) { 19 | int q; 20 | String str, str1; 21 | Scanner in = new Scanner(System.in); 22 | q = in.nextInt(); 23 | for(int i = 0; i < q; i++) { 24 | str = in.next(); 25 | int a = pC(str); 26 | if (a == -1) { 27 | System.out.println(a); 28 | continue; 29 | } 30 | else{ 31 | int b = str.length() - a - 1; 32 | str1 = str.substring(0,a) + str.substring(a+1); 33 | int c = pC(str1); 34 | if (c == -1) { 35 | System.out.println(a); 36 | continue; 37 | } 38 | str1 = str.substring(0,b) + str.substring(b+1); 39 | c = pC(str1); 40 | if (c == -1) { 41 | System.out.println(b); 42 | continue; 43 | } 44 | } 45 | System.out.println("-1"); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Strings/Pangrams.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | void insertionSortCharacter(char *p,int n) 10 | { 11 | int i,j; 12 | char key; 13 | for(i=0;i=0&&(*(p+j))>key) 18 | { 19 | *(p+j+1)=*(p+j); 20 | j=j-1; 21 | } 22 | *(p+j+1)=key; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int i,j,k,l; 29 | char s[1001]; 30 | gets(s); 31 | l=strlen(s); 32 | insertionSortCharacter(s,l); 33 | for(i=65,j=97;i<91;i++,j++) 34 | { 35 | for(k=0;k 7 | #include 8 | 9 | int main() 10 | { 11 | unsigned long t,i,j,k,l,m,n,len,maxl,arr[32],nine; 12 | char str[33]; 13 | scanf("%lu",&t); 14 | while(t--) 15 | { 16 | scanf("%s",str); 17 | len=strlen(str); 18 | maxl=len/2; 19 | for(i=1;i<=maxl;i++) 20 | { 21 | for(j=0;j subStrings(char str[], int n) { 24 | ArrayList al = new ArrayList(); 25 | for (int len = 1; len <= n; len++) { 26 | for (int i = 0; i <= n - len; i++) { 27 | int j = i + len - 1; 28 | String st = ""; 29 | for (int k = i; k <= j; k++) { 30 | st = st + str[k]; 31 | } 32 | al.add(st); 33 | } 34 | } 35 | return al; 36 | } 37 | 38 | // Complete the sherlockAndAnagrams function below. 39 | static int sherlockAndAnagrams(String s) { 40 | ArrayList al = new ArrayList(); 41 | int len = s.length(); 42 | al = subStrings(s.toCharArray(), len); 43 | int sz = al.size(); 44 | int count = 0; 45 | for(int i = 0; i < sz; i++){ 46 | for(int j = i + 1; j < sz; j++){ 47 | String x = al.get(i); 48 | x = sortString(x); 49 | String y = al.get(j); 50 | y = sortString(y); 51 | if(x.length() != y.length()){ 52 | break; 53 | } 54 | if(x.equals(y)){ 55 | count++; 56 | } 57 | } 58 | } 59 | return count; 60 | } 61 | 62 | private static final Scanner scanner = new Scanner(System.in); 63 | 64 | public static void main(String[] args) throws IOException { 65 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 66 | 67 | int q = scanner.nextInt(); 68 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 69 | 70 | for (int qItr = 0; qItr < q; qItr++) { 71 | String s = scanner.nextLine(); 72 | 73 | int result = sherlockAndAnagrams(s); 74 | 75 | bufferedWriter.write(String.valueOf(result)); 76 | bufferedWriter.newLine(); 77 | } 78 | 79 | bufferedWriter.close(); 80 | 81 | scanner.close(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Strings/SherlockAndTheValidString.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the isValid function below. 12 | static String isValid(String s) { 13 | Map hm = new HashMap<>(); 14 | for(int i = 0; i < s.length(); i++){ 15 | hm.put(s.charAt(i), hm.getOrDefault(s.charAt(i), 0) + 1); 16 | } 17 | Map hm2 = new HashMap<>(); 18 | for(char c : hm.keySet()){ 19 | hm2.put(hm.get(c), hm2.getOrDefault(hm.get(c), 0) + 1); 20 | } 21 | if(hm2.size() > 2){ 22 | return "NO"; 23 | } 24 | if(hm2.size() < 2){ 25 | return "YES"; 26 | } 27 | int arr[] = new int[2]; 28 | int i = 0; 29 | for(int x : hm2.keySet()){ 30 | arr[i] = x; 31 | i++; 32 | } 33 | if(hm2.get(arr[0]) > 1 && hm2.get(arr[1]) > 1){ 34 | return "NO"; 35 | } 36 | if(hm2.get(arr[0]) == 1 && hm2.get(arr[1]) == 1){ 37 | if(Math.abs(arr[0] - arr[1]) != 1){ 38 | return "NO"; 39 | } 40 | } 41 | else if(hm2.get(arr[1]) == 1 && arr[1] - arr[0] != 1){ 42 | return "NO"; 43 | } 44 | return "YES"; 45 | } 46 | 47 | private static final Scanner scanner = new Scanner(System.in); 48 | 49 | public static void main(String[] args) throws IOException { 50 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 51 | 52 | String s = scanner.nextLine(); 53 | 54 | String result = isValid(s); 55 | 56 | bufferedWriter.write(result); 57 | bufferedWriter.newLine(); 58 | 59 | bufferedWriter.close(); 60 | 61 | scanner.close(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Strings/StringConstruction.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | #include 9 | 10 | void merge(char arr[], int l, int m, int r) 11 | { 12 | int i, j, k; 13 | int n1 = m - l + 1; 14 | int n2 = r - m; 15 | char L[n1], R[n2]; 16 | for (i = 0; i < n1; i++) 17 | L[i] = arr[l + i]; 18 | for (j = 0; j < n2; j++) 19 | R[j] = arr[m + 1+ j]; 20 | i = 0; 21 | j = 0; 22 | k = l; 23 | while (i < n1 && j < n2) 24 | { 25 | if (L[i] <= R[j]) 26 | { 27 | arr[k] = L[i]; 28 | i++; 29 | } 30 | else 31 | { 32 | arr[k] = R[j]; 33 | j++; 34 | } 35 | k++; 36 | } 37 | while (i < n1) 38 | { 39 | arr[k] = L[i]; 40 | i++; 41 | k++; 42 | } 43 | while (j < n2) 44 | { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | void mergeSort(char arr[], int l, int r) 51 | { 52 | if (l < r) 53 | { 54 | int m = l+(r-l)/2; 55 | mergeSort(arr, l, m); 56 | mergeSort(arr, m+1, r); 57 | merge(arr, l, m, r); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | int n,dollar,len,i; 64 | char s[100001]; 65 | scanf("%d",&n); 66 | while(n--) 67 | { 68 | scanf("%s",s); 69 | len=strlen(s); 70 | mergeSort(s,0,len-1); 71 | dollar=1; 72 | for(i=1;i 7 | #include 8 | 9 | int main() 10 | { 11 | int n,i,lc=1,uc=1,sp=1,dig=1,x,y; 12 | char *str; 13 | scanf("%d",&n); 14 | str=(char *)malloc((n+1)*sizeof(char)); 15 | scanf("%s",str); 16 | for(i=0;i=97 && str[i]<=122) 19 | lc=0; 20 | if(str[i]>=65 && str[i]<=90) 21 | uc=0; 22 | if(str[i]=='!' || str[i]=='@' || str[i]=='#' || str[i]=='$' || str[i]=='%' || str[i]=='^' || str[i]=='&' || str[i]=='*' || str[i]=='(' || str[i]==')' || str[i]=='-' || str[i]=='+') 23 | sp=0; 24 | if(str[i]>=48 && str[i]<=57) 25 | dig=0; 26 | if(lc+uc+sp+dig==0) 27 | break; 28 | } 29 | x=lc+uc+sp+dig; 30 | y=6-n; 31 | if(n>6) 32 | printf("%d",x); 33 | else 34 | { 35 | if(y>x) 36 | x=y; 37 | printf("%d",x); 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Strings/SuperReducedString.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | void SuperReducedString(char * s,char * u) 10 | { 11 | int i,j=0,len,count; 12 | char a,t[102]; 13 | len=strlen(s); 14 | *(s+len)='A'; 15 | *(s+len+1)='\0'; 16 | a=*s; 17 | count=1; 18 | for(i=1;i<=len;i++) 19 | { 20 | if(s[i]==a) 21 | count++; 22 | else 23 | { 24 | if(count%2!=0) 25 | { 26 | t[j]=a; 27 | j++; 28 | } 29 | a=s[i]; 30 | count=1; 31 | } 32 | } 33 | t[j]='\0'; 34 | *(s+len)='\0'; 35 | if(strcmp(s,t)==0) 36 | { 37 | strcpy(u,t); 38 | } 39 | else 40 | { 41 | SuperReducedString(t,u); 42 | } 43 | } 44 | int main() 45 | { 46 | int i,len; 47 | char s[102],u[102]; 48 | scanf("%s",s); 49 | SuperReducedString(s,u); 50 | len=strlen(u); 51 | if(len==0) 52 | printf("Empty String"); 53 | else 54 | printf("%s",u); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Strings/TheLove-LetterMystery.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | #include 8 | 9 | int AbsoluteDiff(int a, int b) 10 | { 11 | if(a>=b) 12 | { 13 | return a-b; 14 | } 15 | return b-a; 16 | } 17 | 18 | int main() 19 | { 20 | char str[10001]; 21 | int q,len,i,j,count; 22 | scanf("%d",&q); 23 | while(q--) 24 | { 25 | count=0; 26 | scanf("%s",str); 27 | len=strlen(str); 28 | for(i=0,j=len;i max) 37 | max = temp.length(); 38 | } 39 | } 40 | System.out.println(max); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Strings/TwoStrings.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | #include 7 | int main() 8 | { 9 | int i,p; 10 | char a; 11 | int S1[26],S2[26]; 12 | scanf("%d",&p); 13 | a=getchar(); 14 | while(p--) 15 | { 16 | for(i=0;i<26;i++) 17 | { 18 | S1[i]=0; 19 | S2[i]=0; 20 | } 21 | do 22 | { 23 | a=getchar(); 24 | S1[a-'a']=1; 25 | }while(a>='a' && a<='z'); 26 | do 27 | { 28 | a=getchar(); 29 | S2[a-'a']=1; 30 | }while(a>='a' && a<='z'); 31 | for(i=0;i<26;i++) 32 | { 33 | if(S1[i]==S2[i] && S1[i]==1) 34 | { 35 | printf("YES\n"); 36 | break; 37 | } 38 | } 39 | if(i==26) 40 | { 41 | printf("NO\n"); 42 | } 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Strings/WeightedUniformStrings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * @author : Anmol Agrawal 4 | * 5 | */ 6 | import java.util.Scanner; 7 | class Solution { 8 | public static void main(String args[]) { 9 | int n, temp = 0; 10 | int alpha[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 11 | String str; 12 | Scanner in = new Scanner(System.in); 13 | str = in.next(); 14 | char a = '0'; 15 | for(int i = 0; i < str.length(); i++) { 16 | if(str.charAt(i) != a) { 17 | if(a != '0' && alpha[a - 'a'] < temp) { 18 | alpha[a - 'a'] = temp; 19 | } 20 | a = str.charAt(i); 21 | temp = 1; 22 | } 23 | else { 24 | temp++; 25 | } 26 | } 27 | if(alpha[a - 'a'] < temp) { 28 | alpha[a - 'a'] = temp; 29 | } 30 | n = in.nextInt(); 31 | for(int j = 0; j < n; j++) { 32 | int i; 33 | int x = in.nextInt(); 34 | for(i = 1; i <= 26; i++) { 35 | if(x % i == 0 && x / i <= alpha[i-1]) { 36 | System.out.println("Yes"); 37 | break; 38 | } 39 | } 40 | if(i == 27) 41 | System.out.println("No"); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Warmup/A Very Big Sum/A Very Big Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | char** split_string(char*); 13 | 14 | // Complete the aVeryBigSum function below. 15 | long aVeryBigSum(int ar_count, long* ar) { 16 | long sum = 0; 17 | for(int i = 0; i < ar_count; i++){ 18 | sum += *(ar + i); 19 | } 20 | return sum; 21 | } 22 | 23 | int main() 24 | { 25 | FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); 26 | 27 | char* ar_count_endptr; 28 | char* ar_count_str = readline(); 29 | int ar_count = strtol(ar_count_str, &ar_count_endptr, 10); 30 | 31 | if (ar_count_endptr == ar_count_str || *ar_count_endptr != '\0') { exit(EXIT_FAILURE); } 32 | 33 | char** ar_temp = split_string(readline()); 34 | 35 | long* ar = malloc(ar_count * sizeof(long)); 36 | 37 | for (int i = 0; i < ar_count; i++) { 38 | char* ar_item_endptr; 39 | char* ar_item_str = *(ar_temp + i); 40 | long ar_item = strtol(ar_item_str, &ar_item_endptr, 10); 41 | 42 | if (ar_item_endptr == ar_item_str || *ar_item_endptr != '\0') { exit(EXIT_FAILURE); } 43 | 44 | *(ar + i) = ar_item; 45 | } 46 | 47 | long result = aVeryBigSum(ar_count, ar); 48 | 49 | fprintf(fptr, "%ld\n", result); 50 | 51 | fclose(fptr); 52 | 53 | return 0; 54 | } 55 | 56 | char* readline() { 57 | size_t alloc_length = 1024; 58 | size_t data_length = 0; 59 | char* data = malloc(alloc_length); 60 | 61 | while (true) { 62 | char* cursor = data + data_length; 63 | char* line = fgets(cursor, alloc_length - data_length, stdin); 64 | 65 | if (!line) { break; } 66 | 67 | data_length += strlen(cursor); 68 | 69 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 70 | 71 | size_t new_length = alloc_length << 1; 72 | data = realloc(data, new_length); 73 | 74 | if (!data) { break; } 75 | 76 | alloc_length = new_length; 77 | } 78 | 79 | if (data[data_length - 1] == '\n') { 80 | data[data_length - 1] = '\0'; 81 | } 82 | 83 | data = realloc(data, data_length); 84 | 85 | return data; 86 | } 87 | 88 | char** split_string(char* str) { 89 | char** splits = NULL; 90 | char* token = strtok(str, " "); 91 | 92 | int spaces = 0; 93 | 94 | while (token) { 95 | splits = realloc(splits, sizeof(char*) * ++spaces); 96 | if (!splits) { 97 | return splits; 98 | } 99 | 100 | splits[spaces - 1] = token; 101 | 102 | token = strtok(NULL, " "); 103 | } 104 | 105 | return splits; 106 | } 107 | -------------------------------------------------------------------------------- /Warmup/A Very Big Sum/A Very Big Sum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the aVeryBigSum function below. 12 | static long aVeryBigSum(long[] ar) { 13 | long sum = 0; 14 | for(long x : ar){ 15 | sum += x; 16 | } 17 | return sum; 18 | } 19 | 20 | private static final Scanner scanner = new Scanner(System.in); 21 | 22 | public static void main(String[] args) throws IOException { 23 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 24 | 25 | int arCount = scanner.nextInt(); 26 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 27 | 28 | long[] ar = new long[arCount]; 29 | 30 | String[] arItems = scanner.nextLine().split(" "); 31 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 32 | 33 | for (int i = 0; i < arCount; i++) { 34 | long arItem = Long.parseLong(arItems[i]); 35 | ar[i] = arItem; 36 | } 37 | 38 | long result = aVeryBigSum(ar); 39 | 40 | bufferedWriter.write(String.valueOf(result)); 41 | bufferedWriter.newLine(); 42 | 43 | bufferedWriter.close(); 44 | 45 | scanner.close(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Warmup/A Very Big Sum/README.md: -------------------------------------------------------------------------------- 1 | # A Very Big Sum 2 | 3 |

Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.

4 |

Function Description

5 |

Complete the aVeryBigSum function in the editor below. It must return the sum of all array elements.

6 |

aVeryBigSum has the following parameter(s):

7 |
    8 |
  • ar: an array of integers .
  • 9 |
10 |

Input Format

11 |

The first line of the input consists of an integer n.
12 | The next line contains n space-separated integers contained in the array.

13 |

Output Format

14 |

Print the integer sum of the elements in the array.

15 |

Constraints
16 |

    17 |
  • 1 <= n <= 10
  • 18 |
  • 0 <= ar[i] <= 1010
  • 19 |
20 |

21 |

Sample Input

22 |
5
23 | 1000000001 1000000002 1000000003 1000000004 1000000005
24 | 
25 |

Output

26 |
5000000015
27 | 
28 |

Note:

29 |

The range of the 32-bit integer is (-231) to (231 - 1) or [-2147483648, 2147483647].

30 |

When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

31 | -------------------------------------------------------------------------------- /Warmup/Birthday Cake Candles/Birthday Cake Candles.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | char** split_string(char*); 13 | 14 | // Complete the birthdayCakeCandles function below. 15 | int birthdayCakeCandles(int ar_count, int* ar) { 16 | int i, count = 1, l; 17 | l = *ar; 18 | for(i = 1; i < ar_count; i++){ 19 | if(*(ar+i) > l){ 20 | l = *(ar + i); 21 | count = 1; 22 | } 23 | else if(*(ar + i) == l){ 24 | count++; 25 | } 26 | } 27 | return count; 28 | } 29 | 30 | int main() 31 | { 32 | FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); 33 | 34 | char* ar_count_endptr; 35 | char* ar_count_str = readline(); 36 | int ar_count = strtol(ar_count_str, &ar_count_endptr, 10); 37 | 38 | if (ar_count_endptr == ar_count_str || *ar_count_endptr != '\0') { exit(EXIT_FAILURE); } 39 | 40 | char** ar_temp = split_string(readline()); 41 | 42 | int* ar = malloc(ar_count * sizeof(int)); 43 | 44 | for (int i = 0; i < ar_count; i++) { 45 | char* ar_item_endptr; 46 | char* ar_item_str = *(ar_temp + i); 47 | int ar_item = strtol(ar_item_str, &ar_item_endptr, 10); 48 | 49 | if (ar_item_endptr == ar_item_str || *ar_item_endptr != '\0') { exit(EXIT_FAILURE); } 50 | 51 | *(ar + i) = ar_item; 52 | } 53 | 54 | int result = birthdayCakeCandles(ar_count, ar); 55 | 56 | fprintf(fptr, "%d\n", result); 57 | 58 | fclose(fptr); 59 | 60 | return 0; 61 | } 62 | 63 | char* readline() { 64 | size_t alloc_length = 1024; 65 | size_t data_length = 0; 66 | char* data = malloc(alloc_length); 67 | 68 | while (true) { 69 | char* cursor = data + data_length; 70 | char* line = fgets(cursor, alloc_length - data_length, stdin); 71 | 72 | if (!line) { break; } 73 | 74 | data_length += strlen(cursor); 75 | 76 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 77 | 78 | size_t new_length = alloc_length << 1; 79 | data = realloc(data, new_length); 80 | 81 | if (!data) { break; } 82 | 83 | alloc_length = new_length; 84 | } 85 | 86 | if (data[data_length - 1] == '\n') { 87 | data[data_length - 1] = '\0'; 88 | } 89 | 90 | data = realloc(data, data_length); 91 | 92 | return data; 93 | } 94 | 95 | char** split_string(char* str) { 96 | char** splits = NULL; 97 | char* token = strtok(str, " "); 98 | 99 | int spaces = 0; 100 | 101 | while (token) { 102 | splits = realloc(splits, sizeof(char*) * ++spaces); 103 | if (!splits) { 104 | return splits; 105 | } 106 | 107 | splits[spaces - 1] = token; 108 | 109 | token = strtok(NULL, " "); 110 | } 111 | 112 | return splits; 113 | } 114 | -------------------------------------------------------------------------------- /Warmup/Birthday Cake Candles/BirthdayCakeCandles.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the birthdayCakeCandles function below. 12 | static int birthdayCakeCandles(int[] ar) { 13 | int count = 1; 14 | int l = ar[0]; 15 | for(int i = 1; i < ar.length; i++){ 16 | if(ar[i] > l){ 17 | l = ar[i]; 18 | count = 1; 19 | } 20 | else if(ar[i] == l){ 21 | count++; 22 | } 23 | } 24 | return count; 25 | } 26 | 27 | private static final Scanner scanner = new Scanner(System.in); 28 | 29 | public static void main(String[] args) throws IOException { 30 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 31 | 32 | int arCount = scanner.nextInt(); 33 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 34 | 35 | int[] ar = new int[arCount]; 36 | 37 | String[] arItems = scanner.nextLine().split(" "); 38 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 39 | 40 | for (int i = 0; i < arCount; i++) { 41 | int arItem = Integer.parseInt(arItems[i]); 42 | ar[i] = arItem; 43 | } 44 | 45 | int result = birthdayCakeCandles(ar); 46 | 47 | bufferedWriter.write(String.valueOf(result)); 48 | bufferedWriter.newLine(); 49 | 50 | bufferedWriter.close(); 51 | 52 | scanner.close(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Warmup/Birthday Cake Candles/BirthdayCakeCandles.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | 5 | process.stdin.resume(); 6 | process.stdin.setEncoding('utf-8'); 7 | 8 | let inputString = ''; 9 | let currentLine = 0; 10 | 11 | process.stdin.on('data', inputStdin => { 12 | inputString += inputStdin; 13 | }); 14 | 15 | process.stdin.on('end', _ => { 16 | inputString = inputString.replace(/\s*$/, '') 17 | .split('\n') 18 | .map(str => str.replace(/\s*$/, '')); 19 | 20 | main(); 21 | }); 22 | 23 | function readLine() { 24 | return inputString[currentLine++]; 25 | } 26 | 27 | // Complete the birthdayCakeCandles function below. 28 | function birthdayCakeCandles(ar) { 29 | let count = 1; 30 | let l = ar[0]; 31 | for(let i = 1; i < ar.length; i++){ 32 | if(ar[i] > l){ 33 | l = ar[i]; 34 | count = 1; 35 | } 36 | else if(ar[i] == l){ 37 | count++; 38 | } 39 | } 40 | return count; 41 | } 42 | 43 | function main() { 44 | const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 45 | 46 | const arCount = parseInt(readLine(), 10); 47 | 48 | const ar = readLine().split(' ').map(arTemp => parseInt(arTemp, 10)); 49 | 50 | let result = birthdayCakeCandles(ar); 51 | 52 | ws.write(result + "\n"); 53 | 54 | ws.end(); 55 | } 56 | -------------------------------------------------------------------------------- /Warmup/Compare the Triplets/Compare the Triplets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | char* readline(); 13 | char* ltrim(char*); 14 | char* rtrim(char*); 15 | char** split_string(char*); 16 | 17 | // Complete the compareTriplets function below. 18 | 19 | /* 20 | * To return the integer array from the function, you should: 21 | * - Store the size of the array to be returned in the result_count variable 22 | * - Allocate the array statically or dynamically 23 | * 24 | * For example, 25 | * int* return_integer_array_using_static_allocation(int* result_count) { 26 | * *result_count = 5; 27 | * 28 | * static int a[5] = {1, 2, 3, 4, 5}; 29 | * 30 | * return a; 31 | * } 32 | * 33 | * int* return_integer_array_using_dynamic_allocation(int* result_count) { 34 | * *result_count = 5; 35 | * 36 | * int *a = malloc(5 * sizeof(int)); 37 | * 38 | * for (int i = 0; i < 5; i++) { 39 | * *(a + i) = i + 1; 40 | * } 41 | * 42 | * return a; 43 | * } 44 | * 45 | */ 46 | int* compareTriplets(int a_count, int* a, int b_count, int* b, int* result_count) { 47 | *result_count = 2; 48 | int* ans = malloc(*result_count * sizeof(int)); 49 | *ans = 0; 50 | *(ans + 1) = 0; 51 | for(int i = 0; i < a_count; i++){ 52 | if(*(a + i) > *(b + i)){ 53 | *(ans) = *ans + 1; 54 | } 55 | if(*(a + i) < *(b + i)){ 56 | *(ans + 1) = *(ans + 1) +1; 57 | } 58 | } 59 | return ans; 60 | } 61 | 62 | int main() 63 | { 64 | FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); 65 | 66 | char** a_temp = split_string(rtrim(readline())); 67 | 68 | int* a = malloc(3 * sizeof(int)); 69 | 70 | for (int i = 0; i < 3; i++) { 71 | char* a_item_endptr; 72 | char* a_item_str = *(a_temp + i); 73 | int a_item = strtol(a_item_str, &a_item_endptr, 10); 74 | 75 | if (a_item_endptr == a_item_str || *a_item_endptr != '\0') { exit(EXIT_FAILURE); } 76 | 77 | *(a + i) = a_item; 78 | } 79 | 80 | int a_count = 3; 81 | 82 | char** b_temp = split_string(rtrim(readline())); 83 | 84 | int* b = malloc(3 * sizeof(int)); 85 | 86 | for (int i = 0; i < 3; i++) { 87 | char* b_item_endptr; 88 | char* b_item_str = *(b_temp + i); 89 | int b_item = strtol(b_item_str, &b_item_endptr, 10); 90 | 91 | if (b_item_endptr == b_item_str || *b_item_endptr != '\0') { exit(EXIT_FAILURE); } 92 | 93 | *(b + i) = b_item; 94 | } 95 | 96 | int b_count = 3; 97 | 98 | int result_count; 99 | int* result = compareTriplets(a_count, a, b_count, b, &result_count); 100 | 101 | for (int i = 0; i < result_count; i++) { 102 | fprintf(fptr, "%d", *(result + i)); 103 | 104 | if (i != result_count - 1) { 105 | fprintf(fptr, " "); 106 | } 107 | } 108 | 109 | fprintf(fptr, "\n"); 110 | 111 | fclose(fptr); 112 | 113 | return 0; 114 | } 115 | 116 | char* readline() { 117 | size_t alloc_length = 1024; 118 | size_t data_length = 0; 119 | char* data = malloc(alloc_length); 120 | 121 | while (true) { 122 | char* cursor = data + data_length; 123 | char* line = fgets(cursor, alloc_length - data_length, stdin); 124 | 125 | if (!line) { 126 | break; 127 | } 128 | 129 | data_length += strlen(cursor); 130 | 131 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { 132 | break; 133 | } 134 | 135 | alloc_length <<= 1; 136 | 137 | data = realloc(data, alloc_length); 138 | 139 | if (!data) { 140 | data = '\0'; 141 | 142 | break; 143 | } 144 | } 145 | 146 | if (data[data_length - 1] == '\n') { 147 | data[data_length - 1] = '\0'; 148 | 149 | data = realloc(data, data_length); 150 | 151 | if (!data) { 152 | data = '\0'; 153 | } 154 | } else { 155 | data = realloc(data, data_length + 1); 156 | 157 | if (!data) { 158 | data = '\0'; 159 | } else { 160 | data[data_length] = '\0'; 161 | } 162 | } 163 | 164 | return data; 165 | } 166 | 167 | char* ltrim(char* str) { 168 | if (!str) { 169 | return '\0'; 170 | } 171 | 172 | if (!*str) { 173 | return str; 174 | } 175 | 176 | while (*str != '\0' && isspace(*str)) { 177 | str++; 178 | } 179 | 180 | return str; 181 | } 182 | 183 | char* rtrim(char* str) { 184 | if (!str) { 185 | return '\0'; 186 | } 187 | 188 | if (!*str) { 189 | return str; 190 | } 191 | 192 | char* end = str + strlen(str) - 1; 193 | 194 | while (end >= str && isspace(*end)) { 195 | end--; 196 | } 197 | 198 | *(end + 1) = '\0'; 199 | 200 | return str; 201 | } 202 | 203 | char** split_string(char* str) { 204 | char** splits = NULL; 205 | char* token = strtok(str, " "); 206 | 207 | int spaces = 0; 208 | 209 | while (token) { 210 | splits = realloc(splits, sizeof(char*) * ++spaces); 211 | 212 | if (!splits) { 213 | return splits; 214 | } 215 | 216 | splits[spaces - 1] = token; 217 | 218 | token = strtok(NULL, " "); 219 | } 220 | 221 | return splits; 222 | } 223 | -------------------------------------------------------------------------------- /Warmup/Compare the Triplets/Compare the Triplets.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.function.*; 8 | import java.util.regex.*; 9 | import java.util.stream.*; 10 | import static java.util.stream.Collectors.joining; 11 | import static java.util.stream.Collectors.toList; 12 | 13 | public class Solution { 14 | 15 | // Complete the compareTriplets function below. 16 | static List compareTriplets(List a, List b) { 17 | List ans = new ArrayList<>(); 18 | ans.add(0); 19 | ans.add(0); 20 | for(int i = 0; i < a.size(); i++){ 21 | if(a.get(i) > b.get(i)){ 22 | ans.set(0, ans.get(0) + 1); 23 | } 24 | if(a.get(i) < b.get(i)){ 25 | ans.set(1, ans.get(1) + 1); 26 | } 27 | } 28 | return ans; 29 | } 30 | 31 | public static void main(String[] args) throws IOException { 32 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 33 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 34 | 35 | List a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) 36 | .map(Integer::parseInt) 37 | .collect(toList()); 38 | 39 | List b = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) 40 | .map(Integer::parseInt) 41 | .collect(toList()); 42 | 43 | List result = compareTriplets(a, b); 44 | 45 | bufferedWriter.write( 46 | result.stream() 47 | .map(Object::toString) 48 | .collect(joining(" ")) 49 | + "\n" 50 | ); 51 | 52 | bufferedReader.close(); 53 | bufferedWriter.close(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Warmup/Compare the Triplets/README.md: -------------------------------------------------------------------------------- 1 | # Compare the Triplets 2 | 3 |

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty. 4 |

5 |

The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).

6 |

The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].

7 |
    8 |
  • If a[i] > b[i], then Alice is awarded 1 point.
  • 9 |
  • If a[i] < b[i], then Bob is awarded 1 point.
  • 10 |
  • If a[i] = b[i], then neither person receives a point.
  • 11 |
12 |

Comparison points is the total points a person earned.

13 |

Given a and b, determine their respective comparison points.

14 |

Example

15 |

a = [1, 2, 3]
b = [3, 2, 1]

16 |
    17 |
  • For elements *0*, Bob is awarded a point because a[0] .
  • 18 | 19 |
  • For the equal elements a[1] and b[1], no points are earned.
  • 20 |
  • Finally, for elements 2, a[2] > b[2] so Alice receives a point.
  • 21 |
    22 |
23 | 24 |

The return array is [1, 1] with Alice's score first and Bob's second.

25 |

Function Description

26 |

Complete the function compareTriplets in the editor below.

27 |

compareTriplets has the following parameter(s):

28 |
    29 |
  • int a[3]: Alice's challenge rating
  • 30 |
  • int b[3]: Bob's challenge rating
  • 31 |
32 |

Return

33 |
34 |
    35 |
  • int[2]: Alice's score is in the first position, and Bob's score is in the second.
  • 36 |
37 |

Input Format

38 |

The first line contains 3 space-separated integers, a[0], a[1], and a[2], the respective values in triplet a.
39 | The second line contains 3 space-separated integers, b[0], b[1], and b[2], the respective values in triplet b.

40 |

Constraints

41 |
    42 |
  • 1 ≤ a[i] ≤ 100
  • 43 |
  • 1 ≤ b[i] ≤ 100
  • 44 |
45 |

Sample Input 0

46 |
5 6 7
47 | 3 6 10
48 |

Sample Output 0

49 |
1 1
50 |

Explanation 0

51 |

In this example:

52 |
    53 |
  • a = (a[0], a[1], a[2]) = (5, 6, 7)
  • 54 |
  • b = (b[0], b[1], b[2]) = (3, 6, 10)
  • 55 |
56 |

Now, let's compare each individual score:

57 |
    58 |
  • a[0] > b[0], so Alice receives 1 point.
  • 59 |
  • a[1] = b[1], so nobody receives a point.
  • 60 |
  • a[2] < b[2], so Bob receives 1 point.
  • 61 |
62 |

Alice's comparison score is 1, and Bob's comparison score is 1. Thus, we return the array [1,1].

63 |

Sample Input 1

64 |
17 28 30
65 | 99 16 8
66 |

Sample Output 1

67 |
2 1
68 |

Explanation 1

69 |

Comparing the 0th elements, 17 < 99 so Bob receives a point.
70 | Comparing the 1st and 2nd elements, 28 > 16 and 30 > 8 so Alice receives two points.
71 | The return array is [2, 1].

72 | -------------------------------------------------------------------------------- /Warmup/Diagonal Difference/Diagonal Difference.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | char* readline(); 13 | char* ltrim(char*); 14 | char* rtrim(char*); 15 | char** split_string(char*); 16 | 17 | int parse_int(char*); 18 | 19 | /* 20 | * Complete the 'diagonalDifference' function below. 21 | * 22 | * The function is expected to return an INTEGER. 23 | * The function accepts 2D_INTEGER_ARRAY arr as parameter. 24 | */ 25 | 26 | int diagonalDifference(int arr_rows, int arr_columns, int** arr) { 27 | int a = 0, b = 0; 28 | for(int i = 0; i < arr_rows; i++){ 29 | for(int j = 0; j < arr_columns; j++){ 30 | if(i == j){ 31 | a += arr[i][j]; 32 | } 33 | if((i + j) == (arr_rows - 1)){ 34 | b += arr[i][j]; 35 | } 36 | } 37 | } 38 | return (a > b) ? a - b : b - a; 39 | } 40 | 41 | int main() 42 | { 43 | FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); 44 | 45 | int n = parse_int(ltrim(rtrim(readline()))); 46 | 47 | int** arr = malloc(n * sizeof(int*)); 48 | 49 | for (int i = 0; i < n; i++) { 50 | *(arr + i) = malloc(n * (sizeof(int))); 51 | 52 | char** arr_item_temp = split_string(rtrim(readline())); 53 | 54 | for (int j = 0; j < n; j++) { 55 | int arr_item = parse_int(*(arr_item_temp + j)); 56 | 57 | *(*(arr + i) + j) = arr_item; 58 | } 59 | } 60 | 61 | int result = diagonalDifference(n, n, arr); 62 | 63 | fprintf(fptr, "%d\n", result); 64 | 65 | fclose(fptr); 66 | 67 | return 0; 68 | } 69 | 70 | char* readline() { 71 | size_t alloc_length = 1024; 72 | size_t data_length = 0; 73 | 74 | char* data = malloc(alloc_length); 75 | 76 | while (true) { 77 | char* cursor = data + data_length; 78 | char* line = fgets(cursor, alloc_length - data_length, stdin); 79 | 80 | if (!line) { 81 | break; 82 | } 83 | 84 | data_length += strlen(cursor); 85 | 86 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { 87 | break; 88 | } 89 | 90 | alloc_length <<= 1; 91 | 92 | data = realloc(data, alloc_length); 93 | 94 | if (!data) { 95 | data = '\0'; 96 | 97 | break; 98 | } 99 | } 100 | 101 | if (data[data_length - 1] == '\n') { 102 | data[data_length - 1] = '\0'; 103 | 104 | data = realloc(data, data_length); 105 | 106 | if (!data) { 107 | data = '\0'; 108 | } 109 | } else { 110 | data = realloc(data, data_length + 1); 111 | 112 | if (!data) { 113 | data = '\0'; 114 | } else { 115 | data[data_length] = '\0'; 116 | } 117 | } 118 | 119 | return data; 120 | } 121 | 122 | char* ltrim(char* str) { 123 | if (!str) { 124 | return '\0'; 125 | } 126 | 127 | if (!*str) { 128 | return str; 129 | } 130 | 131 | while (*str != '\0' && isspace(*str)) { 132 | str++; 133 | } 134 | 135 | return str; 136 | } 137 | 138 | char* rtrim(char* str) { 139 | if (!str) { 140 | return '\0'; 141 | } 142 | 143 | if (!*str) { 144 | return str; 145 | } 146 | 147 | char* end = str + strlen(str) - 1; 148 | 149 | while (end >= str && isspace(*end)) { 150 | end--; 151 | } 152 | 153 | *(end + 1) = '\0'; 154 | 155 | return str; 156 | } 157 | 158 | char** split_string(char* str) { 159 | char** splits = NULL; 160 | char* token = strtok(str, " "); 161 | 162 | int spaces = 0; 163 | 164 | while (token) { 165 | splits = realloc(splits, sizeof(char*) * ++spaces); 166 | 167 | if (!splits) { 168 | return splits; 169 | } 170 | 171 | splits[spaces - 1] = token; 172 | 173 | token = strtok(NULL, " "); 174 | } 175 | 176 | return splits; 177 | } 178 | 179 | int parse_int(char* str) { 180 | char* endptr; 181 | int value = strtol(str, &endptr, 10); 182 | 183 | if (endptr == str || *endptr != '\0') { 184 | exit(EXIT_FAILURE); 185 | } 186 | 187 | return value; 188 | } 189 | -------------------------------------------------------------------------------- /Warmup/Diagonal Difference/Diagonal Difference.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.function.*; 8 | import java.util.regex.*; 9 | import java.util.stream.*; 10 | import static java.util.stream.Collectors.joining; 11 | import static java.util.stream.Collectors.toList; 12 | 13 | class Result { 14 | 15 | /* 16 | * Complete the 'diagonalDifference' function below. 17 | * 18 | * The function is expected to return an INTEGER. 19 | * The function accepts 2D_INTEGER_ARRAY arr as parameter. 20 | */ 21 | 22 | public static int diagonalDifference(List> arr) { 23 | int a = 0, b = 0; 24 | for(int i = 0; i < arr.size(); i++){ 25 | for(int j = 0; j < arr.get(i).size(); j++){ 26 | if(i == j){ 27 | a += arr.get(i).get(j); 28 | } 29 | if((i + j) == (arr.size() - 1)){ 30 | b += arr.get(i).get(j); 31 | } 32 | } 33 | } 34 | return (a > b) ? a - b : b - a; 35 | } 36 | 37 | } 38 | 39 | public class Solution { 40 | public static void main(String[] args) throws IOException { 41 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 42 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 43 | 44 | int n = Integer.parseInt(bufferedReader.readLine().trim()); 45 | 46 | List> arr = new ArrayList<>(); 47 | 48 | IntStream.range(0, n).forEach(i -> { 49 | try { 50 | arr.add( 51 | Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) 52 | .map(Integer::parseInt) 53 | .collect(toList()) 54 | ); 55 | } catch (IOException ex) { 56 | throw new RuntimeException(ex); 57 | } 58 | }); 59 | 60 | int result = Result.diagonalDifference(arr); 61 | 62 | bufferedWriter.write(String.valueOf(result)); 63 | bufferedWriter.newLine(); 64 | 65 | bufferedReader.close(); 66 | bufferedWriter.close(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Warmup/Diagonal Difference/Diagonal Difference.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | 5 | process.stdin.resume(); 6 | process.stdin.setEncoding('utf-8'); 7 | 8 | let inputString = ''; 9 | let currentLine = 0; 10 | 11 | process.stdin.on('data', function(inputStdin) { 12 | inputString += inputStdin; 13 | }); 14 | 15 | process.stdin.on('end', function() { 16 | inputString = inputString.split('\n'); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | /* 26 | * Complete the 'diagonalDifference' function below. 27 | * 28 | * The function is expected to return an INTEGER. 29 | * The function accepts 2D_INTEGER_ARRAY arr as parameter. 30 | */ 31 | 32 | function diagonalDifference(arr) { 33 | let a = 0, b = 0; 34 | for(let i = 0; i < arr.length; i++){ 35 | for(let j = 0; j < arr[i].length; j++){ 36 | if(i == j){ 37 | a += arr[i][j]; 38 | } 39 | if((i + j) == (arr.length - 1)){ 40 | b += arr[i][j]; 41 | } 42 | } 43 | } 44 | return (a > b) ? a - b : b - a; 45 | } 46 | 47 | function main() { 48 | const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 49 | 50 | const n = parseInt(readLine().trim(), 10); 51 | 52 | let arr = Array(n); 53 | 54 | for (let i = 0; i < n; i++) { 55 | arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); 56 | } 57 | 58 | const result = diagonalDifference(arr); 59 | 60 | ws.write(result + '\n'); 61 | 62 | ws.end(); 63 | } 64 | -------------------------------------------------------------------------------- /Warmup/Diagonal Difference/README.md: -------------------------------------------------------------------------------- 1 | # Diagonal Difference 2 | 3 |

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

4 |

For example, the square matrix arr is shown below:

5 |
1 2 3
 6 | 4 5 6
 7 | 9 8 9
 8 | 
9 |

The left-to-right diagonal = 1 + 5 + 9 = 15. The right to left diagonal = 3 + 5 + 9 = 17. Their absolute difference is |15 - 17| = 2.

10 |

Function description

11 |

Complete the diagonalDifference function in the editor below. It must return an integer representing the absolute diagonal difference.

12 |

diagonalDifference takes the following parameter:

13 |
    14 |
  • arr: an array of integers .
  • 15 |
16 |

Input Format

17 |

The first line contains a single integer, n, the number of rows and columns in the matrix arr.
18 | Each of the next n lines describes a row, arr[i], and consists of n space-separated integers arr[i][j].

19 |

Constraints

20 |
    21 |
  • -100 <= arr[i][j] <= 100
  • 22 |
23 |

Output Format

24 |

Print the absolute difference between the sums of the matrix's two diagonals as a single integer.

25 |

Sample Input

26 |
3
27 | 11 2 4
28 | 4 5 6
29 | 10 8 -12
30 | 
31 |

Sample Output

32 |
15
33 | 
34 |

Explanation

35 |

The primary diagonal is:

36 |
11
37 |    5
38 |      -12
39 | 
40 |

Sum across the primary diagonal: 11 + 5 - 12 = 4

41 |

The secondary diagonal is:

42 |
     4
43 |    5
44 | 10
45 | 
46 |

Sum across the secondary diagonal: 4 + 5 + 10 = 19
47 | Difference: |4 - 19| = 15

48 |

Note: |x| is the absolute value of x

49 | -------------------------------------------------------------------------------- /Warmup/Mini-Max Sum/Mini-MaxSum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | char** split_string(char*); 13 | 14 | // Complete the miniMaxSum function below. 15 | void miniMaxSum(int arr_count, int* arr) { 16 | long sum = *arr; 17 | int min = *arr; 18 | int max = *arr; 19 | for(int i = 1; i < arr_count; i++){ 20 | sum += *(arr + i); 21 | if(*(arr + i) > max){ 22 | max = *(arr + i); 23 | } 24 | if(*(arr + i) < min){ 25 | min = *(arr + i); 26 | } 27 | } 28 | printf("%ld %ld", sum - max, sum - min); 29 | } 30 | 31 | int main() 32 | { 33 | char** arr_temp = split_string(readline()); 34 | 35 | int* arr = malloc(5 * sizeof(int)); 36 | 37 | for (int i = 0; i < 5; i++) { 38 | char* arr_item_endptr; 39 | char* arr_item_str = *(arr_temp + i); 40 | int arr_item = strtol(arr_item_str, &arr_item_endptr, 10); 41 | 42 | if (arr_item_endptr == arr_item_str || *arr_item_endptr != '\0') { exit(EXIT_FAILURE); } 43 | 44 | *(arr + i) = arr_item; 45 | } 46 | 47 | int arr_count = 5; 48 | 49 | miniMaxSum(arr_count, arr); 50 | 51 | return 0; 52 | } 53 | 54 | char* readline() { 55 | size_t alloc_length = 1024; 56 | size_t data_length = 0; 57 | char* data = malloc(alloc_length); 58 | 59 | while (true) { 60 | char* cursor = data + data_length; 61 | char* line = fgets(cursor, alloc_length - data_length, stdin); 62 | 63 | if (!line) { break; } 64 | 65 | data_length += strlen(cursor); 66 | 67 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 68 | 69 | size_t new_length = alloc_length << 1; 70 | data = realloc(data, new_length); 71 | 72 | if (!data) { break; } 73 | 74 | alloc_length = new_length; 75 | } 76 | 77 | if (data[data_length - 1] == '\n') { 78 | data[data_length - 1] = '\0'; 79 | } 80 | 81 | data = realloc(data, data_length); 82 | 83 | return data; 84 | } 85 | 86 | char** split_string(char* str) { 87 | char** splits = NULL; 88 | char* token = strtok(str, " "); 89 | 90 | int spaces = 0; 91 | 92 | while (token) { 93 | splits = realloc(splits, sizeof(char*) * ++spaces); 94 | if (!splits) { 95 | return splits; 96 | } 97 | 98 | splits[spaces - 1] = token; 99 | 100 | token = strtok(NULL, " "); 101 | } 102 | 103 | return splits; 104 | } 105 | -------------------------------------------------------------------------------- /Warmup/Mini-Max Sum/Mini-MaxSum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the miniMaxSum function below. 12 | static void miniMaxSum(int[] arr) { 13 | long sum = 0; 14 | int min = Integer.MAX_VALUE; 15 | int max = Integer.MIN_VALUE; 16 | for(int x : arr){ 17 | sum += x; 18 | if(x > max){ 19 | max = x; 20 | } 21 | if(x < min){ 22 | min = x; 23 | } 24 | } 25 | System.out.println((sum - max) + " " + (sum - min)); 26 | } 27 | 28 | private static final Scanner scanner = new Scanner(System.in); 29 | 30 | public static void main(String[] args) { 31 | int[] arr = new int[5]; 32 | 33 | String[] arrItems = scanner.nextLine().split(" "); 34 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 35 | 36 | for (int i = 0; i < 5; i++) { 37 | int arrItem = Integer.parseInt(arrItems[i]); 38 | arr[i] = arrItem; 39 | } 40 | 41 | miniMaxSum(arr); 42 | 43 | scanner.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Warmup/Mini-Max Sum/Mini-MaxSum.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.stdin.resume(); 4 | process.stdin.setEncoding('utf-8'); 5 | 6 | let inputString = ''; 7 | let currentLine = 0; 8 | 9 | process.stdin.on('data', inputStdin => { 10 | inputString += inputStdin; 11 | }); 12 | 13 | process.stdin.on('end', _ => { 14 | inputString = inputString.replace(/\s*$/, '') 15 | .split('\n') 16 | .map(str => str.replace(/\s*$/, '')); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | // Complete the miniMaxSum function below. 26 | function miniMaxSum(arr) { 27 | let sum = arr[0]; 28 | let min = arr[0]; 29 | let max = arr[0]; 30 | for(let i = 1; i < arr.length; i++){ 31 | sum += arr[i]; 32 | if(arr[i] > max){ 33 | max = arr[i]; 34 | } 35 | if(arr[i] < min){ 36 | min = arr[i]; 37 | } 38 | } 39 | console.log((sum - max) + " " + (sum - min)); 40 | } 41 | 42 | function main() { 43 | const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10)); 44 | 45 | miniMaxSum(arr); 46 | } 47 | -------------------------------------------------------------------------------- /Warmup/Plus Minus/PlusMinus.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | char** split_string(char*); 13 | 14 | // Complete the plusMinus function below. 15 | void plusMinus(int arr_count, int* arr) { 16 | float n = 0.0, p = 0.0; 17 | for(int i = 0; i < arr_count; i++){ 18 | if(*(arr + i) > 0){ 19 | p++; 20 | } 21 | if(*(arr + i) < 0){ 22 | n++; 23 | } 24 | } 25 | float z = (float)arr_count - p - n; 26 | printf("%f\n%f\n%f", p / arr_count, n / arr_count, z / arr_count); 27 | } 28 | 29 | int main() 30 | { 31 | char* n_endptr; 32 | char* n_str = readline(); 33 | int n = strtol(n_str, &n_endptr, 10); 34 | 35 | if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } 36 | 37 | char** arr_temp = split_string(readline()); 38 | 39 | int* arr = malloc(n * sizeof(int)); 40 | 41 | for (int i = 0; i < n; i++) { 42 | char* arr_item_endptr; 43 | char* arr_item_str = *(arr_temp + i); 44 | int arr_item = strtol(arr_item_str, &arr_item_endptr, 10); 45 | 46 | if (arr_item_endptr == arr_item_str || *arr_item_endptr != '\0') { exit(EXIT_FAILURE); } 47 | 48 | *(arr + i) = arr_item; 49 | } 50 | 51 | int arr_count = n; 52 | 53 | plusMinus(arr_count, arr); 54 | 55 | return 0; 56 | } 57 | 58 | char* readline() { 59 | size_t alloc_length = 1024; 60 | size_t data_length = 0; 61 | char* data = malloc(alloc_length); 62 | 63 | while (true) { 64 | char* cursor = data + data_length; 65 | char* line = fgets(cursor, alloc_length - data_length, stdin); 66 | 67 | if (!line) { break; } 68 | 69 | data_length += strlen(cursor); 70 | 71 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 72 | 73 | size_t new_length = alloc_length << 1; 74 | data = realloc(data, new_length); 75 | 76 | if (!data) { break; } 77 | 78 | alloc_length = new_length; 79 | } 80 | 81 | if (data[data_length - 1] == '\n') { 82 | data[data_length - 1] = '\0'; 83 | } 84 | 85 | data = realloc(data, data_length); 86 | 87 | return data; 88 | } 89 | 90 | char** split_string(char* str) { 91 | char** splits = NULL; 92 | char* token = strtok(str, " "); 93 | 94 | int spaces = 0; 95 | 96 | while (token) { 97 | splits = realloc(splits, sizeof(char*) * ++spaces); 98 | if (!splits) { 99 | return splits; 100 | } 101 | 102 | splits[spaces - 1] = token; 103 | 104 | token = strtok(NULL, " "); 105 | } 106 | 107 | return splits; 108 | } 109 | -------------------------------------------------------------------------------- /Warmup/Plus Minus/PlusMinus.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the plusMinus function below. 12 | static void plusMinus(int[] arr) { 13 | float n = 0, p = 0; 14 | for(int x : arr){ 15 | if(x > 0){ 16 | p++; 17 | } 18 | if(x < 0){ 19 | n++; 20 | } 21 | } 22 | float l = (float)arr.length; 23 | float z = l - p - n; 24 | System.out.println(p / l); 25 | System.out.println(n / l); 26 | System.out.println(z / l); 27 | } 28 | 29 | private static final Scanner scanner = new Scanner(System.in); 30 | 31 | public static void main(String[] args) { 32 | int n = scanner.nextInt(); 33 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 34 | 35 | int[] arr = new int[n]; 36 | 37 | String[] arrItems = scanner.nextLine().split(" "); 38 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 39 | 40 | for (int i = 0; i < n; i++) { 41 | int arrItem = Integer.parseInt(arrItems[i]); 42 | arr[i] = arrItem; 43 | } 44 | 45 | plusMinus(arr); 46 | 47 | scanner.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Warmup/Plus Minus/PlusMinus.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.stdin.resume(); 4 | process.stdin.setEncoding('utf-8'); 5 | 6 | let inputString = ''; 7 | let currentLine = 0; 8 | 9 | process.stdin.on('data', inputStdin => { 10 | inputString += inputStdin; 11 | }); 12 | 13 | process.stdin.on('end', _ => { 14 | inputString = inputString.replace(/\s*$/, '') 15 | .split('\n') 16 | .map(str => str.replace(/\s*$/, '')); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | // Complete the plusMinus function below. 26 | function plusMinus(arr) { 27 | let n = 0, p = 0; 28 | let l = arr.length; 29 | for(let i = 0; i < l; i++){ 30 | if(arr[i] > 0){ 31 | p++; 32 | } 33 | if(arr[i] < 0){ 34 | n++; 35 | } 36 | } 37 | let z = l - p - n; 38 | console.log(p / l); 39 | console.log(n / l); 40 | console.log(z / l); 41 | } 42 | 43 | function main() { 44 | const n = parseInt(readLine(), 10); 45 | 46 | const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10)); 47 | 48 | plusMinus(arr); 49 | } 50 | -------------------------------------------------------------------------------- /Warmup/Simple Array Sum/README.md: -------------------------------------------------------------------------------- 1 | # Simple Array Sum 2 | 3 |

Given an array of integers, find the sum of its elements.

4 |

For example, if the array ar = [1,2,3], 1 + 2 + 3 = 6, so return 6.

5 |

Function Description

6 |

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

7 |

simpleArraySum has the following parameter(s):

8 |
    9 |
  • ar: an array of integers
  • 10 |
11 |

Input Format

12 |

The first line contains an integer, n, denoting the size of the array.
13 | The second line contains space-separated integers representing the array's elements.

14 |

Constraints

15 |

0 < n, ar[i] <= 1000

16 |

Output Format

17 |

Print the sum of the array's elements as a single integer.

18 |

Sample Input

19 |
6
20 | 1 2 3 4 10 11
21 | 
22 |

Sample Output

23 |
31
24 | 
25 |

Explanation

26 |

We print the sum of the array's elements: 1+2+3+4+10+11 = 31.

27 | -------------------------------------------------------------------------------- /Warmup/Simple Array Sum/Simple Array Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | char* readline(); 10 | char** split_string(char*); 11 | 12 | /* 13 | * Complete the simpleArraySum function below. 14 | */ 15 | int simpleArraySum(int ar_count, int* ar) { 16 | /* 17 | * Write your code here. 18 | */ 19 | int sum = 0; 20 | for(int i = 0; i < ar_count; i++){ 21 | sum += ar[i]; 22 | } 23 | return sum; 24 | } 25 | 26 | int main() 27 | { 28 | FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); 29 | 30 | char* ar_count_endptr; 31 | char* ar_count_str = readline(); 32 | int ar_count = strtol(ar_count_str, &ar_count_endptr, 10); 33 | 34 | if (ar_count_endptr == ar_count_str || *ar_count_endptr != '\0') { exit(EXIT_FAILURE); } 35 | 36 | char** ar_temp = split_string(readline()); 37 | 38 | int ar[ar_count]; 39 | 40 | for (int ar_itr = 0; ar_itr < ar_count; ar_itr++) { 41 | char* ar_item_endptr; 42 | char* ar_item_str = ar_temp[ar_itr]; 43 | int ar_item = strtol(ar_item_str, &ar_item_endptr, 10); 44 | 45 | if (ar_item_endptr == ar_item_str || *ar_item_endptr != '\0') { exit(EXIT_FAILURE); } 46 | 47 | ar[ar_itr] = ar_item; 48 | } 49 | 50 | int result = simpleArraySum(ar_count, ar); 51 | 52 | fprintf(fptr, "%d\n", result); 53 | 54 | fclose(fptr); 55 | 56 | return 0; 57 | } 58 | 59 | char* readline() { 60 | size_t alloc_length = 1024; 61 | size_t data_length = 0; 62 | char* data = malloc(alloc_length); 63 | 64 | while (true) { 65 | char* cursor = data + data_length; 66 | char* line = fgets(cursor, alloc_length - data_length, stdin); 67 | 68 | if (!line) { break; } 69 | 70 | data_length += strlen(cursor); 71 | 72 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 73 | 74 | size_t new_length = alloc_length << 1; 75 | data = realloc(data, new_length); 76 | 77 | if (!data) { break; } 78 | 79 | alloc_length = new_length; 80 | } 81 | 82 | if (data[data_length - 1] == '\n') { 83 | data[data_length - 1] = '\0'; 84 | } 85 | 86 | data = realloc(data, data_length); 87 | 88 | return data; 89 | } 90 | 91 | char** split_string(char* str) { 92 | char** splits = NULL; 93 | char* token = strtok(str, " "); 94 | 95 | int spaces = 0; 96 | 97 | while (token) { 98 | splits = realloc(splits, sizeof(char*) * ++spaces); 99 | if (!splits) { 100 | return splits; 101 | } 102 | 103 | splits[spaces - 1] = token; 104 | 105 | token = strtok(NULL, " "); 106 | } 107 | 108 | return splits; 109 | } 110 | -------------------------------------------------------------------------------- /Warmup/Simple Array Sum/SimpleArraySum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.text.*; 4 | import java.util.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | /* 10 | * Complete the simpleArraySum function below. 11 | */ 12 | static int simpleArraySum(int[] ar) { 13 | int sum = 0; 14 | for(int i = 0; i < ar.length; i++){ 15 | sum += ar[i]; 16 | } 17 | return sum; 18 | } 19 | 20 | private static final Scanner scanner = new Scanner(System.in); 21 | 22 | public static void main(String[] args) throws IOException { 23 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 24 | 25 | int arCount = Integer.parseInt(scanner.nextLine().trim()); 26 | 27 | int[] ar = new int[arCount]; 28 | 29 | String[] arItems = scanner.nextLine().split(" "); 30 | 31 | for (int arItr = 0; arItr < arCount; arItr++) { 32 | int arItem = Integer.parseInt(arItems[arItr].trim()); 33 | ar[arItr] = arItem; 34 | } 35 | 36 | int result = simpleArraySum(ar); 37 | 38 | bufferedWriter.write(String.valueOf(result)); 39 | bufferedWriter.newLine(); 40 | 41 | bufferedWriter.close(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Warmup/Solve Me First/README.md: -------------------------------------------------------------------------------- 1 | # Solve Me First 2 | 3 |

Complete the function solveMeFirst to compute the sum of two integers.

4 |

Function prototype:

5 |

int solveMeFirst(int a, int b);

6 |

where,

7 |
    8 |
  • a is the first integer input.
  • 9 |
  • b is the second integer input
  • 10 |
11 |

Return values

12 |
    13 |
  • sum of the above two integers
  • 14 |
15 |

Sample Input

16 |
 a = 2
17 |  b = 3
18 | 
19 |

Sample Output

20 |
 5
21 | 
22 |

Explanation

23 |

The sum of the two integers a and b is computed as: 2 + 3 = 5

24 | -------------------------------------------------------------------------------- /Warmup/Solve Me First/SolveMeFirst.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int solveMeFirst(int a, int b) { 7 | return a+b; 8 | } 9 | 10 | int main() { 11 | int num1,num2; 12 | scanf("%d %d",&num1,&num2); 13 | int sum; 14 | sum = solveMeFirst(num1,num2); 15 | printf("%d",sum); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Warmup/Solve Me First/SolveMeFirst.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | 10 | static int solveMeFirst(int a, int b) { 11 | return a+b; 12 | } 13 | 14 | public static void main(String[] args) { 15 | Scanner in = new Scanner(System.in); 16 | int a; 17 | a = in.nextInt(); 18 | int b; 19 | b = in.nextInt(); 20 | int sum; 21 | sum = solveMeFirst(a, b); 22 | System.out.println(sum); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Warmup/Staircase/Staircase.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | 13 | // Complete the staircase function below. 14 | void staircase(int n) { 15 | for(int i = 1; i <= n; i++){ 16 | for(int j = 1; j <= n; j++){ 17 | if(i + j > n){ 18 | printf("#"); 19 | } 20 | else{ 21 | printf(" "); 22 | } 23 | } 24 | printf("\n"); 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | char* n_endptr; 31 | char* n_str = readline(); 32 | int n = strtol(n_str, &n_endptr, 10); 33 | 34 | if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } 35 | 36 | staircase(n); 37 | 38 | return 0; 39 | } 40 | 41 | char* readline() { 42 | size_t alloc_length = 1024; 43 | size_t data_length = 0; 44 | char* data = malloc(alloc_length); 45 | 46 | while (true) { 47 | char* cursor = data + data_length; 48 | char* line = fgets(cursor, alloc_length - data_length, stdin); 49 | 50 | if (!line) { break; } 51 | 52 | data_length += strlen(cursor); 53 | 54 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 55 | 56 | size_t new_length = alloc_length << 1; 57 | data = realloc(data, new_length); 58 | 59 | if (!data) { break; } 60 | 61 | alloc_length = new_length; 62 | } 63 | 64 | if (data[data_length - 1] == '\n') { 65 | data[data_length - 1] = '\0'; 66 | } 67 | 68 | data = realloc(data, data_length); 69 | 70 | return data; 71 | } 72 | -------------------------------------------------------------------------------- /Warmup/Staircase/Staircase.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.security.*; 4 | import java.text.*; 5 | import java.util.*; 6 | import java.util.concurrent.*; 7 | import java.util.regex.*; 8 | 9 | public class Solution { 10 | 11 | // Complete the staircase function below. 12 | static void staircase(int n) { 13 | for(int i = 1; i <= n; i++){ 14 | for(int j = 1; j <= n; j++){ 15 | if(i + j > n){ 16 | System.out.print("#"); 17 | } 18 | else{ 19 | System.out.print(" "); 20 | } 21 | } 22 | System.out.println(); 23 | } 24 | } 25 | 26 | private static final Scanner scanner = new Scanner(System.in); 27 | 28 | public static void main(String[] args) { 29 | int n = scanner.nextInt(); 30 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 31 | 32 | staircase(n); 33 | 34 | scanner.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Warmup/Staircase/Staircase.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | process.stdin.resume(); 4 | process.stdin.setEncoding('utf-8'); 5 | 6 | let inputString = ''; 7 | let currentLine = 0; 8 | 9 | process.stdin.on('data', inputStdin => { 10 | inputString += inputStdin; 11 | }); 12 | 13 | process.stdin.on('end', _ => { 14 | inputString = inputString.replace(/\s*$/, '') 15 | .split('\n') 16 | .map(str => str.replace(/\s*$/, '')); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | // Complete the staircase function below. 26 | function staircase(n) { 27 | for(let i = 1; i <= n; i++){ 28 | for(let j = 1; j <= n; j++){ 29 | if(i + j > n){ 30 | process.stdout.write("#"); 31 | } 32 | else{ 33 | process.stdout.write(" "); 34 | } 35 | } 36 | process.stdout.write("\n"); 37 | } 38 | } 39 | 40 | function main() { 41 | const n = parseInt(readLine(), 10); 42 | 43 | staircase(n); 44 | } 45 | -------------------------------------------------------------------------------- /Warmup/Time Conversion/TimeConversion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | char* readline(); 10 | 11 | /* 12 | * Complete the timeConversion function below. 13 | */ 14 | 15 | /* 16 | * Please either make the string static or allocate on the heap. For example, 17 | * static char str[] = "hello world"; 18 | * return str; 19 | * 20 | * OR 21 | * 22 | * char* str = "hello world"; 23 | * return str; 24 | * 25 | */ 26 | char* timeConversion(char* s) { 27 | int hh = (10 * (s[0] - '0')) + (s[1] - '0'); 28 | if(s[8] == 'P' && hh < 12){ 29 | hh += 12; 30 | } 31 | else if(s[8] == 'A' && hh == 12){ 32 | hh = 0; 33 | } 34 | s[0] = (char)((hh / 10) + '0'); 35 | s[1] = (char)((hh % 10) + '0'); 36 | s[8] = '\0'; 37 | return s; 38 | } 39 | 40 | int main() 41 | { 42 | FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); 43 | 44 | char* s = readline(); 45 | 46 | char* result = timeConversion(s); 47 | 48 | fprintf(fptr, "%s\n", result); 49 | 50 | fclose(fptr); 51 | 52 | return 0; 53 | } 54 | 55 | char* readline() { 56 | size_t alloc_length = 1024; 57 | size_t data_length = 0; 58 | char* data = malloc(alloc_length); 59 | 60 | while (true) { 61 | char* cursor = data + data_length; 62 | char* line = fgets(cursor, alloc_length - data_length, stdin); 63 | 64 | if (!line) { break; } 65 | 66 | data_length += strlen(cursor); 67 | 68 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 69 | 70 | size_t new_length = alloc_length << 1; 71 | data = realloc(data, new_length); 72 | 73 | if (!data) { break; } 74 | 75 | alloc_length = new_length; 76 | } 77 | 78 | if (data[data_length - 1] == '\n') { 79 | data[data_length - 1] = '\0'; 80 | } 81 | 82 | data = realloc(data, data_length); 83 | 84 | return data; 85 | } 86 | -------------------------------------------------------------------------------- /Warmup/Time Conversion/TimeConversion.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.math.*; 3 | import java.text.*; 4 | import java.util.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | /* 10 | * Complete the timeConversion function below. 11 | */ 12 | static String timeConversion(String s) { 13 | /* 14 | * Write your code here. 15 | */ 16 | int hh = (10 * (s.charAt(0) - '0')) + (s.charAt(1) - '0'); 17 | if(s.charAt(8) == 'P' && hh < 12){ 18 | hh += 12; 19 | } 20 | else if(s.charAt(8) == 'A' && hh == 12){ 21 | hh = 0; 22 | } 23 | return "" + (hh / 10) + (hh % 10) + s.substring(2, 8); 24 | } 25 | 26 | private static final Scanner scan = new Scanner(System.in); 27 | 28 | public static void main(String[] args) throws IOException { 29 | BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 30 | 31 | String s = scan.nextLine(); 32 | 33 | String result = timeConversion(s); 34 | 35 | bw.write(result); 36 | bw.newLine(); 37 | 38 | bw.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Warmup/Time Conversion/TimeConversion.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | 5 | process.stdin.resume(); 6 | process.stdin.setEncoding('utf-8'); 7 | 8 | let inputString = ''; 9 | let currentLine = 0; 10 | 11 | process.stdin.on('data', inputStdin => { 12 | inputString += inputStdin; 13 | }); 14 | 15 | process.stdin.on('end', _ => { 16 | inputString = inputString.trim().split('\n').map(str => str.trim()); 17 | 18 | main(); 19 | }); 20 | 21 | function readLine() { 22 | return inputString[currentLine++]; 23 | } 24 | 25 | /* 26 | * Complete the timeConversion function below. 27 | */ 28 | function timeConversion(s) { 29 | /* 30 | * Write your code here. 31 | */ 32 | let hh = (10 * (s[0] - '0')) + (s[1] - '0'); 33 | if(s[8] == 'P' && hh < 12){ 34 | hh += 12; 35 | } 36 | else if(s[8] == 'A' && hh == 12){ 37 | hh = 0; 38 | } 39 | return "" + Math.floor(hh / 10) + (hh % 10) + s.slice(2, 8); 40 | } 41 | 42 | function main() { 43 | const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 44 | 45 | const s = readLine(); 46 | 47 | let result = timeConversion(s); 48 | 49 | ws.write(result + "\n"); 50 | 51 | ws.end(); 52 | } 53 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-midnight 2 | -------------------------------------------------------------------------------- /greedy/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect 2 | --------------------------------------------------------------------------------