Skip to content

Commit b2219b6

Browse files
committed
More Solutions to Problems Added
1 parent 34947e0 commit b2219b6

5 files changed

+142
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
int t = sc.nextInt();
10+
while (t-- != 0) {
11+
int n = sc.nextInt();
12+
int arr[] = new int[n];
13+
int m = sc.nextInt();
14+
while (m-- != 0) {
15+
int a = sc.nextInt();
16+
int b = sc.nextInt();
17+
int k = sc.nextInt();
18+
for (int i = a; i <= b; i++) {
19+
arr[i] += k;
20+
}
21+
}
22+
int max = -111;
23+
for (int i = 0; i < n; i++) {
24+
if (arr[i] > max) {
25+
max = arr[i];
26+
}
27+
}
28+
System.out.println(max);
29+
}
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
int t = sc.nextInt();
10+
while (t-- != 0) {
11+
int n = sc.nextInt();
12+
int a[] = new int[n];
13+
for (int i = 0; i < n; i++) {
14+
a[i] = sc.nextInt();
15+
}
16+
int cnt = 0;
17+
// Counting 0's
18+
for (int i = 0; i < n; i++) {
19+
if (a[i] == 0) {
20+
cnt++;
21+
}
22+
}
23+
// Filling 0's
24+
for (int i = 0; i < cnt; i++) {
25+
a[i] = 0;
26+
}
27+
// Filling 1's
28+
for (int i = cnt; i < n; i++) {
29+
a[i] = 1;
30+
}
31+
// Printing the Binary Array
32+
for (int i = 0; i < n; i++) {
33+
System.out.print(a[i] + " ");
34+
}
35+
System.out.println();
36+
}
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
int t = sc.nextInt();
10+
while (t-- != 0) {
11+
int n = sc.nextInt();
12+
int a[] = new int[n];
13+
for (int i = 0; i < n; i++) {
14+
a[i] = sc.nextInt();
15+
}
16+
int low, mid, high = n - 1;
17+
low = mid = 0;
18+
while (mid <= high) {
19+
if (a[mid] == 0) {
20+
int temp = a[low];
21+
a[low] = a[mid];
22+
a[mid] = temp;
23+
low++;
24+
mid++;
25+
}
26+
else {
27+
int temp = a[mid];
28+
a[mid] = a[high];
29+
a[high] = temp;
30+
high--;
31+
}
32+
}
33+
for (int i = 0; i < n; i++) {
34+
System.out.print(a[i] + " ");
35+
}
36+
System.out.println();
37+
}
38+
}
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
static int bitDifferences(int a[], int n) {
7+
int sum = 0;
8+
int cnt = 0;
9+
for (int i = 0; i < 32; i++) {
10+
for (int j = 0; j < n; j++) {
11+
if ((a[j] & 1<<i) != 0) {
12+
cnt++;
13+
}
14+
}
15+
sum += cnt * (n - cnt) * 2;
16+
cnt = 0;
17+
}
18+
return sum;
19+
}
20+
21+
public static void main (String[] args)
22+
{
23+
Scanner sc = new Scanner(System.in);
24+
int t = sc.nextInt();
25+
while (t-- != 0) {
26+
int n = sc.nextInt();
27+
int a[] = new int[n];
28+
for (int i = 0; i < n; i++) {
29+
a[i] = sc.nextInt();
30+
}
31+
System.out.println(bitDifferences(a, n));
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)