Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f39575b

Browse files
array
1 parent b50539e commit f39575b

13 files changed

+713
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
// https://www.geeksforgeeks.org/find-subarray-with-given-sum/
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.math.BigInteger;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
public class subarrayWithGivenSum {
11+
12+
public static void main(String[] args) throws IOException {
13+
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
int n = Integer.parseInt(br.readLine());
16+
String[] st = br.readLine().split(" ");
17+
List<Integer> ar = new ArrayList<Integer>(Collections.nCopies(st.length, 0));
18+
19+
for (int i = 0; i < ar.size(); i++) {
20+
ar.set(Integer.parseInt(st[0]),i);
21+
}
22+
23+
int tot = ar.stream().mapToInt(Integer::intValue).sum();
24+
25+
//sliding window
26+
27+
28+
}
29+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
*
3+
*
4+
*/
5+
6+
import java.io.*;
7+
import java.util.*;
8+
public class prefixSum
9+
{
10+
static final int IBIG = 1000000007;
11+
public static void main(String[] args)throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
int n, k;
15+
16+
n = Integer.parseInt(br.readLine());
17+
k = Integer.parseInt(br.readLine());
18+
System.out.println(solve(n, k));
19+
20+
}
21+
22+
static void fillPrefixSum(int arr[], int n,
23+
int prefixSum[])
24+
{
25+
prefixSum[0] = arr[0];
26+
for (int i = 1; i < n; ++i)
27+
prefixSum[i] = (prefixSum[i - 1]%IBIG + arr[i]%IBIG)%IBIG;
28+
29+
30+
}
31+
32+
static int solve(int n, int k) {
33+
int arr[] = new int[n];
34+
int arr1[] = new int[n];
35+
36+
37+
for(int i =0; i<arr.length;i++)
38+
{
39+
arr[i]=i+1;
40+
}
41+
42+
for(int i =0; i<k;i++)
43+
{
44+
fillPrefixSum(arr,n,arr1);
45+
arr = arr1.clone();
46+
}
47+
48+
List<Integer> con = new ArrayList<Integer>();
49+
for(int i =0; i<arr.length;i++)
50+
{
51+
52+
con.add(i,arr[i]%10);
53+
54+
}
55+
56+
57+
Collections.sort(con, Collections.reverseOrder());
58+
59+
int[] com = con.stream().mapToInt(Integer::intValue).toArray();
60+
//List<Integer> ar = Arrays.stream(nums).boxed().collect(Collectors.toList());
61+
62+
return (arrayToInt(com));
63+
64+
}
65+
66+
static int arrayToInt(int[] arr)
67+
{ String s ="";
68+
69+
for (int i : arr)
70+
{
71+
s+=i;
72+
}
73+
74+
return (Integer.parseInt(s)%IBIG);
75+
}
76+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* A circular key is given with n integer
3+
* one rotates it k times
4+
* after rotating it k times
5+
*
6+
*
7+
*
8+
*
9+
* if >10
10+
* if <10
11+
*
12+
*/
13+
14+
import java.io.*;
15+
import java.util.*;
16+
public class rotatearray {
17+
18+
public static void main(String[] args) throws IOException {
19+
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
int n = Integer.parseInt(br.readLine());
22+
int k = Integer.parseInt(br.readLine());
23+
String[] st = br.readLine().split(" ");
24+
// using temp list from behind and pasting it in the front
25+
List<Integer> ar = new ArrayList<Integer>(Collections.nCopies(st.length, 0));
26+
27+
for (int i = 0; i < ar.size(); i++) {
28+
ar.set(i,Integer.parseInt(st[i]));
29+
}
30+
31+
//rotating th array using extra list named temp
32+
List<Integer> temp = new ArrayList<Integer>();
33+
for (int i = n-1; i > n-1-k; i--) {
34+
temp.add(Integer.parseInt(st[i]));
35+
ar.remove(ar.size()-1);
36+
}
37+
Collections.reverse(temp);
38+
temp.addAll(ar);
39+
40+
System.out.println(temp.toString());
41+
42+
}
43+
}
44+
45+
//checkout leetcode rotate array
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
/*
2+
*
3+
*https://www.codechef.com/problems/ALTARAY
4+
*/
5+
16
import java.util.*;
7+
28
class alternating_subarray_prefix
39
{
410
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class climbstaircase {
4+
5+
public static void main(String[] args) throws IOException {
6+
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
String[] ar = br.readLine().split(" ");
9+
int n, k;
10+
11+
n = Integer.parseInt(ar[0]);
12+
k = Integer.parseInt(ar[1]);
13+
System.out.println(solve(n, k));
14+
15+
}
16+
17+
static int solve(int n, int k) {
18+
19+
List<Integer> dp = new ArrayList<Integer>(Collections.nCopies(k, 0));
20+
21+
dp.set(0,1);
22+
for (int i = 1; i <= n; i++) {
23+
for(int j=1;j<k;j++)
24+
{if(i-j<0)
25+
continue;
26+
dp.set(i%k,dp.get(i%k)+dp.get((i-j)%k));}
27+
}
28+
29+
return dp.get(n%k);
30+
}
31+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Time complexity: O(N)
3+
Auxiliary Space: O(N)
4+
5+
*/
6+
// Java program to print Longest Path
7+
// from root to leaf in a Binary tree
8+
import java.io.*;
9+
import java.util.ArrayList;
10+
11+
class longestPathFromRootToLeafinBT {
12+
13+
// Binary tree node
14+
static class Node {
15+
Node left;
16+
Node right;
17+
int data;
18+
};
19+
20+
// Function to create a new
21+
// Binary node
22+
// this can even be a constructor in above Node class
23+
static Node newNode(int data) {
24+
Node temp = new Node();
25+
26+
temp.data = data;
27+
temp.left = null;
28+
temp.right = null;
29+
30+
return temp;
31+
}
32+
33+
// Function to find and return the
34+
// longest path
35+
public static ArrayList<Integer> longestPath(Node root) {
36+
37+
// If root is null means there
38+
// is no binary tree so
39+
// return a empty vector
40+
if (root == null) {
41+
ArrayList<Integer> output = new ArrayList<>();
42+
return output;
43+
}
44+
45+
// Recursive call on root.right
46+
ArrayList<Integer> right = longestPath(root.right);
47+
48+
// Recursive call on root.left
49+
ArrayList<Integer> left = longestPath(root.left);
50+
51+
// Compare the size of the two ArrayList
52+
// and insert current node accordingly
53+
if (right.size() < left.size()) {
54+
left.add(root.data);
55+
} else {
56+
right.add(root.data);
57+
}
58+
59+
// Return the appropriate ArrayList
60+
return (left.size() > right.size() ? left : right);
61+
}
62+
63+
// Driver Code
64+
public static void main(String[] args) {
65+
Node root = newNode(1);
66+
root.left = newNode(2);
67+
root.right = newNode(3);
68+
root.left.left = newNode(4);
69+
root.left.right = newNode(5);
70+
root.left.right.right = newNode(6);
71+
72+
ArrayList<Integer> output = longestPath(root);
73+
int n = output.size();
74+
75+
System.out.print(output.get(n - 1));
76+
for (int i = n - 2; i >= 0; i--) {
77+
System.out.print(" -> " + output.get(i));
78+
}
79+
}
80+
}
81+

‎DSAPractice/Trees/template.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.math.BigInteger;
5+
6+
7+
public class ClassName {
8+
9+
public static void main(String[] args)throws IOException {
10+
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
int n=Integer.parseInt(br.readLine());
13+
String[] ar = br.readLine().split(" ");
14+
BigInteger[] st= new BigInteger[ar.length];
15+
16+
for(int i=0;i<ar.length;i++)
17+
{
18+
19+
}
20+
}
21+
}
2.5 MB
Binary file not shown.

‎Leetcode/Array/rotatearray.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// https://leetcode.com/problems/rotate-array/
2+
3+
// right solution: took help from last approach of https://www.geeksforgeeks.org/array-rotation/
4+
//55.8 mb
5+
import java.util.*;
6+
class rotatearray {
7+
public void rotate(int[] nums, int d) {
8+
9+
int n = nums.length;
10+
d = d % n;
11+
int g_c_d =gcd(d,nums.length);
12+
13+
int i , j ,temp,k=0;
14+
for(i = n-1; i > n-1-g_c_d ; i--)
15+
{ temp=nums[i];
16+
j=i;
17+
while(true)
18+
{ k=j-d;
19+
20+
if(k<0)
21+
{
22+
k=k+n;
23+
}
24+
if(k==i)
25+
break;
26+
nums[j]=nums[k];
27+
j=k;
28+
}
29+
nums[j] = temp;
30+
}
31+
32+
33+
}
34+
public int gcd(int a,int b)
35+
{
36+
if(a==0)
37+
return b;
38+
else
39+
return gcd(b%a,a);
40+
41+
}
42+
}
43+
44+
//faster solution but 1.2 more mb 56mb
45+
/**
46+
class Solution {
47+
public void rotate(int[] nums, int k) {
48+
k = k % nums.length;
49+
reverse(nums, 0, nums.length-1);
50+
reverse(nums, 0, k-1);
51+
reverse(nums, k, nums.length-1);
52+
}
53+
public static void reverse(int[]nums, int start, int end){
54+
while(start<end){
55+
int temp = nums[start];
56+
nums[start] = nums[end];
57+
nums[end] = temp;
58+
start++;end--;
59+
}
60+
}
61+
}
62+
**/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /