Skip to content

Commit c7f5392

Browse files
committed
2 parents b74df1f + 43cd23b commit c7f5392

File tree

6 files changed

+502
-53
lines changed

6 files changed

+502
-53
lines changed

Arrays/Good_pairs.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Name : Abhinav kumar
2+
Github username : Abhinavcode13
3+
Repository name : data-structures-and-algorithms
4+
Problem : Find Number of Good Pairs in Go
5+
Issue Number : #529
6+
Problem statement : Given an array of integers nums, return the number of good pairs.
7+
8+
A pair (i, j) is called good if nums[i] == nums[j] and i < j.
9+
10+
Example 1:
11+
12+
Input: nums = [1,2,3,1,1,3]
13+
Output: 4
14+
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
15+
Example 2:
16+
17+
Input: nums = [1,1,1,1]
18+
Output: 6
19+
Explanation: Each pair in the array are good.
20+
Example 3:
21+
22+
Input: nums = [1,2,3]
23+
Output: 0
24+
25+
Constraints:
26+
27+
1 <= nums.length <= 100
28+
1 <= nums[i] <= 100
29+
30+
Explanation of the below cpp code:
31+
32+
This C++ program takes an integer n as input and then reads n integers from the standard input.
33+
It then creates a vector v to store the input integers and a map m to count the frequency of each input integer.
34+
Finally, the program computes the number of pairs of input integers that are equal and outputs this count to the standard output.
35+
36+
The algorithm used to compute the count of pairs is based on the following observation:
37+
for each integer x that appears k times in the input vector, there are k*(k-1)/2 pairs of integers that are equal to x.
38+
This can be seen by choosing any two of the k occurrences of x, which can be done in k*(k-1)/2 ways.
39+
40+
The program uses the map data structure to count the frequency of each integer in the input vector.
41+
It then iterates over the pairs (x,k) in the map and adds k*(k-1)/2 to a variable count. Finally, it outputs the value of count.
42+
43+
Overall, this program has a time complexity of O(n log n) due to the use of the map data structure, which has a logarithmic time complexity for insertion and lookup operations.
44+
However, since the input size is limited to 10^5 integers, the program should run efficiently for typical input sizes.
45+
*/
46+
-------------------------------------------------------------------------//C++ code begins here---------------------------------------------------------------------------------
47+
//Array: Find Number of Good Pairs in Go
48+
49+
#include <bits/stdc++.h>
50+
using namespace std;
51+
52+
int main() {
53+
int n;
54+
cin>>n;
55+
vector<int> v;
56+
map<int,int> m;
57+
for(int i=0;i<n;i++)
58+
{
59+
int x;
60+
cin>>x;
61+
v.push_back(x);
62+
m[x]++;
63+
}
64+
int count = 0;
65+
for(auto it:m)
66+
{
67+
count = count + (it.second-1)*(it.second)/2;
68+
}
69+
cout<<count<<endl;
70+
return 0;
71+
}

Arrays/longest_peak.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Write a function that takes in an array of integers and returns the length of the longest peak
3+
in the array.A peak is defined as adjacent integers in the array that are strictly increasing
4+
until they reach a tip (the highest value in the peak), at which point they become strictly
5+
decreasing. At least three integers are required to form a peak.
6+
"""
7+
8+
9+
10+
#approach
11+
"""
12+
1. iterate through the array from index 1 to len(arr) - 1
13+
2. check if the current element is a peak
14+
3. if it is a peak, then find the length of the peak
15+
4. go to the uphill start
16+
5. go to the downhill end
17+
6. update the ans
18+
"""
19+
# Time Complexity: O(n)
20+
# Space Complexity: O(1)
21+
22+
23+
24+
arr=[1, 2, 3, 3, 4, 0, 10, 6, 5, -1, -3, 2, 3]
25+
26+
27+
def longestPeak(arr: list) -> int:
28+
ans = 0
29+
# iterate through the array from index 1 to len(arr) - 1
30+
for indx in range(1, len(arr) - 1):
31+
# check if the current element is a peak
32+
if arr[indx - 1] < arr[indx] > arr[indx + 1]:
33+
# if it is a peak, then find the length of the peak
34+
uphill_start = downhill_ends = indx
35+
# go to the uphill start
36+
while uphill_start > 0 and arr[uphill_start] > arr[uphill_start - 1]:
37+
uphill_start -= 1
38+
# go to the downhill end
39+
while downhill_ends + 1 < len(arr) and arr[downhill_ends] > arr[downhill_ends + 1]:
40+
downhill_ends += 1
41+
# update the ans
42+
ans = max(ans, (downhill_ends - uphill_start + 1))
43+
return ans
44+
45+
46+
print(longestPeak(arr))
47+
# output: 6

Arrays/squares_of_a_sorted_array.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
3+
Time Complexity: O(n), Space Complexity O(n).
4+
5+
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
6+
7+
Input: nums = [-4,-1,0,3,10]
8+
Output: [0,1,9,16,100]
9+
10+
Input: nums = [-7,-3,2,3,11]
11+
Output: [4,9,9,49,121]
12+
13+
14+
Input: nums = [2,3,11]
15+
Output: [4,9,121]
16+
17+
**/
18+
19+
class Solution {
20+
public int[] sortedSquares(int[] nums) {
21+
22+
int hasNegative = -1; // -1 Indcating that are no negative values in the input array.
23+
int len = nums.length;
24+
25+
// Find the index of the last negative value.
26+
for(int i = 0; i < len; ++i)
27+
{
28+
if(nums[i] < 0)
29+
{
30+
nums[i] = Math.abs(nums[i]); // If there is a negative value make it positive.
31+
hasNegative = i;
32+
}
33+
}
34+
35+
36+
int []ans = new int[nums.length];
37+
38+
if(hasNegative != -1) // check if the array have negative values
39+
{
40+
41+
/**
42+
If there are negative values,
43+
that's divide the input array into two halfs.
44+
both halfs are sorted in increasing order if:
45+
46+
-first half start from a and end at index 0, Where a is the index of the last negative value.
47+
-second half start from (b) and end at (size of the array - 1 (n - 1)) [b, n-1] iclusive, Where b is the index a + 1.
48+
49+
At every step we choose the minimun value between the vlaues at index a and b then,
50+
square the value and store it in array []ans.
51+
**/
52+
int a = hasNegative, b = hasNegative + 1;
53+
int k = 0;
54+
55+
while(a >= 0 && b < len)
56+
{
57+
if(nums[a] <= nums[b]) // Value at index a is the minimum value so we choosed.
58+
{
59+
ans[k] = nums[a] * nums[a];
60+
a--;
61+
}
62+
else
63+
{
64+
ans[k] = nums[b] * nums[b];
65+
b++;
66+
}
67+
k++;
68+
}
69+
70+
while(a >= 0)
71+
{
72+
ans[k++] = nums[a] * nums[a];
73+
a--;
74+
}
75+
76+
while(b < len)
77+
{
78+
ans[k++] = nums[b] * nums[b];
79+
b++;
80+
}
81+
}
82+
else //If there are no negative values, the sloution is straight forward.
83+
{
84+
85+
for(int i = 0; i < len; ++i)
86+
ans[i] = nums[i] * nums[i];
87+
}
88+
return ans;
89+
}
90+
91+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* LEETCODE 876
3+
Given the head of a singly linked list, return the middle node of the linked list.
4+
If there are two middle nodes, return the second middle node.
5+
*Example 1:
6+
Input: head = [1,2,3,4,5]
7+
Output: [3,4,5]
8+
Explanation: The middle node of the list is node 3.
9+
10+
*Example 2:
11+
Input: head = [1,2,3,4,5,6]
12+
Output: [4,5,6]
13+
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
14+
15+
*CODE EXPLAINATION WITH DRY RUN:
16+
This Java code finds the midpoint node of a singly-linked list. If the linked list has an even number
17+
of nodes, it returns the second middle node.
18+
19+
First, the Node class is defined with a constructor that takes in an integer value and initializes
20+
the next reference to null. The insertAtTail() method takes in a head node and an integer value,
21+
and inserts a new node with the given value at the end of the linked list. The printLinkedList() method
22+
takes in a head node and prints out all the values in the linked list.
23+
24+
The makeLinkedList() method prompts the user to enter integers until -1 is inputted. Each integer is
25+
inserted into a new node at the tail of the linked list. The computeMidpoint() method takes in a head
26+
node and returns the middle node(s) of the linked list. If the linked list has no nodes or only one node,
27+
it just returns the head node. Otherwise, it initializes a slow pointer and a fast pointer to the head
28+
node. The while loop advances the fast pointer by two nodes and the slow pointer by one node at each
29+
iteration until the fast pointer reaches the end of the linked list. At that point, the slow pointer
30+
will be pointing to the midpoint node(s) of the linked list.
31+
32+
Finally, in the main() method, a new linked list is created by calling makeLinkedList(). The linked
33+
list is printed using printLinkedList(). The midpoint of the linked list is computed using
34+
computeMidpoint(), and its value is printed out.
35+
36+
*Example Dry Run:
37+
Suppose we have the following input:
38+
1 2 3 4 5 -1
39+
This creates a linked list with the following structure:
40+
1 -> 2 -> 3 -> 4 -> 5 -> null
41+
Initially, the slow pointer and fast pointer both point to the head node, which is 1. In the first iteration of the while loop, the fast pointer moves two nodes ahead to node 3, while the slow pointer moves one node ahead to node 2. In the second iteration, the fast pointer moves another two nodes ahead to null, while the slow pointer moves one more node ahead to node 3. At this point, the slow pointer is pointing to the midpoint node(s) of the linked list.
42+
Therefore, computeMidpoint() returns node 3, which is printed out as output.
43+
44+
*/
45+
import java.util.Scanner;
46+
47+
public class linked_list_compute_midpoint {
48+
49+
static class Node {
50+
51+
int data;
52+
Node next;
53+
54+
public Node(int data) {
55+
this.data = data;
56+
next = null;
57+
}
58+
}
59+
60+
static Node insertAtTail(Node head, int data) {
61+
if (head == null) {
62+
head = new Node(data);
63+
return head;
64+
}
65+
Node n = new Node(data);
66+
Node temp = head;
67+
while (temp.next != null) {
68+
temp = temp.next;
69+
}
70+
temp.next = n;
71+
return head;
72+
}
73+
74+
static void printLinkedList(Node head) {
75+
while (head != null) {
76+
System.out.print(head.data + "->");
77+
head = head.next;
78+
}
79+
}
80+
81+
static Node makeLinkedList() {
82+
Scanner scanner = new Scanner(System.in);
83+
int data = scanner.nextInt();
84+
Node head = null;
85+
while (data != -1) {
86+
head = insertAtTail(head, data);
87+
data = scanner.nextInt();
88+
}
89+
scanner.close();
90+
return head;
91+
}
92+
93+
static Node computeMidpoint(Node head) {
94+
if (head == null || head.next == null) {
95+
return head;
96+
}
97+
Node slow = head;
98+
Node fast = head.next;
99+
while (fast != null && fast.next != null) {
100+
fast = fast.next.next;
101+
slow = slow.next;
102+
}
103+
return slow;
104+
}
105+
106+
public static void main(String[] args) {
107+
Node head = makeLinkedList();
108+
printLinkedList(head);
109+
System.out.println();
110+
Node midpoint = computeMidpoint(head);
111+
System.out.println(midpoint.data);
112+
}
113+
}

0 commit comments

Comments
 (0)