-
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
6395761
commit c8cfbb2
Showing
4 changed files
with
93 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,73 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.util.*; | ||
class ModifyQueue { | ||
public static void main(String[] args) { | ||
|
||
// Taking input using class Scanner | ||
Scanner sc = new Scanner(System.in); | ||
|
||
// Taking total number of testcases | ||
int t = sc.nextInt(); | ||
|
||
while (t-- > 0) { | ||
// Taking count of total number of elements | ||
int n = sc.nextInt(); | ||
|
||
// Taking count of total elements | ||
// that need to be reversed | ||
int k = sc.nextInt(); | ||
|
||
// Creating a Queue | ||
Queue<Integer> q = new LinkedList<>(); | ||
|
||
// adding all the elements to the Queue | ||
while (n-- > 0) { | ||
q.add((int)sc.nextInt()); | ||
} | ||
|
||
// Creating an object of class GfG | ||
GfG g = new GfG(); | ||
|
||
// calling modifyQueue of class GfG | ||
// and passing Queue and k as arguments | ||
// and storing the reuslt in a new Queue | ||
Queue<Integer> ans = g.modifyQueue(q, k); | ||
|
||
// Printing all the elements from the | ||
// new Queue and polling them out | ||
while (!ans.isEmpty()) { | ||
int a = ans.peek(); | ||
ans.poll(); | ||
System.out.print(a + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
// User function Template for Java | ||
|
||
class GfG { | ||
// Function to reverse first k elements of a queue. | ||
public Queue<Integer> modifyQueue(Queue<Integer> q, int k) | ||
{ | ||
// add code here. | ||
Deque<Integer> d = new ArrayDeque<>(); | ||
|
||
for (int i = 0; i < k; i++) | ||
d.push(q.poll()); | ||
|
||
while (!d.isEmpty()) | ||
q.add(d.pop()); | ||
|
||
for (int i = 0; i < q.size() - k; i++) | ||
q.add(q.poll()); | ||
|
||
return q; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
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,16 @@ | ||
class Solution | ||
{ | ||
public boolean halvesAreAlike(String s) | ||
{ | ||
final String a = s.substring(0, s.length() / 2); | ||
final String b = s.substring(s.length() / 2); | ||
final int aVowelsCount = (int) a.chars().filter(c -> isVowel((char) c)).count(); | ||
final int bVowelsCount = (int) b.chars().filter(c -> isVowel((char) c)).count(); | ||
return aVowelsCount == bVowelsCount; | ||
} | ||
|
||
private boolean isVowel(char c) | ||
{ | ||
return "aeiouAEIOU".indexOf(c) != -1; | ||
} | ||
} |