-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
174be60
commit 5b404dc
Showing
8 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(nlogn) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(k) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |