Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

. #14

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

. #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Day6Problem1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Sort and compare.
// Do as you are told.
int main()
{
int n;
int arr[100000];
scanf("%d",&n);
for (int i=0;i<n;i++)
scanf("%d",&arr[i]);

int sorted[100000];
for (int i=0;i<n;i++)
sorted[i]=arr[i];
sort(sorted,sorted+n);
vector<int> diff;
for (int i=0;i<n;i++)
if (sorted[i]!=arr[i])
diff.push_back(i);
if (diff.size()==0)
printf("yes\n");
else if (diff.size()==2)
printf("yes\nswap %d %d\n",diff[0]+1,diff[1]+1); //Index starts at 1!!!
else //Let's try reverse it!
{
int st = diff[0], ed = diff[diff.size()-1];
while (st<ed)
{
swap(arr[st],arr[ed]);
st++;
ed--;
}
int flag=1;
for (int i=0;i<n;i++)
if (sorted[i]!=arr[i])
{
printf("no\n");
return 0;
}
printf("yes\nreverse %d %d\n",diff[0]+1,diff[diff.size()-1]+1); //Index starts at 1!!!
}
}
30 changes: 30 additions & 0 deletions Day6Problem2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int n,c_i,count=0;
scanf("%d",&n);
int *c = malloc(sizeof(int) * n);
for(c_i = 0; c_i < n-1; c_i++){
scanf("%d",&c[c_i]);
}
for(c_i = 0; c_i < n-1; c_i++)
{
if(c[(c_i)+2]==0)
{
c_i++;
count++;
}
else if(c[(c_i)+1]==0)
{
count++;
}
}
printf("%d",count);
return 0;
}
33 changes: 33 additions & 0 deletions Day6Problem3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int t;
scanf("%d",&t);
while (t--)
{
int n;
int arr[1000];
scanf("%d",&n);
for (int i=0;i<n;i++)
scanf("%d",&arr[i]);
int inv = 0;
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++)
if (arr[i] > arr[j])
inv ++;
if (inv%2==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
62 changes: 62 additions & 0 deletions Day6Problem4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

// Main algorithm: in order to rotate the whole matrix, we'll just rotate one ring at a time
// We can do this in-place to achieve O(1) additional space complexity
int main() {
int M, N, R;
cin>>M>>N>>R;
int **matrix = new int*[M];
for(int i = 0; i < M; i++) {
matrix[i] = new int[N];
for(int j = 0; j < N; j++) {
cin>>matrix[i][j];
}
}

int numRings = min(M,N)/2;
for(int i = 0; i < numRings; i++) {
// Subtract the number of 360 degree rotations from R
// A 360 degree rotation = rotating the same number of times as the perimeter of the current ring
int numRotations = R%(2*(M + N - 4*i) - 4);
for(int rotation = 0; rotation < numRotations; rotation++) {
// Rotate the ring (see the clockwise algorithm for an in-depth example of how this is done)
// Rotate top row
for(int j = i; j < N-i-1; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][j+1];
matrix[i][j+1] = tmp;
}
// Rotate right column
for(int j = i; j < M-i-1; j++) {
int tmp = matrix[j][N-i-1];
matrix[j][N-i-1] = matrix[j+1][N-i-1];
matrix[j+1][N-i-1] = tmp;
}
// Rotate bottom row
for(int j = N-i-1; j > i; j--) {
int tmp = matrix[M-i-1][j];
matrix[M-i-1][j] = matrix[M-i-1][j-1];
matrix[M-i-1][j-1] = tmp;
}
// Rotate left column
for(int j = M-i-1; j > i+1; j--) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[j-1][i];
matrix[j-1][i] = tmp;
}
}
}
// Output final matrix
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
cout<<matrix[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
8 changes: 8 additions & 0 deletions Day6Problem5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
pass
d = OrderedCounter(input() for _ in range(int(input())))
print(len(d))
print(*d.values())


23 changes: 23 additions & 0 deletions Raghavtroublemaker/Day5Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.samuelale.practice;
import java.math.BigInteger;
import java.util.Scanner;


public class FibonacciMod {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
BigInteger t1 = s.nextBigInteger();
BigInteger t2 = s.nextBigInteger();
BigInteger n = s.nextBigInteger();

BigInteger ti = new BigInteger("0");
for(int i=3; i<=n.intValue(); i++) {
ti = t1.add(t2.pow(2));
t1 = t2;
t2 = ti;
}
System.out.println(ti);
}

}
71 changes: 71 additions & 0 deletions Raghavtroublemaker/Day5Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.samuelale.practice;

import java.util.;


public class CutTheTree {

private static class Node {
int value;
int total;
List<Node> edges;

Node(int val) {
this.value = val;
total = 0;
edges = new ArrayList<>();
}

@Override
public String toString() {
return "Val: " + value + " | Total: " + total; //+ " | Edges: " + edges.toString();
}
}

private static HashSet<Node> visited = new HashSet<>();

private static void findTotal(Node n) {
visited.add(n);
if(n.edges.isEmpty()) {
n.total = n.value;
} else {
int total = n.value;
for(Node edge : n.edges) {
if(visited.contains(edge)) continue;
visited.add(edge);
findTotal(edge);
total+=edge.total;
}
n.total = total;
}
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();

Node[] nodeArr = new Node[N];
for(int i=0; i<N; i++) {
nodeArr[i] = new Node(input.nextInt());
}

for(int i=0; i<N-1; i++) {
int n1 = input.nextInt()-1;
int n2 = input.nextInt()-1;
nodeArr[n1].edges.add(nodeArr[n2]);
nodeArr[n2].edges.add(nodeArr[n1]);
}

// calculate the totals at each node
findTotal(nodeArr[0]);

int minDiff = Integer.MAX_VALUE;
for(int i=1; i<N; i++) {
int diff = Math.abs(nodeArr[0].total - nodeArr[i].total2);
if(diff < minDiff) {
minDiff = diff;
}
}
System.out.print(minDiff);
}
}
28 changes: 28 additions & 0 deletions Raghavtroublemaker/Day5Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.samuelale.practice;

import java.util.HashSet;
import java.util.Scanner;


public class AlgSearchPairs {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int K = s.nextInt();
HashSet<Integer> valSet = new HashSet<>();
for (int i=0; i<N; i++) {
valSet.add(s.nextInt());
}

HashSet<Integer> modSet = new HashSet<>(valSet);
int pairs = 0;
for(int val : valSet) {
if(modSet.contains(val+K) || modSet.contains(val-K)) {
pairs++;
}
modSet.remove(val);
}
System.out.print(pairs);
}
}
25 changes: 25 additions & 0 deletions Raghavtroublemaker/Day5Problem4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class ANewHope {

/*
* The strategy will be to create a new week and try to swap shirt values to
* look more like the last week until the conditions are met, or we until
* we can no longer move shirts around without violating the conditions.
*/
public int count(int[] firstWeek, int[] lastWeek, int D) {
int N = firstWeek.length;

boolean same = true;
for(int i=0; i<N; i++) same &= firstWeek[i] == lastWeek[i];
if(same) return 1;

int gap = N-D;


return gap;
}

public static void main(String[] args) {
ANewHope hope = new ANewHope();
System.out.println(hope.count(new int[]{1,2,3,4}, new int[]{4,3,2,1}, 3));
}
}
Loading