-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNested loop
119 lines (99 loc) · 2.46 KB
/
Nested loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Divisible Sum Pairs
Given an array of integers and a positive integer k, determine the number of (i,j) pairs in the array where i<j and arr[i] + arr[j] is divisible by k.
Example
arr = [1, 2, 3, 4, 5, 6] k = 5 Three pairs meet the criteria: [1, 4], [2, 3] and [4, 6]. */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s = new Scanner(System.in);
int n =s.nextInt();
int k =s.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] =s.nextInt();
}
int count =0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(i<j){
if((a[i]+a[j])%k==0)
{
count++;
}
}
}
}
System.out.println(count);
}
}
/*
Subarray Sums Divisible by K
Given an integer array nums and an integer k, print the number of non-empty subarrays that have a sum divisible by k.
A subarray is a contiguous part of an array.
Input:
The first line contains a two integers n(size of array n) and k Second line contains n spaced integers
Output:
Print count of subarrays divisible by k
Constraints
1 <= nums.length <= 3 * 104
-10^4 <= nums[i] <= 10^4
2 <= k <= 10^4
Sample Input 1
6 5 4 5 0 -2 -3 1
Sample Output 1
7
Explanation
There are 7 subarrays with a sum divisible by k = 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] =sc.nextInt();
}
int count=0;
for(int i=0;i<n;i++){
int sum =0;
for(int j=i;j<n;j++){
sum += a[j];
if(sum%m==0){
count++;
}
}
}
System.out.println(count);
}
}
// REVERSE OF AN ARRAY
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int temp =0;
for(int i=0;i<n/2;i++){
temp = a[i];
a[i] =a[n-1-i];
a[n-1-i]=temp;
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
}
}