Advertisement
jasonpogi1669

Recreate subarray of length K within a sequence of length N using C++

Feb 26th, 2022 (edited)
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void GenerateSequence(int n, int k) {
  6.     if (k > n) { // K must be less than or equal to N
  7.         return;
  8.     }
  9.     int cur = 1;
  10.     int arr[n] = {0};
  11.     for (int i = 0; i < k; i++) {
  12.         arr[i] = cur;
  13.         cur++;
  14.         if (cur > k) { // reset the sequence
  15.             cur = 1;
  16.         }
  17.     }
  18.     for (int i = k; i < n; i++) { // re-create the subarray of length K
  19.         arr[i] = arr[i - k];
  20.     }
  21.     // sample:
  22.     // N = 9, K = 3
  23.     // {1, 2, 3, 1, 2, 3, 1, 2, 3}
  24.     for (int i = 0; i < n; i++) {
  25.         cout << arr[i] << " ";
  26.     }
  27.     cout << '\n';
  28. }
  29.  
  30. int main() {
  31.     int n, k;
  32.     cin >> n >> k;
  33.     GenerateSequence(n, k);
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement