Skip to content

Commit

Permalink
Added codes for 9 and 10 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 10, 2024
1 parent 174be60 commit 5b404dc
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 0 deletions.
97 changes: 97 additions & 0 deletions GeeksForGeeks/June/09-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class IntArray {
public static int[] input(BufferedReader br, int n) throws IOException {
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]);

return a;
}

public static void print(int[] a) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}

public static void print(ArrayList<Integer> a) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}
}

class GFG {

public static boolean isZigzag(int n, int[] arr) {
int f = 1;

for (int i = 1; i < n; i++) {
if (f == 1) {
if (arr[i - 1] > arr[i]) return false;
} else {
if (arr[i - 1] < arr[i]) return false;
}
f ^= 1;
}

return true;
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {

int n;
n = Integer.parseInt(br.readLine());

int[] arr = IntArray.input(br, n);

Solution obj = new Solution();
obj.zigZag(n, arr);
boolean flag = false;
for (int i = 0; i < n; i++) {
if (arr[i] == i % 2) {
flag = false;
} else {
flag = true;
break;
}
}
if (!flag) {
System.out.println("0");
} else {

boolean check = isZigzag(n, arr);
if (check) {
System.out.println("1");
} else {
System.out.println("0");
}
}
}
}
}

// } Driver Code Ends



class Solution {
public static void zigZag(int n, int[] arr) {
// code here
for (int i = 1; i < n; i++)
if ((i % 2 == 1 && arr[i - 1] > arr[i]) || (i % 2 == 0 && arr[i - 1] < arr[i]))
swap(arr, i - 1, i);
}

private static void swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/09-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
50 changes: 50 additions & 0 deletions GeeksForGeeks/June/10-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while (tc-- > 0) {
String[] inputLine;
int n = Integer.parseInt(br.readLine().trim());
char[] nuts = new char[n], bolts = new char[n];
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
nuts[i] = (inputLine[i].charAt(0));
}
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
bolts[i] = (inputLine[i].charAt(0));
}

new Solution().matchPairs(n, nuts, bolts);
for (int i = 0; i < n; i++) {
System.out.print(nuts[i] + " ");
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print(bolts[i] + " ");
}
System.out.println();
}
}
}

// } Driver Code Ends


// User function Template for Java

class Solution {
void matchPairs(int n, char nuts[], char bolts[]) {
// code here
Arrays.sort(nuts);

Arrays.sort(bolts);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/10-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(nlogn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/June/09-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(k)
17 changes: 17 additions & 0 deletions LeetCode/June/09-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int subarraysDivByK(int[] nums, int k) {
int ans = 0;
int prefix = 0;
int[] count = new int[k];
count[0] = 1;

for (int num : nums)
{
prefix = (prefix + num % k + k) % k;
ans += count[prefix];
++count[prefix];
}

return ans;
}
}
2 changes: 2 additions & 0 deletions LeetCode/June/10-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
21 changes: 21 additions & 0 deletions LeetCode/June/10-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int heightChecker(int[] heights) {
int ans = 0;
int currentHeight = 1;
int[] count = new int[101];

for (int height : heights)
++count[height];

for (int height : heights)
{
while (count[currentHeight] == 0)
++currentHeight;
if (height != currentHeight)
++ans;
--count[currentHeight];
}

return ans;
}
}

0 comments on commit 5b404dc

Please sign in to comment.