Skip to content

Commit 1898c2a

Browse files
SpreehaMadhavBahl
authored andcommitted
day 20 (#183)
1 parent dcac8e1 commit 1898c2a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

day20/Java/Partitions.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 16/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Partitions {
14+
public static void main(String []args)
15+
{
16+
Scanner sc=new Scanner(System.in);
17+
int n,p,i,j;int k=0;
18+
System.out.println("Enter size of original array");
19+
n=sc.nextInt();
20+
int arr[]=new int[n];
21+
for(i=0;i<n;i++)
22+
arr[i]=sc.nextInt();
23+
System.out.println("Enter size of partition");
24+
p=sc.nextInt();
25+
for(i=0;i<arr.length;i=i+p)
26+
{
27+
int part[]=Arrays.copyOfRange(arr,i,i+p);
28+
k++;
29+
if(arr.length%2!=0 && k==(arr.length/p+1))
30+
p=arr.length%p;
31+
System.out.print("{ ");
32+
for(j=0;j<p;j++)
33+
System.out.print(part[j]+" ");
34+
System.out.print("}\n");
35+
}
36+
}
37+
}

day20/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,44 @@ function partition (array, size) {
8686
return partitionedArray;
8787
}
8888
```
89+
90+
## Java Implementation
91+
92+
### [Solution](./Java/Partitions.java)
93+
94+
```java
95+
/**
96+
* @date 16/01/19
97+
* @author SPREEHA DUTTA
98+
*/
99+
import java.util.*;
100+
public class Partitions {
101+
public static void main(String []args)
102+
{
103+
Scanner sc=new Scanner(System.in);
104+
int n,p,i,j;int k=0;
105+
System.out.println("Enter size of original array");
106+
n=sc.nextInt();
107+
int arr[]=new int[n];
108+
for(i=0;i<n;i++)
109+
arr[i]=sc.nextInt();
110+
System.out.println("Enter size of partition");
111+
p=sc.nextInt();
112+
for(i=0;i<arr.length;i=i+p)
113+
{
114+
int part[]=Arrays.copyOfRange(arr,i,i+p);
115+
k++;
116+
if(arr.length%2!=0 && k==(arr.length/p+1))
117+
p=arr.length%p;
118+
System.out.print("{ ");
119+
for(j=0;j<p;j++)
120+
System.out.print(part[j]+" ");
121+
System.out.print("}\n");
122+
}
123+
}
124+
}
125+
```
126+
89127
## C++ Solution
90128

91129
### [Solution @divyakhetan](./C++/PartitionDay20.cpp)

0 commit comments

Comments
 (0)